class ACT::Vertex

Trie vertex class

Attributes

char[RW]

Letter representing this vertex

children[R]

Array of children ACT::Vertex references, ACT::Vertex

end_indexes[RW]

Array of indexes of word in dictionary Empty if it is intermediate ACT::Vertex in chain

parent[R]

Reference to the parent ACT::Vertex

Public Class Methods

new(parent = nil) click to toggle source

Initializes new vertex

Example:

>> ACT::Vertex.new(@root_vertex)
>> ACT::Vertex.new(@root_vertex)

Optional arguments:

parent: (ACT::Vertex)
# File lib/act/vertex.rb, line 28
def initialize(parent = nil)
  @char = nil
  @parent = parent
  @children = []
  @end_indexes = []
end

Public Instance Methods

add_child(char, end_index) click to toggle source

Initializes new ACT::Vertex and adds it to the parent attribute

# File lib/act/vertex.rb, line 37
def add_child(char, end_index)
  child = get_child(char)
  if child
    child.end_indexes << end_index unless end_index.nil?
    child
  else
    init_subchild(char, end_index)
  end
end
children_chars() click to toggle source

Returns array of characters from array of children ACT::Vertex

# File lib/act/vertex.rb, line 55
def children_chars
  @children.map(&:char)
end
get_child(char) click to toggle source

Returns child ACT::Vertex by letter, from children attribute

# File lib/act/vertex.rb, line 49
def get_child(char)
  @children.find { |c| c.char == char }
end

Private Instance Methods

init_subchild(char, end_index) click to toggle source
# File lib/act/vertex.rb, line 65
def init_subchild(char, end_index)
  child = self.class.new(self)
  child.char = char
  child.end_indexes << end_index unless end_index.nil?
  @children << child
  child
end
vertex() click to toggle source
# File lib/act/vertex.rb, line 61
def vertex
  self
end