Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring - increase MAINTAINABILITY #17

Merged
merged 3 commits into from
Oct 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions lib/multi_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,17 @@ def merge_overlaps(merge_same_value = true)
end

def -(other)
if other.is_a?(MultiRange)
new_multi_range = dup
other.ranges.each{|range| new_multi_range -= range }
return new_multi_range
end
return difference_with_other_multi_range(other) if other.is_a?(MultiRange)

new_ranges = @ranges.dup

return MultiRange.new(new_ranges) if new_ranges.empty?
return MultiRange.new(new_ranges) if other.begin > @ranges.last.end # 大於最大值
return MultiRange.new(new_ranges) if other.end < @ranges.first.begin # 小於最小值
return MultiRange.new(new_ranges) if not overlaps_with_range?(other)

changed_size = 0
@ranges.each_with_index do |range, idx|
next if other.begin > range.end # 大於這個 range
break if other.end < range.begin # 小於這個 range

sub_range1 = range.begin...other.begin

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

sub_ranges = possible_sub_ranges_of(range, other)
new_ranges[idx + changed_size, 1] = sub_ranges
changed_size += sub_ranges.size - 1
break if other.end <= range.end # 沒有超過一個 range 的範圍
Expand Down Expand Up @@ -160,4 +141,34 @@ def can_combine?(range1, range2, merge_same_value)
return range1.end >= range2.begin if @is_float
return range1.end + 1 >= range2.begin
end

def difference_with_other_multi_range(other)
new_multi_range = dup
other.ranges.each{|range| new_multi_range -= range }
return new_multi_range
end

def possible_sub_ranges_of(range, other)
sub_range1 = range.begin...other.begin

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
return sub_ranges
end

def overlaps_with_range?(range)
return false if @ranges.empty?
return false if range.begin > @ranges.last.end # larger than maxinum
return false if range.end < @ranges.first.begin # smaller than mininum
return true
end
end