PreviousValue returns the previous largest value in the bitmap, or -1 if none is present. This function should not be used inside a performance-sensitive loop: prefer iterators if performance is a concern.
(target uint32)
| 2233 | // a performance-sensitive loop: prefer iterators if |
| 2234 | // performance is a concern. |
| 2235 | func (rb *Bitmap) PreviousValue(target uint32) int64 { |
| 2236 | if rb.IsEmpty() { |
| 2237 | return -1 |
| 2238 | } |
| 2239 | |
| 2240 | originalKey := highbits(target) |
| 2241 | query := lowbits(target) |
| 2242 | var prevValue int64 |
| 2243 | prevValue = -1 |
| 2244 | containerIndex := rb.highlowcontainer.advanceUntil(originalKey, -1) |
| 2245 | |
| 2246 | if containerIndex == rb.highlowcontainer.size() { |
| 2247 | return int64(rb.Maximum()) |
| 2248 | } |
| 2249 | |
| 2250 | if rb.highlowcontainer.getKeyAtIndex(containerIndex) > originalKey { |
| 2251 | // target absent, key of first container after target too high |
| 2252 | containerIndex-- |
| 2253 | } |
| 2254 | |
| 2255 | for containerIndex != -1 && prevValue == -1 { |
| 2256 | containerKey := rb.highlowcontainer.getKeyAtIndex(containerIndex) |
| 2257 | container := rb.highlowcontainer.getContainer(containerKey) |
| 2258 | // if containerKey > originalKey then we are past the container which mapped to the original key |
| 2259 | // in that case we can just return the minimum from that container |
| 2260 | var responseBit int |
| 2261 | if containerKey < originalKey { |
| 2262 | bit, err := container.safeMaximum() |
| 2263 | |
| 2264 | if err == nil { |
| 2265 | responseBit = -1 |
| 2266 | } |
| 2267 | responseBit = int(bit) |
| 2268 | } else { |
| 2269 | responseBit = container.previousValue(query) |
| 2270 | } |
| 2271 | |
| 2272 | if responseBit == -1 { |
| 2273 | prevValue = -1 |
| 2274 | } else { |
| 2275 | prevValue = int64(combineLoHi32(uint32(responseBit), uint32(containerKey))) |
| 2276 | } |
| 2277 | containerIndex-- |
| 2278 | } |
| 2279 | |
| 2280 | return prevValue |
| 2281 | } |
| 2282 | |
| 2283 | // NextAbsentValue returns the next largest missing value in the bitmap, or -1 |
| 2284 | // if none is present. This function should not be used inside |