Class: KTree::KTree::Node
- Inherits:
-
Object
- Object
- KTree::KTree::Node
- Includes:
- Enumerable
- Defined in:
- lib/k-tree/k-tree.rb
Overview
A node containing other nodes, 2^tuple
Instance Attribute Summary collapse
-
#center ⇒ Object
readonly
Coordinates of this node.
-
#depth ⇒ Object
readonly
Coordinates of this node.
-
#lower ⇒ Object
readonly
Coordinates of this node.
-
#refob ⇒ Object
Reference object for this node.
-
#tuple ⇒ Object
readonly
Coordinates of this node.
-
#upper ⇒ Object
readonly
Coordinates of this node.
Instance Method Summary collapse
- #children ⇒ Object
-
#create_children(&block) ⇒ Object
recursive function to create child nodes.
- #each(&block) ⇒ Object
-
#initialize(vupper, vlower, depth) ⇒ Node
constructor
A new instance of Node.
-
#ntants ⇒ Object
iterate through all the upper, lower vectors of the possible children.
Constructor Details
#initialize(vupper, vlower, depth) ⇒ Node
Returns a new instance of Node.
32 33 34 35 36 37 38 |
# File 'lib/k-tree/k-tree.rb', line 32 def initialize(vupper, vlower, depth) @upper = vupper @lower = vlower @depth = depth @center = (vupper + vlower) / 2.0 @tuple = vupper.size end |
Instance Attribute Details
#center ⇒ Object (readonly)
Coordinates of this node
27 28 29 |
# File 'lib/k-tree/k-tree.rb', line 27 def center @center end |
#depth ⇒ Object (readonly)
Coordinates of this node
27 28 29 |
# File 'lib/k-tree/k-tree.rb', line 27 def depth @depth end |
#lower ⇒ Object (readonly)
Coordinates of this node
27 28 29 |
# File 'lib/k-tree/k-tree.rb', line 27 def lower @lower end |
#refob ⇒ Object
Reference object for this node
30 31 32 |
# File 'lib/k-tree/k-tree.rb', line 30 def refob @refob end |
#tuple ⇒ Object (readonly)
Coordinates of this node
27 28 29 |
# File 'lib/k-tree/k-tree.rb', line 27 def tuple @tuple end |
#upper ⇒ Object (readonly)
Coordinates of this node
27 28 29 |
# File 'lib/k-tree/k-tree.rb', line 27 def upper @upper end |
Instance Method Details
#children ⇒ Object
40 |
# File 'lib/k-tree/k-tree.rb', line 40 def children; @children ||= []; end |
#create_children(&block) ⇒ Object
recursive function to create child nodes.
54 55 56 57 58 59 60 61 |
# File 'lib/k-tree/k-tree.rb', line 54 def create_children(&block) if block.(self, cv = ntants) unless @depth == 0 @children = cv.map{|vupper, vlower| Node.new(vupper, vlower, @depth - 1)} @children.each{|child| child.create_children &block} end end end |
#each(&block) ⇒ Object
63 64 65 66 |
# File 'lib/k-tree/k-tree.rb', line 63 def each(&block) block.(self) children.each{|child| child.each &block } end |
#ntants ⇒ Object
iterate through all the upper, lower vectors of the possible children
44 45 46 47 48 49 50 51 |
# File 'lib/k-tree/k-tree.rb', line 44 def ntants d = @upper - @center (0...(2**@tuple)).map do |bits| k = [Vector[*(0...@tuple).map{|i| (bits & (1 << 1)) == 0 ? @center[i]+d[i] : @center[i]-d[i]}], @center] yield *k if block_given? k end end |