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

Method And

roaring.go:1367–1414  ·  view source on GitHub ↗

And computes the intersection between two bitmaps and stores the result in the current bitmap

(x2 *Bitmap)

Source from the content-addressed store, hash-verified

1365
1366// And computes the intersection between two bitmaps and stores the result in the current bitmap
1367func (rb *Bitmap) And(x2 *Bitmap) {
1368 pos1 := 0
1369 pos2 := 0
1370 intersectionsize := 0
1371 length1 := rb.highlowcontainer.size()
1372 length2 := x2.highlowcontainer.size()
1373
1374main:
1375 for {
1376 if pos1 < length1 && pos2 < length2 {
1377 s1 := rb.highlowcontainer.getKeyAtIndex(pos1)
1378 s2 := x2.highlowcontainer.getKeyAtIndex(pos2)
1379 for {
1380 if s1 == s2 {
1381 c1 := rb.highlowcontainer.getWritableContainerAtIndex(pos1)
1382 c2 := x2.highlowcontainer.getContainerAtIndex(pos2)
1383 diff := c1.iand(c2)
1384 if !diff.isEmpty() {
1385 rb.highlowcontainer.replaceKeyAndContainerAtIndex(intersectionsize, s1, diff, false)
1386 intersectionsize++
1387 }
1388 pos1++
1389 pos2++
1390 if (pos1 == length1) || (pos2 == length2) {
1391 break main
1392 }
1393 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1394 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1395 } else if s1 < s2 {
1396 pos1 = rb.highlowcontainer.advanceUntil(s2, pos1)
1397 if pos1 == length1 {
1398 break main
1399 }
1400 s1 = rb.highlowcontainer.getKeyAtIndex(pos1)
1401 } else { // s1 > s2
1402 pos2 = x2.highlowcontainer.advanceUntil(s1, pos2)
1403 if pos2 == length2 {
1404 break main
1405 }
1406 s2 = x2.highlowcontainer.getKeyAtIndex(pos2)
1407 }
1408 }
1409 } else {
1410 break
1411 }
1412 }
1413 rb.highlowcontainer.resize(intersectionsize)
1414}
1415
1416// OrCardinality returns the cardinality of the union between two bitmaps, bitmaps are not modified
1417func (rb *Bitmap) OrCardinality(x2 *Bitmap) uint64 {

Callers 15

TestBitmapCOWFunction · 0.95
TestFlipBigACOWFunction · 0.95
AndAnyMethod · 0.95
TestBitmapFunction · 0.95
TestFlipBigAFunction · 0.95
TestExample_roaring060Function · 0.45
smatAndFunction · 0.45
TestBitmapExtraCOWFunction · 0.45
rTestCOWFunction · 0.45
FastAndFunction · 0.45
TestRoaring_AndMaskFunction · 0.45

Calls 9

iandMethod · 0.65
isEmptyMethod · 0.65
sizeMethod · 0.45
getKeyAtIndexMethod · 0.45
getContainerAtIndexMethod · 0.45
advanceUntilMethod · 0.45
resizeMethod · 0.45

Tested by 15

TestBitmapCOWFunction · 0.76
TestFlipBigACOWFunction · 0.76
TestBitmapFunction · 0.76
TestFlipBigAFunction · 0.76
TestExample_roaring060Function · 0.36
TestBitmapExtraCOWFunction · 0.36
rTestCOWFunction · 0.36
TestRoaring_AndMaskFunction · 0.36
TestBitmapExtraFunction · 0.36
rTestFunction · 0.36
TestRleAndOrXor16Function · 0.36