-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
35 lines (30 loc) · 1.1 KB
/
tree.go
1
2
3
4
5
6
7
8
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
package main
import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)
type Tree[T constraints.Ordered] struct {
root *leaf[T]
}
func (receiver *Tree[T]) SearchMultipleIntervals(value T, includeInit, includeEnd bool) ([]IntervalWithValue[T], error) {
if &receiver.root == nil {
return nil, errorRootIsNil{}
}
return receiver.root.SearchMultipleIntervals(make([]IntervalWithValue[T], 0), value, includeInit, includeEnd)
}
func (receiver *Tree[T]) SearchFirstInterval(value T, includeInit, includeEnd bool) (IntervalWithValue[T], error) {
if &receiver.root == nil {
return IntervalWithValue[T]{}, errorRootIsNil{}
}
return receiver.root.SearchFirstInterval(value, includeInit, includeEnd)
}
func GenerateTree[T constraints.Ordered](intervals IntervalsWithValue[T], searchOverlapping bool) (Tree[T], error) {
slices.SortFunc(intervals, func(i, j IntervalWithValue[T]) bool {
return i.Compare(j) <= 0
})
if searchOverlapping && intervals.detectOverlapping() {
return Tree[T]{}, errorIntervalsOverlapped{}
}
root := GenerateLeaf(intervals, 0, len(intervals))
return Tree[T]{root: root}, nil
}