-
-
Notifications
You must be signed in to change notification settings - Fork 627
/
ordered-bitmap.go
73 lines (60 loc) · 1.48 KB
/
ordered-bitmap.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
package torrent
import (
"iter"
g "github.com/anacrolix/generics"
list "github.com/bahlo/generic-list-go"
"github.com/anacrolix/torrent/typed-roaring"
)
type orderedBitmap[T typedRoaring.BitConstraint] struct {
bitmap typedRoaring.Bitmap[T]
// There should be way more efficient ways to do this.
order list.List[T]
elements map[T]*list.Element[T]
}
func (o *orderedBitmap[T]) IterateSnapshot(f func(T) bool) {
o.bitmap.Clone().Iterate(f)
}
func (o *orderedBitmap[T]) IsEmpty() bool {
return o.bitmap.IsEmpty()
}
func (o *orderedBitmap[T]) GetCardinality() uint64 {
return uint64(o.order.Len())
}
func (o *orderedBitmap[T]) Contains(index T) bool {
return o.bitmap.Contains(index)
}
func (o *orderedBitmap[T]) Add(index T) {
o.bitmap.Add(index)
if _, ok := o.elements[index]; !ok {
g.MakeMapIfNilAndSet(&o.elements, index, o.order.PushBack(index))
}
}
func (o *orderedBitmap[T]) Rank(index T) uint64 {
return o.bitmap.Rank(index)
}
func (o *orderedBitmap[T]) Iterate(f func(T) bool) (all bool) {
for e := o.order.Front(); e != nil; e = e.Next() {
if !f(e.Value) {
return
}
}
all = true
return
}
func (o *orderedBitmap[T]) Iterator() iter.Seq[T] {
return func(yield func(T) bool) {
for e := o.order.Front(); e != nil; e = e.Next() {
if !yield(e.Value) {
return
}
}
}
}
func (o *orderedBitmap[T]) CheckedRemove(index T) bool {
if !o.bitmap.CheckedRemove(index) {
return false
}
o.order.Remove(o.elements[index])
delete(o.elements, index)
return true
}