refactor
This commit is contained in:
@@ -273,7 +273,7 @@ export namespace sum {
|
||||
|
||||
//#region implementations
|
||||
|
||||
abstract class BaseAsyncEnumerable<TElement> implements AsyncEnumerable<TElement> {
|
||||
export abstract class BaseAsyncEnumerable<TElement> implements AsyncEnumerable<TElement> {
|
||||
[Symbol.asyncIterator]() {
|
||||
return this.iterator();
|
||||
}
|
||||
|
||||
@@ -128,12 +128,10 @@ export class BitArray implements Iterable<boolean> {
|
||||
}
|
||||
|
||||
public isFull() {
|
||||
// return enumerable.sequence(this).all(bit => bit);
|
||||
return this.#bits.subarray(0, this.#wholeBytes).every(byte => byte === FULL_BYTE) && (this.#remainingBits === 0 || this.#bits[this.#wholeBytes] === this.#remainingBitsMask);
|
||||
}
|
||||
|
||||
public isEmpty() {
|
||||
// return enumerable.sequence(this).none(bit => bit);
|
||||
return this.#bits.every(byte => byte === 0);
|
||||
}
|
||||
|
||||
@@ -151,11 +149,6 @@ export class BitArray implements Iterable<boolean> {
|
||||
public and(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// let i = 0;
|
||||
// for (const byte of getBytes(other)) {
|
||||
// this.#bits[i++] &= byte;
|
||||
// }
|
||||
|
||||
for (let i = 0; i < this.#length; i++) {
|
||||
this.#bits[i] &= other.#bits[i];
|
||||
}
|
||||
@@ -166,11 +159,6 @@ export class BitArray implements Iterable<boolean> {
|
||||
public or(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// let i = 0;
|
||||
// for (const byte of getBytes(other)) {
|
||||
// this.#bits[i++] |= byte;
|
||||
// }
|
||||
|
||||
for (let i = 0; i < this.#length; i++) {
|
||||
this.#bits[i] |= other.#bits[i];
|
||||
}
|
||||
@@ -181,11 +169,6 @@ export class BitArray implements Iterable<boolean> {
|
||||
public xor(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// let i = 0;
|
||||
// for (const byte of getBytes(other)) {
|
||||
// this.#bits[i++] ^= byte;
|
||||
// }
|
||||
|
||||
for (let i = 0; i < this.#length; i++) {
|
||||
this.#bits[i] ^= other.#bits[i];
|
||||
}
|
||||
@@ -208,11 +191,6 @@ export class BitArray implements Iterable<boolean> {
|
||||
public andNot(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// let i = 0;
|
||||
// for (const byte of getBytes(other)) {
|
||||
// this.#bits[i++] &= ~byte;
|
||||
// }
|
||||
|
||||
for (let i = 0; i < this.#length; i++) {
|
||||
this.#bits[i] &= ~other.#bits[i];
|
||||
}
|
||||
@@ -223,14 +201,12 @@ export class BitArray implements Iterable<boolean> {
|
||||
public contains(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// return enumerable.sequence(this).zip(enumerable.sequence(other)).where(([, b]) => b).all(([a, b]) => a && b);
|
||||
return arrayLike(this.#bits).zip(arrayLike(other.#bits)).all(([a, b]) => (a & b) === b);
|
||||
}
|
||||
|
||||
public intersects(other: BitArray) {
|
||||
this.#ensureSameSize(other);
|
||||
|
||||
// return enumerable.sequence(this).zip(enumerable.sequence(other)).any(([a, b]) => a && b);
|
||||
return arrayLike(this.#bits).zip(arrayLike(other.#bits)).any(([a, b]) => (a & b) !== 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,6 @@ export interface Collector<TElement, TAccumulator, TResult> {
|
||||
finalize(accumulator: TAccumulator): TResult;
|
||||
}
|
||||
|
||||
// export interface Collector2<TElement, TResult> {
|
||||
// accumulate(element: TElement): void;
|
||||
// finalize(): TResult;
|
||||
// }
|
||||
|
||||
class SimpleCollector<TElement, TAccumulator, TResult> implements Collector<TElement, TAccumulator, TResult> {
|
||||
readonly #initialize: () => TAccumulator;
|
||||
readonly #accumulate: (accumulator: TAccumulator, element: TElement) => void;
|
||||
|
||||
@@ -43,8 +43,8 @@ class NativeEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
}
|
||||
|
||||
class CustomEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
readonly #list: [K, V][] = [];
|
||||
readonly #keyComparer: Equater<K>;
|
||||
readonly #list = new Array<[K, V]>();
|
||||
|
||||
constructor(keyComparer: Equater<K>) {
|
||||
this.#keyComparer = keyComparer;
|
||||
@@ -85,9 +85,7 @@ class CustomEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
}
|
||||
|
||||
remove(key: K) {
|
||||
const length = this.#list.length;
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
for (let i = 0; i < this.#list.length; i++) {
|
||||
if (this.#keyComparer(key, this.#list[i][0])) {
|
||||
const removed = this.#list.splice(i, 1);
|
||||
return removed[0][1];
|
||||
@@ -151,8 +149,8 @@ class NativeAsyncEqualityMap<K, V> implements AsyncEqualityMap<K, V> {
|
||||
}
|
||||
|
||||
class CustomAsyncEqualityMap<K, V> implements AsyncEqualityMap<K, V> {
|
||||
readonly #list: [K, V][] = [];
|
||||
readonly #keyComparer: MaybeAsyncEquater<K>;
|
||||
readonly #list = new Array<[K, V]>();
|
||||
|
||||
constructor(keyComparer: MaybeAsyncEquater<K>) {
|
||||
this.#keyComparer = keyComparer;
|
||||
@@ -193,9 +191,7 @@ class CustomAsyncEqualityMap<K, V> implements AsyncEqualityMap<K, V> {
|
||||
}
|
||||
|
||||
async remove(key: K) {
|
||||
const length = this.#list.length;
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
for (let i = 0; i < this.#list.length; i++) {
|
||||
if (await this.#keyComparer(key, this.#list[i][0])) {
|
||||
const removed = this.#list.splice(i, 1);
|
||||
return removed[0][1];
|
||||
|
||||
@@ -41,8 +41,8 @@ class NativeEqualitySet<T> implements EqualitySet<T> {
|
||||
}
|
||||
|
||||
class CustomEqualitySet<T> implements EqualitySet<T> {
|
||||
readonly #equater: Equater<T>;
|
||||
readonly #list: T[] = [];
|
||||
readonly #equater: Equater<T>;
|
||||
|
||||
constructor(equater: Equater<T>) {
|
||||
this.#equater = equater;
|
||||
@@ -137,8 +137,8 @@ class NativeAsyncEqualitySet<T> implements AsyncEqualitySet<T> {
|
||||
}
|
||||
|
||||
class CustomAsyncEqualitySet<T> implements AsyncEqualitySet<T> {
|
||||
readonly #equater: MaybeAsyncEquater<T>;
|
||||
readonly #list: T[] = [];
|
||||
readonly #equater: MaybeAsyncEquater<T>;
|
||||
|
||||
constructor(equater: MaybeAsyncEquater<T>) {
|
||||
this.#equater = equater;
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
import { BitArray } from "./bitarray.js";
|
||||
import { asArray } from "./utils.js";
|
||||
|
||||
export interface ElementPredicate<T = any> {
|
||||
(index: number, obj: T): boolean;
|
||||
}
|
||||
export type ElementPredicate<T = any> = (index: number, obj: T) => boolean;
|
||||
export type ElementWeight<T = any> = (index: number, obj: T) => number;
|
||||
export type RandomGenerator = () => number;
|
||||
|
||||
export interface ElementWeight<T = any> {
|
||||
(index: number, obj: T): number;
|
||||
}
|
||||
|
||||
export interface RandomGenerator {
|
||||
(): number;
|
||||
}
|
||||
|
||||
export interface RandomOptions<T> {
|
||||
export interface RandomOptions<T = any> {
|
||||
predicate?: ElementPredicate<T>;
|
||||
weight?: ElementWeight<T>;
|
||||
random?: RandomGenerator;
|
||||
@@ -23,7 +15,7 @@ export const alwaysTrue: ElementPredicate = () => true;
|
||||
export const weightOfOne: ElementWeight = () => 1.0;
|
||||
export const mathRandom: RandomGenerator = () => Math.random();
|
||||
|
||||
const defaultOptions = Object.freeze<Required<RandomOptions<any>>>({
|
||||
const defaultOptions = Object.freeze<Required<RandomOptions>>({
|
||||
predicate: alwaysTrue,
|
||||
weight: weightOfOne,
|
||||
random: mathRandom
|
||||
@@ -50,7 +42,7 @@ function mergeOptions<T>(first: RandomOptions<T> | undefined, second: RandomOpti
|
||||
|
||||
function withDefaultOptions<T>(options: RandomOptions<T> | undefined): Required<RandomOptions<T>> {
|
||||
if (!options) {
|
||||
return { ...defaultOptions };
|
||||
return defaultOptions;
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -98,7 +90,7 @@ export class RandomPicker<T> {
|
||||
public constructor(elements: T[], options?: RandomOptions<T>) {
|
||||
this.#elements = elements;
|
||||
this.#flags = new BitArray(elements.length);
|
||||
this.#options = withDefaultOptions(mergeOptions({ predicate: (i, o) => this.#flags.get(i) }, options));
|
||||
this.#options = withDefaultOptions(mergeOptions({ predicate: i => this.#flags.get(i) }, options));
|
||||
|
||||
this.reset();
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ class EnumerableMarker {
|
||||
|
||||
}
|
||||
|
||||
abstract class BaseEnumerable<TElement> extends EnumerableMarker implements Enumerable<TElement> {
|
||||
export abstract class BaseEnumerable<TElement> extends EnumerableMarker implements Enumerable<TElement> {
|
||||
[Symbol.iterator]() {
|
||||
return this.iterator();
|
||||
}
|
||||
|
||||
@@ -54,8 +54,7 @@ export function defaultArrayComparer<T>(a: T, b: T) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const aStr = a === null ? "null" : a.toString();
|
||||
const bStr = b === null ? "null" : b.toString();
|
||||
const aStr = `${a}`, bStr = `${b}`;
|
||||
|
||||
return aStr > bStr ? 1 : aStr < bStr ? -1 : 0;
|
||||
}
|
||||
@@ -81,7 +80,7 @@ export function* asGenerator<T>(iterator: Iterator<T>) {
|
||||
const next = iterator.next();
|
||||
|
||||
if (next.done) {
|
||||
break;
|
||||
return next.value;
|
||||
}
|
||||
|
||||
yield next.value;
|
||||
@@ -93,7 +92,7 @@ export async function* asAsyncGenerator<T>(iterator: MaybeAsyncIterator<T>) {
|
||||
const next = await iterator.next();
|
||||
|
||||
if (next.done) {
|
||||
break;
|
||||
return next.value;
|
||||
}
|
||||
|
||||
yield next.value;
|
||||
|
||||
Reference in New Issue
Block a user