-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.ts
138 lines (134 loc) · 3.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
export const enum IteratorType {
NORMAL = 0,
REVERSE = 1
}
export abstract class ContainerIterator<T> {
/**
* @description Iterator's type.
*/
readonly iteratorType: IteratorType;
/**
* @internal
*/
protected _node: unknown;
protected constructor(iteratorType: IteratorType = IteratorType.NORMAL) {
this.iteratorType = iteratorType;
}
/**
* @description Pointers to element.
* @return The value of the pointer's element.
*/
abstract get pointer(): T;
/**
* @description Set pointer's value (some containers are unavailable).
* @param newValue The new value you want to set.
*/
abstract set pointer(newValue: T);
/**
* @description Move `this` iterator to pre.
*/
abstract pre(): this;
/**
* @description Move `this` iterator to next.
*/
abstract next(): this;
/**
* @param obj The other iterator you want to compare.
* @return Boolean about if this equals to obj.
* @example container.find(1).equals(container.end());
*/
abstract equals(obj: ContainerIterator<T>): boolean;
/**
* @description Get a copy of itself.<br/>
* We do not guarantee the safety of this function.<br/>
* Please ensure that the iterator will not fail.
* @return The copy of self.
*/
abstract copy(): ContainerIterator<T>;
}
export abstract class Base {
/**
* @description Container's size.
* @internal
*/
protected _length = 0;
/**
* @return The size of the container.
*/
size() {
return this._length;
}
/**
* @return Boolean about if the container is empty.
*/
empty() {
return this._length === 0;
}
/**
* @description Clear the container.
*/
abstract clear(): void;
}
export abstract class Container<T> extends Base {
/**
* @return Iterator pointing to the beginning element.
*/
abstract begin(): ContainerIterator<T>;
/**
* @return Iterator pointing to the super end like c++.
*/
abstract end(): ContainerIterator<T>;
/**
* @return Iterator pointing to the end element.
*/
abstract rBegin(): ContainerIterator<T>;
/**
* @return Iterator pointing to the super begin like c++.
*/
abstract rEnd(): ContainerIterator<T>;
/**
* @return The first element of the container.
*/
abstract front(): T | undefined;
/**
* @return The last element of the container.
*/
abstract back(): T | undefined;
/**
* @description Iterate over all elements in the container.
* @param callback Callback function like Array.forEach.
*/
abstract forEach(callback: (element: T, index: number) => void): void;
/**
* @param element The element you want to find.
* @return An iterator pointing to the element if found, or super end if not found.
*/
abstract find(element: T): ContainerIterator<T>;
/**
* @description Gets the value of the element at the specified position.
*/
abstract getElementByPos(pos: number): T;
/**
* @description Removes the element at the specified position.
* @param pos The element's position you want to remove.
*/
abstract eraseElementByPos(pos: number): void;
/**
* @description Removes element by iterator and move `iter` to next.
* @param iter The iterator you want to erase.
* @example container.eraseElementByIterator(container.begin());
*/
abstract eraseElementByIterator(
iter: ContainerIterator<T>
): ContainerIterator<T>;
/**
* @description Using for `for...of` syntax like Array.
*/
abstract [Symbol.iterator](): Generator<T, void, undefined>;
}
export type initContainer<T> = (
{ size: number } |
{ length: number } |
{ size(): number }
) &
{ forEach(callback: (element: T) => void): void; };