-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
ArrayContainer.ts
240 lines (207 loc) · 7.15 KB
/
ArrayContainer.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//================================================================
/**
* @packageDocumentation
* @module std.internal
*/
//================================================================
import { ILinearContainerBase } from "./ILinearContainerBase";
import { Container } from "../../../base/container/Container";
import { IContainer } from "../../../base/container/IContainer";
import { IForwardIterator } from "../../../iterator/IForwardIterator";
import { ArrayIteratorBase } from "../../iterator/ArrayIteratorBase";
import { ArrayReverseIteratorBase } from "../../iterator/ArrayReverseIteratorBase";
import { ErrorGenerator } from "../../exception/ErrorGenerator";
import { RangeError } from "../../../exception/RangeError";
import { Repeater } from "../../iterator/disposable/Repeater";
/**
* Base array container.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export abstract class ArrayContainer<T extends ElemT,
SourceT extends IContainer<T, SourceT, IteratorT, ReverseT, ElemT>,
ArrayT extends ArrayContainer<T, SourceT, ArrayT, IteratorT, ReverseT, ElemT>,
IteratorT extends ArrayIteratorBase<T, SourceT, ArrayT, IteratorT, ReverseT, ElemT>,
ReverseT extends ArrayReverseIteratorBase<T, SourceT, ArrayT, IteratorT, ReverseT, ElemT>,
ElemT>
extends Container<T, SourceT, IteratorT, ReverseT, ElemT>
implements ILinearContainerBase<T, SourceT, IteratorT, ReverseT, ElemT>
{
/* ---------------------------------------------------------
CONSTRUCTORS
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public abstract resize(n: number): void;
protected abstract source(): SourceT;
/* =========================================================
ACCESSORS
- ITERATORS
- INDEXES
============================================================
ITERATORS
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public begin(): IteratorT
{
return this.nth(0);
}
/**
* @inheritDoc
*/
public end(): IteratorT
{
return this.nth(this.size());
}
public abstract nth(index: number): IteratorT;
/* ---------------------------------------------------------
INDEXES
--------------------------------------------------------- */
/**
* Get element at specific position.
*
* @param index Specific position.
* @return The element at the *index*.
*/
public at(index: number): T
{
return this._At(index);
}
protected abstract _At(index: number): T;
/**
* Change element at specific position.
*
* @param index Specific position.
* @param val The new value to change.
*/
public set(index: number, val: T): void
{
if (index < 0)
throw ErrorGenerator.negative_index(this.source(), "at", index);
else if (index >= this.size())
throw ErrorGenerator.excessive_index(this.source(), "at", index, this.size());
this._Set(index, val)
}
protected abstract _Set(index: number, val: T): void;
/**
* @inheritDoc
*/
public front(): T;
/**
* @inheritDoc
*/
public front(val: T): void;
public front(val?: T): T | void
{
if (arguments.length === 0)
return this.at(0);
else
this.set(0, val!);
}
/**
* @inheritDoc
*/
public back(): T;
/**
* @inheritDoc
*/
public back(val: T): void;
public back(val?: T): T | void
{
let index: number = this.size() - 1;
if (arguments.length === 0)
return this.at(index);
else
this.set(index, val!);
}
/* =========================================================
ELEMENTS I/O
- INSERT
- ERASE
- SWAP
============================================================
INSERT
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public abstract push_back(val: T): void;
/**
* @inheritDoc
*/
public insert(pos: IteratorT, val: T): IteratorT;
/**
* @inheritDoc
*/
public insert(pos: IteratorT, n: number, val: T): IteratorT;
/**
* @inheritDoc
*/
public insert<InputIterator extends Readonly<IForwardIterator<T, InputIterator>>>
(pos: IteratorT, first: InputIterator, last: InputIterator): IteratorT;
public insert(pos: IteratorT, ...args: any[]): IteratorT
{
// VALIDATION
if (pos._Get_array() !== <any>this)
throw ErrorGenerator.not_my_iterator(this.source(), "insert");
else if (pos.index() < 0)
throw ErrorGenerator.negative_iterator(this.source(), "insert", pos.index());
else if (pos.index() > this.size())
pos = this.end();
// BRANCHES
if (args.length === 1)
return this._Insert_by_repeating_val(pos, 1, args[0]);
else if (args.length === 2 && typeof args[0] === "number")
return this._Insert_by_repeating_val(pos, args[0], args[1]);
else
return this._Insert_by_range(pos, args[0], args[1]);
}
protected _Insert_by_repeating_val(position: IteratorT, n: number, val: T): IteratorT
{
let first: Repeater<T> = new Repeater(0, val);
let last: Repeater<T> = new Repeater(n);
return this._Insert_by_range(position, first, last);
}
protected abstract _Insert_by_range<InputIterator extends Readonly<IForwardIterator<T, InputIterator>>>
(pos: IteratorT, first: InputIterator, last: InputIterator): IteratorT;
/* ---------------------------------------------------------
ERASE
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public pop_back(): void
{
if (this.empty() === true)
throw ErrorGenerator.empty(this.source(), "pop_back");
this._Pop_back();
}
protected abstract _Pop_back(): void;
/**
* @inheritDoc
*/
public erase(it: IteratorT): IteratorT;
/**
* @inheritDoc
*/
public erase(first: IteratorT, last: IteratorT): IteratorT;
public erase(first: IteratorT, last: IteratorT = first.next()): IteratorT
{
// VALIDATION
if (first._Get_array() !== <any>this || last._Get_array() !== <any>this)
throw ErrorGenerator.not_my_iterator(this.source(), "erase");
else if (first.index() < 0)
throw ErrorGenerator.negative_iterator(this.source(), "erase", first.index());
else if (first.index() > last.index())
throw new RangeError(`Error on ${ErrorGenerator.get_class_name(this.source())}.erase(): first iterator has greater index than last -> (first = ${first.index()}, last = ${last.index()}).`);
// ADJUSTMENT
if (first.index() >= this.size())
return this.end();
// ERASE ELEMENTS
return this._Erase_by_range(first, last);
}
protected abstract _Erase_by_range(first: IteratorT, last: IteratorT): IteratorT;
}