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

Method invert

runcontainer.go:1465–1496  ·  view source on GitHub ↗

invert returns a new container (not inplace), that is the inversion of rc. For each bit b in rc, the returned value has !b

()

Source from the content-addressed store, hash-verified

1463// the inversion of rc. For each bit b in rc, the
1464// returned value has !b
1465func (rc *runContainer16) invert() *runContainer16 {
1466 ni := len(rc.iv)
1467 var m []interval16
1468 switch ni {
1469 case 0:
1470 return &runContainer16{iv: []interval16{newInterval16Range(0, MaxUint16)}}
1471 case 1:
1472 return &runContainer16{iv: rc.invertlastInterval(0, 0)}
1473 }
1474 var invstart int
1475 ult := ni - 1
1476 for i, cur := range rc.iv {
1477 if i == ult {
1478 // invertlastInteval will add both intervals (b) and (c) in
1479 // diagram below.
1480 m = append(m, rc.invertlastInterval(uint16(invstart), i)...)
1481 break
1482 }
1483 // INVAR: i and cur are not the last interval, there is a next at i+1
1484 //
1485 // ........[cur.start, cur.last] ...... [next.start, next.last]....
1486 // ^ ^ ^
1487 // (a) (b) (c)
1488 //
1489 // Now: we add interval (a); but if (a) is empty, for cur.start==0, we skip it.
1490 if cur.start > 0 {
1491 m = append(m, newInterval16Range(uint16(invstart), cur.start-1))
1492 }
1493 invstart = int(cur.last() + 1)
1494 }
1495 return &runContainer16{iv: m}
1496}
1497
1498func (iv interval16) equal(b interval16) bool {
1499 return iv.start == b.start && iv.length == b.length

Callers 2

NotMethod · 0.80

Calls 3

invertlastIntervalMethod · 0.95
newInterval16RangeFunction · 0.85
lastMethod · 0.80

Tested by 1