class TimeFrame::Overlaps
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
Public Class Methods
new(array1, array2)
click to toggle source
# File lib/time_frame/time_frame_overlaps.rb, line 10 def initialize(array1, array2) @array1 = array1.reject(&:empty?) @array2 = array2.reject(&:empty?) end
Public Instance Methods
each(&block)
click to toggle source
# 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
Private Instance Methods
current_pair_overlaps?()
click to toggle source
# File lib/time_frame/time_frame_overlaps.rb, line 43 def current_pair_overlaps? @array1.first.overlaps? @array2.first end
each_array_has_many_items?()
click to toggle source
# File lib/time_frame/time_frame_overlaps.rb, line 35 def each_array_has_many_items? @array1.many? || @array2.many? end
shift()
click to toggle source
# File lib/time_frame/time_frame_overlaps.rb, line 26 def shift if @array2.one? || @array1.many? && @array1.second.min < @array2.second.min @array1.shift else @array2.shift end end
yield_current_pair() { |first, first| ... }
click to toggle source
# File lib/time_frame/time_frame_overlaps.rb, line 39 def yield_current_pair yield @array1.first, @array2.first end