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.



12
13
14
15
# File 'lib/abstract_node.rb', line 12

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.



17
18
19
# File 'lib/abstract_node.rb', line 17

def data
  @data
end

#sub_nodesObject (readonly)

Returns the value of attribute sub_nodes.



17
18
19
# File 'lib/abstract_node.rb', line 17

def sub_nodes
  @sub_nodes
end

Instance Method Details

#[](index) ⇒ Object



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

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

#[]=(index, sub_node) ⇒ Object



23
24
25
26
# File 'lib/abstract_node.rb', line 23

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

#delete(index) ⇒ Object



46
47
48
# File 'lib/abstract_node.rb', line 46

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

#each_index(&block) ⇒ Object



42
43
44
# File 'lib/abstract_node.rb', line 42

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

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



36
37
38
# File 'lib/abstract_node.rb', line 36

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

#each_pair(&block) ⇒ Object



32
33
34
# File 'lib/abstract_node.rb', line 32

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

#leaf?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/abstract_node.rb', line 57

def leaf?
  @sub_nodes.empty?
end

#merge!(sub_nodes) ⇒ Object



28
29
30
# File 'lib/abstract_node.rb', line 28

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

#nb_sub_nodesObject Also known as: size, length



50
51
52
# File 'lib/abstract_node.rb', line 50

def nb_sub_nodes
  @sub_nodes.size
end

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



61
62
63
64
65
66
67
# File 'lib/abstract_node.rb', line 61

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