Class: Mspire::Bin
- Inherits:
-
Range
- Object
- Range
- Mspire::Bin
- Defined in:
- lib/mspire/bin.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.bin(bins, objects, *data_capture_obj, &block) ⇒ Object
O(m + n) speed to bin objects.
Instance Method Summary collapse
- #<<(val) ⇒ Object
-
#initialize(*args) ⇒ Bin
constructor
A new instance of Bin.
- #inspect ⇒ Object
Constructor Details
#initialize(*args) ⇒ Bin
Returns a new instance of Bin.
6 7 8 9 |
# File 'lib/mspire/bin.rb', line 6 def initialize(*args) super(*args) @data = [] end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/mspire/bin.rb', line 4 def data @data end |
Class Method Details
.bin(bins, objects, *data_capture_obj, &block) ⇒ Object
O(m + n) speed to bin objects. bin objects must respond to === . the object to bin must be a value that is sortable (< > ==), or you can pass in a block to get the value. bins and objects must be accessible by index (e.g., bins). if data_capture is given, it should be a parallel array to bins, and each object should respond to the ‘<<’ method. Otherwise, the bins themselves will be used to push data onto.
Here’s a simple example of binning x,y points where we want to bin the points based on the x value:
bins = (0...10).map {|i| Mspire::Bin.new(i, i+1, false) }
points = [[2.2, 100], [3.5, 200], [8.8, 150]]
Mspire::Bin.bin(bins, points) {|point| point.first }
# --or--: Mspire::Bin.bin(bins, points, &:first)
An example where we want to use a separate data store:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mspire/bin.rb', line 40 def self.bin(bins, objects, *data_capture_obj, &block) obj_e = objects.each ; obj = obj_e.next data_capture = data_capture_obj.first || bins bin_i = 0 # the bin index cbin = bins[bin_i] # the current bin done = false until done value = (block.nil? ? obj : block.call(obj)) if cbin.begin <= value until cbin === value && data_capture[bin_i] << obj bin_i += 1 cbin=bins[bin_i] || (done=true && break) end obj=obj_e.next rescue done=true else while cbin.begin > value && !done obj=obj_e.next rescue done=true && break value = (block.nil? ? obj : block.call(obj)) end end end data_capture end |
Instance Method Details
#<<(val) ⇒ Object
15 16 17 |
# File 'lib/mspire/bin.rb', line 15 def <<(val) @data << val end |
#inspect ⇒ Object
11 12 13 |
# File 'lib/mspire/bin.rb', line 11 def inspect "<(" + super + ") @data=#{data.inspect}>" end |