chenyc
2025-12-09 65e034683b28d799e73c7d7e5e4769fab5b9bc9c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * @description The iterator type including `NORMAL` and `REVERSE`.
 */
export declare const enum IteratorType {
    NORMAL = 0,
    REVERSE = 1
}
export declare abstract class ContainerIterator<T> {
    /**
     * @description The container pointed to by the iterator.
     */
    abstract readonly container: Container<T>;
    /**
     * @description Iterator's type.
     * @example
     * console.log(container.end().iteratorType === IteratorType.NORMAL);  // true
     */
    readonly iteratorType: IteratorType;
    /**
     * @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>): boolean;
    /**
     * @description Pointers to element.
     * @returns The value of the pointer's element.
     * @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 declare abstract class Base {
    /**
     * @returns The size of the container.
     * @example
     * const container = new Vector([1, 2]);
     * console.log(container.length); // 2
     */
    get length(): number;
    /**
     * @returns The size of the container.
     * @example
     * const container = new Vector([1, 2]);
     * console.log(container.size()); // 2
     */
    size(): number;
    /**
     * @returns Whether the container is empty.
     * @example
     * container.clear();
     * console.log(container.empty());  // true
     */
    empty(): boolean;
    /**
     * @description Clear the container.
     * @example
     * container.clear();
     * console.log(container.empty());  // true
     */
    abstract clear(): void;
}
export declare abstract class Container<T> extends Base {
    /**
     * @returns Iterator pointing to the beginning element.
     * @example
     * const begin = container.begin();
     * const end = container.end();
     * for (const it = begin; !it.equals(end); it.next()) {
     *   doSomething(it.pointer);
     * }
     */
    abstract begin(): ContainerIterator<T>;
    /**
     * @returns Iterator pointing to the super end like c++.
     * @example
     * const begin = container.begin();
     * const end = container.end();
     * for (const it = begin; !it.equals(end); it.next()) {
     *   doSomething(it.pointer);
     * }
     */
    abstract end(): ContainerIterator<T>;
    /**
     * @returns Iterator pointing to the end element.
     * @example
     * const rBegin = container.rBegin();
     * const rEnd = container.rEnd();
     * for (const it = rBegin; !it.equals(rEnd); it.next()) {
     *   doSomething(it.pointer);
     * }
     */
    abstract rBegin(): ContainerIterator<T>;
    /**
     * @returns Iterator pointing to the super begin like c++.
     * @example
     * const rBegin = container.rBegin();
     * const rEnd = container.rEnd();
     * for (const it = rBegin; !it.equals(rEnd); it.next()) {
     *   doSomething(it.pointer);
     * }
     */
    abstract rEnd(): ContainerIterator<T>;
    /**
     * @returns The first element of the container.
     */
    abstract front(): T | undefined;
    /**
     * @returns The last element of the container.
     */
    abstract back(): T | undefined;
    /**
     * @param element - The element you want to find.
     * @returns An iterator pointing to the element if found, or super end if not found.
     * @example
     * container.find(1).equals(container.end());
     */
    abstract find(element: T): ContainerIterator<T>;
    /**
     * @description Iterate over all elements in the container.
     * @param callback - Callback function like Array.forEach.
     * @example
     * container.forEach((element, index) => console.log(element, index));
     */
    abstract forEach(callback: (element: T, index: number, container: Container<T>) => void): void;
    /**
     * @description Gets the value of the element at the specified position.
     * @example
     * const val = container.getElementByPos(-1); // throw a RangeError
     */
    abstract getElementByPos(pos: number): T;
    /**
     * @description Removes the element at the specified position.
     * @param pos - The element's position you want to remove.
     * @returns The container length after erasing.
     * @example
     * container.eraseElementByPos(-1); // throw a RangeError
     */
    abstract eraseElementByPos(pos: number): number;
    /**
     * @description Removes element by iterator and move `iter` to next.
     * @param iter - The iterator you want to erase.
     * @returns The next iterator.
     * @example
     * container.eraseElementByIterator(container.begin());
     * container.eraseElementByIterator(container.end()); // throw a RangeError
     */
    abstract eraseElementByIterator(iter: ContainerIterator<T>): ContainerIterator<T>;
    /**
     * @description Using for `for...of` syntax like Array.
     * @example
     * for (const element of container) {
     *   console.log(element);
     * }
     */
    abstract [Symbol.iterator](): Generator<T, void>;
}
/**
 * @description The initial data type passed in when initializing the container.
 */
export declare type initContainer<T> = {
    size?: number | (() => number);
    length?: number;
    forEach: (callback: (el: T) => void) => void;
};