1
0

move BitArray in its namerspace

This commit is contained in:
2025-05-24 13:21:23 +02:00
parent c98e462142
commit 75219bc716
2 changed files with 37 additions and 18 deletions

View File

@@ -2,17 +2,18 @@ import { asArray } from "../utils.js";
import { EmptyBitArray, BitArrayImpl } from "./impl.js"; import { EmptyBitArray, BitArrayImpl } from "./impl.js";
import { BitArray } from "./types.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 { export function create(length: number): BitArray {
if (length < 0) { if (length < 0) {
throw new Error("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<boolean>): BitArray { export function from(bits: Iterable<boolean>) {
const arr = asArray(bits); const arr = asArray(bits);
const result = create(arr.length); const result = create(arr.length);
@@ -21,8 +22,27 @@ export function from(bits: Iterable<boolean>): BitArray {
} }
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 { export { BitArray };
return from(bits);
}

View File

@@ -1,5 +1,4 @@
import { create as createBitArray } from "../bitarray/index.js"; import { BitArray } from "../bitarray/index.js";
import { BitArray } from "../bitarray/types.js";
import { asArray } from "../utils.js"; import { asArray } from "../utils.js";
import { AsyncRandomOptions, ElementPredicate, ElementWeight, RandomGenerator, RandomOptions } from "./types.js"; import { AsyncRandomOptions, ElementPredicate, ElementWeight, RandomGenerator, RandomOptions } from "./types.js";
@@ -121,7 +120,7 @@ export class RandomPicker<T> {
public constructor(elements: Iterable<T>, length: number, options?: RandomOptions<T>) { public constructor(elements: Iterable<T>, length: number, options?: RandomOptions<T>) {
this.#elements = elements; this.#elements = elements;
this.#flags = createBitArray(length); this.#flags = BitArray.create(length);
this.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options)); this.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options));
this.reset(); this.reset();