MCPcopy Create free account
hub / github.com/RoaringBitmap/roaring / OrCardinality

Method OrCardinality

roaring64/roaring64.go:488–538  ·  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

486
487// OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
488func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {
489 pos1 := 0
490 pos2 := 0
491 length1 := rb.highlowcontainer.size()
492 length2 := x2.highlowcontainer.size()
493 answer := uint64(0)
494main:
495 for {
496 if (pos1 < length1) && (pos2 < length2) {
497 s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
498 s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
499
500 for {
501 if s1 < s2 {
502 answer += rb.highlowcontainer.getContainerAtIndex(pos1).GetCardinality()
503 pos1++
504 if pos1 == length1 {
505 break main
506 }
507 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
508 } else if s1 > s2 {
509 answer += x2.highlowcontainer.getContainerAtIndex(pos2).GetCardinality()
510 pos2++
511 if pos2 == length2 {
512 break main
513 }
514 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
515 } else {
516 // TODO: could be faster if we did not have to materialize the container
517 answer += roaring.Or(rb.highlowcontainer.getContainerAtIndex(pos1), x2.highlowcontainer.getContainerAtIndex(pos2)).GetCardinality()
518 pos1++
519 pos2++
520 if (pos1 == length1) || (pos2 == length2) {
521 break main
522 }
523 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
524 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
525 }
526 }
527 } else {
528 break
529 }
530 }
531 for ; pos1 < length1; pos1++ {
532 answer += rb.highlowcontainer.getContainerAtIndex(pos1).GetCardinality()
533 }
534 for ; pos2 < length2; pos2++ {
535 answer += x2.highlowcontainer.getContainerAtIndex(pos2).GetCardinality()
536 }
537 return answer
538}
539
540// AndCardinality returns the cardinality of the intersection between two bitmaps, bitmaps are not modified
541func (rb *Bitmap) AndCardinality(x2 *Bitmap) uint64 {

Callers 2

TestFastCardCOWFunction · 0.95
TestFastCardFunction · 0.95

Calls 5

sizeMethod · 0.45
getKeyAtIndexMethod · 0.45
GetCardinalityMethod · 0.45
getContainerAtIndexMethod · 0.45
OrMethod · 0.45

Tested by 2

TestFastCardCOWFunction · 0.76
TestFastCardFunction · 0.76