allow creating RandomPicker from iterable and separately specified length
This commit is contained in:
@@ -91,20 +91,21 @@ export class RandomPicker<T> {
|
|||||||
readonly #flags: BitArray;
|
readonly #flags: BitArray;
|
||||||
readonly #options: Required<RandomOptions<T>>;
|
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.#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.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options));
|
||||||
|
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static from<T>(iterable: Iterable<T>, options?: RandomOptions<T>) {
|
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[]) {
|
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() {
|
public get state() {
|
||||||
|
|||||||
Reference in New Issue
Block a user