1
0

allow creating RandomPicker from iterable and separately specified length

This commit is contained in:
2024-05-09 16:28:05 +02:00
parent b48f97d1c3
commit a1f43287a0

View File

@@ -91,20 +91,21 @@ export class RandomPicker<T> {
readonly #flags: BitArray;
readonly #options: Required<RandomOptions<T>>;
public constructor(elements: T[], options?: RandomOptions<T>) {
public constructor(elements: Iterable<T>, length: number, options?: RandomOptions<T>) {
this.#elements = elements;
this.#flags = BitArray.create(elements.length);
this.#flags = BitArray.create(length);
this.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options));
this.reset();
}
public static from<T>(iterable: Iterable<T>, options?: RandomOptions<T>) {
return new this<T>(asArray(iterable), options);
const arr = asArray(iterable);
return new this<T>(arr, arr.length, options);
}
public static of<T>(options?: RandomOptions<T>, ...values: T[]) {
return new this<T>(values, options);
return new this<T>(values, values.length, options);
}
public get state() {