Class: Birch::Tree
- Inherits:
-
Object
- Object
- Birch::Tree
- Includes:
- Enumerable
- Defined in:
- lib/birch/pure.rb,
ext/birch/native.c
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#features ⇒ Object
Returns the value of attribute features.
-
#id ⇒ Object
Returns the value of attribute id.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#add(node_or_nodes) ⇒ Object
(also: #<<)
Add a child(ren) to this node.
-
#each ⇒ Object
Iterate over each children of the node.
- #find(id_or_node) ⇒ Object
-
#get(feature) ⇒ Object
(also: #[])
Return the feature with the supplied name.
-
#has_children? ⇒ Boolean
Boolean - does this node have children.
-
#has_edges? ⇒ Boolean
Boolean - does this node have children.
-
#has_feature?(feature) ⇒ Boolean
(also: #has?)
Boolean - does the node have the feature?.
-
#has_features? ⇒ Boolean
Boolean - does the node have features?.
-
#has_parent? ⇒ Boolean
Boolean - does the node have a parent?.
-
#initialize(value, id) ⇒ Object
constructor
Initialize the node with its value and id.
-
#is_leaf? ⇒ Boolean
Boolean - is this node’s subtree empty?.
-
#is_root? ⇒ Boolean
Boolean - is this node parent-less?.
- #link(edge) ⇒ Object
-
#remove(id) ⇒ Object
Removes a node by ID and returns it.
-
#remove_all! ⇒ Object
Remove all children and set them as root.
-
#root ⇒ Object
Return the root of the tree.
-
#set(feature, value) ⇒ Object
(also: #[]=)
Set the feature to the supplied value.
-
#set_as_root! ⇒ Object
Remove from parent and set as root.
-
#size ⇒ Object
Return the total number of nodes in the subtree.
-
#unset(feature) ⇒ Object
Unset a feature.
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
9 10 11 |
# File 'lib/birch/pure.rb', line 9 def children @children end |
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
9 10 11 |
# File 'lib/birch/pure.rb', line 9 def edges @edges end |
#features ⇒ Object
Returns the value of attribute features.
8 9 10 |
# File 'lib/birch/pure.rb', line 8 def features @features end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/birch/pure.rb', line 7 def id @id end |
#parent ⇒ Object
Returns the value of attribute parent.
8 9 10 |
# File 'lib/birch/pure.rb', line 8 def parent @parent end |
#value ⇒ Object
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 |
#each ⇒ Object
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
234 |
# File 'ext/birch/native.c', line 234 def has_children?; !@children.empty?; end |
#has_edges? ⇒ Boolean
Boolean - does this node have children
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?
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?
272 |
# File 'ext/birch/native.c', line 272 def has_features?; !@features.empty?; end |
#has_parent? ⇒ Boolean
Boolean - does the node have a parent?
252 |
# File 'ext/birch/native.c', line 252 def has_parent?; !@parent.nil? ;end |
#is_leaf? ⇒ Boolean
Boolean - is this node’s subtree empty?
216 |
# File 'ext/birch/native.c', line 216 def is_leaf?; @children.empty?; end |
#is_root? ⇒ Boolean
Boolean - is this node parent-less?
225 |
# File 'ext/birch/native.c', line 225 def is_root?; @parent.nil?; end |
#link(edge) ⇒ Object
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 |
#root ⇒ Object
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 |
#size ⇒ Object
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 |