Skip to content

Commit

Permalink
refactor: rename some function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLY201 committed Jun 12, 2023
1 parent c0e8534 commit f62ab82
Show file tree
Hide file tree
Showing 25 changed files with 418 additions and 390 deletions.
20 changes: 10 additions & 10 deletions conf/isolate.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
{
"name": "stack",
"version": "4.4.0",
"sourceRoot": "src/other/stack.ts"
"type": "other"
},
{
"name": "queue",
"version": "4.4.0",
"sourceRoot": "src/other/queue.ts"
"type": "other"
},
{
"name": "priority-queue",
"version": "4.4.0",
"sourceRoot": "src/other/priority-queue.ts"
"type": "other"
},
{
"name": "vector",
"version": "4.4.0",
"sourceRoot": "src/sequential/vector.ts"
"type": "sequential"
},
{
"name": "link-list",
"version": "4.4.0",
"sourceRoot": "src/sequential/link-list.ts"
"type": "sequential"
},
{
"name": "deque",
"version": "4.4.0",
"sourceRoot": "src/sequential/deque.ts"
"type": "sequential"
},
{
"name": "ordered-set",
"version": "4.4.1",
"sourceRoot": "src/tree/ordered-set.ts"
"type": "tree"
},
{
"name": "ordered-map",
"version": "4.4.1",
"sourceRoot": "src/tree/ordered-map.ts"
"type": "tree"
},
{
"name": "hash-set",
"version": "4.4.0",
"sourceRoot": "src/hash/hash-set.ts"
"type": "hash"
},
{
"name": "hash-map",
"version": "4.4.0",
"sourceRoot": "src/hash/hash-map.ts"
"type": "hash"
}
],
"sharedFiles": [
Expand Down
1 change: 0 additions & 1 deletion karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module.exports = function (config: Config) {
// @ts-ignore
karmaTypescriptConfig: {
compilerOptions: {
target: 'ES5',
module: 'commonjs'
},
tsconfig: 'tsconfig.json'
Expand Down
2 changes: 1 addition & 1 deletion perf/sequential-perf/coomon.perf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { testReportFormat } from '../index';
import SequentialContainer from '@/sequential/base/index';
import SequentialContainer from '@/sequential/base';

function testSequentialContainer(container: SequentialContainer<number>, testNum: number) {
let startTime, endTime;
Expand Down
112 changes: 11 additions & 101 deletions src/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,4 @@
/**
* @description The iterator type including `NORMAL` and `REVERSE`.
*/
export const enum IteratorType {
NORMAL = 0,
REVERSE = 1
}

export abstract class ContainerIterator<T> {
/**
* @description The container pointed to by the iterator.
*/
abstract readonly container: Container<T>;
/**
* @internal
*/
abstract _node: unknown;
/**
* @description Iterator's type.
* @example
* console.log(container.end().type === IteratorType.NORMAL); // true
*/
readonly type: IteratorType;
/**
* @internal
*/
protected constructor(props: {
type?: IteratorType
} = {}) {
const { type = IteratorType.NORMAL } = props;
this.type = type;
}
/**
* @param iter - The other iterator you want to compare.
* @returns Whether this equals to obj.
* @example
* container.find(1).equals(container.end());
*/
equals(iter: ContainerIterator<T>) {
return this._node === iter._node;
}
/**
* @description Pointers to item.
* @returns The value of the pointer's item.
* @example
* const val = container.begin().pointer;
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue - The new value you want to set.
* @example
* (<LinkList<number>>container).begin().pointer = 1;
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [0, 1]
* const pre = iter.pre();
* console.log(pre === iter); // true
* console.log(pre.equals(iter)); // true
* console.log(pre.pointer, iter.pointer); // 0, 0
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.next();
* console.log(next === iter); // true
* console.log(next.equals(iter)); // true
* console.log(next.pointer, iter.pointer); // 2, 2
*/
abstract next(): this;
/**
* @description Get a copy of itself.
* @returns The copy of self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.copy().next();
* console.log(next === iter); // false
* console.log(next.equals(iter)); // false
* console.log(next.pointer, iter.pointer); // 2, 1
*/
abstract copy(): ContainerIterator<T>;
}

export type CallbackFn<T, C, R> = (value: T, index: number, container: C) => R;
import { Iterator } from '@/base/iterator';

export abstract class Base {
/**
Expand Down Expand Up @@ -142,7 +52,7 @@ export abstract class Container<T> extends Base {
* doSomething(it.pointer);
* }
*/
abstract begin(): ContainerIterator<T>;
abstract begin(): Iterator<T>;
/**
* @returns Iterator pointing to the super end like c++.
* @example
Expand All @@ -152,7 +62,7 @@ export abstract class Container<T> extends Base {
* doSomething(it.pointer);
* }
*/
abstract end(): ContainerIterator<T>;
abstract end(): Iterator<T>;
/**
* @returns Iterator pointing to the end item.
* @example
Expand All @@ -162,7 +72,7 @@ export abstract class Container<T> extends Base {
* doSomething(it.pointer);
* }
*/
abstract rBegin(): ContainerIterator<T>;
abstract rBegin(): Iterator<T>;
/**
* @returns Iterator pointing to the super begin like c++.
* @example
Expand All @@ -172,7 +82,7 @@ export abstract class Container<T> extends Base {
* doSomething(it.pointer);
* }
*/
abstract rEnd(): ContainerIterator<T>;
abstract rEnd(): Iterator<T>;
/**
* @returns The first item of the container.
*/
Expand All @@ -187,14 +97,14 @@ export abstract class Container<T> extends Base {
* @example
* container.find(1).equals(container.end());
*/
abstract find(item: T): ContainerIterator<T>;
abstract find(item: T): Iterator<T>;
/**
* @description Iterate over all elements in the container.
* @param callback - Callback function like Array.forEach.
* @example
* container.forEach((item, index) => console.log(item, index));
*/
abstract forEach(callback: CallbackFn<T, this, void>): void;
abstract forEach(callback: (value: T, index: number, container: this) => void): void;
/**
* @internal
*/
Expand All @@ -220,11 +130,11 @@ export abstract class Container<T> extends Base {
* container.erase(container.begin());
* container.erase(container.end()); // throw a RangeError
*/
abstract erase(iter: ContainerIterator<T>): ContainerIterator<T>;
abstract erase(iter: Iterator<T>): Iterator<T>;
abstract entries(): IterableIterator<[unknown, unknown]>;
abstract every(callback: CallbackFn<T, this, unknown>): boolean;
abstract filter(callback: CallbackFn<T, this, unknown>): Container<T>;
abstract some(callback: CallbackFn<T, this, unknown>): boolean;
abstract every(callback: (value: T, index: number, container: this) => unknown): boolean;
abstract filter(callback: (value: T, index: number, container: this) => unknown): Container<T>;
abstract some(callback: (value: T, index: number, container: this) => unknown): boolean;
abstract values(): IterableIterator<unknown>;
/**
* @description Using for `for...of` syntax like Array.
Expand Down
91 changes: 91 additions & 0 deletions src/base/iterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Container } from '@/base/index';

/**
* @description The iterator type including `NORMAL` and `REVERSE`.
*/
export const enum ITERATOR_TYPE {
NORMAL = 0,
REVERSE = 1
}

export abstract class Iterator<T> {
/**
* @description The container pointed to by the iterator.
*/
abstract readonly container: Container<T>;
/**
* @internal
*/
abstract _node: unknown;
/**
* @description Iterator's type.
* @example
* console.log(container.end().type === ITERATOR_TYPE.NORMAL); // true
*/
readonly type: ITERATOR_TYPE;
/**
* @internal
*/
protected constructor(props: {
type?: ITERATOR_TYPE
} = {}) {
const { type = ITERATOR_TYPE.NORMAL } = props;
this.type = type;
}
/**
* @param iter - The other iterator you want to compare.
* @returns Whether this equals to obj.
* @example
* container.find(1).equals(container.end());
*/
equals(iter: Iterator<T>) {
return this._node === iter._node;
}
/**
* @description Pointers to item.
* @returns The value of the pointer's item.
* @example
* const val = container.begin().pointer;
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue - The new value you want to set.
* @example
* (<LinkList<number>>container).begin().pointer = 1;
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [0, 1]
* const pre = iter.pre();
* console.log(pre === iter); // true
* console.log(pre.equals(iter)); // true
* console.log(pre.pointer, iter.pointer); // 0, 0
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
* @returns The iterator's self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.next();
* console.log(next === iter); // true
* console.log(next.equals(iter)); // true
* console.log(next.pointer, iter.pointer); // 2, 2
*/
abstract next(): this;
/**
* @description Get a copy of itself.
* @returns The copy of self.
* @example
* const iter = container.find(1); // container = [1, 2]
* const next = iter.copy().next();
* console.log(next === iter); // false
* console.log(next.equals(iter)); // false
* console.log(next.pointer, iter.pointer); // 2, 1
*/
abstract copy(): Iterator<T>;
}

0 comments on commit f62ab82

Please sign in to comment.