1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
25 "github.com/apache/arrow/go/v6/arrow"
26 "github.com/apache/arrow/go/v6/arrow/bitutil"
29 // BitRun represents a run of bits with the same value of length Len
30 // with Set representing if the group of bits were 1 or 0.
36 // BitRunReader is an interface that is usable by multiple callers to provide
37 // multiple types of bit run readers such as a reverse reader and so on.
39 // It's a convenience interface for counting contiguous set/unset bits in a bitmap.
40 // In places where BitBlockCounter can be used, then it would be preferred to use that
41 // as it would be faster than using BitRunReader.
42 type BitRunReader interface {
46 func (b BitRun) String() string {
47 return fmt.Sprintf("{Length: %d, set=%t}", b.Len, b.Set)
50 type bitRunReader struct {
58 // NewBitRunReader returns a reader for the given bitmap, offset and length that
59 // grabs runs of the same value bit at a time for easy iteration.
60 func NewBitRunReader(bitmap []byte, offset int64, length int64) BitRunReader {
62 bitmap: bitmap[offset/8:],
64 length: (offset % 8) + length,
71 ret.curRunBitSet = bitutil.BitIsNotSet(bitmap, int(offset))
72 bitsRemaining := length + ret.pos
73 ret.loadWord(bitsRemaining)
74 ret.word = ret.word &^ LeastSignificantBitMask(ret.pos)
78 // NextRun returns a new BitRun containing the number of contiguous bits with the
79 // same value. Len == 0 indicates the end of the bitmap.
80 func (b *bitRunReader) NextRun() BitRun {
81 if b.pos >= b.length {
82 return BitRun{0, false}
85 // This implementation relies on a efficient implementations of
86 // CountTrailingZeros and assumes that runs are more often then
87 // not. The logic is to incrementally find the next bit change
88 // from the current position. This is done by zeroing all
89 // bits in word_ up to position_ and using the TrailingZeroCount
90 // to find the index of the next set bit.
92 // The runs alternate on each call, so flip the bit.
93 b.curRunBitSet = !b.curRunBitSet
96 startOffset := start & 63
98 // Invert the word for proper use of CountTrailingZeros and
99 // clear bits so CountTrailingZeros can do it magic.
100 b.word = ^b.word &^ LeastSignificantBitMask(startOffset)
102 // Go forward until the next change from unset to set.
103 newbits := int64(bits.TrailingZeros64(b.word)) - startOffset
106 if IsMultipleOf64(b.pos) && b.pos < b.length {
107 b.advanceUntilChange()
109 return BitRun{b.pos - start, b.curRunBitSet}
112 func (b *bitRunReader) advanceUntilChange() {
115 b.bitmap = b.bitmap[arrow.Uint64SizeBytes:]
117 newbits = int64(bits.TrailingZeros64(b.word))
119 if !IsMultipleOf64(b.pos) || b.pos >= b.length || newbits <= 0 {
125 func (b *bitRunReader) loadNextWord() {
126 b.loadWord(b.length - b.pos)
129 func (b *bitRunReader) loadWord(bitsRemaining int64) {
131 if bitsRemaining >= 64 {
132 b.word = binary.LittleEndian.Uint64(b.bitmap)
134 nbytes := bitutil.BytesForBits(bitsRemaining)
135 wordptr := (*(*[8]byte)(unsafe.Pointer(&b.word)))[:]
136 copy(wordptr, b.bitmap[:nbytes])
138 bitutil.SetBitTo(wordptr, int(bitsRemaining), bitutil.BitIsNotSet(wordptr, int(bitsRemaining-1)))
139 // reset the value to little endian for big endian architectures
140 b.word = ToLEUint64(b.word)
144 // 1. For unset, CountTrailingZeros works naturally so we don't
146 // 2. Otherwise invert so we can use CountTrailingZeros.