Class: TSS::Vertex
- Inherits:
-
Object
- Object
- TSS::Vertex
- Defined in:
- lib/tss/vertex.rb
Overview
Trie vertex class
Instance Attribute Summary collapse
-
#char ⇒ Object
Letter representing this vertex.
-
#children ⇒ Object
readonly
Array of children TSS::Vertex references for nested models (full trie, Aho-Corasick trie) or as list of nested vertexes of root vertex of flat trie.
-
#end_indexes ⇒ Object
Array of indexes of word in dictionary Empty if it is intermediate TSS::Vertex in chain.
-
#links ⇒ Object
readonly
Array of TSS::Vertex links for flat trie model, also used as suffixes of Aho-Corasick trie.
-
#parent ⇒ Object
Reference to the parent TSS::Vertex.
-
#root ⇒ Object
Reference to the root vertex of the trie.
Instance Method Summary collapse
-
#add_child(char, end_index) ⇒ Object
Initializes new TSS::Vertex and adds it to the parent attribute.
-
#add_link(vertex, end_index = nil) ⇒ Object
Adds reference to the linked vertexes.
-
#children_chars ⇒ Object
Returns array of characters from array of children TSS::Vertex.
-
#get_child(char) ⇒ Object
Returns child TSS::Vertex by letter, from children attribute.
-
#get_link(char) ⇒ Object
Returns link by character.
-
#initialize(parent = nil) {|_self| ... } ⇒ Vertex
constructor
Initializes new vertex *
parent
is parent TSS::Vertex Example: >> TSS::Vertex.new(@root_vertex) >> TSS::Vertex.new(@root_vertex) Optional arguments: parent: (TSS::Vertex). - #vertex ⇒ Object
Constructor Details
#initialize(parent = nil) {|_self| ... } ⇒ Vertex
Initializes new vertex
-
parent
is parent TSS::Vertex
Example:
>> TSS::Vertex.new(@root_vertex)
>> TSS::Vertex.new(@root_vertex)
Optional arguments:
parent: (TSS::Vertex)
39 40 41 42 43 44 45 46 |
# File 'lib/tss/vertex.rb', line 39 def initialize(parent = nil) @char = nil @parent = parent @children = [] @links = [] @end_indexes = [] yield(self) if block_given? end |
Instance Attribute Details
#char ⇒ Object
Letter representing this vertex
29 30 31 |
# File 'lib/tss/vertex.rb', line 29 def char @char end |
#children ⇒ Object (readonly)
Array of children TSS::Vertex references for nested models (full trie, Aho-Corasick trie) or as list of nested vertexes of root vertex of flat trie
18 19 20 |
# File 'lib/tss/vertex.rb', line 18 def children @children end |
#end_indexes ⇒ Object
Array of indexes of word in dictionary Empty if it is intermediate TSS::Vertex in chain
26 27 28 |
# File 'lib/tss/vertex.rb', line 26 def end_indexes @end_indexes end |
#links ⇒ Object (readonly)
Array of TSS::Vertex links for flat trie model, also used as suffixes of Aho-Corasick trie
22 23 24 |
# File 'lib/tss/vertex.rb', line 22 def links @links end |
#parent ⇒ Object
Reference to the parent TSS::Vertex
13 14 15 |
# File 'lib/tss/vertex.rb', line 13 def parent @parent end |
#root ⇒ Object
Reference to the root vertex of the trie
10 11 12 |
# File 'lib/tss/vertex.rb', line 10 def root @root end |
Instance Method Details
#add_child(char, end_index) ⇒ Object
Initializes new TSS::Vertex and adds it to the parent attribute
50 51 52 53 54 55 56 57 58 |
# File 'lib/tss/vertex.rb', line 50 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 |
#add_link(vertex, end_index = nil) ⇒ Object
Adds reference to the linked vertexes
68 69 70 |
# File 'lib/tss/vertex.rb', line 68 def add_link(vertex, end_index = nil) @links << TSS::Link.new(vertex, end_index) end |
#children_chars ⇒ Object
Returns array of characters from array of children TSS::Vertex
80 81 82 |
# File 'lib/tss/vertex.rb', line 80 def children_chars @children.map(&:char) end |
#get_child(char) ⇒ Object
Returns child TSS::Vertex by letter, from children attribute
62 63 64 |
# File 'lib/tss/vertex.rb', line 62 def get_child(char) @children.find { |c| c.char == char } end |
#get_link(char) ⇒ Object
Returns link by character
74 75 76 |
# File 'lib/tss/vertex.rb', line 74 def get_link(char) @links.find { |l| l.char == char } end |
#vertex ⇒ Object
84 85 86 |
# File 'lib/tss/vertex.rb', line 84 def vertex self end |