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) ⇒ 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
Compresses the existing trie using redundant node elimination.
-
#compress! ⇒ self
Compresses the existing trie using redundant node elimination.
-
#compressed? ⇒ Boolean
Indicates if the root Node can be compressed or not.
-
#concat(words) ⇒ 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
Size of the Root Node‘s children tree.
-
#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) {|String| ... } ⇒ 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.
118 119 120 |
# File 'lib/rambling/trie/container.rb', line 118 def == other root == other.root end |
#[](letter) ⇒ Nodes::Node
Get Node corresponding to a given letter.
140 141 142 |
# File 'lib/rambling/trie/container.rb', line 140 def [] letter root[letter] end |
#add(word) ⇒ Nodes::Node Also known as: <<
Adds a word to the trie.
30 31 32 |
# File 'lib/rambling/trie/container.rb', line 30 def add word root.add reversed_char_symbols word end |
#children ⇒ Array<Nodes::Node>
Root node’s child nodes.
147 148 149 |
# File 'lib/rambling/trie/container.rb', line 147 def children root.children end |
#children_tree ⇒ Hash<Symbol, Nodes::Node>
Root node’s children tree.
155 156 157 |
# File 'lib/rambling/trie/container.rb', line 155 def children_tree root.children_tree end |
#compress ⇒ Container
Compresses the existing trie using redundant node elimination. Returns a new trie with the compressed root.
57 58 59 60 61 |
# File 'lib/rambling/trie/container.rb', line 57 def compress return self if root.compressed? 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.
49 50 51 52 |
# File 'lib/rambling/trie/container.rb', line 49 def compress! self.root = compress_root unless root.compressed? self end |
#compressed? ⇒ Boolean
Indicates if the root Node can be compressed or not.
161 162 163 |
# File 'lib/rambling/trie/container.rb', line 161 def compressed? root.compressed? end |
#concat(words) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
40 41 42 |
# File 'lib/rambling/trie/container.rb', line 40 def concat words words.map { |word| add word } end |
#each {|String| ... } ⇒ self
Iterates over the words contained in the trie.
125 126 127 128 129 |
# File 'lib/rambling/trie/container.rb', line 125 def each return enum_for :each unless block_given? root.each { |word| yield word } end |
#inspect ⇒ String
Returns a string representation of the container.
132 133 134 |
# File 'lib/rambling/trie/container.rb', line 132 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.
176 177 178 |
# File 'lib/rambling/trie/container.rb', line 176 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.
67 68 69 |
# File 'lib/rambling/trie/container.rb', line 67 def partial_word? word = '' root.partial_word? word.chars end |
#push(*words) ⇒ Array<Nodes::Node>
Adds all provided words to the trie.
78 79 80 |
# File 'lib/rambling/trie/container.rb', line 78 def push *words concat words end |
#scan(word = '') ⇒ Array<String> Also known as: words
Returns all words that start with the specified characters.
95 96 97 |
# File 'lib/rambling/trie/container.rb', line 95 def scan word = '' root.scan(word.chars).to_a end |
#size ⇒ Integer
Size of the Root Node‘s children tree.
182 183 184 |
# File 'lib/rambling/trie/container.rb', line 182 def size root.size end |
#to_a ⇒ Array<String>
Array of words contained in the root Node.
168 169 170 |
# File 'lib/rambling/trie/container.rb', line 168 def to_a root.to_a end |
#word?(word = '') ⇒ Boolean Also known as: include?
Checks if a whole word exists in the trie.
87 88 89 |
# File 'lib/rambling/trie/container.rb', line 87 def word? word = '' root.word? word.chars end |
#words_within(phrase) {|String| ... } ⇒ Array<String>
Returns all words within a string that match a word contained in the trie.
103 104 105 |
# File 'lib/rambling/trie/container.rb', line 103 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.
111 112 113 |
# File 'lib/rambling/trie/container.rb', line 111 def words_within? phrase words_within_root(phrase).any? end |