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

Method OrCardinality

roaring.go:1417–1467  ·  view source on GitHub ↗

OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified

(x2 *Bitmap)

Source from the content-addressed store, hash-verified

1415
1416// OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
1417func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {
1418 pos1 := 0
1419 pos2 := 0
1420 length1 := rb.highlowcontainer.size()
1421 length2 := x2.highlowcontainer.size()
1422 answer := uint64(0)
1423main:
1424 for {
1425 if (pos1 < length1) && (pos2 < length2) {
1426 s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
1427 s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
1428
1429 for {
1430 if s1 < s2 {
1431 answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
1432 pos1++
1433 if pos1 == length1 {
1434 break main
1435 }
1436 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1437 } else if s1 > s2 {
1438 answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
1439 pos2++
1440 if pos2 == length2 {
1441 break main
1442 }
1443 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1444 } else {
1445 // TODO: could be faster if we did not have to materialize the container
1446 answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).or(x2.highlowcontainer.getContainerAtIndex(pos2)).getCardinality())
1447 pos1++
1448 pos2++
1449 if (pos1 == length1) || (pos2 == length2) {
1450 break main
1451 }
1452 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1453 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1454 }
1455 }
1456 } else {
1457 break
1458 }
1459 }
1460 for ; pos1 < length1; pos1++ {
1461 answer += uint64(rb.highlowcontainer.getContainerAtIndex(pos1).getCardinality())
1462 }
1463 for ; pos2 < length2; pos2++ {
1464 answer += uint64(x2.highlowcontainer.getContainerAtIndex(pos2).getCardinality())
1465 }
1466 return answer
1467}
1468
1469// AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
1470func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {

Callers 5

TestFastCardCOWFunction · 0.95
TestFastCardFunction · 0.95
TestFastCardUnequalKeysFunction · 0.95
smatOrCardinalityFunction · 0.45
smatXorCardinalityFunction · 0.45

Calls 5

getCardinalityMethod · 0.65
orMethod · 0.65
sizeMethod · 0.45
getKeyAtIndexMethod · 0.45
getContainerAtIndexMethod · 0.45

Tested by 3

TestFastCardCOWFunction · 0.76
TestFastCardFunction · 0.76
TestFastCardUnequalKeysFunction · 0.76