lib/rambling/trie/node.rb in rambling-trie-0.9.3 vs lib/rambling/trie/node.rb in rambling-trie-1.0.0
- old
+ new
@@ -1,82 +1,92 @@
module Rambling
module Trie
- # A representation of a node in the Trie data structure.
+ # A representation of a node in the trie data structure.
class Node
extend Rambling::Trie::Forwardable
- include Rambling::Trie::Compression
+ include Rambling::Trie::Compressable
include Rambling::Trie::Enumerable
- include Rambling::Trie::Inspector
+ include Rambling::Trie::Comparable
+ include Rambling::Trie::Stringifyable
+ include Rambling::Trie::Inspectable
delegate [
:[],
:[]=,
:delete,
:has_key?
] => :children_tree
- # Letter or letters corresponding to this node.
- # @return [Symbol, nil] the corresponding letter(s) or nil.
+ # @overload letter
+ # Letter(s) corresponding to the current node.
+ # @overload letter=(letter)
+ # Sets the letter(s) corresponding to the current node. Ensures the
+ # {Node#letter #letter} in the {Node#parent #parent}'s
+ # {Node#children_tree #children_tree} is updated.
+ # @param [String, Symbol, nil] letter the new letter value.
+ # @return [Symbol, nil] the corresponding letter(s).
attr_reader :letter
- # Children nodes.
- # @return [Hash] the children_tree hash, consisting of :letter => node.
+ # Children nodes tree.
+ # @return [Hash] the children_tree hash, consisting of `:letter => node`.
attr_accessor :children_tree
# Parent node.
- # @return [Node, nil] the parent node or nil for the root element.
+ # @return [Node, nil] the parent of the current node.
attr_accessor :parent
- # Creates a new Node.
- # @param [Node, nil] parent the parent of this node.
+ # Creates a new node.
+ # @param [Node, nil] parent the parent of the current node.
def initialize parent = nil
self.parent = parent
self.children_tree = {}
end
- # String representation of the current node, if it is a terminal node.
- # @return [String] the string representation of the current node.
- # @raise [InvalidOperation] if node is not terminal or is root.
- def as_word
- raise InvalidOperation, 'Cannot represent branch as a word' if letter && !terminal?
- to_s
- end
-
- # Children nodes of the current node.
- # @return [Array] the array of children nodes contained in the current node.
+ # Children nodes.
+ # @return [Array<Node>] the array of children nodes contained in the
+ # current node.
def children
children_tree.values
end
- # If the current node is the root node.
- # @return [Boolean] `true` only if the node does not have a parent
+ # Indicates if the current node is the root node.
+ # @return [Boolean] `true` if the node does not have a parent, `false`
+ # otherwise.
def root?
!parent
end
- # Flag for terminal nodes.
+ # Indicates if a {Node Node} is terminal or not.
# @return [Boolean] `true` for terminal nodes, `false` otherwise.
def terminal?
!!terminal
end
- # Force [Node] to be `terminal`
+ # Mark {Node Node} as terminal.
# @return [Node] the modified node.
def terminal!
self.terminal = true
self
end
- # String representation of the current node.
- # @return [String] the string representation of the current node.
- def to_s
- parent.to_s << letter.to_s
- end
-
def letter= letter
if letter
@letter = letter.to_sym
parent[letter] = self if parent
+ end
+ end
+
+ # Returns all words that match a prefix of any length within chars.
+ # @param [String] chars the chars to base the prefix on.
+ # @return [Enumerator<String>] all the words that match a prefix given by
+ # chars.
+ # @yield [String] each word found.
+ 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
private