Class: ACT::Vertex

Inherits:
Object
  • Object
show all
Defined in:
lib/act/vertex.rb

Overview

Trie vertex class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Vertex

Initializes new vertex

  • parent is parent ACT::Vertex

Example:

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

Optional arguments:

parent: (ACT::Vertex)


28
29
30
31
32
33
# File 'lib/act/vertex.rb', line 28

def initialize(parent = nil)
  @char = nil
  @parent = parent
  @children = []
  @end_indexes = []
end

Instance Attribute Details

#charObject

Letter representing this vertex



18
19
20
# File 'lib/act/vertex.rb', line 18

def char
  @char
end

#childrenObject (readonly)

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



11
12
13
# File 'lib/act/vertex.rb', line 11

def children
  @children
end

#end_indexesObject

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



15
16
17
# File 'lib/act/vertex.rb', line 15

def end_indexes
  @end_indexes
end

#parentObject (readonly)

Reference to the parent ACT::Vertex



8
9
10
# File 'lib/act/vertex.rb', line 8

def parent
  @parent
end

Instance Method Details

#add_child(char, end_index) ⇒ Object

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



37
38
39
40
41
42
43
44
45
# 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_charsObject

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



55
56
57
# File 'lib/act/vertex.rb', line 55

def children_chars
  @children.map(&:char)
end

#get_child(char) ⇒ Object

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



49
50
51
# File 'lib/act/vertex.rb', line 49

def get_child(char)
  @children.find { |c| c.char == char }
end