Class: Rambling::Trie::Nodes::Node
- Inherits:
-
Object
- Object
- Rambling::Trie::Nodes::Node
- Includes:
- Comparable[TValue], Compressible[TValue], Enumerable[TValue], Inspectable[TValue], Comparable, Compressible, Enumerable, Inspectable, Stringifyable, Stringifyable[TValue]
- Defined in:
- lib/rambling/trie/nodes/node.rb,
sig/lib/rambling/trie/nodes/node.rbs
Overview
A representation of a node in the trie data structure. :reek:MissingSafeMethod { exclude: [ terminal! ] } :reek:RepeatedConditional { max_ifs: 3 }
Direct Known Subclasses
Instance Attribute Summary collapse
-
#children_tree ⇒ Hash<Symbol, Node>
Child nodes tree.
-
#letter ⇒ Symbol?
The corresponding letter(s).
-
#parent ⇒ Node?
Parent node.
-
#terminal ⇒ Boolean?
Returns the value of attribute terminal.
-
#value ⇒ TValue?
Arbitrary value stored in this node.
Instance Method Summary collapse
-
#[](letter) ⇒ Node
Get Node corresponding to a given letter.
-
#[]=(letter, node) ⇒ Node
Set the Node that corresponds to a given letter.
- #add ⇒ Node[TValue]
-
#children ⇒ Array<Node>
Child nodes.
- #children_match_prefix(_chars) {|arg0| ... } ⇒ Enumerator[String, void] abstract
- #closest_node(_chars) ⇒ Node[TValue] abstract
- #compressed? ⇒ Boolean
- #delete(letter) ⇒ Node?
-
#first_child ⇒ Node?
First child node.
-
#initialize(letter = nil, parent = nil, children_tree = nil) ⇒ Node
constructor
Creates a new node.
-
#key?(letter) ⇒ Boolean
(also: #has_key?)
Check if a Node's children tree contains a given letter.
-
#match_prefix(chars) {|String| ... } ⇒ Enumerator<String>
Returns all words that match a prefix of any length within chars.
- #missing ⇒ Node[TValue]
-
#partial_word?(chars) ⇒ Boolean
Checks if a path for a set of characters exists in the trie.
- #partial_word_chars?(_chars) ⇒ Boolean abstract
-
#root? ⇒ Boolean
Indicates if the current node is the root node.
-
#scan(chars) ⇒ Node
Returns the node that starts with the specified characters.
-
#terminal! ⇒ self
Mark Node as terminal.
-
#terminal? ⇒ Boolean
Indicates if a Node is terminal or not.
-
#word?(chars = []) ⇒ Boolean
Checks if a path for set of characters represents a word in the trie.
- #word_chars?(_chars) ⇒ Boolean abstract
Methods included from Inspectable
#attributes, #children_inspect, #class_name, #inspect, #letter_inspect, #terminal_inspect, #value_inspect
Methods included from Stringifyable
Methods included from Comparable
Methods included from Enumerable
Methods included from Compressible
Constructor Details
#initialize(letter = nil, parent = nil, children_tree = nil) ⇒ Node
Creates a new node.
45 46 47 48 49 |
# File 'lib/rambling/trie/nodes/node.rb', line 45 def initialize letter = nil, parent = nil, children_tree = nil @letter = letter @parent = parent @children_tree = children_tree || {} end |
Instance Attribute Details
#children_tree ⇒ Hash<Symbol, Node>
Child nodes tree.
29 30 31 |
# File 'lib/rambling/trie/nodes/node.rb', line 29 def children_tree @children_tree end |
#letter ⇒ Symbol? #letter=(letter) ⇒ Symbol?
Returns the corresponding letter(s).
25 26 27 |
# File 'lib/rambling/trie/nodes/node.rb', line 25 def letter @letter end |
#parent ⇒ Node?
Parent node.
33 34 35 |
# File 'lib/rambling/trie/nodes/node.rb', line 33 def parent @parent end |
#terminal ⇒ Boolean?
Returns the value of attribute terminal.
172 173 174 |
# File 'lib/rambling/trie/nodes/node.rb', line 172 def terminal @terminal end |
#value ⇒ TValue?
Arbitrary value stored in this node
37 38 39 |
# File 'lib/rambling/trie/nodes/node.rb', line 37 def value @value end |
Instance Method Details
#[](letter) ⇒ Node
Get Node corresponding to a given letter.
132 133 134 |
# File 'lib/rambling/trie/nodes/node.rb', line 132 def [] letter children_tree[letter] end |
#[]=(letter, node) ⇒ Node
Set the Node that corresponds to a given letter.
141 142 143 |
# File 'lib/rambling/trie/nodes/node.rb', line 141 def []= letter, node children_tree[letter] = node end |
#add ⇒ Node[TValue]
19 |
# File 'sig/lib/rambling/trie/nodes/node.rbs', line 19
def add: (Array[Symbol], ?TValue?) -> Node[TValue]
|
#children ⇒ Array<Node>
Child nodes.
53 54 55 |
# File 'lib/rambling/trie/nodes/node.rb', line 53 def children children_tree.values end |
#children_match_prefix(_chars) {|arg0| ... } ⇒ Enumerator[String, void]
Subclass and override #children_match_prefix to match a prefix against child nodes.
176 177 178 |
# File 'lib/rambling/trie/nodes/node.rb', line 176 def children_match_prefix _chars not_implemented end |
#closest_node(_chars) ⇒ Node[TValue]
Subclass and override #closest_node to return the deepest matching node.
194 195 196 |
# File 'lib/rambling/trie/nodes/node.rb', line 194 def closest_node _chars not_implemented end |
#compressed? ⇒ Boolean
37 |
# File 'sig/lib/rambling/trie/nodes/node.rbs', line 37
def compressed?: -> bool
|
#delete(letter) ⇒ Node?
158 159 160 |
# File 'lib/rambling/trie/nodes/node.rb', line 158 def delete letter children_tree.delete letter end |
#first_child ⇒ Node?
First child node.
59 60 61 |
# File 'lib/rambling/trie/nodes/node.rb', line 59 def first_child children_tree.each_value.first end |
#key?(letter) ⇒ Boolean Also known as: has_key?
Check if a Node's children tree contains a given letter.
149 150 151 |
# File 'lib/rambling/trie/nodes/node.rb', line 149 def key? letter children_tree.key? letter end |
#match_prefix(chars) {|String| ... } ⇒ Enumerator<String>
Returns all words that match a prefix of any length within chars.
118 119 120 121 122 123 124 125 126 |
# File 'lib/rambling/trie/nodes/node.rb', line 118 def match_prefix chars return enum_for :match_prefix, chars unless block_given? yield as_word if terminal? children_match_prefix chars do |word| yield word end end |
#missing ⇒ Node[TValue]
166 167 168 |
# File 'lib/rambling/trie/nodes/node.rb', line 166 def missing Rambling::Trie::Nodes::Missing.new end |
#partial_word?(chars) ⇒ Boolean
Checks if a path for a set of characters exists in the trie.
89 90 91 92 93 |
# File 'lib/rambling/trie/nodes/node.rb', line 89 def partial_word? chars return true if chars.empty? partial_word_chars? chars end |
#partial_word_chars?(_chars) ⇒ Boolean
Subclass and override #partial_word_chars? to check for a partial-word path.
182 183 184 |
# File 'lib/rambling/trie/nodes/node.rb', line 182 def partial_word_chars? _chars not_implemented end |
#root? ⇒ Boolean
Indicates if the current node is the root node.
65 66 67 |
# File 'lib/rambling/trie/nodes/node.rb', line 65 def root? !parent end |
#scan(chars) ⇒ Node
Returns the node that starts with the specified characters.
108 109 110 111 112 |
# File 'lib/rambling/trie/nodes/node.rb', line 108 def scan chars return self if chars.empty? closest_node chars end |
#terminal! ⇒ self
Mark Node as terminal.
77 78 79 80 |
# File 'lib/rambling/trie/nodes/node.rb', line 77 def terminal! self.terminal = true self end |
#terminal? ⇒ Boolean
Indicates if a Node is terminal or not.
71 72 73 |
# File 'lib/rambling/trie/nodes/node.rb', line 71 def terminal? !!terminal end |
#word?(chars = []) ⇒ Boolean
Checks if a path for set of characters represents a word in the trie.
98 99 100 101 102 |
# File 'lib/rambling/trie/nodes/node.rb', line 98 def word? chars = [] return terminal? if chars.empty? word_chars? chars end |
#word_chars?(_chars) ⇒ Boolean
Subclass and override #word_chars? to check for a full-word path.
188 189 190 |
# File 'lib/rambling/trie/nodes/node.rb', line 188 def word_chars? _chars not_implemented end |