bitmap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Package bitmap provides a []bool/bitmap implementation with standardized
  2. // iteration. Bitmaps are the equivalent of []bool, with improved compression
  3. // for runs of similar values, and faster operations on ranges and the like.
  4. package bitmap
  5. import (
  6. "github.com/RoaringBitmap/roaring"
  7. "github.com/anacrolix/missinggo/iter"
  8. )
  9. type (
  10. BitIndex = uint32
  11. BitRange = uint64
  12. )
  13. type Interface interface {
  14. Len() int
  15. }
  16. // Bitmaps store the existence of values in [0,math.MaxUint32] more
  17. // efficiently than []bool. The empty value starts with no bits set.
  18. type Bitmap struct {
  19. RB *roaring.Bitmap
  20. }
  21. const (
  22. MaxInt BitIndex = roaring.MaxUint32
  23. ToEnd BitRange = roaring.MaxRange
  24. )
  25. // The number of set bits in the bitmap. Also known as cardinality.
  26. func (me Bitmap) Len() BitRange {
  27. if me.RB == nil {
  28. return 0
  29. }
  30. return me.RB.GetCardinality()
  31. }
  32. func (me Bitmap) ToSortedSlice() []BitIndex {
  33. if me.RB == nil {
  34. return nil
  35. }
  36. return me.RB.ToArray()
  37. }
  38. func (me *Bitmap) lazyRB() *roaring.Bitmap {
  39. if me.RB == nil {
  40. me.RB = roaring.NewBitmap()
  41. }
  42. return me.RB
  43. }
  44. func (me Bitmap) Iter(cb iter.Callback) {
  45. me.IterTyped(func(i int) bool {
  46. return cb(i)
  47. })
  48. }
  49. // Returns true if all values were traversed without early termination.
  50. func (me Bitmap) IterTyped(f func(int) bool) bool {
  51. if me.RB == nil {
  52. return true
  53. }
  54. it := me.RB.Iterator()
  55. for it.HasNext() {
  56. if !f(int(it.Next())) {
  57. return false
  58. }
  59. }
  60. return true
  61. }
  62. func checkInt(i BitIndex) {
  63. // Nothing to do if BitIndex is uint32, as this matches what roaring can handle.
  64. }
  65. func (me *Bitmap) Add(is ...BitIndex) {
  66. rb := me.lazyRB()
  67. for _, i := range is {
  68. checkInt(i)
  69. rb.Add(i)
  70. }
  71. }
  72. func (me *Bitmap) AddRange(begin, end BitRange) {
  73. // Filter here so we don't prematurely create a bitmap before having roaring do this check
  74. // anyway.
  75. if begin >= end {
  76. return
  77. }
  78. me.lazyRB().AddRange(begin, end)
  79. }
  80. func (me *Bitmap) Remove(i BitIndex) bool {
  81. if me.RB == nil {
  82. return false
  83. }
  84. return me.RB.CheckedRemove(uint32(i))
  85. }
  86. func (me *Bitmap) Union(other Bitmap) {
  87. me.lazyRB().Or(other.lazyRB())
  88. }
  89. func (me Bitmap) Contains(i BitIndex) bool {
  90. if me.RB == nil {
  91. return false
  92. }
  93. return me.RB.Contains(i)
  94. }
  95. func (me *Bitmap) Sub(other Bitmap) {
  96. if other.RB == nil {
  97. return
  98. }
  99. if me.RB == nil {
  100. return
  101. }
  102. me.RB.AndNot(other.RB)
  103. }
  104. func (me *Bitmap) Clear() {
  105. if me.RB == nil {
  106. return
  107. }
  108. me.RB.Clear()
  109. }
  110. func (me Bitmap) Copy() (ret Bitmap) {
  111. ret = me
  112. if ret.RB != nil {
  113. ret.RB = ret.RB.Clone()
  114. }
  115. return
  116. }
  117. func (me *Bitmap) FlipRange(begin, end BitRange) {
  118. me.lazyRB().Flip(begin, end)
  119. }
  120. func (me Bitmap) Get(bit BitIndex) bool {
  121. return me.RB != nil && me.RB.Contains(bit)
  122. }
  123. func (me *Bitmap) Set(bit BitIndex, value bool) {
  124. if value {
  125. me.lazyRB().Add(bit)
  126. } else {
  127. if me.RB != nil {
  128. me.RB.Remove(bit)
  129. }
  130. }
  131. }
  132. func (me *Bitmap) RemoveRange(begin, end BitRange) *Bitmap {
  133. if me.RB == nil {
  134. return me
  135. }
  136. me.RB.RemoveRange(begin, end)
  137. return me
  138. }
  139. func (me Bitmap) IsEmpty() bool {
  140. return me.RB == nil || me.RB.IsEmpty()
  141. }