Class: Rambling::Trie::Nodes::Raw

Inherits:
Node
  • Object
show all
Defined in:
lib/rambling/trie/nodes/raw.rb,
sig/lib/rambling/trie/nodes/raw.rbs

Overview

A representation of a node in an uncompressed trie data structure. :reek:RepeatedConditional { max_ifs: 4 }

Instance Attribute Summary

Attributes inherited from Node

#children_tree, #letter, #parent, #terminal, #value

Instance Method Summary collapse

Methods inherited from Node

#[], #[]=, #children, #delete, #first_child, #initialize, #key?, #match_prefix, #missing, #partial_word?, #root?, #scan, #terminal!, #terminal?, #word?

Methods included from Inspectable

#attributes, #children_inspect, #children_tree, #class_name, #inspect, #letter, #letter_inspect, #terminal, #terminal_inspect, #value, #value_inspect

Methods included from Stringifyable

#as_word, #children_tree, #letter, #parent, #terminal?, #to_s

Methods included from Comparable

#==, #children_tree, #letter, #terminal?, #value

Methods included from Enumerable

#as_word, #children_tree, #each, #empty_enum, #terminal?

Methods included from Compressible

#children_tree, #compressible?, #root?, #terminal?

Constructor Details

This class inherits a constructor from Rambling::Trie::Nodes::Node

Instance Method Details

#add(reversed_chars, value = nil) ⇒ Node

Note:

This method consumes the array by popping each element during recursion, leaving it empty on return.

Adds a word to the current raw (uncompressed) trie node.

Parameters:

  • reversed_chars (Array<Symbol>)

    the char array to add to the trie, in reverse order.

  • (Array[Symbol])
  • (TValue, nil)

Returns:

  • (Node)

    the added/modified node based on the word added.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rambling/trie/nodes/raw.rb', line 13

def add reversed_chars, value = nil
  if reversed_chars.empty?
    unless root?
      self.value = value
      terminal!
    end
    self
  else
    add_to_children_tree reversed_chars, value
  end
end

#add_to_children_tree(chars, value = nil) ⇒ Node[TValue]

Parameters:

  • (Array[Symbol])
  • (TValue, nil)

Returns:



33
34
35
36
37
38
# File 'lib/rambling/trie/nodes/raw.rb', line 33

def add_to_children_tree chars, value = nil
  letter = chars.pop || raise
  child = children_tree[letter] || new_node(letter)
  child.add chars, value
  child
end

#children_match_prefix(chars) {|arg0| ... } ⇒ Enumerator[String, void]

Parameters:

  • (Array[String])

Yields:

Yield Parameters:

  • arg0 (String)

Yield Returns:

  • (void)

Returns:

  • (Enumerator[String, void])


70
71
72
73
74
75
76
77
78
79
# File 'lib/rambling/trie/nodes/raw.rb', line 70

def children_match_prefix chars
  return enum_for :children_match_prefix, chars unless block_given?

  return empty_enum if chars.empty?

  child = children_tree[(chars.shift || raise).to_sym]
  return empty_enum unless child

  child.match_prefix(chars) { |word| yield word }
end

#closest_node(chars) ⇒ Node[TValue]

Parameters:

  • (Array[String])

Returns:



62
63
64
65
66
67
68
# File 'lib/rambling/trie/nodes/raw.rb', line 62

def closest_node chars
  letter = (chars.shift || raise).to_sym
  child = children_tree[letter]
  return missing unless child

  child.scan chars
end

#compressed?Boolean

Always return false for a raw (uncompressed) node.

Returns:

  • (Boolean)

    always false for a raw (uncompressed) node.



27
28
29
# File 'lib/rambling/trie/nodes/raw.rb', line 27

def compressed?
  false
end

#new_node(letter) ⇒ Node[TValue]

Parameters:

  • (Symbol)

Returns:



40
41
42
43
44
# File 'lib/rambling/trie/nodes/raw.rb', line 40

def new_node letter
  node = Rambling::Trie::Nodes::Raw.new letter, self
  children_tree[letter] = node
  node
end

#partial_word_chars?(chars = []) ⇒ Boolean

Parameters:

  • (Array[String])

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/rambling/trie/nodes/raw.rb', line 46

def partial_word_chars? chars = []
  letter = (chars.shift || raise).to_sym
  child = children_tree[letter]
  return false unless child

  child.partial_word? chars
end

#word_chars?(chars = []) ⇒ Boolean

Parameters:

  • (Array[String])

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/rambling/trie/nodes/raw.rb', line 54

def word_chars? chars = []
  letter = (chars.shift || raise).to_sym
  child = children_tree[letter]
  return false unless child

  child.word? chars
end