Flip negates the bits in the given range (i.e., [rangeStart,rangeEnd)), any integer present in this range and in the bitmap is removed, and any integer present in the range and not in the bitmap is added, a new bitmap is returned leaving the current bitmap unchanged. The function uses 64-bit parame
(bm *Bitmap, rangeStart, rangeEnd uint64)
| 2111 | // The function uses 64-bit parameters even though a Bitmap stores 32-bit values because it is allowed and meaningful to use [0,uint64(0x100000000)) as a range |
| 2112 | // while uint64(0x100000000) cannot be represented as a 32-bit value. |
| 2113 | func Flip(bm *Bitmap, rangeStart, rangeEnd uint64) *Bitmap { |
| 2114 | if rangeStart >= rangeEnd { |
| 2115 | return bm.Clone() |
| 2116 | } |
| 2117 | |
| 2118 | if rangeStart > MaxUint32 { |
| 2119 | panic("rangeStart > MaxUint32") |
| 2120 | } |
| 2121 | if rangeEnd-1 > MaxUint32 { |
| 2122 | panic("rangeEnd-1 > MaxUint32") |
| 2123 | } |
| 2124 | |
| 2125 | answer := NewBitmap() |
| 2126 | hbStart := uint32(highbits(uint32(rangeStart))) |
| 2127 | lbStart := uint32(lowbits(uint32(rangeStart))) |
| 2128 | hbLast := uint32(highbits(uint32(rangeEnd - 1))) |
| 2129 | lbLast := uint32(lowbits(uint32(rangeEnd - 1))) |
| 2130 | |
| 2131 | // copy the containers before the active area |
| 2132 | answer.highlowcontainer.appendCopiesUntil(bm.highlowcontainer, uint16(hbStart)) |
| 2133 | |
| 2134 | var max uint32 = maxLowBit |
| 2135 | for hb := hbStart; hb <= hbLast; hb++ { |
| 2136 | var containerStart uint32 |
| 2137 | if hb == hbStart { |
| 2138 | containerStart = lbStart |
| 2139 | } |
| 2140 | containerLast := max |
| 2141 | if hb == hbLast { |
| 2142 | containerLast = lbLast |
| 2143 | } |
| 2144 | |
| 2145 | i := bm.highlowcontainer.getIndex(uint16(hb)) |
| 2146 | j := answer.highlowcontainer.getIndex(uint16(hb)) |
| 2147 | |
| 2148 | if i >= 0 { |
| 2149 | c := bm.highlowcontainer.getContainerAtIndex(i).not(int(containerStart), int(containerLast)+1) |
| 2150 | if !c.isEmpty() { |
| 2151 | answer.highlowcontainer.insertNewKeyValueAt(-j-1, uint16(hb), c) |
| 2152 | } |
| 2153 | |
| 2154 | } else { // *think* the range of ones must never be |
| 2155 | // empty. |
| 2156 | answer.highlowcontainer.insertNewKeyValueAt(-j-1, uint16(hb), |
| 2157 | rangeOfOnes(int(containerStart), int(containerLast))) |
| 2158 | } |
| 2159 | } |
| 2160 | // copy the containers after the active area. |
| 2161 | answer.highlowcontainer.appendCopiesAfter(bm.highlowcontainer, uint16(hbLast)) |
| 2162 | |
| 2163 | return answer |
| 2164 | } |
| 2165 | |
| 2166 | // SetCopyOnWrite sets this bitmap to use copy-on-write so that copies are fast and memory conscious |
| 2167 | // if the parameter is true, otherwise we leave the default where hard copies are made |
searching dependent graphs…