Class: TwitterCldr::Collation::TrieWithFallback

Inherits:
Utils::Trie
  • Object
show all
Defined in:
lib/twitter_cldr/collation/trie_with_fallback.rb

Overview

Trie that delegates all not found keys to the fallback.

Note: methods #get and #find_prefix have a bit different behavior. The first one, #get, delegates to the fallback any key that was not found. On the other hand, #find_refix delegates the key only if none of its prefixes was found.

E.g., if the fallback contains key [1, 2] with value ‘12’ and the trie itself contains only key [1] with value ‘1’ results will be the following:

trie.get([1, 2]) #=> '12' - key [1, 2] wasn't found in the trie, so it was delegated to the fallback where the
                            value '12' was found.

trie.find_prefix([1, 2]) #=> ['1', 1, suffixes] - key [1, 2] is not present in the trie, but its prefix [1] was
                                                  found, so the fallback wasn't used.

trie.find_prefix([3, 2]) - the trie itself includes neither key [3, 2] nor its prefix [3], so this call is
                           delegated to the fallback.

This special behavior of the #find_prefix method allows ‘hiding’ fallback keys that contain more than one element by adding their one element prefixes to the trie itself. This feature is useful for some applications, e.g., for suppressing contractions in a tailored fractional collation elements trie.

Instance Attribute Summary collapse

Attributes inherited from Utils::Trie

#root

Instance Method Summary collapse

Methods inherited from Utils::Trie

#add, #each_starting_with, #empty?, #lock, #locked?, #marshal_dump, #marshal_load, #set, #starters, #to_hash

Constructor Details

#initialize(fallback) ⇒ TrieWithFallback

Returns a new instance of TrieWithFallback.



35
36
37
38
# File 'lib/twitter_cldr/collation/trie_with_fallback.rb', line 35

def initialize(fallback)
  super()
  self.fallback = fallback
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



33
34
35
# File 'lib/twitter_cldr/collation/trie_with_fallback.rb', line 33

def fallback
  @fallback
end

Instance Method Details

#find_prefix(key) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/twitter_cldr/collation/trie_with_fallback.rb', line 44

def find_prefix(key)
  value, prefix_size, suffixes = super

  if prefix_size > 0
    [value, prefix_size, suffixes]
  else
    fallback.find_prefix(key)
  end
end

#get(key) ⇒ Object



40
41
42
# File 'lib/twitter_cldr/collation/trie_with_fallback.rb', line 40

def get(key)
  super || fallback.get(key)
end