splited big file into smaller files by functionality
This commit is contained in:
52
overlap.go
Normal file
52
overlap.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package interval
|
||||
|
||||
import "math"
|
||||
|
||||
func (intvls *intervals) HasOverlapped() bool {
|
||||
intvls.Overlapped()
|
||||
if intvls.OverlappedList != nil && len(intvls.OverlappedList) > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (intvls *intervals) Overlapped() []*Interval {
|
||||
if intvls.OverlappedList == nil {
|
||||
intvls.OverlappedList = intvls.calculateOverlapped()
|
||||
}
|
||||
return intvls.OverlappedList
|
||||
}
|
||||
|
||||
func (intvls *intervals) calculateOverlapped() []*Interval {
|
||||
intvls.Sort()
|
||||
list := []*Interval{}
|
||||
lastMinLow := math.MaxInt64
|
||||
lastMaxHigh := math.MinInt64
|
||||
for i, intvl := range intvls.Intervals {
|
||||
if i > 0 {
|
||||
lowInBetween := inBetweenInclusive(lastMinLow, intvl.Low, intvl.High) || inBetweenInclusive(intvl.Low, lastMinLow, lastMaxHigh)
|
||||
highInBetween := inBetweenInclusive(lastMaxHigh, intvl.Low, intvl.High) || inBetweenInclusive(intvl.High, lastMinLow, lastMaxHigh)
|
||||
if lowInBetween || highInBetween {
|
||||
greaterLow := max(intvl.Low, lastMinLow)
|
||||
lowerHigh := min(intvl.High, lastMaxHigh)
|
||||
list = append(list, &Interval{Low: greaterLow, High: lowerHigh})
|
||||
}
|
||||
}
|
||||
if intvl.Low < lastMinLow {
|
||||
lastMinLow = intvl.Low
|
||||
}
|
||||
if intvl.High > lastMaxHigh {
|
||||
lastMaxHigh = intvl.High
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (intvls *intervals) isOverlapping(value int, overlapped []*Interval) bool {
|
||||
for _, ovrlp := range overlapped {
|
||||
if inBetweenInclusive(value, ovrlp.Low, ovrlp.High) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user