Class: AbstractNode

Inherits:
Object show all
Includes:
Abstract
Defined in:
lib/abstract_node.rb

Direct Known Subclasses

IndexedNode, LabeledNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Abstract

included

Constructor Details

#initialize(data = nil, *sub_nodes) ⇒ AbstractNode

Returns a new instance of AbstractNode.



14
15
16
17
# File 'lib/abstract_node.rb', line 14

def initialize(data=nil, *sub_nodes)
  @data = data
  self.each_node { |sub_node| check_sub_node_type(sub_node) }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/abstract_node.rb', line 19

def data
  @data
end

#sub_nodesObject (readonly)

Returns the value of attribute sub_nodes.



19
20
21
# File 'lib/abstract_node.rb', line 19

def sub_nodes
  @sub_nodes
end

Instance Method Details

#[](index) ⇒ Object



21
22
23
# File 'lib/abstract_node.rb', line 21

def [](index)
  @sub_nodes[index]
end

#[]=(index, sub_node) ⇒ Object



25
26
27
28
# File 'lib/abstract_node.rb', line 25

def []=(index, sub_node)
  check_sub_node_type(sub_node)
  @sub_nodes[index] = sub_node
end

#delete(index) ⇒ Object



48
49
50
# File 'lib/abstract_node.rb', line 48

def delete(index)
  @sub_nodes.delete(index)
end

#each_index(&block) ⇒ Object



44
45
46
# File 'lib/abstract_node.rb', line 44

def each_index(&block)
  @sub_nodes.each_pair { |index, sub_node| block[index] }
end

#each_node(&block) ⇒ Object Also known as: each



38
39
40
# File 'lib/abstract_node.rb', line 38

def each_node(&block)
  @sub_nodes.each_pair { |index, sub_node| block[sub_node] }
end

#each_pair(&block) ⇒ Object



34
35
36
# File 'lib/abstract_node.rb', line 34

def each_pair(&block)
  @sub_nodes.each_pair { |index, sub_node| block[index, sub_node] }
end

#leaf?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/abstract_node.rb', line 59

def leaf?
  @sub_nodes.empty?
end

#merge!(sub_nodes) ⇒ Object



30
31
32
# File 'lib/abstract_node.rb', line 30

def merge!(sub_nodes)
  sub_nodes.each { |index, sub_node| self[index] = sub_node }
end

#nb_sub_nodesObject Also known as: size, length



52
53
54
# File 'lib/abstract_node.rb', line 52

def nb_sub_nodes
  @sub_nodes.size
end

#pre_depth_first(index = nil, &block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/abstract_node.rb', line 63

def pre_depth_first(index=nil, &block)
  block[index, self]
  @sub_nodes.each_pair do |index, sub_node|
    sub_node.pre_depth_first(index, &block)
  end
  nil
end