-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
VectorContainer.ts
210 lines (183 loc) · 5.26 KB
/
VectorContainer.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
//================================================================
/**
* @packageDocumentation
* @module std.internal
*/
//================================================================
import { ArrayContainer } from "./ArrayContainer";
import { IContainer } from "../../../base/container/IContainer";
import { IForwardIterator } from "../../../iterator/IForwardIterator";
import { ArrayIteratorBase } from "../../iterator/ArrayIteratorBase";
import { ArrayReverseIteratorBase } from "../../iterator/ArrayReverseIteratorBase";
export abstract class VectorContainer<T,
SourceT extends IContainer<T, SourceT, IteratorT, ReverseT, T>,
ArrayT extends VectorContainer<T, SourceT, ArrayT, IteratorT, ReverseT>,
IteratorT extends ArrayIteratorBase<T, SourceT, ArrayT, IteratorT, ReverseT, T>,
ReverseT extends ArrayReverseIteratorBase<T, SourceT, ArrayT, IteratorT, ReverseT, T>>
extends ArrayContainer<T, SourceT, ArrayT, IteratorT, ReverseT, T>
{
protected data_!: T[];
/* ---------------------------------------------------------
CONSTRUCTORS
--------------------------------------------------------- */
/**
* Default Constructor.
*/
protected constructor()
{
super();
}
/**
* @inheritDoc
*/
public assign(n: number, val: T): void;
/**
* @inheritDoc
*/
public assign<InputIterator extends Readonly<IForwardIterator<T, InputIterator>>>
(begin: InputIterator, end: InputIterator): void;
public assign(first: any, second: any): void
{
this.clear();
this.insert(this.end(), first, second);
}
/**
* @inheritDoc
*/
public clear(): void
{
this.data_.splice(0, this.data_.length);
}
/**
* @inheritDoc
*/
public resize(n: number)
{
this.data_.length = n;
}
/* =========================================================
ACCESSORS
========================================================= */
/**
* @inheritDoc
*/
public size(): number
{
return this.data_.length;
}
protected _At(index: number): T
{
return this.data_[index];
}
protected _Set(index: number, val: T): void
{
this.data_[index] = val;
}
/**
* Access data.
*
* @return An array capsuled by this {@link Vector}.
*/
public data(): Array<T>
{
return this.data_;
}
/**
* @inheritDoc
*/
public [Symbol.iterator](): IterableIterator<T>
{
return this.data_[Symbol.iterator]();
}
/* =========================================================
ELEMENTS I/O
- INSERT
- ERASE
============================================================
INSERT
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public push(...items: T[]): number
{
return this.data_.push(...items);
}
/**
* @inheritDoc
*/
public push_back(val: T): void
{
this.data_.push(val);
}
protected _Insert_by_range<InputIterator extends Readonly<IForwardIterator<T, InputIterator>>>
(position: IteratorT, first: InputIterator, last: InputIterator): IteratorT
{
if (position.index() >= this.size())
{
// WHEN INSERT TO THE LAST
let prev_size: number = this.size();
for (; !first.equals(last); first = first.next())
this.data_.push(first.value);
return this.nth(prev_size);
}
else
{
//----
// INSERT TO THE MIDDLE POSITION
//----
// CUT RIGHT SIDE
let spliced_array: T[] = this.data_.splice(position.index());
// INSERT ELEMENTS
for (; !first.equals(last); first = first.next())
this.data_.push(first.value);
this.data_.push(...spliced_array); // CONCAT THE SPLICEDS
return position;
}
}
/* ---------------------------------------------------------
ERASE
--------------------------------------------------------- */
protected _Pop_back(): void
{
this.data_.pop();
}
protected _Erase_by_range(first: IteratorT, last: IteratorT): IteratorT
{
if (first.index() >= this.size())
return first;
// ERASE ELEMENTS
if (last.index() >= this.size())
{
this.data_.splice(first.index());
return this.end();
}
else
this.data_.splice(first.index(), last.index() - first.index());
return first;
}
/* ---------------------------------------------------------------
UTILITIES
--------------------------------------------------------------- */
/**
* @inheritDoc
*/
public equals(obj: SourceT): boolean
{
return this.data_ === (<any>obj as this).data_;
}
/**
* @inheritDoc
*/
public swap(obj: SourceT): void
{
[this.data_, (<any>obj as this).data_] = [(<any>obj as this).data_, this.data_];
}
/**
* @inheritDoc
*/
public toJSON(): Array<T>
{
return this.data_;
}
}