UNPKG

@mirei/ts-collections

Version:

A collection of wrappers for common data structures in TypeScript

2,191 lines 162 kB
class h { /* istanbul ignore next */ constructor() { } static equalityComparator = (t, e) => Object.is(t, e); static orderComparator = (t, e) => t === e ? 0 : t > e ? 1 : -1; static reverseOrderComparator = (t, e) => t === e ? 0 : t > e ? -1 : 1; } class j { comparer; constructor(t) { this.comparer = t ?? h.equalityComparator; } aggregate(t, e, r) { return Bt(this, t, e, r); } aggregateBy(t, e, r, n) { return zt(this, t, e, r, n); } all(t) { return Gt(this, t); } any(t) { return Dt(this, t); } append(t) { return Rt(this, t); } average(t) { return At(this, t); } cast() { return It(this); } chunk(t) { return Ot(this, t); } combinations(t) { return Et(this, t); } concat(t) { return Pt(this, t); } contains(t, e) { return e ??= this.comparer, N(this, t, e); } count(t) { return qt(this, t); } countBy(t, e) { return Mt(this, t, e); } cycle(t) { return Vt(this, t); } defaultIfEmpty(t) { return Kt(this, t); } distinct(t) { return Nt(this, t); } distinctBy(t, e) { return Wt(this, t, e); } elementAt(t) { return jt(this, t); } elementAtOrDefault(t) { return Tt(this, t); } except(t, e) { return e = e ?? this.comparer, Ft(this, t, e); } exceptBy(t, e, r) { return $t(this, t, e, r); } first(t) { return Jt(this, t); } firstOrDefault(t) { return Qt(this, t); } forEach(t) { let e = 0; for (const r of this) t(r, e++); } groupBy(t, e) { return Ht(this, t, e); } groupJoin(t, e, r, n, s) { return Ut(this, t, e, r, n, s); } index() { return Yt(this); } intersect(t, e) { return e = e ?? this.comparer, Xt(this, t, e); } intersectBy(t, e, r) { return Zt(this, t, e, r); } intersperse(t) { return _t(this, t); } join(t, e, r, n, s, o) { return te(this, t, e, r, n, s, o); } last(t) { return ee(this, t); } lastOrDefault(t) { return re(this, t); } max(t) { return ne(this, t); } maxBy(t, e) { return se(this, t, e); } min(t) { return ie(this, t); } minBy(t, e) { return oe(this, t, e); } none(t) { return ae(this, t); } ofType(t) { return ue(this, t); } orderBy(t, e) { return le(this, t, e); } orderByDescending(t, e) { return he(this, t, e); } pairwise(t) { return ce(this, t); } partition(t) { return fe(this, t); } permutations(t) { return ye(this, t); } prepend(t) { return de(this, t); } product(t) { return pe(this, t); } reverse() { return ct(this); } scan(t, e) { return we(this, t, e); } select(t) { return ft(this, t); } selectMany(t) { return me(this, t); } sequenceEqual(t, e) { return e ??= this.comparer, ge(this, t, e); } shuffle() { return ke(this); } single(t) { return ve(this, t); } singleOrDefault(t) { return be(this, t); } skip(t) { return Ce(this, t); } skipLast(t) { return xe(this, t); } skipWhile(t) { return Se(this, t); } span(t) { return Le(this, t); } step(t) { return Be(this, t); } sum(t) { return ze(this, t); } take(t) { return Ge(this, t); } takeLast(t) { return De(this, t); } takeWhile(t) { return Re(this, t); } toArray() { return Ae(this); } toCircularLinkedList(t) { return Ie(this, t); } toDictionary(t, e, r) { return Oe(this, t, e, r); } toEnumerableSet() { return Ee(this); } toImmutableDictionary(t, e, r) { return Pe(this, t, e, r); } toImmutableList(t) { return qe(this, t); } toImmutablePriorityQueue(t) { return Me(this, t); } toImmutableQueue(t) { return Ve(this, t); } toImmutableSet() { return Ke(this); } toImmutableSortedDictionary(t, e, r, n) { return Ne(this, t, e, r, n); } toImmutableSortedSet(t) { return We(this, t); } toImmutableStack(t) { return je(this, t); } toLinkedList(t) { return t ??= this.comparer, Te(this, t); } toList(t) { return t ??= this.comparer, Fe(this, t); } toLookup(t, e, r) { return $e(this, t, e, r); } toMap(t, e) { return Je(this, t, e); } toObject(t, e) { return Qe(this, t, e); } toPriorityQueue(t) { return He(this, t); } toQueue(t) { return t ??= this.comparer, Ue(this, t); } toSet() { return Ye(this); } toSortedDictionary(t, e, r, n) { return Xe(this, t, e, r, n); } toSortedSet(t) { return Ze(this, t); } toStack(t) { return t ??= this.comparer, _e(this, t); } union(t, e) { return e ??= this.comparer, tr(this, t, e); } unionBy(t, e, r) { return er(this, t, e, r); } where(t) { return rr(this, t); } windows(t) { return nr(this, t); } zip(t, e) { return sr(this, t, e); } getIterableSize(t) { return t instanceof Array ? t.length : t instanceof Set || t instanceof Map ? t.size : t instanceof j ? t.count() : u(t).count(); } } class C { key; value; constructor(t, e) { this.key = t, this.value = e; } equals(t, e, r) { return e ??= h.equalityComparator, r ??= h.equalityComparator, e(this.key, t.key) && r(this.value, t.value); } } class k { constructor(t) { this.iterable = t, this.#t = new d(() => t); } #t; /** * Creates an empty sequence. * * @template TElement The type of elements in the sequence. * @returns {IEnumerable<TElement>} An empty sequence. */ static empty() { return new k([]); } /** * Creates an enumerable sequence from the given source. * @template TElement The type of elements in the sequence. * @param source The source iterable that will be converted to an enumerable sequence. * @returns {IEnumerable<TElement>} An enumerable sequence that contains the elements of the source. */ static from(t) { return new k(t); } /** * Creates a range of numbers starting from the specified start value and containing the specified count of elements. * @param {number} start The start value of the range. * @param {number} count The number of elements in the range. * @returns {IEnumerable<number>} An enumerable range of numbers. */ static range(t, e) { return new d(function* () { for (let r = 0; r < e; ++r) yield t + r; }); } /** * Repeats the specified element a specified number of times. * * @template TElement The type of the element to repeat. * @param {TElement} element The element to repeat. * @param {number} count The number of times to repeat the element. * @returns {IEnumerable<TElement>} An Iterable representing the repeated elements. */ static repeat(t, e) { return new d(function* () { for (let r = 0; r < e; ++r) yield t; }); } *[Symbol.iterator]() { yield* this.iterable; } aggregate(t, e, r) { return this.#t.aggregate(t, e, r); } aggregateBy(t, e, r, n) { return this.#t.aggregateBy(t, e, r, n); } all(t) { return this.#t.all(t); } any(t) { return this.#t.any(t); } append(t) { return this.#t.append(t); } average(t) { return this.#t.average(t); } cast() { return this.#t.cast(); } chunk(t) { return this.#t.chunk(t); } combinations(t) { return this.#t.combinations(t); } concat(t) { return this.#t.concat(t); } contains(t, e) { return this.#t.contains(t, e); } count(t) { return this.#t.count(t); } countBy(t, e) { return this.#t.countBy(t, e); } cycle(t) { return this.#t.cycle(t); } defaultIfEmpty(t) { return this.#t.defaultIfEmpty(t); } distinct(t) { return this.#t.distinct(t); } distinctBy(t, e) { return this.#t.distinctBy(t, e); } elementAt(t) { return this.#t.elementAt(t); } elementAtOrDefault(t) { return this.#t.elementAtOrDefault(t); } except(t, e) { return this.#t.except(t, e); } exceptBy(t, e, r) { return this.#t.exceptBy(t, e, r); } first(t) { return this.#t.first(t); } firstOrDefault(t) { return this.#t.firstOrDefault(t); } forEach(t) { this.#t.forEach(t); } groupBy(t, e) { return this.#t.groupBy(t, e); } groupJoin(t, e, r, n, s) { return this.#t.groupJoin(t, e, r, n, s); } index() { return this.#t.index(); } intersect(t, e) { return this.#t.intersect(t, e); } intersectBy(t, e, r) { return this.#t.intersectBy(t, e, r); } intersperse(t) { return this.#t.intersperse(t); } join(t, e, r, n, s, o) { return this.#t.join(t, e, r, n, s, o); } last(t) { return this.#t.last(t); } lastOrDefault(t) { return this.#t.lastOrDefault(t); } max(t) { return this.#t.max(t); } maxBy(t, e) { return this.#t.maxBy(t, e); } min(t) { return this.#t.min(t); } minBy(t, e) { return this.#t.minBy(t, e); } none(t) { return this.#t.none(t); } ofType(t) { return this.#t.ofType(t); } orderBy(t, e) { return this.#t.orderBy(t, e); } orderByDescending(t, e) { return this.#t.orderByDescending(t, e); } pairwise(t) { return this.#t.pairwise(t); } partition(t) { return this.#t.partition(t); } permutations(t) { return this.#t.permutations(t); } prepend(t) { return this.#t.prepend(t); } product(t) { return this.#t.product(t); } reverse() { return this.#t.reverse(); } scan(t, e) { return this.#t.scan(t, e); } select(t) { return this.#t.select(t); } selectMany(t) { return this.#t.selectMany(t); } sequenceEqual(t, e) { return this.#t.sequenceEqual(t, e); } shuffle() { return this.#t.shuffle(); } single(t) { return this.#t.single(t); } singleOrDefault(t) { return this.#t.singleOrDefault(t); } skip(t) { return this.#t.skip(t); } skipLast(t) { return this.#t.skipLast(t); } skipWhile(t) { return this.#t.skipWhile(t); } span(t) { return this.#t.span(t); } step(t) { return this.#t.step(t); } sum(t) { return this.#t.sum(t); } take(t) { return this.#t.take(t); } takeLast(t) { return this.#t.takeLast(t); } takeWhile(t) { return this.#t.takeWhile(t); } toArray() { return this.#t.toArray(); } toCircularLinkedList(t) { return this.#t.toCircularLinkedList(t); } toDictionary(t, e, r) { return this.#t.toDictionary(t, e, r); } toEnumerableSet() { return this.#t.toEnumerableSet(); } toImmutableDictionary(t, e, r) { return this.#t.toImmutableDictionary(t, e, r); } toImmutableList(t) { return this.#t.toImmutableList(t); } toImmutablePriorityQueue(t) { return this.#t.toImmutablePriorityQueue(t); } toImmutableQueue(t) { return this.#t.toImmutableQueue(t); } toImmutableSet() { return this.#t.toImmutableSet(); } toImmutableSortedDictionary(t, e, r, n) { return this.#t.toImmutableSortedDictionary(t, e, r, n); } toImmutableSortedSet(t) { return this.#t.toImmutableSortedSet(t); } toImmutableStack(t) { return this.#t.toImmutableStack(t); } toLinkedList(t) { return this.#t.toLinkedList(t); } toList(t) { return this.#t.toList(t); } toLookup(t, e, r) { return this.#t.toLookup(t, e, r); } toMap(t, e) { return this.#t.toMap(t, e); } toObject(t, e) { return this.#t.toObject(t, e); } toPriorityQueue(t) { return this.#t.toPriorityQueue(t); } toQueue(t) { return this.#t.toQueue(t); } toSet() { return this.#t.toSet(); } toSortedDictionary(t, e, r, n) { return this.#t.toSortedDictionary(t, e, r, n); } toSortedSet(t) { return this.#t.toSortedSet(t); } toStack(t) { return this.#t.toStack(t); } union(t, e) { return this.#t.union(t, e); } unionBy(t, e, r) { return this.#t.unionBy(t, e, r); } where(t) { return this.#t.where(t); } windows(t) { return this.#t.windows(t); } zip(t, e) { return this.#t.zip(t, e); } } class tt extends j { #t; #e; constructor(t) { super( (r, n) => Object.is(r.key, n.key) || (t ?? h.orderComparator)(r.key, n.key) === 0 ), this.#t = t; const e = (r, n) => this.#t(r.key, n.key); this.#e = new ut([], e); } static create(t, e, r, n = h.orderComparator) { if (t == null) throw new Error("source cannot be null."); if (e == null) throw new Error("keySelector cannot be null."); if (r == null) throw new Error("valueSelector cannot be null."); const s = new tt(n); for (const o of t) { const a = s.#e.find((l) => n(e(o), l.key) === 0); a ? a.source.add(r(o)) : s.#e.insert(new rt(e(o), new w([r(o)]))); } return s; } *[Symbol.iterator]() { yield* this.#e; } get(t) { return this.#e.findBy(t, (r) => r.key, this.#t) ?? k.empty(); } hasKey(t) { return !!this.#e.findBy(t, (e) => e.key, this.#t); } size() { return this.#e.size(); } get length() { return this.#e.length; } } class g extends Error { constructor(t = "Index is out of bounds.") { typeof t == "number" ? super(`Index ${t} is out of bounds.`) : super(t); } } class x extends Error { constructor(t, e) { e ? (t = `Invalid argument: ${e}. ${t}`, super(t)) : super(t); } } class F extends Error { constructor(t = "Sequence contains more than one element.") { super(t); } } class $ extends Error { constructor(t = "Sequence contains more than one matching element.") { super(t); } } class p extends Error { constructor(t = "Sequence contains no elements.") { super(t); } } class W extends Error { constructor(t = "Sequence contains no matching element.") { super(t); } } class ir extends Number { static [Symbol.hasInstance] = (t) => typeof t == "number"; } class or extends String { static [Symbol.hasInstance] = (t) => typeof t == "string"; } class ar extends Boolean { static [Symbol.hasInstance] = (t) => typeof t == "boolean"; } class ur extends Object { static [Symbol.hasInstance] = (t) => typeof t == "object"; } class lr { static [Symbol.hasInstance] = (t) => typeof t == "bigint"; } class hr { static [Symbol.hasInstance] = (t) => typeof t == "symbol"; } const wt = (i) => { const t = i.name; return t === "Number" ? ir : t === "String" ? or : t === "Boolean" ? ar : t === "BigInt" ? lr : t === "Object" ? ur : t === "Symbol" ? hr : i; }, mt = (i, t, e) => { for (const n of i) if (e(n.key, t)) return n.group; const r = []; return i.push({ key: t, group: r }), r; }, gt = (i, t, e) => { for (const r of i) if (e(r.key, t)) return r.group; return null; }, kt = (i, t, e, r) => { const n = e(i); let s = !1; for (const o of t) if (r(n, o.key)) { o.elements.push(i), s = !0; break; } s || t.push({ key: n, elements: [i] }); }, cr = (i, t, e) => { const r = []; if (!i) return r; for (const n of i) kt(n, r, t, e); return r; }, fr = async (i, t, e) => { const r = []; if (!i) return r; for await (const n of i) kt(n, r, t, e); return r; }, vt = function* (i, t, e, r, n, s) { let o = !1; for (const a of e) if (r(t, a.key)) for (const l of a.elements) yield n(i, l), o = !0; s && !o && (yield n(i, null)); }, bt = function* (i, t) { const e = i.length, r = t ?? e; if (r < 0 || r > e) throw new x("Invalid permutation size.", "size"); if (r === 0 || e === 0) return yield k.empty(); const n = new Array(e).fill(!1), s = new Array(r); function* o(a) { if (a === r) return yield k.from(s); for (let l = 0; l < e; l++) n[l] || (n[l] = !0, s[a] = i[l], yield* o(a + 1), n[l] = !1); } yield* o(0); }; class d { constructor(t) { this.iterable = t; } *[Symbol.iterator]() { yield* this.iterable(); } aggregate(t, e, r) { let n; if (e == null) { if (!this.any()) throw new p(); n = this.first(); for (const s of this.skip(1)) n = t(n, s); } else { n = e; for (const s of this) n = t(n, s); } return r ? r(n) : n; } aggregateBy(t, e, r, n) { return n ??= h.equalityComparator, this.groupBy(t, n).select((o) => new C(o.key, o.source.aggregate(r, e instanceof Function ? e(o.key) : e))); } all(t) { for (const e of this) if (!t(e)) return !1; return !0; } any(t) { if (!t) return !this[Symbol.iterator]().next().done; for (const e of this) if (t(e)) return !0; return !1; } append(t) { return new d(() => this.appendGenerator(t)); } average(t) { if (!this.any()) throw new p(); let e = 0, r = 0; for (const n of this) e += t?.(n) ?? n, r++; return e / r; } cast() { return new d(() => this.castGenerator()); } chunk(t) { if (t < 1) throw new x("Size must be greater than 0.", "size"); return new d(() => this.chunkGenerator(t)); } combinations(t) { if (t != null && t < 0) throw new x("Size must be greater than or equal to 0.", "size"); return new d(() => this.combinationsGenerator(t)); } concat(t) { return new d(() => this.concatGenerator(t)); } contains(t, e) { e ??= h.equalityComparator; for (const r of this) if (e(r, t)) return !0; return !1; } count(t) { let e = 0; if (!t) { for (const r of this) ++e; return e; } for (const r of this) t(r) && ++e; return e; } countBy(t, e) { return e ??= h.equalityComparator, this.groupBy(t, e).select((n) => new C(n.key, n.source.count())); } cycle(t) { return new d(() => this.cycleGenerator(t)); } defaultIfEmpty(t) { return new d(() => this.defaultIfEmptyGenerator(t)); } distinct(t) { const e = t ?? h.equalityComparator; return new d(() => this.unionGenerator(k.empty(), e)); } distinctBy(t, e) { const r = e ?? h.equalityComparator; return new d(() => this.unionByGenerator(k.empty(), t, r)); } elementAt(t) { if (t < 0) throw new g(t); let e = 0; for (const r of this) { if (t === e) return r; ++e; } throw new g(t); } elementAtOrDefault(t) { let e = 0; for (const r of this) { if (t === e) return r; ++e; } return null; } except(t, e) { return e ??= h.equalityComparator, new d(() => this.exceptGenerator(t, e)); } exceptBy(t, e, r) { return r ??= h.equalityComparator, new d(() => this.exceptByGenerator(t, e, r)); } first(t) { if (!this.any()) throw new p(); for (const e of this) if (!t || t(e)) return e; throw new W(); } firstOrDefault(t) { for (const e of this) if (!t || t(e)) return e; return null; } forEach(t) { let e = 0; for (const r of this) t(r, e++); } groupBy(t, e) { return e ??= h.equalityComparator, new d(() => this.groupByGenerator(t, e)); } groupJoin(t, e, r, n, s) { return s ??= h.equalityComparator, new d(() => this.groupJoinGenerator(t, e, r, n, s)); } index() { return new d(() => this.indexGenerator()); } intersect(t, e) { return e ??= h.equalityComparator, new d(() => this.intersectGenerator(t, e)); } intersectBy(t, e, r) { return r ??= h.equalityComparator, new d(() => this.intersectByGenerator(t, e, r)); } intersperse(t) { return new d(() => this.intersperseGenerator(t)); } join(t, e, r, n, s, o) { return s ??= h.equalityComparator, new d(() => this.joinGenerator(t, e, r, n, s, o)); } last(t) { let e = !1, r = null; for (const n of this) (!t || t(n)) && (r = n, e = !0); if (!e) throw t ? new W() : new p(); return r; } lastOrDefault(t) { let e = null; for (const r of this) (!t || t(r)) && (e = r); return e; } max(t) { let e = null; if (t) { for (const r of this) e = Math.max(e ?? Number.NEGATIVE_INFINITY, t(r)); if (e == null) throw new p(); return e; } else { for (const r of this) e = Math.max(e ?? Number.NEGATIVE_INFINITY, r); if (e == null) throw new p(); return e; } } maxBy(t, e) { let r = null, n = null; for (const s of this) { const o = t(s); (n == null || (e ?? h.orderComparator)(o, n) > 0) && (r = s, n = o); } if (r == null) throw new p(); return r; } min(t) { let e = null; if (t) { for (const r of this) e = Math.min(e ?? Number.POSITIVE_INFINITY, t(r)); if (e == null) throw new p(); return e; } else { for (const r of this) e = Math.min(e ?? Number.POSITIVE_INFINITY, r); if (e == null) throw new p(); return e; } } minBy(t, e) { let r = null, n = null; for (const s of this) { const o = t(s); (n == null || (e ?? h.orderComparator)(o, n) < 0) && (r = s, n = o); } if (r == null) throw new p(); return r; } none(t) { if (!t) return !!this[Symbol.iterator]().next().done; for (const e of this) if (t(e)) return !1; return !0; } ofType(t) { return new d(() => this.ofTypeGenerator(t)); } orderBy(t, e) { return O.createOrderedEnumerable(this, t, !0, !1, e); } orderByDescending(t, e) { return O.createOrderedEnumerable(this, t, !1, !1, e); } pairwise(t) { return new d(() => this.pairwiseGenerator(t ??= (e, r) => [e, r])); } partition(t) { const e = new w(), r = new w(); for (const n of this) t(n) ? e.add(n) : r.add(n); return [new k(e), new k(r)]; } permutations(t) { if (t != null && t < 1) throw new x("Size must be greater than 0.", "size"); return new d(() => this.permutationsGenerator(t)); } prepend(t) { return new d(() => this.prependGenerator(t)); } product(t) { if (!this.any()) throw new p(); let e = 1; for (const r of this) e *= t?.(r) ?? r; return e; } reverse() { return new d(() => this.reverseGenerator()); } scan(t, e) { return new d(() => this.scanGenerator(t, e)); } select(t) { return new d(() => this.selectGenerator(t)); } selectMany(t) { return new d(() => this.selectManyGenerator(t)); } sequenceEqual(t, e) { e ??= h.equalityComparator; const r = this[Symbol.iterator](), n = t[Symbol.iterator](); let s = r.next(), o = n.next(); if (s.done && o.done) return !0; for (; !s.done && !o.done; ) { if (!e(s.value, o.value)) return !1; if (s = r.next(), o = n.next(), s.done && o.done) return !0; } return !1; } shuffle() { return new d(() => this.shuffleGenerator()); } single(t) { let e = null, r = !1; if (!this.any()) throw new p(); for (const n of this) if (!t || t(n)) { if (r) throw t ? new $() : new F(); e = n, r = !0; } if (!r) throw t ? new W() : new p(); return e; } singleOrDefault(t) { let e = null, r = !1; for (const n of this) if (!t || t(n)) { if (r) throw t ? new $() : new F(); e = n, r = !0; } return e; } skip(t) { return new d(() => this.skipGenerator(t)); } skipLast(t) { return new d(() => this.skipLastGenerator(t)); } skipWhile(t) { return new d(() => this.skipWhileGenerator(t)); } span(t) { const e = new w(), r = new w(); let n = !1; for (const s of this) n ? r.add(s) : t(s) ? e.add(s) : (n = !0, r.add(s)); return [new k(e), new k(r)]; } step(t) { if (t < 1) throw new x("Step must be greater than 0.", "step"); return new d(() => this.stepGenerator(t)); } sum(t) { if (!this.any()) throw new p(); let e = 0; for (const r of this) e += t?.(r) ?? r; return e; } take(t) { return new d(() => this.takeGenerator(t)); } takeLast(t) { return new d(() => this.takeLastGenerator(t)); } takeWhile(t) { return new d(() => this.takeWhileGenerator(t)); } thenBy(t, e) { return O.createOrderedEnumerable(this, t, !0, !0, e); } thenByDescending(t, e) { return O.createOrderedEnumerable(this, t, !1, !0, e); } toArray() { return Array.from(this); } toCircularLinkedList(t) { return new at(this, t); } toDictionary(t, e, r) { const n = new Z(k.empty(), r); for (const s of this) { const o = s instanceof C ? t?.(s) ?? s.key : t(s), a = s instanceof C ? e?.(s) ?? s.value : e(s); n.add(o, a); } return n; } toEnumerableSet() { return new ht(this); } toImmutableDictionary(t, e, r) { const n = this.toDictionary(t, e, r), s = n.keys().zip(n.values()).select((o) => new C(o[0], o[1])); return M.create(s); } toImmutableList(t) { return L.create(this, t); } toImmutablePriorityQueue(t) { return G.create(this, t); } toImmutableQueue(t) { return A.create(this, t); } toImmutableSet() { return z.create(this); } toImmutableSortedDictionary(t, e, r, n) { const s = this.toSortedDictionary(t, e, r, n), o = s.keys().zip(s.values()).select((a) => new C(a[0], a[1])); return V.create(o); } toImmutableSortedSet(t) { return B.create(this, t); } toImmutableStack(t) { return K.create(this, t); } toLinkedList(t) { return new T(this, t); } toList(t) { return new w(this, t); } toLookup(t, e, r) { return tt.create(this, t, e, r); } toMap(t, e) { const r = /* @__PURE__ */ new Map(); for (const n of this) { const s = n instanceof C ? t?.(n) ?? n.key : t(n), o = n instanceof C ? e?.(n) ?? n.value : e(n); r.set(s, o); } return r; } toObject(t, e) { const r = {}; for (const n of this) { const s = n instanceof C ? t?.(n) ?? n.key : t(n); r[s] = n instanceof C ? e?.(n) ?? n.value : e(n); } return r; } toPriorityQueue(t) { return new mr(this, t); } toQueue(t) { return new it(this, t); } toSet() { return new Set(this); } toSortedDictionary(t, e, r, n) { const s = new _([], r, n); for (const o of this) { const a = o instanceof C ? t?.(o) ?? o.key : t(o), l = o instanceof C ? e?.(o) ?? o.value : e(o); s.add(a, l); } return s; } toSortedSet(t) { return new D(this, t); } toStack(t) { return new ot(this, t); } union(t, e) { return e ??= h.equalityComparator, new d(() => this.unionGenerator(t, e)); } unionBy(t, e, r) { return r ??= h.equalityComparator, new d(() => this.unionByGenerator(t, e, r)); } where(t) { return new d(() => this.whereGenerator(t)); } windows(t) { if (t < 1) throw new x("Size must be greater than 0.", "size"); return new d(() => this.windowsGenerator(t)); } zip(t, e) { return new d(() => this.zipGenerator(t, e)); } *appendGenerator(t) { yield* this, yield t; } *castGenerator() { for (const t of this) yield t; } *chunkGenerator(t) { const e = this[Symbol.iterator](); let r = e.next(); for (; !r.done; ) { const n = new w(); for (let s = 0; s < t && !r.done; ++s) n.add(r.value), r = e.next(); yield n; } } *combinationsGenerator(t) { const e = this[Symbol.iterator](); let r = e.next(); if (r.done) return yield* []; const n = new w(); for (; !r.done; ) n.add(r.value), r = e.next(); const s = 1 << n.length, o = /* @__PURE__ */ new Set(); for (let a = 0; a < s; ++a) { const l = new w(); for (let c = 0; c < n.length; ++c) (a & 1 << c) !== 0 && l.add(n.elementAt(c)); if (t == null || l.length === t) { const c = l.aggregate((f, m) => f + m, ","); o.has(c) || (o.add(c), yield l); } } } *concatGenerator(t) { yield* this, yield* t; } *cycleGenerator(t) { if (this.none()) throw new p(); if (t == null) for (; ; ) yield* this; else for (let e = 0; e < t; ++e) yield* this; } *defaultIfEmptyGenerator(t) { this.any() ? yield* this : (yield t ?? null, yield* this); } *exceptByGenerator(t, e, r) { const n = new D([], r), s = new w([], r), { value: o, done: a } = new d(() => t)[Symbol.iterator]().next(); if (a) { const { value: f, done: m } = new d(() => this)[Symbol.iterator]().next(); if (m) return yield* this; const P = e(f), R = typeof r(P, P) == "number" ? n : s; for (const yt of this) { const dt = e(yt); R.contains(dt) || (R.add(dt), yield yt); } return; } const l = e(o), c = typeof r(l, l) == "number" ? n : s; for (const f of t) { const m = e(f); c.contains(m) || c.add(m); } for (const f of this) { const m = e(f); c.contains(m) || (c.add(m), yield f); } } *exceptGenerator(t, e) { return yield* this.exceptByGenerator(t, (r) => r, e); } *groupByGenerator(t, e) { const r = /* @__PURE__ */ new Map(), n = (s) => { for (const o of r.keys()) if (e(o, s)) return o; }; for (const s of this) { const o = t(s); let a, l = o; if (e) { const c = n(o); c !== void 0 && (a = r.get(c), l = c); } else a = r.get(o); if (a) a.source.add(s); else { const c = new w([s]), f = new rt(o, c); r.set(l, f); } } yield* r.values(); } *groupJoinGenerator(t, e, r, n, s) { const o = s ?? h.equalityComparator, a = []; for (const l of t) { const c = r(l); mt(a, c, o).push(l); } for (const l of this) { const c = e(l), f = gt(a, c, o); yield n(l, k.from(f ?? [])); } } *indexGenerator() { let t = 0; for (const e of this) yield [t++, e]; } *intersectByGenerator(t, e, r) { const n = new D([], r), s = new w([], r), { value: o, done: a } = new d(() => t)[Symbol.iterator]().next(); if (a) return yield* k.empty(); const l = e(o), c = typeof r(l, l) == "number" ? n : s; for (const f of t) { const m = e(f); c.contains(m) || c.add(m); } for (const f of this) { const m = e(f); c.remove(m) && (yield f); } } *intersectGenerator(t, e) { return yield* this.intersectByGenerator(t, (r) => r, e); } *intersperseGenerator(t) { let e = 0; for (const r of this) e !== 0 && (yield t), yield r, ++e; } *joinGenerator(t, e, r, n, s, o) { const a = s ?? h.equalityComparator, l = o ?? !1, c = cr(t, r, a); for (const f of this) { const m = e(f); yield* vt( f, m, c, a, n, l ); } } *ofTypeGenerator(t) { const e = typeof t == "string" ? (r) => typeof r === t : (r) => r instanceof wt(t); for (const r of this) e(r) && (yield r); } *pairwiseGenerator(t) { const e = this[Symbol.iterator](); let r = e.next(); for (; !r.done; ) { const n = r; r = e.next(), r.done || (yield t(n.value, r.value)); } } *permutationsGenerator(t) { const e = Array.from(this.distinct()); yield* bt(e, t); } *prependGenerator(t) { yield t, yield* this; } *reverseGenerator() { yield* Array.from(this).reverse(); } *scanGenerator(t, e) { let r; if (e == null) { if (!this.any()) throw new p(); r = this.first(), yield r; for (const n of this.skip(1)) r = t(r, n), yield r; } else { r = e; for (const n of this) r = t(r, n), yield r; } } *selectGenerator(t) { let e = 0; for (const r of this) yield t(r, e++); } *selectManyGenerator(t) { let e = 0; for (const r of this) yield* t(r, e), ++e; } *shuffleGenerator() { const t = Array.from(this); b.shuffle(t), yield* t; } *skipGenerator(t) { let e = 0; for (const r of this) e >= t && (yield r), ++e; } *skipLastGenerator(t) { if (t <= 0) { yield* this; return; } const e = new Array(t); let r = 0, n = 0; for (const s of this) r === t && (yield e[n]), e[n] = s, n = (n + 1) % t, r < t && r++; } *skipWhileGenerator(t) { let e = 0, r = !1; for (const n of this) r ? yield n : t(n, e) ? e++ : (r = !0, yield n); } *stepGenerator(t) { let e = 0; for (const r of this) e % t === 0 && (yield r), ++e; } *takeGenerator(t) { let e = 0; for (const r of this) if (e < t) yield r, e++; else break; } *takeLastGenerator(t) { if (t <= 0) return; const e = new Array(t); let r = 0, n = 0; for (const s of this) { const o = (n + r) % t; e[o] = s, r < t ? r++ : n = (n + 1) % t; } for (let s = 0; s < r; s++) { const o = (n + s) % t; yield e[o]; } } *takeWhileGenerator(t) { let e = 0, r = !1; for (const n of this) if (!r) t(n, e) ? (yield n, ++e) : r = !0; else break; } *unionByGenerator(t, e, r) { const n = r === h.equalityComparator, s = n ? /* @__PURE__ */ new Set() : null, o = n ? null : new Array(); for (const a of [this, t]) for (const l of a) { const c = e(l); let f = !1; if (s) f = s.has(c), f || s.add(c); else if (o) { for (const m of o) if (r(c, m)) { f = !0; break; } f || o.push(c); } f || (yield l); } } *unionGenerator(t, e) { return yield* this.unionByGenerator(t, (r) => r, e ?? h.equalityComparator); } *whereGenerator(t) { let e = 0; for (const r of this) t(r, e) && (yield r), ++e; } *windowsGenerator(t) { const e = this[Symbol.iterator](), r = new w(); for (let n = e.next(); !n.done; n = e.next()) r.add(n.value), r.size() === t && (yield r.toImmutableList(), r.removeAt(0)); } *zipGenerator(t, e) { const r = this[Symbol.iterator](), n = t[Symbol.iterator](); for (; ; ) { const s = r.next(), o = n.next(); if (s.done || o.done) break; yield e?.(s.value, o.value) ?? [s.value, o.value]; } } } class pt extends Error { constructor(t = "No such element exists in the sequence") { super(t); } } class y { constructor(t) { this.iterable = t; } async *[Symbol.asyncIterator]() { yield* this.iterable(); } async aggregate(t, e, r) { let n = null, s = 0; if (e == null) { let o = 0; for await (const a of this) o === 0 ? n = a : n = t(n, a), ++o, ++s; } else { n = e; for await (const o of this) n = t(n, o), ++s; } if (s === 0 && n == null) throw new p(); return r?.(n) ?? n; } aggregateBy(t, e, r, n) { return n ??= h.equalityComparator, this.groupBy(t, n).select((o) => new C(o.key, o.source.aggregate(r, e instanceof Function ? e(o.key) : e))); } async all(t) { for await (const e of this) if (!t(e)) return !1; return !0; } async any(t) { if (!t) return this[Symbol.asyncIterator]().next().then((e) => !e.done); for await (const e of this) if (t(e)) return !0; return !1; } append(t) { return new y(() => this.appendGenerator(t)); } async average(t) { let e = 0, r = 0; for await (const n of this) e += t?.(n) ?? n, ++r; if (r === 0) throw new p(); return e / r; } cast() { return new y(() => this.castGenerator()); } chunk(t) { if (t < 1) throw new x("Size must be greater than 0.", "size"); return new y(() => this.chunkGenerator(t)); } combinations(t) { if (t != null && t < 0) throw new x("Size must be greater than or equal to 0.", "size"); return new y(() => this.combinationsGenerator(t)); } concat(t) { return new y(() => this.concatGenerator(t)); } async contains(t, e) { e ??= h.equalityComparator; for await (const r of this) if (e(r, t)) return !0; return !1; } async count(t) { let e = 0; if (!t) { for await (const r of this) ++e; return e; } for await (const r of this) t(r) && ++e; return e; } countBy(t, e) { return e ??= h.equalityComparator, this.groupBy(t, e).select((n) => new C(n.key, n.source.count())); } cycle(t) { return new y(() => this.cycleGenerator(t)); } defaultIfEmpty(t) { return new y(() => this.defaultIfEmptyGenerator(t)); } distinct(t) { const e = t ?? h.equalityComparator, r = new y(async function* () { yield* []; }); return new y(() => this.unionGenerator( r, e )); } distinctBy(t, e) { e ??= h.equalityComparator; const r = new y(async function* () { yield* []; }); return new y(() => this.unionByGenerator(r, t, e)); } async elementAt(t) { if (t < 0) throw new g(t); let e = 0; for await (const r of this) { if (e === t) return r; ++e; } throw t >= e ? new g(t) : new pt(); } async elementAtOrDefault(t) { if (t < 0) return null; let e = 0; for await (const r of this) { if (e === t) return r; ++e; } return null; } except(t, e) { return e ??= h.equalityComparator, new y(() => this.exceptGenerator(t, e)); } exceptBy(t, e, r) { return r ??= h.equalityComparator, new y(() => this.exceptByGenerator(t, e, r)); } async first(t) { let e = 0; for await (const r of this) if (++e, !t || t(r)) return r; throw e === 0 ? new p() : new W(); } async firstOrDefault(t) { for await (const e of this) if (!t || t(e)) return e; return null; } async forEach(t) { let e = 0; for await (const r of this) t(r, e), ++e; } groupBy(t, e) { const r = e ?? h.equalityComparator; return new y(() => this.groupByGenerator(t, r)); } groupJoin(t, e, r, n, s) { const o = s ?? h.equalityComparator; return new y(() => this.groupJoinGenerator(t, e, r, n, o)); } index() { return new y(() => this.indexGenerator()); } intersect(t, e) { const r = e ?? h.equalityComparator; return new y(() => this.intersectGenerator(t, r)); } intersectBy(t, e, r) { const n = r ?? h.equalityComparator; return new y(() => this.intersectByGenerator(t, e, n)); } intersperse(t) { return new y(() => this.intersperseGenerator(t)); } join(t, e, r, n, s, o) { const a = s ?? h.equalityComparator; return new y(() => this.joinGenerator(t, e, r, n, a, o ?? !1)); } async last(t) { let e = null, r = !1; for await (const n of this) (!t || t(n)) && (e = n, r = !0); if (!r) throw t ? new W() : new p(); return e; } async lastOrDefault(t) { let e = null; for await (const r of this) (!t || t(r)) && (e = r); return e; } async max(t) { let e = null; for await (const r of this) { const n = t ? t(r) : r; e = Math.max(e ?? Number.NEGATIVE_INFINITY, n); } if (e == null) throw new p(); return e; } async maxBy(t, e) { let r = null, n = null; for await (const s of this) { const o = t(s); (n == null || (e?.(o, n) ?? o) > n) && (n = o, r = s); } if (r == null) throw new p(); return r; } async min(t) { let e = null; for await (const r of this) { const n = t ? t(r) : r; e = Math.min(e ?? Number.POSITIVE_INFINITY, n); } if (e == null) throw new p(); return e; } async minBy(t, e) { let r = null, n = null; for await (const s of this) { const o = t(s); (n == null || (e?.(o, n) ?? o) < n) && (n = o, r = s); } if (r == null) throw new p(); return r; } async none(t) { return !await this.any(t); } ofType(t) { return new y(() => this.ofTypeGenerator(t)); } orderBy(t, e) { return E.createOrderedEnumerable(this, t, !0, !1, e); } orderByDescending(t, e) { return E.createOrderedEnumerable(this, t, !1, !1, e); } pairwise(t) { return new y(() => this.pairwiseGenerator(t)); } async partition(t) { const e = [], r = []; for await (const n of this) t(n) ? e.push(n) : r.push(n); return [k.from(e), k.from(r)]; } permutations(t) { if (t != null && t < 1) throw new x("Size must be greater than 0.", "size"); return new y(() => this.permutationsGenerator(t)); } prepend(t) { return new y(() => this.prependGenerator(t)); } async product(t) { let e = 1, r = 0; for await (const n of this) e *= t?.(n) ?? n, ++r; if (r === 0) throw new p(); return e; } reverse() { return new y(() => this.reverseGenerator()); } scan(t, e) { return new y(() => this.scanGenerator(t, e)); } select(t) { return new y(() => this.selectGenerator(t)); } selectMany(t) { return new y(() => this.selectManyGenerator(t)); } async sequenceEqual(t, e) { e ??= h.equalityComparator; const r = this[Symbol.asyncIterator](), n = t[Symbol.asyncIterator](); let s = await r.next(), o = await n.next(); if (s.done && o.done) return !0; for (; !s.done && !o.done; ) { if (!e(s.value, o.value)) return !1; if (s = await r.next(), o = await n.next(), s.done && o.done) return !0; } return !1; } shuffle() { return new y(() => this.shuffleGenerator()); } async single(t) { let e = null, r = !1, n = 0; for await (const s of this) { if (n++, !t) { if (r) throw new F(); e = s, r = !0; } if (t && t(s)) { if (r) throw new $(); e = s, r = !0; } } if (n === 0) throw new p(); if (!r) throw new W(); return e; } async singleOrDefault(t) { let e = null, r = 0; if (t) { for await (const n of this) if (t(n)) { if (r !== 0) throw new $(); e = n, ++r; } } else for await (const n of this) { if (r !== 0) throw new F(); e = n, ++r; } return e; } skip(t) { return new y(() => this.skipGenerator(t)); } skipLast(t) { return new y(() => this.skipLastGenerator(t)); } skipWhile(t) { return new y(() => this.skipWhileGenerator(t)); } async span(t) { const e = new w(), r = new w(); let n = !1; for await (const s of this) !n && t(s) ? e.add(s) : (n = !0, r.add(s)); return [new k(e), new k(r)]; } step(t) { if (t < 1) throw new x("Step must be greater than 0.", "step"); return new y(() => this.stepGenerator(t)); } async sum(t) { let e = 0, r = 0; for await (const n of this) r += t?.(n) ?? n, ++e; if (e === 0) throw new p(); return r; } take(t) { return new y(() => this.takeGenerator(t)); } takeLast(t) { return new y(() => this.takeLastGenerator(t)); } takeWhile(t) { return new y(() => this.takeWhileGenerator(t)); } thenBy(t, e) { return E.createOrderedEnumerable(this, t, !0, !0, e); } thenByDescending(t, e) { return E.createOrderedEnumerable(this, t, !1, !0, e); } async toArray() { const t = []; for await (const e of this) t.push(e); return t; } async toObject(t, e) { const r = {}; for await (const n of this) { const s = n instanceof C ? t?.(n) ?? n.key : t(n); r[s] = n instanceof C ? e?.(n) ?? n.value : e(n); } return r; } union(t, e) { return new y(() => this.unionGenerator(t, e)); } unionBy(t, e, r) { return new y(() => this.unionByGenerator(t, e, r)); } where(t) { return new y(() => this.whereGenerator(t)); } windows(t) { if (t < 1) throw new x("Size must be greater than 0.", "size"); return new y(() => this.windowsGenerator(t)); } zip(t, e) { return new y(() => this.zipGenerator(t, e)); } async *appendGenerator(t) { yield* this, yield t; } async *castGenerator() { for await (const t of this) yield t; } async *chunkGenerator(t) { const e = []; for await (const r of this) e.push(r), e.length === t && (yield k.from([...e]), e.length = 0); e.length > 0 && (yield k.from([...e])); } async *combinationsGenerator(t) { const e = []; for await (const o of this) e.push(o); const r = e.length, n = 1 << r, s = /* @__PURE__ */ new Set(); for (let o = 0; o < n; o++) { const a = new w(); for (let l = 0; l < r; l++) o & 1 << l && a.add(e[l]); if (t === void 0 || a.length === t) { const l = a.aggregate((c, f) => c + f, ","); s.has(l) || (s.add(l), yield a); } } } async *concatGenerator(t) { yield* this, yield* t; } async *cycleGenerator(t) { const e = []; for await (const r of this) e.push(r); if (e.length === 0) throw new p(); if (t == null) for (; ; ) for (const r of e) yield r; else for (let r = 0; r < t; ++r) for (const n of e) yield n; } async *defaultIfEmptyGenerator(t) { let e = !1; for await (const r of this) e = !0, yield r; e || (yield t ?? null); } async *exceptByGenerator(t, e, r) { const n = new D([], r), s = new w([], r), { value: o, done: a } = await new y(() => t)[Symbol.asyncIterator]().next(); if (a) return yield* this; const l = e(o), c = typeof r(l, l) == "number" ? n : s; c.add(l); for await (const f of t) { const m = e(f); c.add(m); } for await (const f of this) { const m = e(f); c.contains(m) || (c.add(m), yield f); } } async *exceptGenerator(t, e) { return yield* this.exceptByGenerator(t, (r) => r, e); } async *groupByGenerator(t, e) { const r = /* @__PURE__ */ new Map(), n = (s) => { for (const o of r.keys()) if (e(o, s)) return o; }; for await (const s of this) { const o = t(s); let a; const l = n(o); if (l !== void 0 && (a = r.get(l), !a)) throw new pt(`Group with key ${l} not found.`); if (a) a.source.add(s); else { const c = new w([s]), f = new rt(o, c); r.set(o, f); } } yield* r.values(); } async *groupJoinGenerator(t, e, r, n, s) { const o = s ?? h.equalityComparator, a = []; for await (const l of t) { const c = r(l); mt(a, c, o).push(l); } for await (const l of this) { const c = e(l), f = gt(a, c, o); yield n(l, k.from(f ?? [])); } } async *indexGenerator() { let t = 0; for await (const e of this) yield [t++, e]; } async *intersectByGenerator(t, e, r) { const n = new D([], r), s = new w([], r), { value: o, done: a } = await new y(() => t)[Symbol.asyncIterator]().next(); if (a) return yield* et.empty(); const l = e(o), c = typeof r(l, l) == "number" ? n : s; c.add(l); for await (const f of t) { const m = e(f); c.contains(m) || c.add(m); } for await (const f of this) { const m = e(f); c.contains(m) && (c.remove(m), yield f); } } async *intersectGenerator(t, e) { return yield* this.intersectByGenerator(t, (r) => r, e); } async *intersperseGenerator(t) { let e = 0; for await (const r of this) e > 0 && (yield t), yield r, ++e; } async *joinGenerator(t, e, r, n, s, o) { const a = s ?? h.equalityComparator, l = o ?? !1, c = await fr(t, r, a); for await (const f of this) { const m = e(f); yield* vt( f, m, c, a, n, l ); } } async *ofTypeGenerator(t) { const e = typeof t == "string" ? (r) => typeof r === t : (r) => r instanceof wt(t); for await (const r of this) e(r) && (yield r); } async *pairwiseGenerator(t) { const e = this[Symbol.asyncIterator](); let r = await e.next(); for (; !r.done; ) { const n = r; r = await e.next(), r.done || (yield t(n.value, r.value)); } } async *permutationsGenerator(t) { const e = await this.distinct().toArray(); yield* bt(e, t); } async *prependGenerator(t) { yield t, yield* this; } async *reverseGenerator() { yield* (await this.toArray()).reverse(); } async *scanGenerator(t, e) { let r = null; if (e == null) { let n = 0; for await (const s of this) n === 0 ? (r = s, yield r) : (r = t(r, s), yield r), ++n; if (n === 0) throw new p(); } else { r = e; for await (const n of this) r = t(r, n), yield r; } } async *selectGenerator(t) { let e = 0; for await (const r of this) yield t(r, e++); } async *selectManyGenerator(t) { let e = 0; for await (const r of this) yield* t(r, e++); } async *shuffleGenerator() { const t = await this.toArray(); b.shuffle(t), yield* t; } async *skipGenerator(t) { let e = 0; if (t <= 0) yield* this; else for await (const r of this) e >= t && (yield r), ++e; } async *skipLastGenerator(t) { if (t <= 0) return yield* this; const e = new Array(t); let r = 0, n = 0; for await (const s of this) r === t && (yield e[n]), e[n] = s, n = (n + 1) % t, r < t && r++; } async *skipWhileGenerator(t) { let e = 0, r = !1; for await (const n of this) r ? yield n : t(n, e) ? ++e : (r = !0, yield n); } async *