48 lines
808 B
Crystal
48 lines
808 B
Crystal
module TimeCost
|
|
class RangeList
|
|
def initialize
|
|
@ranges = [] of Range
|
|
end
|
|
|
|
def add(commit : Commit) : RangeList
|
|
end
|
|
|
|
def add(range : Range) : RangeList
|
|
merged = false
|
|
merged_range = nil
|
|
|
|
# merge
|
|
@ranges.each do |old|
|
|
# pp old
|
|
if (old.overlap? range)
|
|
old.merge range
|
|
merged_range = old
|
|
merged = true
|
|
break
|
|
end
|
|
end
|
|
|
|
# add if needed
|
|
if (merged)
|
|
@ranges.delete merged_range
|
|
self.add merged_range
|
|
else
|
|
@ranges.push range
|
|
end
|
|
end
|
|
|
|
def each
|
|
@ranges.each do |r|
|
|
yield r
|
|
end
|
|
end
|
|
|
|
def sum : Float
|
|
result = 0
|
|
@ranges.each do |r|
|
|
result += r.diff
|
|
end
|
|
return result
|
|
end
|
|
end
|
|
end
|