Class: KnnBall::ResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/knnball/result_set.rb

Overview

This class represents a ball in the tree.

The value of this ball will be its center while its radius is the distance between the center and the most far sub-ball.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ResultSet

Returns a new instance of ResultSet.



17
18
19
20
21
# File 'lib/knnball/result_set.rb', line 17

def initialize(options = {})
  @limit = options[:limit] || 10
  @items = []
  @barrier_value = options[:barrier_value]
end

Instance Attribute Details

#barrier_valueObject (readonly)

Returns the value of attribute barrier_value.



15
16
17
# File 'lib/knnball/result_set.rb', line 15

def barrier_value
  @barrier_value
end

#limitObject (readonly)

Returns the value of attribute limit.



15
16
17
# File 'lib/knnball/result_set.rb', line 15

def limit
  @limit
end

Instance Method Details

#add(value, item) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/knnball/result_set.rb', line 27

def add(value, item)
  return false unless(eligible?(value))
  
  if @barrier_value.nil? || value > @barrier_value || @items.empty?
    @barrier_value = value
    @items.push [value, item]
  else
    idx = 0
    begin 
      while(value > @items[idx][0])
        idx = idx + 1
      end
    rescue
      raise "ArrayOutOfBound for #{value} at index #{idx} for a limit of #{limit}"
    end
    @items.insert idx, [value, item]
  end
  
  if @items.count > limit
    @items.pop
  end
  
  @barrier_value = @items.last[0]
  return true
end

#eligible?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/knnball/result_set.rb', line 23

def eligible?(value)
  @barrier_value.nil? || @items.count < limit || value < @barrier_value
end

#itemsObject



53
54
55
# File 'lib/knnball/result_set.rb', line 53

def items
  @items.map {|i| i[1]}
end