From 1f7c246fafd0d667c06c85ce88cd8edc683af465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BECHER?= Date: Sat, 24 May 2025 13:21:23 +0200 Subject: [PATCH] move BitArray in its namerspace --- src/bitarray/index.ts | 50 ++++++++++++++++++++++++++++++------------- src/random/index.ts | 5 ++--- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/bitarray/index.ts b/src/bitarray/index.ts index 8fa2b94..cd56eb8 100644 --- a/src/bitarray/index.ts +++ b/src/bitarray/index.ts @@ -2,27 +2,47 @@ import { asArray } from "../utils.js"; import { EmptyBitArray, BitArrayImpl } from "./impl.js"; import { BitArray } from "./types.js"; -export const EMPTY = new EmptyBitArray(); +namespace BitArray { + export const EMPTY: BitArray = new EmptyBitArray(); -export function create(length: number): BitArray { - if (length < 0) { - throw new Error("length < 0"); + export function create(length: number): BitArray { + if (length < 0) { + throw new Error("length < 0"); + } + + return length === 0 ? EMPTY : new BitArrayImpl(length); } - return length === 0 ? EMPTY : new BitArrayImpl(length); -} + export function from(bits: Iterable) { + const arr = asArray(bits); + const result = create(arr.length); -export function from(bits: Iterable): BitArray { - const arr = asArray(bits); - const result = create(arr.length); + for (let i = 0; i < arr.length; i++) { + result.set(i, arr[i]); + } - for (let i = 0; i < arr.length; i++) { - result.set(i, arr[i]); + return result; } - return result; + export function of(...bits: boolean[]) { + return from(bits); + } + + export function and(a: BitArray, b: BitArray) { + return a.copy().and(b); + } + + export function or(a: BitArray, b: BitArray) { + return a.copy().or(b); + } + + export function xor(a: BitArray, b: BitArray) { + return a.copy().xor(b); + } + + export function not(a: BitArray) { + return a.copy().not(); + } } -export function of(...bits: boolean[]): BitArray { - return from(bits); -} +export { BitArray }; diff --git a/src/random/index.ts b/src/random/index.ts index 820424c..4ae507b 100644 --- a/src/random/index.ts +++ b/src/random/index.ts @@ -1,5 +1,4 @@ -import { create as createBitArray } from "../bitarray/index.js"; -import { BitArray } from "../bitarray/types.js"; +import { BitArray } from "../bitarray/index.js"; import { asArray } from "../utils.js"; import { AsyncRandomOptions, ElementPredicate, ElementWeight, RandomGenerator, RandomOptions } from "./types.js"; @@ -121,7 +120,7 @@ export class RandomPicker { public constructor(elements: Iterable, length: number, options?: RandomOptions) { this.#elements = elements; - this.#flags = createBitArray(length); + this.#flags = BitArray.create(length); this.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options)); this.reset();