Class: Dawg::Node
- Inherits:
-
Object
- Object
- Dawg::Node
- Defined in:
- lib/dawg/node/node.rb
Constant Summary collapse
- @@next_id =
0
Instance Attribute Summary collapse
-
#edge_count ⇒ Object
Returns the value of attribute edge_count.
-
#edges ⇒ Object
Returns the value of attribute edges.
-
#final ⇒ Object
Returns the value of attribute final.
-
#id ⇒ Object
Returns the value of attribute id.
-
#index ⇒ Object
Returns the value of attribute index.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](letter) ⇒ Object
- #each_edge(&block) ⇒ Object
- #hash ⇒ Object
-
#initialize(id: @@next_id, final: false, edge_count: 0, index: -1)) ⇒ Node
constructor
A new instance of Node.
- #to_s ⇒ Object
Constructor Details
#initialize(id: @@next_id, final: false, edge_count: 0, index: -1)) ⇒ Node
Returns a new instance of Node.
7 8 9 10 11 12 13 14 |
# File 'lib/dawg/node/node.rb', line 7 def initialize(id: @@next_id, final: false, edge_count: 0, index: -1) @id = id @@next_id += 1 @final = final @edge_count = edge_count @index = index @edges = {} end |
Instance Attribute Details
#edge_count ⇒ Object
Returns the value of attribute edge_count.
5 6 7 |
# File 'lib/dawg/node/node.rb', line 5 def edge_count @edge_count end |
#edges ⇒ Object
Returns the value of attribute edges.
5 6 7 |
# File 'lib/dawg/node/node.rb', line 5 def edges @edges end |
#final ⇒ Object
Returns the value of attribute final.
5 6 7 |
# File 'lib/dawg/node/node.rb', line 5 def final @final end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/dawg/node/node.rb', line 5 def id @id end |
#index ⇒ Object
Returns the value of attribute index.
5 6 7 |
# File 'lib/dawg/node/node.rb', line 5 def index @index end |
Instance Method Details
#==(other) ⇒ Object
37 38 39 |
# File 'lib/dawg/node/node.rb', line 37 def ==(other) to_s == other.to_s end |
#[](letter) ⇒ Object
41 42 43 |
# File 'lib/dawg/node/node.rb', line 41 def [](letter) @edges[letter] end |
#each_edge(&block) ⇒ Object
45 46 47 48 49 |
# File 'lib/dawg/node/node.rb', line 45 def each_edge(&block) @edges.each do |letter, node| yield letter end end |
#hash ⇒ Object
33 34 35 |
# File 'lib/dawg/node/node.rb', line 33 def hash to_s.hash end |
#to_s ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dawg/node/node.rb', line 16 def to_s arr = [] if @final arr<<'1' else arr<<'0' end @edges.each do |label,node| arr << label.to_s arr << node.id.to_s end arr.join('_') end |