Class: Birch::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/birch/pure.rb,
ext/birch/native.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, id) ⇒ Object

Initialize the node with its value and id. Setup containers for the children, features and dependencies of this node.



13
14
15
16
17
18
19
20
# File 'ext/birch/native.c', line 13

def initialize(value, id)
  @value, @id = value, id
  @parent = nil
  @children = []
  @children_hash = {}
  @features = {}
  @edges = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/birch/pure.rb', line 9

def children
  @children
end

#edgesObject (readonly)

Returns the value of attribute edges.



9
10
11
# File 'lib/birch/pure.rb', line 9

def edges
  @edges
end

#featuresObject

Returns the value of attribute features.



8
9
10
# File 'lib/birch/pure.rb', line 8

def features
  @features
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/birch/pure.rb', line 7

def id
  @id
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/birch/pure.rb', line 8

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/birch/pure.rb', line 7

def value
  @value
end

Instance Method Details

#add(node_or_nodes) ⇒ Object Also known as: <<

Add a child(ren) to this node. Return first child added.



89
90
91
92
93
94
95
96
97
# File 'ext/birch/native.c', line 89

def add(node_or_nodes)
  nodes = [*node_or_nodes]
  nodes.each do |node|
    @children << node
    @children_hash[node.id] = node
    node.parent = self
  end
  nodes.first
end

#eachObject

Iterate over each children of the node.



170
171
172
# File 'ext/birch/native.c', line 170

def each
  @children.each{|c| yield c}
end

#find(id_or_node) ⇒ Object



186
187
188
189
190
191
# File 'ext/birch/native.c', line 186

def find(id_or_node)
  id = id_or_node.is_a?(Tree) ? id_or_node.id : id_or_node
  return @children_hash[id] if @children_hash.has_key?(id)
  @children.each{|c|  r = c.find(id); return r if r}
  nil
end

#get(feature) ⇒ Object Also known as: []

Return the feature with the supplied name.



126
127
128
129
130
131
132
133
134
135
# File 'ext/birch/native.c', line 126

def get(feature)
  case feature
  when :id 
    @id
  when :value
    @value
  else
    @features[feature]
  end
end

#has_children?Boolean

Boolean - does this node have children

Returns:

  • (Boolean)


234
# File 'ext/birch/native.c', line 234

def has_children?; !@children.empty?; end

#has_edges?Boolean

Boolean - does this node have children

Returns:

  • (Boolean)


243
# File 'ext/birch/native.c', line 243

def has_edges?; !@edges.empty?; end

#has_feature?(feature) ⇒ Boolean Also known as: has?

Boolean - does the node have the feature?

Returns:

  • (Boolean)


261
262
263
# File 'ext/birch/native.c', line 261

def has_feature?(feature)
  @features.has_key?(feature)
end

#has_features?Boolean

Boolean - does the node have features?

Returns:

  • (Boolean)


272
# File 'ext/birch/native.c', line 272

def has_features?; !@features.empty?; end

#has_parent?Boolean

Boolean - does the node have a parent?

Returns:

  • (Boolean)


252
# File 'ext/birch/native.c', line 252

def has_parent?; !@parent.nil? ;end

#is_leaf?Boolean

Boolean - is this node’s subtree empty?

Returns:

  • (Boolean)


216
# File 'ext/birch/native.c', line 216

def is_leaf?; @children.empty?; end

#is_root?Boolean

Boolean - is this node parent-less?

Returns:

  • (Boolean)


225
# File 'ext/birch/native.c', line 225

def is_root?; @parent.nil?; end


281
282
283
284
# File 'ext/birch/native.c', line 281

def link(edge)
  @edges << edge
  edge
end

#remove(id) ⇒ Object

Removes a node by ID and returns it



306
307
308
309
310
311
312
313
314
315
# File 'ext/birch/native.c', line 306

def remove(id)
  node = @children_hash.delete(id)
  if node.nil?
    raise ArgumentError, 
    "Given ID is not a children of this node."
  end
  @children.delete(node)
  node.set_as_root!
  node
end

#remove_all!Object

Remove all children and set them as root.



338
339
340
341
342
343
# File 'ext/birch/native.c', line 338

def remove_all!
  @children.each{|c| c.set_as_root!}
  @children = []
  @children_hash = {}
  nil
end

#rootObject

Return the root of the tree.



64
65
66
# File 'ext/birch/native.c', line 64

def root
  @parent.nil? ? self : root(parent)
end

#set(feature, value) ⇒ Object Also known as: []=

Set the feature to the supplied value.



137
138
139
# File 'ext/birch/native.c', line 137

def set(feature, value)
  @features[feature] = value
end

#set_as_root!Object

Remove from parent and set as root



295
296
297
298
299
300
301
302
# File 'ext/birch/native.c', line 295

def set_as_root!
  if has_parent?
    @parent = nil
  else
    raise "Node is already the root."
  end
  nil
end

#sizeObject

Return the total number of nodes in the subtree



150
151
152
# File 'ext/birch/native.c', line 150

def size
  1 + (@children.map(&:size).reduce(:+) || 0)
end

#unset(feature) ⇒ Object

Unset a feature.



142
143
144
# File 'ext/birch/native.c', line 142

def unset(feature)
  @features.delete(feature)
end