diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-04-04 10:52:13 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-04-04 10:52:13 -0600 |
commit | 607ea25ea1c0397749db39a15bd52c5e0d3cf552 (patch) | |
tree | bc7a628ffb336b1c790f84b3b489e8235b4ec81d /lib | |
parent | d39e73e41249f5daaf60069a9c77f5728d5d4398 (diff) |
containers: IntervalTree: Add a sanity check that intervals aren't backward
Diffstat (limited to 'lib')
-rw-r--r-- | lib/containers/intervaltree.go | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/containers/intervaltree.go b/lib/containers/intervaltree.go index d2e2732..16a9fdd 100644 --- a/lib/containers/intervaltree.go +++ b/lib/containers/intervaltree.go @@ -4,6 +4,10 @@ package containers +import ( + "fmt" +) + type interval[K Ordered[K]] struct { Min, Max K } @@ -71,11 +75,17 @@ func (t *IntervalTree[K, V]) Equal(u *IntervalTree[K, V]) bool { func (t *IntervalTree[K, V]) Insert(val V) { t.init() + min := t.MinFn(val) + max := t.MaxFn(val) + if max.Compare(min) < 0 { + panic(fmt.Errorf("containers.IntervalTree.Insert: max < min: [%v, %v]: %v", + min, max, val)) + } t.inner.Insert(intervalValue[K, V]{ Val: val, ValSpan: interval[K]{ - Min: t.MinFn(val), - Max: t.MaxFn(val), + Min: min, + Max: max, }, }) } |