Class Batfish::BKTree::Node
In: lib/data/bktree.rb
Parent: Object

This class represents a node in a BKTree.

Methods

add   fetch_near   new  

Attributes

children  [R] 
value  [R] 

Public Class methods

Initialize a BKTree Node with the given object as its value or nil if no object is given.

[Source]

# File lib/data/bktree.rb, line 106
      def initialize(obj=nil)
        @value = obj
        @children = {}
      end

Public Instance methods

Add a node to a node in a recursive way.

[Source]

# File lib/data/bktree.rb, line 113
      def add(obj)
        distance = obj.distance_to(@value)
        if child = @children[distance]
          child.add(obj)
        else
          @children[distance] = Node.new(obj)
        end
      end

Calls block once for each object within threshold distance of the given object that are part of a child node of self.

[Source]

# File lib/data/bktree.rb, line 126
      def fetch_near(obj, threshold, &block)
        distance = @value.distance_to(obj)
        if distance <= threshold
          yield @value
        end
        ((distance - threshold)..(distance + threshold)).each do |d|
          if child = @children[d]
            child.fetch_near(obj, threshold, &block)
          end
        end
      end

[Validate]