Class: Pickup::CircleIterator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, func, max, opts = {}) ⇒ CircleIterator

Returns a new instance of CircleIterator.



35
36
37
38
39
40
41
# File 'lib/pickup.rb', line 35

def initialize(obj, func, max, opts={})
  @obj = obj.dup
  @func = func
  @max = max
  @key_func = opts[:key_func] || key_func
  @weight_func = opts[:weight_func] || weight_func
end

Instance Attribute Details

#funcObject (readonly)

Returns the value of attribute func.



33
34
35
# File 'lib/pickup.rb', line 33

def func
  @func
end

#key_funcObject (readonly)

Returns the value of attribute key_func.



33
34
35
# File 'lib/pickup.rb', line 33

def key_func
  @key_func
end

#maxObject (readonly)

Returns the value of attribute max.



33
34
35
# File 'lib/pickup.rb', line 33

def max
  @max
end

#objObject (readonly)

Returns the value of attribute obj.



33
34
35
# File 'lib/pickup.rb', line 33

def obj
  @obj
end

#weight_funcObject (readonly)

Returns the value of attribute weight_func.



33
34
35
# File 'lib/pickup.rb', line 33

def weight_func
  @weight_func
end

Instance Method Details

#eachObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pickup.rb', line 59

def each
  until obj.empty?
    start = 0
    obj.each do |item|
      key = key_func.call(item)
      weight = weight_func.call(item)

      val = func.call(weight)
      start += val
      if yield([key, start, max])
        obj.delete key
        @max -= val
      end
    end
  end
end