lib/multi_range.rb in multi_range-1.2.0 vs lib/multi_range.rb in multi_range-1.3.0

- old
+ new

@@ -24,21 +24,21 @@ def initialize(ranges) @ranges = ranges.map{|s| s.is_a?(Numeric) ? s..s : s }.sort_by(&:begin).freeze @is_float = @ranges.any?{|range| range.begin.is_a?(Float) || range.end.is_a?(Float) } end - def merge_overlaps + def merge_overlaps(merge_same_value = true) return MultiRange.new([]) if @ranges.size == 0 new_ranges = [] current_range = nil @ranges.each do |range| next current_range = range if current_range == nil next if range.end <= current_range.end - if can_combine?(current_range, range) + if can_combine?(current_range, range, merge_same_value) current_range = range.exclude_end? ? current_range.begin...range.end : current_range.begin..range.end else new_ranges << current_range current_range = range end @@ -66,11 +66,15 @@ next if other.begin > range.end # 大於這個 range break if other.end < range.begin # 小於這個 range sub_range1 = range.begin...other.begin - sub_range2_begin = other.exclude_end? ? other.end : other.end + (other.end.is_a?(Float) ? Float::EPSILON : 1) + sub_range2_begin = if other.exclude_end? + other.end + else + other.end + (other.end.is_a?(Float) ? Float::EPSILON : 1) + end sub_range2 = range.exclude_end? ? sub_range2_begin...range.end : sub_range2_begin..range.end sub_ranges = [] sub_ranges << sub_range1 if sub_range1.begin <= sub_range1.end sub_ranges << sub_range2 if sub_range2.begin <= sub_range2.end @@ -142,13 +146,18 @@ def max range = @ranges.last return range.max if range end + def contain_overlaps? + merge_overlaps(false).ranges != ranges + end + private # make sure that range1.begin <= range2.begin - def can_combine?(range1, range2) + def can_combine?(range1, range2, merge_same_value) + return merge_same_value if range1.end == range2.begin and range1.exclude_end? return range1.end >= range2.begin if @is_float return range1.end + 1 >= range2.begin end end