invert returns a new container (not inplace), that is the inversion of rc. For each bit b in rc, the returned value has !b
()
| 1463 | // the inversion of rc. For each bit b in rc, the |
| 1464 | // returned value has !b |
| 1465 | func (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 | |
| 1498 | func (iv interval16) equal(b interval16) bool { |
| 1499 | return iv.start == b.start && iv.length == b.length |