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