Module: IntervalNotation::Operations
- Included in:
- IntervalNotation
- Defined in:
- lib/interval_notation/operations.rb
Class Method Summary collapse
-
.combine(interval_sets, combiner) ⇒ Object
Internal method which combines intervals according to an algorithm given by a combiner.
-
.intersection(intervals) ⇒ Object
Intersection of multiple intervals.
-
.union(intervals) ⇒ Object
Union of multiple intervals.
Class Method Details
.combine(interval_sets, combiner) ⇒ Object
Internal method which combines intervals according to an algorithm given by a combiner. Combiner tells whether current section or point should be included to a new interval.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/interval_notation/operations.rb', line 9 def combine(interval_sets, combiner) points = interval_sets.each_with_index.flat_map{|interval_set, interval_set_index| interval_set.intervals.flat_map{|interval| interval.interval_boundaries(interval_set_index) } }.sort_by(&:value) intervals = [] incl_from = nil from = nil points.chunk(&:value).each do |point_value, points_on_place| combiner.pass(points_on_place) if combiner.previous_state if combiner.state unless combiner.include_last_point intervals << BasicIntervals.interval_by_boundary_inclusion(incl_from, from, false, point_value) incl_from = false from = point_value end else to = point_value incl_to = combiner.include_last_point intervals << BasicIntervals.interval_by_boundary_inclusion(incl_from, from, incl_to, to) from = nil # easier to find an error (but not necessary code) incl_from = nil # ditto end else if combiner.state from = point_value incl_from = combiner.include_last_point else intervals << BasicIntervals::Point.new(point_value) if combiner.include_last_point end end end IntervalSet.new_unsafe(intervals) end |
.intersection(intervals) ⇒ Object
Intersection of multiple intervals
56 57 58 |
# File 'lib/interval_notation/operations.rb', line 56 def intersection(intervals) combine(intervals, IntersectCombiner.new(intervals.size)) end |
.union(intervals) ⇒ Object
Union of multiple intervals.
51 52 53 |
# File 'lib/interval_notation/operations.rb', line 51 def union(intervals) combine(intervals, UnionCombiner.new(intervals.size)) end |