add cartesian product
This commit is contained in:
@@ -673,6 +673,10 @@ export abstract class BaseSequence<TElement> extends SequenceMarker implements S
|
||||
return new ZippedSequence<TElement, TOther>(this, wrap(sequence));
|
||||
}
|
||||
|
||||
cartesianProduct<TOther>(sequence: Iterable<TOther>): Sequence<[TElement, TOther]> {
|
||||
return new CartesianProductSequence<TElement, TOther>(this, wrap(sequence));
|
||||
}
|
||||
|
||||
indexed(): Sequence<[number, TElement]> {
|
||||
return new IndexedSequence<TElement>(this);
|
||||
}
|
||||
@@ -1039,6 +1043,10 @@ export class DelegatedSequence<TElement> extends SequenceMarker implements Seque
|
||||
return this.#sequence.zip(sequence);
|
||||
}
|
||||
|
||||
cartesianProduct<TOther>(sequence: Iterable<TOther>) {
|
||||
return this.#sequence.cartesianProduct(sequence);
|
||||
}
|
||||
|
||||
indexed() {
|
||||
return this.#sequence.indexed();
|
||||
}
|
||||
@@ -2094,6 +2102,46 @@ export class ZippedSequence<T, U> extends BaseSequence<[T, U]> {
|
||||
}
|
||||
}
|
||||
|
||||
export class CartesianProductSequence<T, U> extends BaseSequence<[T, U]> {
|
||||
readonly #first: Sequence<T>;
|
||||
readonly #second: Sequence<U>;
|
||||
|
||||
constructor(first: Sequence<T>, second: Sequence<U>) {
|
||||
super();
|
||||
|
||||
this.#first = first;
|
||||
this.#second = second;
|
||||
}
|
||||
|
||||
override nonEnumeratedCount() {
|
||||
const n1 = this.#first.nonEnumeratedCount();
|
||||
|
||||
if (n1 < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const n2 = this.#second.nonEnumeratedCount();
|
||||
|
||||
if (n2 < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return n1 * n2;
|
||||
}
|
||||
|
||||
override maxCount() {
|
||||
return this.#first.maxCount() * this.#second.maxCount();
|
||||
}
|
||||
|
||||
override *iterator() {
|
||||
for (const firstObj of this.#first) {
|
||||
for (const secondObj of this.#second) {
|
||||
yield [firstObj, secondObj] as [T, U];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class UnionSequence<T> extends BaseSequence<T> {
|
||||
readonly #first: Sequence<T>;
|
||||
readonly #second: Sequence<T>;
|
||||
|
||||
Reference in New Issue
Block a user