summaryrefslogtreecommitdiff
path: root/lib/containers/intervaltree.go
blob: 16bc91644371344530a1bda8e8eaa0afe83463c1 (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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package containers

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

func (ival intervalKey[K]) ContainsFn(fn func(K) int) bool {
	return fn(ival.Min) >= 0 && fn(ival.Max) <= 0
}

func (a intervalKey[K]) Cmp(b intervalKey[K]) int {
	if d := a.Min.Cmp(b.Min); d != 0 {
		return d
	}
	return a.Max.Cmp(b.Max)
}

type intervalValue[K Ordered[K], V any] struct {
	Val            V
	SpanOfChildren intervalKey[K]
}

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

func (t *IntervalTree[K, V]) keyFn(v intervalValue[K, V]) intervalKey[K] {
	return intervalKey[K]{
		Min: t.MinFn(v.Val),
		Max: t.MaxFn(v.Val),
	}
}

func (t *IntervalTree[K, V]) attrFn(node *RBNode[intervalValue[K, V]]) {
	max := t.MaxFn(node.Value.Val)
	if node.Left != nil && node.Left.Value.SpanOfChildren.Max.Cmp(max) > 0 {
		max = node.Left.Value.SpanOfChildren.Max
	}
	if node.Right != nil && node.Right.Value.SpanOfChildren.Max.Cmp(max) > 0 {
		max = node.Right.Value.SpanOfChildren.Max
	}
	node.Value.SpanOfChildren.Max = max

	min := t.MinFn(node.Value.Val)
	if node.Left != nil && node.Left.Value.SpanOfChildren.Min.Cmp(min) < 0 {
		min = node.Left.Value.SpanOfChildren.Min
	}
	if node.Right != nil && node.Right.Value.SpanOfChildren.Min.Cmp(min) < 0 {
		min = node.Right.Value.SpanOfChildren.Min
	}
	node.Value.SpanOfChildren.Min = min
}

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

func (t *IntervalTree[K, V]) Delete(min, max K) {
	t.init()
	t.inner.Delete(intervalKey[K]{
		Min: min,
		Max: max,
	})
}

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()
	t.inner.Insert(intervalValue[K, V]{Val: val})
}

func (t *IntervalTree[K, V]) Min() (K, bool) {
	if t.inner.root == nil {
		var zero K
		return zero, false
	}
	return t.inner.root.Value.SpanOfChildren.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.SpanOfChildren.Max, true
}

func (t *IntervalTree[K, V]) Lookup(k K) (V, bool) {
	return t.Search(k.Cmp)
}

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

func (t *IntervalTree[K, V]) searchAll(fn func(K) int, node *RBNode[intervalValue[K, V]], ret *[]V) {
	if node == nil {
		return
	}
	if !node.Value.SpanOfChildren.ContainsFn(fn) {
		return
	}
	t.searchAll(fn, node.Left, ret)
	if t.keyFn(node.Value).ContainsFn(fn) {
		*ret = append(*ret, node.Value.Val)
	}
	t.searchAll(fn, node.Right, ret)
}

func (t *IntervalTree[K, V]) SearchAll(fn func(K) int) []V {
	var ret []V
	t.searchAll(fn, t.inner.root, &ret)
	return ret
}

// TODO: func (t *IntervalTree[K, V]) Walk(fn func(*RBNode[V]) error) error