summaryrefslogtreecommitdiff
path: root/lib/containers/intervaltree.go
blob: 16a9fdd06d5f3ec4149042675a4eb8f835bce6e8 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package containers

import (
	"fmt"
)

type interval[K Ordered[K]] struct {
	Min, Max K
}

// Compare implements Ordered.
func (a interval[K]) Compare(b interval[K]) int {
	if d := a.Min.Compare(b.Min); d != 0 {
		return d
	}
	return a.Max.Compare(b.Max)
}

// ContainsFn returns whether this interval contains the range matched
// by the given function.
func (ival interval[K]) ContainsFn(fn func(K) int) bool {
	return fn(ival.Min) >= 0 && fn(ival.Max) <= 0
}

type intervalValue[K Ordered[K], V any] struct {
	Val       V
	ValSpan   interval[K]
	ChildSpan interval[K]
}

// Compare implements Ordered.
func (a intervalValue[K, V]) Compare(b intervalValue[K, V]) int {
	return a.ValSpan.Compare(b.ValSpan)
}

type IntervalTree[K Ordered[K], V any] struct {
	MinFn func(V) K
	MaxFn func(V) K
	inner RBTree[intervalValue[K, V]]
}

func (*IntervalTree[K, V]) attrFn(node *RBNode[intervalValue[K, V]]) {
	max := node.Value.ValSpan.Max
	if node.Left != nil && node.Left.Value.ChildSpan.Max.Compare(max) > 0 {
		max = node.Left.Value.ChildSpan.Max
	}
	if node.Right != nil && node.Right.Value.ChildSpan.Max.Compare(max) > 0 {
		max = node.Right.Value.ChildSpan.Max
	}
	node.Value.ChildSpan.Max = max

	min := node.Value.ValSpan.Min
	if node.Left != nil && node.Left.Value.ChildSpan.Min.Compare(min) < 0 {
		min = node.Left.Value.ChildSpan.Min
	}
	if node.Right != nil && node.Right.Value.ChildSpan.Min.Compare(min) < 0 {
		min = node.Right.Value.ChildSpan.Min
	}
	node.Value.ChildSpan.Min = min
}

func (t *IntervalTree[K, V]) init() {
	if t.inner.AttrFn == nil {
		t.inner.AttrFn = t.attrFn
	}
}

func (t *IntervalTree[K, V]) Equal(u *IntervalTree[K, V]) bool {
	return t.inner.Equal(&u.inner)
}

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: min,
			Max: max,
		},
	})
}

func (t *IntervalTree[K, V]) Min() (K, bool) {
	if t.inner.root == nil {
		var zero K
		return zero, false
	}
	return t.inner.root.Value.ChildSpan.Min, true
}

func (t *IntervalTree[K, V]) Max() (K, bool) {
	if t.inner.root == nil {
		var zero K
		return zero, false
	}
	return t.inner.root.Value.ChildSpan.Max, true
}

func (t *IntervalTree[K, V]) Search(fn func(K) int) (V, bool) {
	node := t.inner.root
	for node != nil {
		switch {
		case node.Value.ValSpan.ContainsFn(fn):
			return node.Value.Val, true
		case node.Left != nil && node.Left.Value.ChildSpan.ContainsFn(fn):
			node = node.Left
		case node.Right != nil && node.Right.Value.ChildSpan.ContainsFn(fn):
			node = node.Right
		default:
			node = nil
		}
	}
	var zero V
	return zero, false
}

func (t *IntervalTree[K, V]) Range(fn func(V) bool) {
	t.inner.Range(func(node *RBNode[intervalValue[K, V]]) bool {
		return fn(node.Value.Val)
	})
}

func (t *IntervalTree[K, V]) Subrange(rangeFn func(K) int, handleFn func(V) bool) {
	t.subrange(t.inner.root, rangeFn, handleFn)
}

func (t *IntervalTree[K, V]) subrange(node *RBNode[intervalValue[K, V]], rangeFn func(K) int, handleFn func(V) bool) bool {
	if node == nil {
		return true
	}
	if !node.Value.ChildSpan.ContainsFn(rangeFn) {
		return true
	}
	if !t.subrange(node.Left, rangeFn, handleFn) {
		return false
	}
	if node.Value.ValSpan.ContainsFn(rangeFn) {
		if !handleFn(node.Value.Val) {
			return false
		}
	}
	if !t.subrange(node.Right, rangeFn, handleFn) {
		return false
	}
	return true
}