Class: KTree::KTree::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/k-tree/k-tree.rb

Overview

A node containing other nodes, 2^tuple

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#centerObject (readonly)

Coordinates of this node



27
28
29
# File 'lib/k-tree/k-tree.rb', line 27

def center
  @center
end

#depthObject (readonly)

Coordinates of this node



27
28
29
# File 'lib/k-tree/k-tree.rb', line 27

def depth
  @depth
end

#lowerObject (readonly)

Coordinates of this node



27
28
29
# File 'lib/k-tree/k-tree.rb', line 27

def lower
  @lower
end

#refobObject

Reference object for this node



30
31
32
# File 'lib/k-tree/k-tree.rb', line 30

def refob
  @refob
end

#tupleObject (readonly)

Coordinates of this node



27
28
29
# File 'lib/k-tree/k-tree.rb', line 27

def tuple
  @tuple
end

#upperObject (readonly)

Coordinates of this node



27
28
29
# File 'lib/k-tree/k-tree.rb', line 27

def upper
  @upper
end

Instance Method Details

#childrenObject



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

#ntantsObject

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