16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ciexport default class BitSet { 176e80583aSopenharmony_ci 186e80583aSopenharmony_ci private static readonly NUMBER_BITS_NUM = 64; 196e80583aSopenharmony_ci 206e80583aSopenharmony_ci private readonly mBitArray: number[] = null; 216e80583aSopenharmony_ci 226e80583aSopenharmony_ci private readonly mSize: number; 236e80583aSopenharmony_ci 246e80583aSopenharmony_ci /** 256e80583aSopenharmony_ci * constructor 266e80583aSopenharmony_ci * 276e80583aSopenharmony_ci * @param {number} size, size of BitSet 286e80583aSopenharmony_ci */ 296e80583aSopenharmony_ci constructor(size: number) { 306e80583aSopenharmony_ci this.mSize = size; 316e80583aSopenharmony_ci const allocSize = Math.ceil(size / BitSet.NUMBER_BITS_NUM); 326e80583aSopenharmony_ci this.mBitArray = new Array<number>(allocSize); 336e80583aSopenharmony_ci for (let i = 0; i < this.mBitArray.length; i++) { 346e80583aSopenharmony_ci this.mBitArray[i] = 0; 356e80583aSopenharmony_ci } 366e80583aSopenharmony_ci } 376e80583aSopenharmony_ci 386e80583aSopenharmony_ci /** 396e80583aSopenharmony_ci * get the bit value of index 406e80583aSopenharmony_ci * 416e80583aSopenharmony_ci * @param {number} index, index of BitSet 426e80583aSopenharmony_ci * @return {boolean} true if set 436e80583aSopenharmony_ci */ 446e80583aSopenharmony_ci get(index: number): boolean { 456e80583aSopenharmony_ci if (index < 0 || index >= this.mSize) { 466e80583aSopenharmony_ci return false; 476e80583aSopenharmony_ci } 486e80583aSopenharmony_ci 496e80583aSopenharmony_ci const arrayIndex = Math.floor(index / BitSet.NUMBER_BITS_NUM); 506e80583aSopenharmony_ci const bitIndex = index % BitSet.NUMBER_BITS_NUM; 516e80583aSopenharmony_ci return (this.mBitArray[arrayIndex] & (1 << bitIndex)) != 0; 526e80583aSopenharmony_ci } 536e80583aSopenharmony_ci 546e80583aSopenharmony_ci /** 556e80583aSopenharmony_ci * set the bit value of index as true 566e80583aSopenharmony_ci * 576e80583aSopenharmony_ci * @param {number} index, index of BitSet 586e80583aSopenharmony_ci */ 596e80583aSopenharmony_ci set(index: number) { 606e80583aSopenharmony_ci if (index < 0 || index >= this.mSize) { 616e80583aSopenharmony_ci return; 626e80583aSopenharmony_ci } 636e80583aSopenharmony_ci 646e80583aSopenharmony_ci const arrayIndex = Math.floor(index / BitSet.NUMBER_BITS_NUM); 656e80583aSopenharmony_ci const bitIndex = index % BitSet.NUMBER_BITS_NUM; 666e80583aSopenharmony_ci this.mBitArray[arrayIndex] |= 1 << bitIndex; 676e80583aSopenharmony_ci } 686e80583aSopenharmony_ci 696e80583aSopenharmony_ci /** 706e80583aSopenharmony_ci * call all the bit value of the BitSet 716e80583aSopenharmony_ci * 726e80583aSopenharmony_ci */ 736e80583aSopenharmony_ci clear() { 746e80583aSopenharmony_ci this.mBitArray.fill(0); 756e80583aSopenharmony_ci } 766e80583aSopenharmony_ci}