Class: Dawg::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/dawg/node/node.rb

Constant Summary collapse

@@next_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

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_countObject

Returns the value of attribute edge_count.



5
6
7
# File 'lib/dawg/node/node.rb', line 5

def edge_count
  @edge_count
end

#edgesObject

Returns the value of attribute edges.



5
6
7
# File 'lib/dawg/node/node.rb', line 5

def edges
  @edges
end

#finalObject

Returns the value of attribute final.



5
6
7
# File 'lib/dawg/node/node.rb', line 5

def final
  @final
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/dawg/node/node.rb', line 5

def id
  @id
end

#indexObject

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

#hashObject



33
34
35
# File 'lib/dawg/node/node.rb', line 33

def hash
  to_s.hash
end

#to_sObject



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