|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +// FilterBitArray is an array of bits based on byte unit, so 8 bits at each |
| 20 | +// index. The array automatically increases if the set index is larger than the |
| 21 | +// current capacity. The bit index starts at 0. |
| 22 | +type FilterBitArray []byte |
| 23 | + |
| 24 | +// NewFilterBitArray creates an array with the specified bit-size. This is an |
| 25 | +// optimization to make array once for the expected capacity rather than |
| 26 | +// using Set function to auto-increase the array. |
| 27 | +func NewFilterBitArray(size uint) FilterBitArray { |
| 28 | + ba := make(FilterBitArray, (size-1)/8+1) |
| 29 | + return ba |
| 30 | +} |
| 31 | + |
| 32 | +// Capacity returns the number of bits in the FilterBitArray. |
| 33 | +func (ba *FilterBitArray) Capacity() uint { |
| 34 | + return uint(len(*ba) * 8) |
| 35 | +} |
| 36 | + |
| 37 | +// Set assigns 1 to the specified bit-index, which is starting from 0. |
| 38 | +// Set automatically increases the array to accommodate the bit-index. |
| 39 | +func (ba *FilterBitArray) Set(i uint) { |
| 40 | + // Location of i in the array index is floor(i/8) + 1. If it exceeds the |
| 41 | + // current byte array, we'll make a new one large enough to include the |
| 42 | + // specified bit-index |
| 43 | + if i >= ba.Capacity() { |
| 44 | + array := make([]byte, i/8+1) |
| 45 | + copy(array, *ba) |
| 46 | + *ba = array |
| 47 | + } |
| 48 | + (*ba)[i/8] |= 1 << (i % 8) |
| 49 | +} |
| 50 | + |
| 51 | +// Unset assigns 0 the specified bit-index. If bit-index is larger than capacity, |
| 52 | +// do nothing. |
| 53 | +func (ba *FilterBitArray) Unset(i uint) { |
| 54 | + if i < ba.Capacity() { |
| 55 | + (*ba)[i/8] &^= 1 << (i % 8) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// ValueAt returns the value at the specified bit-index. If bit-index is out |
| 60 | +// of range, return 0. Note that the returned value is in byte, so it may be |
| 61 | +// a power of 2 if not 0. |
| 62 | +func (ba *FilterBitArray) ValueAt(i uint) byte { |
| 63 | + if i < ba.Capacity() { |
| 64 | + return (*ba)[i/8] & (1 << (i % 8)) |
| 65 | + } |
| 66 | + return 0 |
| 67 | +} |
| 68 | + |
| 69 | +// IsSet returns true if the specified bit-index is 1; false otherwise. |
| 70 | +func (ba *FilterBitArray) IsSet(i uint) bool { |
| 71 | + return (ba.ValueAt(i) != 0) |
| 72 | +} |
| 73 | + |
| 74 | +// ToBytes returns the byte array for storage. |
| 75 | +func (ba *FilterBitArray) ToBytes() []byte { |
| 76 | + return *ba |
| 77 | +} |
| 78 | + |
| 79 | +// FromBytes accepts a byte array. |
| 80 | +func (ba *FilterBitArray) FromBytes(bytes []byte) { |
| 81 | + *ba = bytes |
| 82 | +} |
0 commit comments