本站源代码
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1346 行
39KB

  1. // Package roaring is an implementation of Roaring Bitmaps in Go.
  2. // They provide fast compressed bitmap data structures (also called bitset).
  3. // They are ideally suited to represent sets of integers over
  4. // relatively small ranges.
  5. // See http://roaringbitmap.org for details.
  6. package roaring
  7. import (
  8. "bufio"
  9. "bytes"
  10. "encoding/base64"
  11. "fmt"
  12. "io"
  13. "strconv"
  14. )
  15. // Bitmap represents a compressed bitmap where you can add integers.
  16. type Bitmap struct {
  17. highlowcontainer roaringArray
  18. }
  19. // ToBase64 serializes a bitmap as Base64
  20. func (rb *Bitmap) ToBase64() (string, error) {
  21. buf := new(bytes.Buffer)
  22. _, err := rb.WriteTo(buf)
  23. return base64.StdEncoding.EncodeToString(buf.Bytes()), err
  24. }
  25. // FromBase64 deserializes a bitmap from Base64
  26. func (rb *Bitmap) FromBase64(str string) (int64, error) {
  27. data, err := base64.StdEncoding.DecodeString(str)
  28. if err != nil {
  29. return 0, err
  30. }
  31. buf := bytes.NewBuffer(data)
  32. return rb.ReadFrom(buf)
  33. }
  34. // WriteTo writes a serialized version of this bitmap to stream.
  35. // The format is compatible with other RoaringBitmap
  36. // implementations (Java, C) and is documented here:
  37. // https://github.com/RoaringBitmap/RoaringFormatSpec
  38. func (rb *Bitmap) WriteTo(stream io.Writer) (int64, error) {
  39. return rb.highlowcontainer.writeTo(stream)
  40. }
  41. // ToBytes returns an array of bytes corresponding to what is written
  42. // when calling WriteTo
  43. func (rb *Bitmap) ToBytes() ([]byte, error) {
  44. return rb.highlowcontainer.toBytes()
  45. }
  46. // WriteToMsgpack writes a msgpack2/snappy-streaming compressed serialized
  47. // version of this bitmap to stream. The format is not
  48. // compatible with the WriteTo() format, and is
  49. // experimental: it may produce smaller on disk
  50. // footprint and/or be faster to read, depending
  51. // on your content. Currently only the Go roaring
  52. // implementation supports this format.
  53. func (rb *Bitmap) WriteToMsgpack(stream io.Writer) (int64, error) {
  54. return 0, rb.highlowcontainer.writeToMsgpack(stream)
  55. }
  56. // ReadFrom reads a serialized version of this bitmap from stream.
  57. // The format is compatible with other RoaringBitmap
  58. // implementations (Java, C) and is documented here:
  59. // https://github.com/RoaringBitmap/RoaringFormatSpec
  60. func (rb *Bitmap) ReadFrom(stream io.Reader) (int64, error) {
  61. return rb.highlowcontainer.readFrom(stream)
  62. }
  63. // FromBuffer creates a bitmap from its serialized version stored in buffer
  64. //
  65. // The format specification is available here:
  66. // https://github.com/RoaringBitmap/RoaringFormatSpec
  67. //
  68. // The provided byte array (buf) is expected to be a constant.
  69. // The function makes the best effort attempt not to copy data.
  70. // You should take care not to modify buff as it will
  71. // likely result in unexpected program behavior.
  72. //
  73. // Resulting bitmaps are effectively immutable in the following sense:
  74. // a copy-on-write marker is used so that when you modify the resulting
  75. // bitmap, copies of selected data (containers) are made.
  76. // You should *not* change the copy-on-write status of the resulting
  77. // bitmaps (SetCopyOnWrite).
  78. //
  79. func (rb *Bitmap) FromBuffer(buf []byte) (int64, error) {
  80. return rb.highlowcontainer.fromBuffer(buf)
  81. }
  82. // RunOptimize attempts to further compress the runs of consecutive values found in the bitmap
  83. func (rb *Bitmap) RunOptimize() {
  84. rb.highlowcontainer.runOptimize()
  85. }
  86. // HasRunCompression returns true if the bitmap benefits from run compression
  87. func (rb *Bitmap) HasRunCompression() bool {
  88. return rb.highlowcontainer.hasRunCompression()
  89. }
  90. // ReadFromMsgpack reads a msgpack2/snappy-streaming serialized
  91. // version of this bitmap from stream. The format is
  92. // expected is that written by the WriteToMsgpack()
  93. // call; see additional notes there.
  94. func (rb *Bitmap) ReadFromMsgpack(stream io.Reader) (int64, error) {
  95. return 0, rb.highlowcontainer.readFromMsgpack(stream)
  96. }
  97. // MarshalBinary implements the encoding.BinaryMarshaler interface for the bitmap
  98. func (rb *Bitmap) MarshalBinary() ([]byte, error) {
  99. var buf bytes.Buffer
  100. writer := bufio.NewWriter(&buf)
  101. _, err := rb.WriteTo(writer)
  102. if err != nil {
  103. return nil, err
  104. }
  105. err = writer.Flush()
  106. if err != nil {
  107. return nil, err
  108. }
  109. return buf.Bytes(), nil
  110. }
  111. // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for the bitmap
  112. func (rb *Bitmap) UnmarshalBinary(data []byte) error {
  113. var buf bytes.Buffer
  114. _, err := buf.Write(data)
  115. if err != nil {
  116. return err
  117. }
  118. reader := bufio.NewReader(&buf)
  119. _, err = rb.ReadFrom(reader)
  120. return err
  121. }
  122. // NewBitmap creates a new empty Bitmap (see also New)
  123. func NewBitmap() *Bitmap {
  124. return &Bitmap{}
  125. }
  126. // New creates a new empty Bitmap (same as NewBitmap)
  127. func New() *Bitmap {
  128. return &Bitmap{}
  129. }
  130. // Clear resets the Bitmap to be logically empty, but may retain
  131. // some memory allocations that may speed up future operations
  132. func (rb *Bitmap) Clear() {
  133. rb.highlowcontainer.clear()
  134. }
  135. // ToArray creates a new slice containing all of the integers stored in the Bitmap in sorted order
  136. func (rb *Bitmap) ToArray() []uint32 {
  137. array := make([]uint32, rb.GetCardinality())
  138. pos := 0
  139. pos2 := 0
  140. for pos < rb.highlowcontainer.size() {
  141. hs := uint32(rb.highlowcontainer.getKeyAtIndex(pos)) << 16
  142. c := rb.highlowcontainer.getContainerAtIndex(pos)
  143. pos++
  144. c.fillLeastSignificant16bits(array, pos2, hs)
  145. pos2 += c.getCardinality()
  146. }
  147. return array
  148. }
  149. // GetSizeInBytes estimates the memory usage of the Bitmap. Note that this
  150. // might differ slightly from the amount of bytes required for persistent storage
  151. func (rb *Bitmap) GetSizeInBytes() uint64 {
  152. size := uint64(8)
  153. for _, c := range rb.highlowcontainer.containers {
  154. size += uint64(2) + uint64(c.getSizeInBytes())
  155. }
  156. return size
  157. }
  158. // GetSerializedSizeInBytes computes the serialized size in bytes
  159. // of the Bitmap. It should correspond to the
  160. // number of bytes written when invoking WriteTo. You can expect
  161. // that this function is much cheaper computationally than WriteTo.
  162. func (rb *Bitmap) GetSerializedSizeInBytes() uint64 {
  163. return rb.highlowcontainer.serializedSizeInBytes()
  164. }
  165. // BoundSerializedSizeInBytes returns an upper bound on the serialized size in bytes
  166. // assuming that one wants to store "cardinality" integers in [0, universe_size)
  167. func BoundSerializedSizeInBytes(cardinality uint64, universeSize uint64) uint64 {
  168. contnbr := (universeSize + uint64(65535)) / uint64(65536)
  169. if contnbr > cardinality {
  170. contnbr = cardinality
  171. // we can't have more containers than we have values
  172. }
  173. headermax := 8*contnbr + 4
  174. if 4 > (contnbr+7)/8 {
  175. headermax += 4
  176. } else {
  177. headermax += (contnbr + 7) / 8
  178. }
  179. valsarray := uint64(arrayContainerSizeInBytes(int(cardinality)))
  180. valsbitmap := contnbr * uint64(bitmapContainerSizeInBytes())
  181. valsbest := valsarray
  182. if valsbest > valsbitmap {
  183. valsbest = valsbitmap
  184. }
  185. return valsbest + headermax
  186. }
  187. // IntIterable allows you to iterate over the values in a Bitmap
  188. type IntIterable interface {
  189. HasNext() bool
  190. Next() uint32
  191. }
  192. type intIterator struct {
  193. pos int
  194. hs uint32
  195. iter shortIterable
  196. highlowcontainer *roaringArray
  197. }
  198. // HasNext returns true if there are more integers to iterate over
  199. func (ii *intIterator) HasNext() bool {
  200. return ii.pos < ii.highlowcontainer.size()
  201. }
  202. func (ii *intIterator) init() {
  203. if ii.highlowcontainer.size() > ii.pos {
  204. ii.iter = ii.highlowcontainer.getContainerAtIndex(ii.pos).getShortIterator()
  205. ii.hs = uint32(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
  206. }
  207. }
  208. // Next returns the next integer
  209. func (ii *intIterator) Next() uint32 {
  210. x := uint32(ii.iter.next()) | ii.hs
  211. if !ii.iter.hasNext() {
  212. ii.pos = ii.pos + 1
  213. ii.init()
  214. }
  215. return x
  216. }
  217. func newIntIterator(a *Bitmap) *intIterator {
  218. p := new(intIterator)
  219. p.pos = 0
  220. p.highlowcontainer = &a.highlowcontainer
  221. p.init()
  222. return p
  223. }
  224. // ManyIntIterable allows you to iterate over the values in a Bitmap
  225. type ManyIntIterable interface {
  226. // pass in a buffer to fill up with values, returns how many values were returned
  227. NextMany([]uint32) int
  228. }
  229. type manyIntIterator struct {
  230. pos int
  231. hs uint32
  232. iter manyIterable
  233. highlowcontainer *roaringArray
  234. }
  235. func (ii *manyIntIterator) init() {
  236. if ii.highlowcontainer.size() > ii.pos {
  237. ii.iter = ii.highlowcontainer.getContainerAtIndex(ii.pos).getManyIterator()
  238. ii.hs = uint32(ii.highlowcontainer.getKeyAtIndex(ii.pos)) << 16
  239. } else {
  240. ii.iter = nil
  241. }
  242. }
  243. func (ii *manyIntIterator) NextMany(buf []uint32) int {
  244. n := 0
  245. for n < len(buf) {
  246. if ii.iter == nil {
  247. break
  248. }
  249. moreN := ii.iter.nextMany(ii.hs, buf[n:])
  250. n += moreN
  251. if moreN == 0 {
  252. ii.pos = ii.pos + 1
  253. ii.init()
  254. }
  255. }
  256. return n
  257. }
  258. func newManyIntIterator(a *Bitmap) *manyIntIterator {
  259. p := new(manyIntIterator)
  260. p.pos = 0
  261. p.highlowcontainer = &a.highlowcontainer
  262. p.init()
  263. return p
  264. }
  265. // String creates a string representation of the Bitmap
  266. func (rb *Bitmap) String() string {
  267. // inspired by https://github.com/fzandona/goroar/
  268. var buffer bytes.Buffer
  269. start := []byte("{")
  270. buffer.Write(start)
  271. i := rb.Iterator()
  272. counter := 0
  273. if i.HasNext() {
  274. counter = counter + 1
  275. buffer.WriteString(strconv.FormatInt(int64(i.Next()), 10))
  276. }
  277. for i.HasNext() {
  278. buffer.WriteString(",")
  279. counter = counter + 1
  280. // to avoid exhausting the memory
  281. if counter > 0x40000 {
  282. buffer.WriteString("...")
  283. break
  284. }
  285. buffer.WriteString(strconv.FormatInt(int64(i.Next()), 10))
  286. }
  287. buffer.WriteString("}")
  288. return buffer.String()
  289. }
  290. // Iterator creates a new IntIterable to iterate over the integers contained in the bitmap, in sorted order
  291. func (rb *Bitmap) Iterator() IntIterable {
  292. return newIntIterator(rb)
  293. }
  294. // Iterator creates a new ManyIntIterable to iterate over the integers contained in the bitmap, in sorted order
  295. func (rb *Bitmap) ManyIterator() ManyIntIterable {
  296. return newManyIntIterator(rb)
  297. }
  298. // Clone creates a copy of the Bitmap
  299. func (rb *Bitmap) Clone() *Bitmap {
  300. ptr := new(Bitmap)
  301. ptr.highlowcontainer = *rb.highlowcontainer.clone()
  302. return ptr
  303. }
  304. // Minimum get the smallest value stored in this roaring bitmap, assumes that it is not empty
  305. func (rb *Bitmap) Minimum() uint32 {
  306. return uint32(rb.highlowcontainer.containers[0].minimum()) | (uint32(rb.highlowcontainer.keys[0]) << 16)
  307. }
  308. // Maximum get the largest value stored in this roaring bitmap, assumes that it is not empty
  309. func (rb *Bitmap) Maximum() uint32 {
  310. lastindex := len(rb.highlowcontainer.containers) - 1
  311. return uint32(rb.highlowcontainer.containers[lastindex].maximum()) | (uint32(rb.highlowcontainer.keys[lastindex]) << 16)
  312. }
  313. // Contains returns true if the integer is contained in the bitmap
  314. func (rb *Bitmap) Contains(x uint32) bool {
  315. hb := highbits(x)
  316. c := rb.highlowcontainer.getContainer(hb)
  317. return c != nil && c.contains(lowbits(x))
  318. }
  319. // ContainsInt returns true if the integer is contained in the bitmap (this is a convenience method, the parameter is casted to uint32 and Contains is called)
  320. func (rb *Bitmap) ContainsInt(x int) bool {
  321. return rb.Contains(uint32(x))
  322. }
  323. // Equals returns true if the two bitmaps contain the same integers
  324. func (rb *Bitmap) Equals(o interface{}) bool {
  325. srb, ok := o.(*Bitmap)
  326. if ok {
  327. return srb.highlowcontainer.equals(rb.highlowcontainer)
  328. }
  329. return false
  330. }
  331. // Add the integer x to the bitmap
  332. func (rb *Bitmap) Add(x uint32) {
  333. hb := highbits(x)
  334. ra := &rb.highlowcontainer
  335. i := ra.getIndex(hb)
  336. if i >= 0 {
  337. var c container
  338. c = ra.getWritableContainerAtIndex(i).iaddReturnMinimized(lowbits(x))
  339. rb.highlowcontainer.setContainerAtIndex(i, c)
  340. } else {
  341. newac := newArrayContainer()
  342. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.iaddReturnMinimized(lowbits(x)))
  343. }
  344. }
  345. // add the integer x to the bitmap, return the container and its index
  346. func (rb *Bitmap) addwithptr(x uint32) (int, container) {
  347. hb := highbits(x)
  348. ra := &rb.highlowcontainer
  349. i := ra.getIndex(hb)
  350. var c container
  351. if i >= 0 {
  352. c = ra.getWritableContainerAtIndex(i).iaddReturnMinimized(lowbits(x))
  353. rb.highlowcontainer.setContainerAtIndex(i, c)
  354. return i, c
  355. }
  356. newac := newArrayContainer()
  357. c = newac.iaddReturnMinimized(lowbits(x))
  358. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, c)
  359. return -i - 1, c
  360. }
  361. // CheckedAdd adds the integer x to the bitmap and return true if it was added (false if the integer was already present)
  362. func (rb *Bitmap) CheckedAdd(x uint32) bool {
  363. // TODO: add unit tests for this method
  364. hb := highbits(x)
  365. i := rb.highlowcontainer.getIndex(hb)
  366. if i >= 0 {
  367. C := rb.highlowcontainer.getWritableContainerAtIndex(i)
  368. oldcard := C.getCardinality()
  369. C = C.iaddReturnMinimized(lowbits(x))
  370. rb.highlowcontainer.setContainerAtIndex(i, C)
  371. return C.getCardinality() > oldcard
  372. }
  373. newac := newArrayContainer()
  374. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, newac.iaddReturnMinimized(lowbits(x)))
  375. return true
  376. }
  377. // AddInt adds the integer x to the bitmap (convenience method: the parameter is casted to uint32 and we call Add)
  378. func (rb *Bitmap) AddInt(x int) {
  379. rb.Add(uint32(x))
  380. }
  381. // Remove the integer x from the bitmap
  382. func (rb *Bitmap) Remove(x uint32) {
  383. hb := highbits(x)
  384. i := rb.highlowcontainer.getIndex(hb)
  385. if i >= 0 {
  386. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iremoveReturnMinimized(lowbits(x))
  387. rb.highlowcontainer.setContainerAtIndex(i, c)
  388. if rb.highlowcontainer.getContainerAtIndex(i).getCardinality() == 0 {
  389. rb.highlowcontainer.removeAtIndex(i)
  390. }
  391. }
  392. }
  393. // CheckedRemove removes the integer x from the bitmap and return true if the integer was effectively remove (and false if the integer was not present)
  394. func (rb *Bitmap) CheckedRemove(x uint32) bool {
  395. // TODO: add unit tests for this method
  396. hb := highbits(x)
  397. i := rb.highlowcontainer.getIndex(hb)
  398. if i >= 0 {
  399. C := rb.highlowcontainer.getWritableContainerAtIndex(i)
  400. oldcard := C.getCardinality()
  401. C = C.iremoveReturnMinimized(lowbits(x))
  402. rb.highlowcontainer.setContainerAtIndex(i, C)
  403. if rb.highlowcontainer.getContainerAtIndex(i).getCardinality() == 0 {
  404. rb.highlowcontainer.removeAtIndex(i)
  405. return true
  406. }
  407. return C.getCardinality() < oldcard
  408. }
  409. return false
  410. }
  411. // IsEmpty returns true if the Bitmap is empty (it is faster than doing (GetCardinality() == 0))
  412. func (rb *Bitmap) IsEmpty() bool {
  413. return rb.highlowcontainer.size() == 0
  414. }
  415. // GetCardinality returns the number of integers contained in the bitmap
  416. func (rb *Bitmap) GetCardinality() uint64 {
  417. size := uint64(0)
  418. for _, c := range rb.highlowcontainer.containers {
  419. size += uint64(c.getCardinality())
  420. }
  421. return size
  422. }
  423. // Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality())
  424. func (rb *Bitmap) Rank(x uint32) uint64 {
  425. size := uint64(0)
  426. for i := 0; i < rb.highlowcontainer.size(); i++ {
  427. key := rb.highlowcontainer.getKeyAtIndex(i)
  428. if key > highbits(x) {
  429. return size
  430. }
  431. if key < highbits(x) {
  432. size += uint64(rb.highlowcontainer.getContainerAtIndex(i).getCardinality())
  433. } else {
  434. return size + uint64(rb.highlowcontainer.getContainerAtIndex(i).rank(lowbits(x)))
  435. }
  436. }
  437. return size
  438. }
  439. // Select returns the xth integer in the bitmap
  440. func (rb *Bitmap) Select(x uint32) (uint32, error) {
  441. if rb.GetCardinality() <= uint64(x) {
  442. return 0, fmt.Errorf("can't find %dth integer in a bitmap with only %d items", x, rb.GetCardinality())
  443. }
  444. remaining := x
  445. for i := 0; i < rb.highlowcontainer.size(); i++ {
  446. c := rb.highlowcontainer.getContainerAtIndex(i)
  447. if remaining >= uint32(c.getCardinality()) {
  448. remaining -= uint32(c.getCardinality())
  449. } else {
  450. key := rb.highlowcontainer.getKeyAtIndex(i)
  451. return uint32(key)<<16 + uint32(c.selectInt(uint16(remaining))), nil
  452. }
  453. }
  454. return 0, fmt.Errorf("can't find %dth integer in a bitmap with only %d items", x, rb.GetCardinality())
  455. }
  456. // And computes the intersection between two bitmaps and stores the result in the current bitmap
  457. func (rb *Bitmap) And(x2 *Bitmap) {
  458. pos1 := 0
  459. pos2 := 0
  460. intersectionsize := 0
  461. length1 := rb.highlowcontainer.size()
  462. length2 := x2.highlowcontainer.size()
  463. main:
  464. for {
  465. if pos1 < length1 && pos2 < length2 {
  466. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  467. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  468. for {
  469. if s1 == s2 {
  470. c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
  471. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  472. diff := c1.iand(c2)
  473. if diff.getCardinality() > 0 {
  474. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff, false)
  475. intersectionsize++
  476. }
  477. pos1++
  478. pos2++
  479. if (pos1 == length1) || (pos2 == length2) {
  480. break main
  481. }
  482. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  483. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  484. } else if s1 < s2 {
  485. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  486. if pos1 == length1 {
  487. break main
  488. }
  489. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  490. } else { //s1 > s2
  491. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  492. if pos2 == length2 {
  493. break main
  494. }
  495. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  496. }
  497. }
  498. } else {
  499. break
  500. }
  501. }
  502. rb.highlowcontainer.resize(intersectionsize)
  503. }
  504. // OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
  505. func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {
  506. pos1 := 0
  507. pos2 := 0
  508. length1 := rb.highlowcontainer.size()
  509. length2 := x2.highlowcontainer.size()
  510. answer := uint64(0)
  511. main:
  512. for {
  513. if (pos1 < length1) && (pos2 < length2) {
  514. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  515. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  516. for {
  517. if s1 < s2 {
  518. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
  519. pos1++
  520. if pos1 == length1 {
  521. break main
  522. }
  523. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  524. } else if s1 > s2 {
  525. answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
  526. pos2++
  527. if pos2 == length2 {
  528. break main
  529. }
  530. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  531. } else {
  532. // TODO: could be faster if we did not have to materialize the container
  533. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)).getCardinality())
  534. pos1++
  535. pos2++
  536. if (pos1 == length1) || (pos2 == length2) {
  537. break main
  538. }
  539. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  540. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  541. }
  542. }
  543. } else {
  544. break
  545. }
  546. }
  547. for ; pos1 < length1; pos1++ {
  548. answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
  549. }
  550. for ; pos2 < length2; pos2++ {
  551. answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
  552. }
  553. return answer
  554. }
  555. // AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
  556. func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {
  557. pos1 := 0
  558. pos2 := 0
  559. answer := uint64(0)
  560. length1 := rb.highlowcontainer.size()
  561. length2 := x2.highlowcontainer.size()
  562. main:
  563. for {
  564. if pos1 < length1 && pos2 < length2 {
  565. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  566. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  567. for {
  568. if s1 == s2 {
  569. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  570. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  571. answer += uint64(c1.andCardinality(c2))
  572. pos1++
  573. pos2++
  574. if (pos1 == length1) || (pos2 == length2) {
  575. break main
  576. }
  577. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  578. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  579. } else if s1 < s2 {
  580. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  581. if pos1 == length1 {
  582. break main
  583. }
  584. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  585. } else { //s1 > s2
  586. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  587. if pos2 == length2 {
  588. break main
  589. }
  590. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  591. }
  592. }
  593. } else {
  594. break
  595. }
  596. }
  597. return answer
  598. }
  599. // Intersects checks whether two bitmap intersects, bitmaps are not modified
  600. func (rb *Bitmap) Intersects(x2 *Bitmap) bool {
  601. pos1 := 0
  602. pos2 := 0
  603. length1 := rb.highlowcontainer.size()
  604. length2 := x2.highlowcontainer.size()
  605. main:
  606. for {
  607. if pos1 < length1 && pos2 < length2 {
  608. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  609. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  610. for {
  611. if s1 == s2 {
  612. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  613. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  614. if c1.intersects(c2) {
  615. return true
  616. }
  617. pos1++
  618. pos2++
  619. if (pos1 == length1) || (pos2 == length2) {
  620. break main
  621. }
  622. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  623. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  624. } else if s1 < s2 {
  625. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  626. if pos1 == length1 {
  627. break main
  628. }
  629. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  630. } else { //s1 > s2
  631. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  632. if pos2 == length2 {
  633. break main
  634. }
  635. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  636. }
  637. }
  638. } else {
  639. break
  640. }
  641. }
  642. return false
  643. }
  644. // Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap
  645. func (rb *Bitmap) Xor(x2 *Bitmap) {
  646. pos1 := 0
  647. pos2 := 0
  648. length1 := rb.highlowcontainer.size()
  649. length2 := x2.highlowcontainer.size()
  650. for {
  651. if (pos1 < length1) && (pos2 < length2) {
  652. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  653. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  654. if s1 < s2 {
  655. pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
  656. if pos1 == length1 {
  657. break
  658. }
  659. } else if s1 > s2 {
  660. c := x2.highlowcontainer.getWritableContainerAtIndex(pos2)
  661. rb.highlowcontainer.insertNewKeyValueAt(pos1, x2.highlowcontainer.getKeyAtIndex(pos2), c)
  662. length1++
  663. pos1++
  664. pos2++
  665. } else {
  666. // TODO: couple be computed in-place for reduced memory usage
  667. c := rb.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
  668. if c.getCardinality() > 0 {
  669. rb.highlowcontainer.setContainerAtIndex(pos1, c)
  670. pos1++
  671. } else {
  672. rb.highlowcontainer.removeAtIndex(pos1)
  673. length1--
  674. }
  675. pos2++
  676. }
  677. } else {
  678. break
  679. }
  680. }
  681. if pos1 == length1 {
  682. rb.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  683. }
  684. }
  685. // Or computes the union between two bitmaps and stores the result in the current bitmap
  686. func (rb *Bitmap) Or(x2 *Bitmap) {
  687. pos1 := 0
  688. pos2 := 0
  689. length1 := rb.highlowcontainer.size()
  690. length2 := x2.highlowcontainer.size()
  691. main:
  692. for (pos1 < length1) && (pos2 < length2) {
  693. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  694. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  695. for {
  696. if s1 < s2 {
  697. pos1++
  698. if pos1 == length1 {
  699. break main
  700. }
  701. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  702. } else if s1 > s2 {
  703. rb.highlowcontainer.insertNewKeyValueAt(pos1, s2, x2.highlowcontainer.getContainerAtIndex(pos2).clone())
  704. pos1++
  705. length1++
  706. pos2++
  707. if pos2 == length2 {
  708. break main
  709. }
  710. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  711. } else {
  712. rb.highlowcontainer.replaceKeyAndContainerAtIndex(pos1, s1, rb.highlowcontainer.getWritableContainerAtIndex(pos1).ior(x2.highlowcontainer.getContainerAtIndex(pos2)), false)
  713. pos1++
  714. pos2++
  715. if (pos1 == length1) || (pos2 == length2) {
  716. break main
  717. }
  718. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  719. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  720. }
  721. }
  722. }
  723. if pos1 == length1 {
  724. rb.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  725. }
  726. }
  727. /*func (rb *Bitmap) Or(x2 *Bitmap) {
  728. results := Or(rb, x2) // Todo: could be computed in-place for reduced memory usage
  729. rb.highlowcontainer = results.highlowcontainer
  730. }*/
  731. // AndNot computes the difference between two bitmaps and stores the result in the current bitmap
  732. func (rb *Bitmap) AndNot(x2 *Bitmap) {
  733. pos1 := 0
  734. pos2 := 0
  735. intersectionsize := 0
  736. length1 := rb.highlowcontainer.size()
  737. length2 := x2.highlowcontainer.size()
  738. main:
  739. for {
  740. if pos1 < length1 && pos2 < length2 {
  741. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  742. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  743. for {
  744. if s1 == s2 {
  745. c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
  746. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  747. diff := c1.iandNot(c2)
  748. if diff.getCardinality() > 0 {
  749. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff, false)
  750. intersectionsize++
  751. }
  752. pos1++
  753. pos2++
  754. if (pos1 == length1) || (pos2 == length2) {
  755. break main
  756. }
  757. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  758. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  759. } else if s1 < s2 {
  760. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  761. mustCopyOnWrite := rb.highlowcontainer.needsCopyOnWrite(pos1)
  762. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1, mustCopyOnWrite)
  763. intersectionsize++
  764. pos1++
  765. if pos1 == length1 {
  766. break main
  767. }
  768. s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
  769. } else { //s1 > s2
  770. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  771. if pos2 == length2 {
  772. break main
  773. }
  774. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  775. }
  776. }
  777. } else {
  778. break
  779. }
  780. }
  781. // TODO:implement as a copy
  782. for pos1 < length1 {
  783. c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
  784. s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
  785. mustCopyOnWrite := rb.highlowcontainer.needsCopyOnWrite(pos1)
  786. rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, c1, mustCopyOnWrite)
  787. intersectionsize++
  788. pos1++
  789. }
  790. rb.highlowcontainer.resize(intersectionsize)
  791. }
  792. // Or computes the union between two bitmaps and returns the result
  793. func Or(x1, x2 *Bitmap) *Bitmap {
  794. answer := NewBitmap()
  795. pos1 := 0
  796. pos2 := 0
  797. length1 := x1.highlowcontainer.size()
  798. length2 := x2.highlowcontainer.size()
  799. main:
  800. for (pos1 < length1) && (pos2 < length2) {
  801. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  802. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  803. for {
  804. if s1 < s2 {
  805. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  806. pos1++
  807. if pos1 == length1 {
  808. break main
  809. }
  810. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  811. } else if s1 > s2 {
  812. answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
  813. pos2++
  814. if pos2 == length2 {
  815. break main
  816. }
  817. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  818. } else {
  819. answer.highlowcontainer.appendContainer(s1, x1.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)), false)
  820. pos1++
  821. pos2++
  822. if (pos1 == length1) || (pos2 == length2) {
  823. break main
  824. }
  825. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  826. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  827. }
  828. }
  829. }
  830. if pos1 == length1 {
  831. answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  832. } else if pos2 == length2 {
  833. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  834. }
  835. return answer
  836. }
  837. // And computes the intersection between two bitmaps and returns the result
  838. func And(x1, x2 *Bitmap) *Bitmap {
  839. answer := NewBitmap()
  840. pos1 := 0
  841. pos2 := 0
  842. length1 := x1.highlowcontainer.size()
  843. length2 := x2.highlowcontainer.size()
  844. main:
  845. for pos1 < length1 && pos2 < length2 {
  846. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  847. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  848. for {
  849. if s1 == s2 {
  850. C := x1.highlowcontainer.getContainerAtIndex(pos1)
  851. C = C.and(x2.highlowcontainer.getContainerAtIndex(pos2))
  852. if C.getCardinality() > 0 {
  853. answer.highlowcontainer.appendContainer(s1, C, false)
  854. }
  855. pos1++
  856. pos2++
  857. if (pos1 == length1) || (pos2 == length2) {
  858. break main
  859. }
  860. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  861. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  862. } else if s1 < s2 {
  863. pos1 = x1.highlowcontainer.advanceUntil(s2, pos1)
  864. if pos1 == length1 {
  865. break main
  866. }
  867. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  868. } else { // s1 > s2
  869. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  870. if pos2 == length2 {
  871. break main
  872. }
  873. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  874. }
  875. }
  876. }
  877. return answer
  878. }
  879. // Xor computes the symmetric difference between two bitmaps and returns the result
  880. func Xor(x1, x2 *Bitmap) *Bitmap {
  881. answer := NewBitmap()
  882. pos1 := 0
  883. pos2 := 0
  884. length1 := x1.highlowcontainer.size()
  885. length2 := x2.highlowcontainer.size()
  886. for {
  887. if (pos1 < length1) && (pos2 < length2) {
  888. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  889. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  890. if s1 < s2 {
  891. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  892. pos1++
  893. } else if s1 > s2 {
  894. answer.highlowcontainer.appendCopy(x2.highlowcontainer, pos2)
  895. pos2++
  896. } else {
  897. c := x1.highlowcontainer.getContainerAtIndex(pos1).xor(x2.highlowcontainer.getContainerAtIndex(pos2))
  898. if c.getCardinality() > 0 {
  899. answer.highlowcontainer.appendContainer(s1, c, false)
  900. }
  901. pos1++
  902. pos2++
  903. }
  904. } else {
  905. break
  906. }
  907. }
  908. if pos1 == length1 {
  909. answer.highlowcontainer.appendCopyMany(x2.highlowcontainer, pos2, length2)
  910. } else if pos2 == length2 {
  911. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  912. }
  913. return answer
  914. }
  915. // AndNot computes the difference between two bitmaps and returns the result
  916. func AndNot(x1, x2 *Bitmap) *Bitmap {
  917. answer := NewBitmap()
  918. pos1 := 0
  919. pos2 := 0
  920. length1 := x1.highlowcontainer.size()
  921. length2 := x2.highlowcontainer.size()
  922. main:
  923. for {
  924. if pos1 < length1 && pos2 < length2 {
  925. s1 := x1.highlowcontainer.getKeyAtIndex(pos1)
  926. s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
  927. for {
  928. if s1 < s2 {
  929. answer.highlowcontainer.appendCopy(x1.highlowcontainer, pos1)
  930. pos1++
  931. if pos1 == length1 {
  932. break main
  933. }
  934. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  935. } else if s1 == s2 {
  936. c1 := x1.highlowcontainer.getContainerAtIndex(pos1)
  937. c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
  938. diff := c1.andNot(c2)
  939. if diff.getCardinality() > 0 {
  940. answer.highlowcontainer.appendContainer(s1, diff, false)
  941. }
  942. pos1++
  943. pos2++
  944. if (pos1 == length1) || (pos2 == length2) {
  945. break main
  946. }
  947. s1 = x1.highlowcontainer.getKeyAtIndex(pos1)
  948. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  949. } else { //s1 > s2
  950. pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
  951. if pos2 == length2 {
  952. break main
  953. }
  954. s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
  955. }
  956. }
  957. } else {
  958. break
  959. }
  960. }
  961. if pos2 == length2 {
  962. answer.highlowcontainer.appendCopyMany(x1.highlowcontainer, pos1, length1)
  963. }
  964. return answer
  965. }
  966. // AddMany add all of the values in dat
  967. func (rb *Bitmap) AddMany(dat []uint32) {
  968. if len(dat) == 0 {
  969. return
  970. }
  971. prev := dat[0]
  972. idx, c := rb.addwithptr(prev)
  973. for _, i := range dat[1:] {
  974. if highbits(prev) == highbits(i) {
  975. c = c.iaddReturnMinimized(lowbits(i))
  976. rb.highlowcontainer.setContainerAtIndex(idx, c)
  977. } else {
  978. idx, c = rb.addwithptr(i)
  979. }
  980. prev = i
  981. }
  982. }
  983. // BitmapOf generates a new bitmap filled with the specified integers
  984. func BitmapOf(dat ...uint32) *Bitmap {
  985. ans := NewBitmap()
  986. ans.AddMany(dat)
  987. return ans
  988. }
  989. // Flip negates the bits in the given range (i.e., [rangeStart,rangeEnd)), any integer present in this range and in the bitmap is removed,
  990. // and any integer present in the range and not in the bitmap is added.
  991. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  992. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  993. func (rb *Bitmap) Flip(rangeStart, rangeEnd uint64) {
  994. if rangeEnd > MaxUint32+1 {
  995. panic("rangeEnd > MaxUint32+1")
  996. }
  997. if rangeStart > MaxUint32+1 {
  998. panic("rangeStart > MaxUint32+1")
  999. }
  1000. if rangeStart >= rangeEnd {
  1001. return
  1002. }
  1003. hbStart := highbits(uint32(rangeStart))
  1004. lbStart := lowbits(uint32(rangeStart))
  1005. hbLast := highbits(uint32(rangeEnd - 1))
  1006. lbLast := lowbits(uint32(rangeEnd - 1))
  1007. var max uint32 = maxLowBit
  1008. for hb := hbStart; hb <= hbLast; hb++ {
  1009. var containerStart uint32
  1010. if hb == hbStart {
  1011. containerStart = uint32(lbStart)
  1012. }
  1013. containerLast := max
  1014. if hb == hbLast {
  1015. containerLast = uint32(lbLast)
  1016. }
  1017. i := rb.highlowcontainer.getIndex(hb)
  1018. if i >= 0 {
  1019. c := rb.highlowcontainer.getWritableContainerAtIndex(i).inot(int(containerStart), int(containerLast)+1)
  1020. if c.getCardinality() > 0 {
  1021. rb.highlowcontainer.setContainerAtIndex(i, c)
  1022. } else {
  1023. rb.highlowcontainer.removeAtIndex(i)
  1024. }
  1025. } else { // *think* the range of ones must never be
  1026. // empty.
  1027. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, rangeOfOnes(int(containerStart), int(containerLast)))
  1028. }
  1029. }
  1030. }
  1031. // FlipInt calls Flip after casting the parameters (convenience method)
  1032. func (rb *Bitmap) FlipInt(rangeStart, rangeEnd int) {
  1033. rb.Flip(uint64(rangeStart), uint64(rangeEnd))
  1034. }
  1035. // AddRange adds the integers in [rangeStart, rangeEnd) to the bitmap.
  1036. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1037. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1038. func (rb *Bitmap) AddRange(rangeStart, rangeEnd uint64) {
  1039. if rangeStart >= rangeEnd {
  1040. return
  1041. }
  1042. if rangeEnd-1 > MaxUint32 {
  1043. panic("rangeEnd-1 > MaxUint32")
  1044. }
  1045. hbStart := uint32(highbits(uint32(rangeStart)))
  1046. lbStart := uint32(lowbits(uint32(rangeStart)))
  1047. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1048. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1049. var max uint32 = maxLowBit
  1050. for hb := uint16(hbStart); hb <= uint16(hbLast); hb++ {
  1051. containerStart := uint32(0)
  1052. if hb == uint16(hbStart) {
  1053. containerStart = lbStart
  1054. }
  1055. containerLast := max
  1056. if hb == uint16(hbLast) {
  1057. containerLast = lbLast
  1058. }
  1059. i := rb.highlowcontainer.getIndex(hb)
  1060. if i >= 0 {
  1061. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iaddRange(int(containerStart), int(containerLast)+1)
  1062. rb.highlowcontainer.setContainerAtIndex(i, c)
  1063. } else { // *think* the range of ones must never be
  1064. // empty.
  1065. rb.highlowcontainer.insertNewKeyValueAt(-i-1, hb, rangeOfOnes(int(containerStart), int(containerLast)))
  1066. }
  1067. }
  1068. }
  1069. // RemoveRange removes the integers in [rangeStart, rangeEnd) from the bitmap.
  1070. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1071. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1072. func (rb *Bitmap) RemoveRange(rangeStart, rangeEnd uint64) {
  1073. if rangeStart >= rangeEnd {
  1074. return
  1075. }
  1076. if rangeEnd-1 > MaxUint32 {
  1077. // logically, we should assume that the user wants to
  1078. // remove all values from rangeStart to infinity
  1079. // see https://github.com/RoaringBitmap/roaring/issues/141
  1080. rangeEnd = uint64(0x100000000)
  1081. }
  1082. hbStart := uint32(highbits(uint32(rangeStart)))
  1083. lbStart := uint32(lowbits(uint32(rangeStart)))
  1084. hbLast := uint32(highbits(uint32(rangeEnd - 1)))
  1085. lbLast := uint32(lowbits(uint32(rangeEnd - 1)))
  1086. var max uint32 = maxLowBit
  1087. if hbStart == hbLast {
  1088. i := rb.highlowcontainer.getIndex(uint16(hbStart))
  1089. if i < 0 {
  1090. return
  1091. }
  1092. c := rb.highlowcontainer.getWritableContainerAtIndex(i).iremoveRange(int(lbStart), int(lbLast+1))
  1093. if c.getCardinality() > 0 {
  1094. rb.highlowcontainer.setContainerAtIndex(i, c)
  1095. } else {
  1096. rb.highlowcontainer.removeAtIndex(i)
  1097. }
  1098. return
  1099. }
  1100. ifirst := rb.highlowcontainer.getIndex(uint16(hbStart))
  1101. ilast := rb.highlowcontainer.getIndex(uint16(hbLast))
  1102. if ifirst >= 0 {
  1103. if lbStart != 0 {
  1104. c := rb.highlowcontainer.getWritableContainerAtIndex(ifirst).iremoveRange(int(lbStart), int(max+1))
  1105. if c.getCardinality() > 0 {
  1106. rb.highlowcontainer.setContainerAtIndex(ifirst, c)
  1107. ifirst++
  1108. }
  1109. }
  1110. } else {
  1111. ifirst = -ifirst - 1
  1112. }
  1113. if ilast >= 0 {
  1114. if lbLast != max {
  1115. c := rb.highlowcontainer.getWritableContainerAtIndex(ilast).iremoveRange(int(0), int(lbLast+1))
  1116. if c.getCardinality() > 0 {
  1117. rb.highlowcontainer.setContainerAtIndex(ilast, c)
  1118. } else {
  1119. ilast++
  1120. }
  1121. } else {
  1122. ilast++
  1123. }
  1124. } else {
  1125. ilast = -ilast - 1
  1126. }
  1127. rb.highlowcontainer.removeIndexRange(ifirst, ilast)
  1128. }
  1129. // Flip negates the bits in the given range (i.e., [rangeStart,rangeEnd)), any integer present in this range and in the bitmap is removed,
  1130. // and any integer present in the range and not in the bitmap is added, a new bitmap is returned leaving
  1131. // the current bitmap unchanged.
  1132. // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range
  1133. // while uint64(0x100000000) cannot be represented as a 32-bit value.
  1134. func Flip(bm *Bitmap, rangeStart, rangeEnd uint64) *Bitmap {
  1135. if rangeStart >= rangeEnd {
  1136. return bm.Clone()
  1137. }
  1138. if rangeStart > MaxUint32 {
  1139. panic("rangeStart > MaxUint32")
  1140. }
  1141. if rangeEnd-1 > MaxUint32 {
  1142. panic("rangeEnd-1 > MaxUint32")
  1143. }
  1144. answer := NewBitmap()
  1145. hbStart := highbits(uint32(rangeStart))
  1146. lbStart := lowbits(uint32(rangeStart))
  1147. hbLast := highbits(uint32(rangeEnd - 1))
  1148. lbLast := lowbits(uint32(rangeEnd - 1))
  1149. // copy the containers before the active area
  1150. answer.highlowcontainer.appendCopiesUntil(bm.highlowcontainer, hbStart)
  1151. var max uint32 = maxLowBit
  1152. for hb := hbStart; hb <= hbLast; hb++ {
  1153. var containerStart uint32
  1154. if hb == hbStart {
  1155. containerStart = uint32(lbStart)
  1156. }
  1157. containerLast := max
  1158. if hb == hbLast {
  1159. containerLast = uint32(lbLast)
  1160. }
  1161. i := bm.highlowcontainer.getIndex(hb)
  1162. j := answer.highlowcontainer.getIndex(hb)
  1163. if i >= 0 {
  1164. c := bm.highlowcontainer.getContainerAtIndex(i).not(int(containerStart), int(containerLast)+1)
  1165. if c.getCardinality() > 0 {
  1166. answer.highlowcontainer.insertNewKeyValueAt(-j-1, hb, c)
  1167. }
  1168. } else { // *think* the range of ones must never be
  1169. // empty.
  1170. answer.highlowcontainer.insertNewKeyValueAt(-j-1, hb,
  1171. rangeOfOnes(int(containerStart), int(containerLast)))
  1172. }
  1173. }
  1174. // copy the containers after the active area.
  1175. answer.highlowcontainer.appendCopiesAfter(bm.highlowcontainer, hbLast)
  1176. return answer
  1177. }
  1178. // SetCopyOnWrite sets this bitmap to use copy-on-write so that copies are fast and memory conscious
  1179. // if the parameter is true, otherwise we leave the default where hard copies are made
  1180. // (copy-on-write requires extra care in a threaded context).
  1181. // Calling SetCopyOnWrite(true) on a bitmap created with FromBuffer is unsafe.
  1182. func (rb *Bitmap) SetCopyOnWrite(val bool) {
  1183. rb.highlowcontainer.copyOnWrite = val
  1184. }
  1185. // GetCopyOnWrite gets this bitmap's copy-on-write property
  1186. func (rb *Bitmap) GetCopyOnWrite() (val bool) {
  1187. return rb.highlowcontainer.copyOnWrite
  1188. }
  1189. // FlipInt calls Flip after casting the parameters (convenience method)
  1190. func FlipInt(bm *Bitmap, rangeStart, rangeEnd int) *Bitmap {
  1191. return Flip(bm, uint64(rangeStart), uint64(rangeEnd))
  1192. }
  1193. // Statistics provides details on the container types in use.
  1194. type Statistics struct {
  1195. Cardinality uint64
  1196. Containers uint64
  1197. ArrayContainers uint64
  1198. ArrayContainerBytes uint64
  1199. ArrayContainerValues uint64
  1200. BitmapContainers uint64
  1201. BitmapContainerBytes uint64
  1202. BitmapContainerValues uint64
  1203. RunContainers uint64
  1204. RunContainerBytes uint64
  1205. RunContainerValues uint64
  1206. }
  1207. // Stats returns details on container type usage in a Statistics struct.
  1208. func (rb *Bitmap) Stats() Statistics {
  1209. stats := Statistics{}
  1210. stats.Containers = uint64(len(rb.highlowcontainer.containers))
  1211. for _, c := range rb.highlowcontainer.containers {
  1212. stats.Cardinality += uint64(c.getCardinality())
  1213. switch c.(type) {
  1214. case *arrayContainer:
  1215. stats.ArrayContainers++
  1216. stats.ArrayContainerBytes += uint64(c.getSizeInBytes())
  1217. stats.ArrayContainerValues += uint64(c.getCardinality())
  1218. case *bitmapContainer:
  1219. stats.BitmapContainers++
  1220. stats.BitmapContainerBytes += uint64(c.getSizeInBytes())
  1221. stats.BitmapContainerValues += uint64(c.getCardinality())
  1222. case *runContainer16:
  1223. stats.RunContainers++
  1224. stats.RunContainerBytes += uint64(c.getSizeInBytes())
  1225. stats.RunContainerValues += uint64(c.getCardinality())
  1226. }
  1227. }
  1228. return stats
  1229. }
上海开阖软件有限公司 沪ICP备12045867号-1