-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixmap.go
215 lines (187 loc) · 6.47 KB
/
prefixmap.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package netipds
import (
"fmt"
"net/netip"
)
// PrefixMapBuilder builds an immutable [PrefixMap].
//
// The zero value is a valid PrefixMapBuilder representing a builder with zero
// Prefixes.
//
// Call [PrefixMapBuilder.PrefixMap] to obtain an immutable PrefixMap from a
// PrefixMapBuilder.
//
// If Lazy == true, then path compression is delayed until a PrefixMap is
// created. The builder itself remains uncompressed. Lazy mode can dramatically
// reduce the time required to build a large PrefixMap.
type PrefixMapBuilder[T any] struct {
Lazy bool
tree tree[T]
}
// Get returns the value associated with the exact Prefix provided, if any.
func (m *PrefixMapBuilder[T]) Get(p netip.Prefix) (T, bool) {
return m.tree.get(keyFromPrefix(p))
}
// Set associates v with p.
func (m *PrefixMapBuilder[T]) Set(p netip.Prefix, v T) error {
if !p.IsValid() {
return fmt.Errorf("Prefix is not valid: %v", p)
}
// TODO so should m.tree just be a *tree[T]?
if m.Lazy {
m.tree = *(m.tree.insertLazy(keyFromPrefix(p), v))
} else {
m.tree = *(m.tree.insert(keyFromPrefix(p), v))
}
return nil
}
// Remove removes p from m. Only the exact Prefix provided is removed;
// descendants are not.
//
// To remove entire sections of IP space at once, see
// [PrefixMapBuilder.Filter].
func (m *PrefixMapBuilder[T]) Remove(p netip.Prefix) error {
if !p.IsValid() {
return fmt.Errorf("Prefix is not valid: %v", p)
}
m.tree.remove(keyFromPrefix(p))
return nil
}
// Filter removes all Prefixes that are not encompassed by s from m.
func (m *PrefixMapBuilder[T]) Filter(s *PrefixSet) {
m.tree.filter(&s.tree)
}
// PrefixMap returns an immutable PrefixMap representing the current state of m.
//
// The builder remains usable after calling PrefixMap.
func (m *PrefixMapBuilder[T]) PrefixMap() *PrefixMap[T] {
t := m.tree.copy()
if m.Lazy && t != nil {
t = t.compress()
}
return &PrefixMap[T]{*t, t.size()}
}
func (s *PrefixMapBuilder[T]) String() string {
return s.tree.stringImpl("", "", false)
}
// PrefixMap is a map of [netip.Prefix] to T. It is implemented as a binary
// radix tree.
//
// Use [PrefixMapBuilder] to construct PrefixMaps.
type PrefixMap[T any] struct {
tree tree[T]
size int
}
// Get returns the value associated with the exact Prefix provided, if any.
func (m *PrefixMap[T]) Get(p netip.Prefix) (T, bool) {
return m.tree.get(keyFromPrefix(p))
}
// Contains returns true if this map includes the exact Prefix provided.
func (m *PrefixMap[T]) Contains(p netip.Prefix) bool {
return m.tree.contains(keyFromPrefix(p))
}
// Encompasses returns true if this map includes a Prefix which completely
// encompasses p. The encompassing Prefix may be p itself.
func (m *PrefixMap[T]) Encompasses(p netip.Prefix) bool {
return m.tree.encompasses(keyFromPrefix(p), false)
}
// EncompassesStrict returns true if this map includes a Prefix which
// completely encompasses p. The encompassing Prefix must be an ancestor of p,
// not p itself.
func (m *PrefixMap[T]) EncompassesStrict(p netip.Prefix) bool {
return m.tree.encompasses(keyFromPrefix(p), true)
}
// OverlapsPrefix returns true if this map includes a Prefix which overlaps p.
func (m *PrefixMap[T]) OverlapsPrefix(p netip.Prefix) bool {
return m.tree.overlapsKey(keyFromPrefix(p))
}
func (m *PrefixMap[T]) rootOf(
p netip.Prefix,
strict bool,
) (outPfx netip.Prefix, val T, ok bool) {
label, val, ok := m.tree.rootOf(keyFromPrefix(p), strict)
if !ok {
return outPfx, val, false
}
return label.toPrefix(), val, true
}
// RootOf returns the shortest-prefix ancestor of p in m, if any.
// If p itself has an entry and has no ancestors, then p's entry is returned.
func (m *PrefixMap[T]) RootOf(p netip.Prefix) (netip.Prefix, T, bool) {
return m.rootOf(p, false)
}
// RootOfStrict returns the shortest-prefix ancestor of p in m, if any. If p
// has no ancestors in m, then RootOfStrict returns zero values and false.
func (m *PrefixMap[T]) RootOfStrict(p netip.Prefix) (netip.Prefix, T, bool) {
return m.rootOf(p, true)
}
func (m *PrefixMap[T]) parentOf(
p netip.Prefix,
strict bool,
) (outPfx netip.Prefix, val T, ok bool) {
key, val, ok := m.tree.parentOf(keyFromPrefix(p), strict)
if !ok {
return outPfx, val, false
}
return key.toPrefix(), val, true
}
// ParentOf returns the longest-prefix ancestor of p in m, if any. If p itself
// has an entry, then p's entry is returned.
func (m *PrefixMap[T]) ParentOf(p netip.Prefix) (netip.Prefix, T, bool) {
return m.parentOf(p, false)
}
// ParentOfStrict returns the longest-prefix ancestor of p in m, if any.
// If p has no ancestors in the map, then ParentOfStrict returns zero values
// and false.
func (m *PrefixMap[T]) ParentOfStrict(p netip.Prefix) (netip.Prefix, T, bool) {
return m.parentOf(p, true)
}
// ToMap returns a map of all Prefixes in m to their associated values.
func (m *PrefixMap[T]) ToMap() map[netip.Prefix]T {
res := make(map[netip.Prefix]T)
m.tree.walk(key{}, func(n *tree[T]) bool {
if n.hasEntry {
res[n.key.toPrefix()] = n.value
}
return false
})
return res
}
// DescendantsOf returns a PrefixMap containing all descendants of p in m,
// including p itself if it has an entry.
func (m *PrefixMap[T]) DescendantsOf(p netip.Prefix) *PrefixMap[T] {
t := m.tree.descendantsOf(keyFromPrefix(p), false)
return &PrefixMap[T]{*t, t.size()}
}
// DescendantsOfStrict returns a PrefixMap containing all descendants of p in
// m, excluding p itself.
func (m *PrefixMap[T]) DescendantsOfStrict(p netip.Prefix) *PrefixMap[T] {
t := m.tree.descendantsOf(keyFromPrefix(p), true)
return &PrefixMap[T]{*t, t.size()}
}
// AncestorsOf returns a PrefixMap containing all ancestors of p in m,
// including p itself if it has an entry.
func (m *PrefixMap[T]) AncestorsOf(p netip.Prefix) *PrefixMap[T] {
t := m.tree.ancestorsOf(keyFromPrefix(p), false)
return &PrefixMap[T]{*t, t.size()}
}
// AncestorsOfStrict returns a PrefixMap containing all ancestors of p in m,
// excluding p itself.
func (m *PrefixMap[T]) AncestorsOfStrict(p netip.Prefix) *PrefixMap[T] {
t := m.tree.ancestorsOf(keyFromPrefix(p), true)
return &PrefixMap[T]{*t, t.size()}
}
// Filter returns a new PrefixMap containing the entries of m that are
// encompassed by s.
func (m *PrefixMap[T]) Filter(s *PrefixSet) *PrefixMap[T] {
t := m.tree.filterCopy(&s.tree)
return &PrefixMap[T]{*t, t.size()}
}
// String returns a human-readable representation of m's tree structure.
func (m *PrefixMap[T]) String() string {
return m.tree.stringImpl("", "", false)
}
// Size returns the number of entries in m.
func (m *PrefixMap[T]) Size() int {
return m.size
}