MCPcopy
hub / github.com/RoaringBitmap/roaring / AndCardinality

Method AndCardinality

roaring.go:1470–1513  ·  view source on GitHub ↗

AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified

(x2 *Bitmap)

Source from the content-addressed store, hash-verified

1468
1469// AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
1470func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {
1471 pos1 := 0
1472 pos2 := 0
1473 answer := uint64(0)
1474 length1 := rb.highlowcontainer.size()
1475 length2 := x2.highlowcontainer.size()
1476
1477main:
1478 for {
1479 if pos1 < length1 && pos2 < length2 {
1480 s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
1481 s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
1482 for {
1483 if s1 == s2 {
1484 c1 := rb.highlowcontainer.getContainerAtIndex(pos1)
1485 c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
1486 answer += uint64(c1.andCardinality(c2))
1487 pos1++
1488 pos2++
1489 if (pos1 == length1) || (pos2 == length2) {
1490 break main
1491 }
1492 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1493 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1494 } else if s1 < s2 {
1495 pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
1496 if pos1 == length1 {
1497 break main
1498 }
1499 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1500 } else { // s1 > s2
1501 pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
1502 if pos2 == length2 {
1503 break main
1504 }
1505 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1506 }
1507 }
1508 } else {
1509 break
1510 }
1511 }
1512 return answer
1513}
1514
1515// IntersectsWithInterval checks whether a bitmap 'rb' and an open interval '[x,y)' intersect.
1516func (rb *Bitmap) IntersectsWithInterval(x, y uint64) bool {

Callers 7

TestFastCardCOWFunction · 0.95
TestFastCardFunction · 0.95
smatAndCardinalityFunction · 0.45
smatXorCardinalityFunction · 0.45
TestIssue181Function · 0.45
SumMethod · 0.45

Calls 5

andCardinalityMethod · 0.65
sizeMethod · 0.45
getKeyAtIndexMethod · 0.45
getContainerAtIndexMethod · 0.45
advanceUntilMethod · 0.45

Tested by 4

TestFastCardCOWFunction · 0.76
TestFastCardFunction · 0.76
TestIssue181Function · 0.36