Skip to content

Commit

Permalink
refactor: change constructor parmas to object
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLY201 committed Jun 9, 2023
1 parent c53cef9 commit 8bc48fe
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 186 deletions.
5 changes: 4 additions & 1 deletion perf/other-perf/priority-queue.perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ function testPriorityQueue(arr: number[], testNum: number) {
const reportList: testReportFormat = [];

startTime = Date.now();
const myPriority = new PriorityQueue(arr, (x: number, y: number) => y - x, false);
const myPriority = new PriorityQueue(arr, {
cmp: (x: number, y: number) => y - x,
copy: false
});
endTime = Date.now();
reportList.push({
testFunc: 'constructor',
Expand Down
11 changes: 7 additions & 4 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ export abstract class ContainerIterator<T> {
/**
* @description Iterator's type.
* @example
* console.log(container.end().iteratorType === IteratorType.NORMAL); // true
* console.log(container.end().type === IteratorType.NORMAL); // true
*/
readonly iteratorType: IteratorType;
readonly type: IteratorType;
/**
* @internal
*/
protected constructor(iteratorType = IteratorType.NORMAL) {
this.iteratorType = iteratorType;
protected constructor(props: {
type?: IteratorType
} = {}) {
const { type = IteratorType.NORMAL } = props;
this.type = type;
}
/**
* @param iter - The other iterator you want to compare.
Expand Down
15 changes: 8 additions & 7 deletions src/hash/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ export abstract class HashContainerIterator<K, V> extends ContainerIterator<K |
/**
* @internal
*/
protected constructor(
node: HashLinkNode<K, V>,
header: HashLinkNode<K, V>,
iteratorType?: IteratorType
) {
super(iteratorType);
protected constructor(props: {
node: HashLinkNode<K, V>,
header: HashLinkNode<K, V>,
type?: IteratorType
}) {
super(props);
const { node, header } = props;
this._node = node;
this._header = header;
if (this.iteratorType === IteratorType.NORMAL) {
if (this.type === IteratorType.NORMAL) {
this.pre = function () {
if (this._node._pre === this._header) {
throwIteratorAccessError();
Expand Down
52 changes: 41 additions & 11 deletions src/hash/hash-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { throwIteratorAccessError } from '@/utils/throwError';

class HashMapIterator<K, V> extends HashContainerIterator<K, V> {
readonly container: HashMap<K, V>;
constructor(
constructor(props: {
node: HashLinkNode<K, V>,
header: HashLinkNode<K, V>,
container: HashMap<K, V>,
iteratorType?: IteratorType
) {
super(node, header, iteratorType);
this.container = container;
type?: IteratorType
}) {
super(props);
this.container = props.container;
}
get pointer() {
if (this._node === this._header) {
Expand All @@ -34,7 +34,12 @@ class HashMapIterator<K, V> extends HashContainerIterator<K, V> {
});
}
copy() {
return new HashMapIterator<K, V>(this._node, this._header, this.container, this.iteratorType);
return new HashMapIterator<K, V>({
node: this._node,
header: this._header,
container: this.container,
type: this.type
});
}
// @ts-ignore
equals(iter: HashMapIterator<K, V>): boolean;
Expand All @@ -51,16 +56,37 @@ class HashMap<K, V> extends HashContainer<K, V> {
});
}
begin() {
return new HashMapIterator<K, V>(this._head, this._header, this);
/**
* this._head, this._header, this
*/
return new HashMapIterator<K, V>({
node: this._head,
header: this._header,
container: this
});
}
end() {
return new HashMapIterator<K, V>(this._header, this._header, this);
return new HashMapIterator<K, V>({
node: this._header,
header: this._header,
container: this
});
}
rBegin() {
return new HashMapIterator<K, V>(this._tail, this._header, this, IteratorType.REVERSE);
return new HashMapIterator<K, V>({
node: this._tail,
header: this._header,
container: this,
type: IteratorType.REVERSE
});
}
rEnd() {
return new HashMapIterator<K, V>(this._header, this._header, this, IteratorType.REVERSE);
return new HashMapIterator<K, V>({
node: this._header,
header: this._header,
container: this,
type: IteratorType.REVERSE
});
}
front() {
if (this._length === 0) return;
Expand Down Expand Up @@ -117,7 +143,11 @@ class HashMap<K, V> extends HashContainer<K, V> {
*/
find(key: K, isObject?: boolean) {
const node = this._findElementNode(key, isObject);
return new HashMapIterator<K, V>(node, this._header, this);
return new HashMapIterator<K, V>({
node,
header: this._header,
container: this
});
}
forEach(callback: CallbackFn<[K, V], this, void>) {
let index = 0;
Expand Down
55 changes: 41 additions & 14 deletions src/hash/hash-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { throwIteratorAccessError } from '@/utils/throwError';

class HashSetIterator<K> extends HashContainerIterator<K, undefined> {
readonly container: HashSet<K>;
constructor(
node: HashLinkNode<K, undefined>,
header: HashLinkNode<K, undefined>,
container: HashSet<K>,
iteratorType?: IteratorType
) {
super(node, header, iteratorType);
this.container = container;
constructor(props: {
node: HashLinkNode<K, undefined>,
header: HashLinkNode<K, undefined>,
container: HashSet<K>,
type?: IteratorType
}) {
super(props);
this.container = props.container;
}
get pointer() {
if (this._node === this._header) {
Expand All @@ -20,7 +20,12 @@ class HashSetIterator<K> extends HashContainerIterator<K, undefined> {
return this._node._key;
}
copy() {
return new HashSetIterator<K>(this._node, this._header, this.container, this.iteratorType);
return new HashSetIterator<K>({
node: this._node,
header: this._header,
container: this.container,
type: this.type
});
}
// @ts-ignore
equals(iter: HashSetIterator<K>): boolean;
Expand All @@ -37,16 +42,34 @@ class HashSet<K> extends HashContainer<K, undefined> {
});
}
begin() {
return new HashSetIterator<K>(this._head, this._header, this);
return new HashSetIterator<K>({
node: this._head,
header: this._header,
container: this
});
}
end() {
return new HashSetIterator<K>(this._header, this._header, this);
return new HashSetIterator<K>({
node: this._header,
header: this._header,
container: this
});
}
rBegin() {
return new HashSetIterator<K>(this._tail, this._header, this, IteratorType.REVERSE);
return new HashSetIterator<K>({
node: this._tail,
header: this._header,
container: this,
type: IteratorType.REVERSE
});
}
rEnd() {
return new HashSetIterator<K>(this._header, this._header, this, IteratorType.REVERSE);
return new HashSetIterator<K>({
node: this._header,
header: this._header,
container: this,
type: IteratorType.REVERSE
});
}
front(): K | undefined {
return this._head._key;
Expand Down Expand Up @@ -83,7 +106,11 @@ class HashSet<K> extends HashContainer<K, undefined> {
*/
find(key: K, isObject?: boolean) {
const node = this._findElementNode(key, isObject);
return new HashSetIterator<K>(node, this._header, this);
return new HashSetIterator<K>({
node,
header: this._header,
container: this
});
}
forEach(callback: CallbackFn<K, this, void>) {
let index = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/other/priority-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class PriorityQueue<T> extends Base {
* new PriorityQueue([1, 2, 3], (x, y) => x - y);
* new PriorityQueue([1, 2, 3], (x, y) => x - y, false);
*/
constructor(
entries: Entries<T> = [],
cmp: CompareFn<T> = compareFromL2S,
copy = true
) {
constructor(entries: Entries<T> = [], options: {
cmp?: CompareFn<T>
copy?: boolean
} = {}) {
super();
const { cmp = compareFromL2S, copy = true } = options;
this._cmp = cmp;
if (Array.isArray(entries)) {
this._priorityQueue = copy ? [...entries] : entries;
Expand Down
14 changes: 7 additions & 7 deletions src/sequential/base/random-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export abstract class RandomIterator<T> extends ContainerIterator<T> {
/**
* @internal
*/
protected constructor(
index: number,
iteratorType?: IteratorType
) {
super(iteratorType);
this._node = index;
if (this.iteratorType === IteratorType.NORMAL) {
protected constructor(props: {
node: number,
type?: IteratorType
}) {
super(props);
this._node = props.node;
if (this.type === IteratorType.NORMAL) {
this.pre = function () {
if (this._node === 0) {
throwIteratorAccessError();
Expand Down

0 comments on commit 8bc48fe

Please sign in to comment.