Class: Rambling::Trie::Container
- Inherits:
-
Object
- Object
- Rambling::Trie::Container
- Includes:
- Enumerable
- Defined in:
- lib/rambling/trie/container.rb
Overview
Wrapper on top of trie data structure.
Instance Attribute Summary collapse
-
#root ⇒ Nodes::Node
readonly
The root node of this trie.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares two trie data structures.
-
#[](letter) ⇒ Nodes::Node
Get Node corresponding to a given letter.
-
#add(word, value = nil) ⇒ Nodes::Node
(also: #<<)
Adds a word to the trie.
-
#children ⇒ Array<Nodes::Node>
Root node’s child nodes.
-
#children_tree ⇒ Hash<Symbol, Nodes::Node>
Root node’s children tree.
-
#compress ⇒ Container
deprecated
Deprecated.
Calling #compress on an already-compressed trie is deprecated and will raise InvalidOperation in the next major version. Use #compressed? to guard if needed.
-
#compress! ⇒ self
Compresses the existing trie using redundant node elimination.
-
#compressed? ⇒ Boolean
Indicates if the root Node can be compressed or not.
-
#concat(words, values = nil) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
-
#each {|String| ... } ⇒ self
Iterates over the words contained in the trie.
-
#initialize(root, compressor) {|self| ... } ⇒ Container
constructor
Creates a new trie.
-
#inspect ⇒ String
A string representation of the container.
-
#key?(letter) ⇒ Boolean
(also: #has_key?, #has_letter?)
Check if a letter is part of the root Nodes::Node‘s children tree.
-
#partial_word?(word = '') ⇒ Boolean
(also: #match?)
Checks if a path for a word or partial word exists in the trie.
-
#push(*words) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
-
#scan(word = '') ⇒ Array<String>
(also: #words)
Returns all words that start with the specified characters.
-
#size ⇒ Integer
Number of words contained in the trie.
-
#to_a ⇒ Array<String>
Array of words contained in the root Node.
-
#word?(word = '') ⇒ Boolean
(also: #include?)
Checks if a whole word exists in the trie.
-
#words_within(phrase) ⇒ Array<String>
Returns all words within a string that match a word contained in the trie.
-
#words_within?(phrase) ⇒ Boolean
Checks if there are any valid words in a given string.
Constructor Details
Instance Attribute Details
#root ⇒ Nodes::Node
The root node of this trie.
11 12 13 |
# File 'lib/rambling/trie/container.rb', line 11 def root @root end |
Instance Method Details
#==(other) ⇒ Boolean
Compares two trie data structures.
139 140 141 |
# File 'lib/rambling/trie/container.rb', line 139 def == other root == other.root end |
#[](letter) ⇒ Nodes::Node
Get Node corresponding to a given letter.
163 164 165 |
# File 'lib/rambling/trie/container.rb', line 163 def [] letter root[letter] end |
#add(word, value = nil) ⇒ Nodes::Node Also known as: <<
Adds a word to the trie.
31 32 33 |
# File 'lib/rambling/trie/container.rb', line 31 def add word, value = nil root.add reversed_char_symbols(word), value end |
#children ⇒ Array<Nodes::Node>
Root node’s child nodes.
170 171 172 |
# File 'lib/rambling/trie/container.rb', line 170 def children root.children end |
#children_tree ⇒ Hash<Symbol, Nodes::Node>
Root node’s children tree.
178 179 180 |
# File 'lib/rambling/trie/container.rb', line 178 def children_tree root.children_tree end |
#compress ⇒ Container
Calling #compress on an already-compressed trie is deprecated and will raise InvalidOperation in the next major version. Use #compressed? to guard if needed.
Compresses the existing trie using redundant node elimination. Returns a new trie with the compressed root.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rambling/trie/container.rb', line 72 def compress if root.compressed? warn <<~WARN.chomp.tr("\n", ' ') [DEPRECATED] Calling `compress` on an already-compressed trie is deprecated and will raise `InvalidOperation` in the next major version. Called from #{caller_locations(1, 1)&.first} WARN return Rambling::Trie::Container.new root, compressor end Rambling::Trie::Container.new compress_root, compressor end |
#compress! ⇒ self
This method replaces the root Raw node with a Compressed version of it.
Compresses the existing trie using redundant node elimination. Marks the trie as compressed. Does nothing if the trie has already been compressed.
63 64 65 66 |
# File 'lib/rambling/trie/container.rb', line 63 def compress! self.root = compress_root unless root.compressed? self end |
#compressed? ⇒ Boolean
Indicates if the root Node can be compressed or not.
184 185 186 |
# File 'lib/rambling/trie/container.rb', line 184 def compressed? root.compressed? end |
#concat(words, values = nil) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rambling/trie/container.rb', line 43 def concat words, values = nil if values words_size = words.size values_size = values.size unless words_size == values_size raise ArgumentError, "words and values must have the same size (words: #{words_size}, values: #{values_size})" end words.each_with_index.map { |word, index| add(word, values[index]) } else words.map { |word| add word } end end |
#each {|String| ... } ⇒ self
Iterates over the words contained in the trie.
146 147 148 149 150 151 152 |
# File 'lib/rambling/trie/container.rb', line 146 def each return enum_for :each unless block_given? root.each { |word| yield word } self end |
#inspect ⇒ String
Returns a string representation of the container.
155 156 157 |
# File 'lib/rambling/trie/container.rb', line 155 def inspect "#<#{self.class.name} root: #{root.inspect}>" end |
#key?(letter) ⇒ Boolean Also known as: has_key?, has_letter?
Check if a letter is part of the root Nodes::Node‘s children tree.
199 200 201 |
# File 'lib/rambling/trie/container.rb', line 199 def key? letter root.key? letter end |
#partial_word?(word = '') ⇒ Boolean Also known as: match?
Checks if a path for a word or partial word exists in the trie.
89 90 91 |
# File 'lib/rambling/trie/container.rb', line 89 def partial_word? word = '' root.partial_word? word.chars end |
#push(*words) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
100 101 102 |
# File 'lib/rambling/trie/container.rb', line 100 def push *words concat words end |
#scan(word = '') ⇒ Array<String> Also known as: words
Returns all words that start with the specified characters.
117 118 119 |
# File 'lib/rambling/trie/container.rb', line 117 def scan word = '' root.scan(word.chars).to_a end |
#size ⇒ Integer
Number of words contained in the trie.
205 206 207 |
# File 'lib/rambling/trie/container.rb', line 205 def size root.size end |
#to_a ⇒ Array<String>
Array of words contained in the root Node.
191 192 193 |
# File 'lib/rambling/trie/container.rb', line 191 def to_a root.to_a end |
#word?(word = '') ⇒ Boolean Also known as: include?
Checks if a whole word exists in the trie.
109 110 111 |
# File 'lib/rambling/trie/container.rb', line 109 def word? word = '' root.word? word.chars end |
#words_within(phrase) ⇒ Array<String>
Returns all words within a string that match a word contained in the trie.
124 125 126 |
# File 'lib/rambling/trie/container.rb', line 124 def words_within phrase words_within_root(phrase).to_a end |
#words_within?(phrase) ⇒ Boolean
Checks if there are any valid words in a given string.
132 133 134 |
# File 'lib/rambling/trie/container.rb', line 132 def words_within? phrase words_within_root(phrase).any? end |