-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered.go
More file actions
367 lines (328 loc) · 8.6 KB
/
ordered.go
File metadata and controls
367 lines (328 loc) · 8.6 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package sets
import (
"cmp"
"database/sql/driver"
"encoding/json"
"fmt"
"iter"
"slices"
)
// Ordered maintains the order that elements were added in. It uses a gap buffer with a Fenwick tree
// (binary indexed tree) to provide O(log N) Remove, At, and Index operations while keeping Add and
// Contains at O(1) amortized and O(1) respectively. It is not safe for concurrent use.
//
// Complexity:
// - Add: O(1) amortized
// - Remove: O(log N) amortized
// - Contains: O(1)
// - At: O(log N)
// - Index: O(log N)
// - Iterator: O(N)
type Ordered[M cmp.Ordered] struct {
idx map[M]int // element -> physical slot index
slots []M // physical slots (may contain gaps from removals)
alive []bool // slot occupancy bitmap
bit []int // Fenwick tree (1-indexed) for prefix sums of alive slots
count int // number of alive elements
}
var _ OrderedSet[int] = new(Ordered[int])
var _ driver.Valuer = new(Ordered[int])
// NewOrdered returns an empty *Ordered[M].
func NewOrdered[M cmp.Ordered]() *Ordered[M] {
return &Ordered[M]{
idx: make(map[M]int),
slots: make([]M, 0),
alive: make([]bool, 0),
bit: make([]int, 1), // bit[0] is unused sentinel
}
}
// NewOrderedFrom returns a new *Ordered[M] filled with the values from the sequence.
func NewOrderedFrom[M cmp.Ordered](seq iter.Seq[M]) *Ordered[M] {
s := NewOrdered[M]()
for x := range seq {
s.Add(x)
}
return s
}
// NewOrderedWith returns a new *Ordered[M] with the values provided.
func NewOrderedWith[M cmp.Ordered](m ...M) *Ordered[M] {
return NewOrderedFrom(slices.Values(m))
}
// --- Fenwick tree (binary indexed tree) operations ---
func (s *Ordered[M]) bitUpdate(i, delta int) {
for i++; i < len(s.bit); i += i & (-i) {
s.bit[i] += delta
}
}
func (s *Ordered[M]) bitQuery(i int) int {
var sum int
for i++; i > 0; i -= i & (-i) {
sum += s.bit[i]
}
return sum
}
// bitFindKth finds the physical slot index of the k-th alive element (0-indexed k).
func (s *Ordered[M]) bitFindKth(k int) int {
k++ // convert to 1-indexed count
var pos int
bitmask := 1
for bitmask < len(s.bit) {
bitmask <<= 1
}
bitmask >>= 1
for bitmask > 0 {
next := pos + bitmask
if next < len(s.bit) && s.bit[next] < k {
k -= s.bit[next]
pos = next
}
bitmask >>= 1
}
return pos
}
// rebuildBIT reconstructs the Fenwick tree from the alive bitmap.
// When growing, it doubles capacity to amortize rebuild cost.
func (s *Ordered[M]) rebuildBIT() {
needed := len(s.slots) + 1
newCap := needed
if needed > len(s.bit) {
newCap = max(needed, len(s.bit)*2)
}
newCap = max(newCap, 2)
s.bit = make([]int, newCap)
// O(N) Fenwick tree construction
for i := range s.slots {
if s.alive[i] {
s.bit[i+1] = 1
}
}
for i := 1; i < len(s.bit); i++ {
j := i + (i & (-i))
if j < len(s.bit) {
s.bit[j] += s.bit[i]
}
}
}
// compact removes gaps by rebuilding the slots, alive, and bit arrays.
func (s *Ordered[M]) compact() {
if s.count == len(s.slots) {
return
}
newSlots := make([]M, 0, s.count)
newAlive := make([]bool, 0, s.count)
for i, v := range s.slots {
if s.alive[i] {
s.idx[v] = len(newSlots)
newSlots = append(newSlots, v)
newAlive = append(newAlive, true)
}
}
s.slots = newSlots
s.alive = newAlive
s.rebuildBIT()
}
func (s *Ordered[M]) maybeCompact() {
if s.count > 0 && len(s.slots) > 2*s.count {
s.compact()
}
}
// elements returns a slice of all alive elements in insertion order.
func (s *Ordered[M]) elements() []M {
out := make([]M, 0, s.count)
for i, v := range s.slots {
if s.alive[i] {
out = append(out, v)
}
}
return out
}
// --- Set interface ---
// Contains returns true if the set contains the element.
func (s *Ordered[M]) Contains(m M) bool {
_, ok := s.idx[m]
return ok
}
// Clear the set and returns the number of elements removed.
func (s *Ordered[M]) Clear() int {
n := s.count
if s.idx == nil {
s.idx = make(map[M]int)
} else {
for k := range s.idx {
delete(s.idx, k)
}
}
if s.slots == nil {
s.slots = make([]M, 0)
} else {
s.slots = s.slots[:0]
}
if s.alive == nil {
s.alive = make([]bool, 0)
} else {
s.alive = s.alive[:0]
}
s.bit = make([]int, 1)
s.count = 0
return n
}
// Add an element to the set. Returns true if the element was added, false if it was already present. Elements are added
// to the end of the ordered set.
func (s *Ordered[M]) Add(m M) bool {
if s.Contains(m) {
return false
}
p := len(s.slots)
s.slots = append(s.slots, m)
s.alive = append(s.alive, true)
s.idx[m] = p
s.count++
if p+2 > len(s.bit) {
s.rebuildBIT()
} else {
s.bitUpdate(p, 1)
}
return true
}
// Remove an element from the set. Returns true if the element was removed, false if it was not present.
func (s *Ordered[M]) Remove(m M) bool {
p, ok := s.idx[m]
if !ok {
return false
}
s.alive[p] = false
s.bitUpdate(p, -1)
delete(s.idx, m)
s.count--
s.maybeCompact()
return true
}
// Cardinality returns the number of elements in the set.
func (s *Ordered[M]) Cardinality() int {
if s == nil {
return 0
}
return s.count
}
// Iterator yields all elements in the set in order.
func (s *Ordered[M]) Iterator(yield func(M) bool) {
for i, v := range s.slots {
if s.alive[i] {
if !yield(v) {
return
}
}
}
}
// Clone returns a copy of the set. The underlying type is the same as the original set.
func (s *Ordered[M]) Clone() Set[M] {
return NewOrderedFrom(s.Iterator)
}
// Ordered iteration yields the index and value of each element in the set in order.
func (s *Ordered[M]) Ordered(yield func(int, M) bool) {
var j int
for i, v := range s.slots {
if s.alive[i] {
if !yield(j, v) {
return
}
j++
}
}
}
// Backwards iteration yields the index and value of each element in the set in reverse order.
func (s *Ordered[M]) Backwards(yield func(int, M) bool) {
j := s.count - 1
for i := len(s.slots) - 1; i >= 0; i-- {
if s.alive[i] {
if !yield(j, s.slots[i]) {
return
}
j--
}
}
}
// NewEmptyOrdered returns a new empty ordered set of the same underlying type.
func (s *Ordered[M]) NewEmptyOrdered() OrderedSet[M] {
return NewOrdered[M]()
}
// NewEmpty returns a new empty set of the same underlying type.
func (s *Ordered[M]) NewEmpty() Set[M] {
return NewOrdered[M]()
}
// Pop removes and returns an element from the set. If the set is empty, it returns the zero value of M and false.
func (s *Ordered[M]) Pop() (M, bool) {
for k := range s.idx {
s.Remove(k)
return k, true
}
var m M
return m, false
}
// Sort the set in ascending order.
func (s *Ordered[M]) Sort() {
s.compact()
slices.Sort(s.slots)
for i, v := range s.slots {
s.idx[v] = i
}
// BIT is all-ones after compact; sort doesn't change alive status.
}
// At returns the element at the index. If the index is out of bounds, the second return value is false.
func (s *Ordered[M]) At(i int) (M, bool) {
var zero M
if i < 0 || i >= s.count {
return zero, false
}
p := s.bitFindKth(i)
return s.slots[p], true
}
// Index returns the index of the element in the set, or -1 if not present.
func (s *Ordered[M]) Index(m M) int {
p, ok := s.idx[m]
if !ok {
return -1
}
return s.bitQuery(p) - 1
}
// String returns a string representation of the set. It returns a string of the form OrderedSet[T](<elements>).
func (s *Ordered[M]) String() string {
var m M
return fmt.Sprintf("OrderedSet[%T](%v)", m, s.elements())
}
// Value implements the driver.Valuer interface. It returns the JSON representation of the set.
func (s *Ordered[M]) Value() (driver.Value, error) {
return s.MarshalJSON()
}
// MarshalJSON implements json.Marshaler. It will marshal the set into a JSON array of the elements in the set. If the
// set is empty an empty JSON array is returned.
func (s *Ordered[M]) MarshalJSON() ([]byte, error) {
vals := s.elements()
if len(vals) == 0 {
return []byte("[]"), nil
}
d, err := json.Marshal(vals)
if err != nil {
return d, fmt.Errorf("marshaling ordered set: %w", err)
}
return d, nil
}
// UnmarshalJSON implements json.Unmarshaler. It expects a JSON array of the elements in the set. If the set is empty,
// it returns an empty set. If the JSON is invalid, it returns an error.
func (s *Ordered[M]) UnmarshalJSON(d []byte) error {
t := make([]M, 0)
if err := json.Unmarshal(d, &t); err != nil {
return fmt.Errorf("unmarshaling ordered set: %w", err)
}
s.Clear()
for _, v := range t {
s.Add(v)
}
return nil
}
// Scan implements the sql.Scanner interface. It scans the value from the database into the set. It expects a JSON array
// of the elements in the set. If the JSON is invalid an error is returned. If the value is nil an empty set is
// returned.
func (s *Ordered[M]) Scan(src any) error {
return scanValue[M](src, s.Clear, s.UnmarshalJSON)
}