Class: TimeFrame::Overlaps

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

Overview

Traverses all intersections of in the cross product of two arrays of time_frames and yields the block for each pair (linear runtime)

NOTE:

  • requires each of the arrays to consist of pairwise disjoint elements

  • requires each of the arrays to be sorted

Instance Method Summary collapse

Constructor Details

#initialize(array1, array2) ⇒ Overlaps

Returns a new instance of Overlaps.



10
11
12
13
# File 'lib/time_frame/time_frame_overlaps.rb', line 10

def initialize(array1, array2)
  @array1 = array1.reject(&:empty?)
  @array2 = array2.reject(&:empty?)
end

Instance Method Details

#each(&block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/time_frame/time_frame_overlaps.rb', line 15

def each(&block)
  return [] if @array1.empty? || @array2.empty?
  yield_current_pair(&block) if current_pair_overlaps?
  while each_array_has_many_items?
    shift
    yield_current_pair(&block) if current_pair_overlaps?
  end
end