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,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
19 export function getBool(_data: any, _index: number, byte: number, bit: number) {
20 return (byte & 1 << bit) !== 0;
24 export function getBit(_data: any, _index: number, byte: number, bit: number): 0 | 1 {
25 return (byte & 1 << bit) >> bit as (0 | 1);
29 export function setBool(bytes: Uint8Array, index: number, value: any) {
31 !!(bytes[index >> 3] |= (1 << (index % 8))) || true :
32 !(bytes[index >> 3] &= ~(1 << (index % 8))) && false ;
36 export function truncateBitmap(offset: number, length: number, bitmap: Uint8Array) {
37 const alignedSize = (bitmap.byteLength + 7) & ~7;
38 if (offset > 0 || bitmap.byteLength < alignedSize) {
39 const bytes = new Uint8Array(alignedSize);
40 // If the offset is a multiple of 8 bits, it's safe to slice the bitmap
41 bytes.set(offset % 8 === 0 ? bitmap.subarray(offset >> 3) :
42 // Otherwise iterate each bit from the offset and return a new one
43 packBools(new BitIterator(bitmap, offset, length, null, getBool)).subarray(0, alignedSize));
50 export function packBools(values: Iterable<any>) {
51 const xs: number[] = [];
52 let i = 0, bit = 0, byte = 0;
53 for (const value of values) {
54 value && (byte |= 1 << bit);
60 if (i === 0 || bit > 0) { xs[i++] = byte; }
61 const b = new Uint8Array((xs.length + 7) & ~7);
67 export class BitIterator<T> implements IterableIterator<T> {
74 private bytes: Uint8Array,
76 private length: number,
78 private get: (context: any, index: number, byte: number, bit: number) => T
81 this.byteIndex = begin >> 3;
82 this.byte = bytes[this.byteIndex++];
86 next(): IteratorResult<T> {
87 if (this.index < this.length) {
90 this.byte = this.bytes[this.byteIndex++];
93 value: this.get(this.context, this.index++, this.byte, this.bit++)
96 return { done: true, value: null };
105 * Compute the population count (the number of bits set to 1) for a range of bits in a Uint8Array.
106 * @param vector The Uint8Array of bits for which to compute the population count.
107 * @param lhs The range's left-hand side (or start) bit
108 * @param rhs The range's right-hand side (or end) bit
111 export function popcnt_bit_range(data: Uint8Array, lhs: number, rhs: number): number {
112 if (rhs - lhs <= 0) { return 0; }
113 // If the bit range is less than one byte, sum the 1 bits in the bit range
116 for (const bit of new BitIterator(data, lhs, rhs - lhs, data, getBit)) {
121 // Get the next lowest multiple of 8 from the right hand side
122 const rhsInside = rhs >> 3 << 3;
123 // Get the next highest multiple of 8 from the left hand side
124 const lhsInside = lhs + (lhs % 8 === 0 ? 0 : 8 - lhs % 8);
126 // Get the popcnt of bits between the left hand side, and the next highest multiple of 8
127 popcnt_bit_range(data, lhs, lhsInside) +
128 // Get the popcnt of bits between the right hand side, and the next lowest multiple of 8
129 popcnt_bit_range(data, rhsInside, rhs) +
130 // Get the popcnt of all bits between the left and right hand sides' multiples of 8
131 popcnt_array(data, lhsInside >> 3, (rhsInside - lhsInside) >> 3)
136 export function popcnt_array(arr: ArrayBufferView, byteOffset?: number, byteLength?: number) {
137 let cnt = 0, pos = byteOffset! | 0;
138 const view = new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
139 const len = byteLength === void 0 ? arr.byteLength : pos + byteLength;
140 while (len - pos >= 4) {
141 cnt += popcnt_uint32(view.getUint32(pos));
144 while (len - pos >= 2) {
145 cnt += popcnt_uint32(view.getUint16(pos));
148 while (len - pos >= 1) {
149 cnt += popcnt_uint32(view.getUint8(pos));
156 export function popcnt_uint32(uint32: number): number {
158 i = i - ((i >>> 1) & 0x55555555);
159 i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
160 return (((i + (i >>> 4)) & 0x0F0F0F0F) * 0x01010101) >>> 24;