AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
(x2 *Bitmap)
| 539 | |
| 540 | // AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified |
| 541 | func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 { |
| 542 | pos1 := 0 |
| 543 | pos2 := 0 |
| 544 | answer := uint64(0) |
| 545 | length1 := rb.highlowcontainer.size() |
| 546 | length2 := x2.highlowcontainer.size() |
| 547 | |
| 548 | main: |
| 549 | for { |
| 550 | if pos1 < length1 && pos2 < length2 { |
| 551 | s1 := rb.highlowcontainer.getKeyAtIndex(pos1) |
| 552 | s2 := x2.highlowcontainer.getKeyAtIndex(pos2) |
| 553 | for { |
| 554 | if s1 == s2 { |
| 555 | c1 := rb.highlowcontainer.getContainerAtIndex(pos1) |
| 556 | c2 := x2.highlowcontainer.getContainerAtIndex(pos2) |
| 557 | answer += c1.AndCardinality(c2) |
| 558 | pos1++ |
| 559 | pos2++ |
| 560 | if (pos1 == length1) || (pos2 == length2) { |
| 561 | break main |
| 562 | } |
| 563 | s1 = rb.highlowcontainer.getKeyAtIndex(pos1) |
| 564 | s2 = x2.highlowcontainer.getKeyAtIndex(pos2) |
| 565 | } else if s1 < s2 { |
| 566 | pos1 = rb.highlowcontainer.advanceUntil(s2, pos1) |
| 567 | if pos1 == length1 { |
| 568 | break main |
| 569 | } |
| 570 | s1 = rb.highlowcontainer.getKeyAtIndex(pos1) |
| 571 | } else { // s1 > s2 |
| 572 | pos2 = x2.highlowcontainer.advanceUntil(s1, pos2) |
| 573 | if pos2 == length2 { |
| 574 | break main |
| 575 | } |
| 576 | s2 = x2.highlowcontainer.getKeyAtIndex(pos2) |
| 577 | } |
| 578 | } |
| 579 | } else { |
| 580 | break |
| 581 | } |
| 582 | } |
| 583 | return answer |
| 584 | } |
| 585 | |
| 586 | // Intersects checks whether two bitmap intersects, bitmaps are not modified |
| 587 | func (rb *Bitmap) Intersects(x2 *Bitmap) bool { |