4 "github.com/anacrolix/generics"
5 "github.com/anacrolix/torrent/typed-roaring"
6 list "github.com/bahlo/generic-list-go"
9 type orderedBitmap[T typedRoaring.BitConstraint] struct {
10 bitmap typedRoaring.Bitmap[T]
11 // There should be way more efficient ways to do this.
13 elements map[T]*list.Element[T]
16 func (o *orderedBitmap[T]) IterateSnapshot(f func(T) bool) {
17 o.bitmap.Clone().Iterate(f)
20 func (o *orderedBitmap[T]) IsEmpty() bool {
21 return o.bitmap.IsEmpty()
24 func (o *orderedBitmap[T]) GetCardinality() uint64 {
25 return uint64(o.order.Len())
28 func (o *orderedBitmap[T]) Contains(index T) bool {
29 return o.bitmap.Contains(index)
32 func (o *orderedBitmap[T]) Add(index T) {
34 if _, ok := o.elements[index]; !ok {
35 generics.MakeMapIfNilAndSet(&o.elements, index, o.order.PushBack(index))
39 func (o *orderedBitmap[T]) Rank(index T) uint64 {
40 return o.bitmap.Rank(index)
43 func (o *orderedBitmap[T]) Iterate(f func(T) bool) {
44 for e := o.order.Front(); e != nil; e = e.Next() {
51 func (o *orderedBitmap[T]) CheckedRemove(index T) bool {
52 if !o.bitmap.CheckedRemove(index) {
55 o.order.Remove(o.elements[index])
56 delete(o.elements, index)