/
SetContainer.ts
269 lines (235 loc) · 8.35 KB
/
SetContainer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//================================================================
/**
* @packageDocumentation
* @module std.base
*/
//================================================================
import { IAssociativeContainer } from "../../internal/container/associative/IAssociativeContainer";
import { IContainer } from "./IContainer";
import { Container } from "./Container";
import { IForwardIterator } from "../../iterator/IForwardIterator";
import { ILinearContainerBase } from "../../internal/container/linear/ILinearContainerBase";
import { NativeArrayIterator } from "../../internal/iterator/disposable/NativeArrayIterator";
import { Pair } from "../../utility/Pair";
import { Temporary } from "../../internal/functional/Temporary";
/**
* Basic set container.
*
* @type Key Key type
* @type Unique Whether duplicated key is blocked or not
* @type Source Derived type extending this {@link SetContainer}
* @type IteratorT Iterator type
* @type ReverseT Reverse iterator type
*
* @author Jeongho Nam - https://github.com/samchon
*/
export abstract class SetContainer<Key,
Unique extends boolean,
SourceT extends SetContainer<Key, Unique, SourceT, IteratorT, ReverseT>,
IteratorT extends SetContainer.Iterator<Key, Unique, SourceT, IteratorT, ReverseT>,
ReverseT extends SetContainer.ReverseIterator<Key, Unique, SourceT, IteratorT, ReverseT>>
extends Container<Key, SourceT, IteratorT, ReverseT, Key>
implements IAssociativeContainer<Key, Key, SourceT, IteratorT, ReverseT, Key>
{
protected data_: ILinearContainerBase<Key, SourceT, IteratorT, ReverseT>;
/* ---------------------------------------------------------
CONSTURCTORS
--------------------------------------------------------- */
/**
* Default Constructor.
*/
protected constructor(factory: (thisArg: SourceT) => ILinearContainerBase<Key, SourceT, IteratorT, ReverseT>)
{
super();
this.data_ = factory(<Temporary>this);
}
/**
* @inheritDoc
*/
public assign<InputIterator extends Readonly<IForwardIterator<Key, InputIterator>>>
(first: InputIterator, last: InputIterator): void
{
// INSERT
this.clear();
this.insert(first, last);
}
/**
* @inheritDoc
*/
public clear(): void
{
// TO BE ABSTRACT
this.data_.clear();
}
/* =========================================================
ACCESSORS
- ITERATORS
- ELEMENTS
============================================================
ITERATOR
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public abstract find(key: Key): IteratorT;
/**
* @inheritDoc
*/
public begin(): IteratorT
{
return this.data_.begin();
}
/**
* @inheritDoc
*/
public end(): IteratorT
{
return this.data_.end();
}
/* ---------------------------------------------------------
ELEMENTS
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public has(key: Key): boolean
{
return !this.find(key).equals(this.end());
}
/**
* @inheritDoc
*/
public abstract count(key: Key): number;
/**
* @inheritDoc
*/
public size(): number
{
return this.data_.size();
}
/* =========================================================
ELEMENTS I/O
- INSERT
- ERASE
- UTILITY
- POST-PROCESS
============================================================
INSERT
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public push(...items: Key[]): number
{
if (items.length === 0)
return this.size();
// INSERT BY RANGE
let first: NativeArrayIterator<Key> = new NativeArrayIterator(items, 0);
let last: NativeArrayIterator<Key> = new NativeArrayIterator(items, items.length);
this._Insert_by_range(first, last);
// RETURN SIZE
return this.size();
}
public insert(key: Key): SetContainer.InsertRet<Key, Unique, SourceT, IteratorT, ReverseT>;
public insert(hint: IteratorT, key: Key): IteratorT;
public insert<InputIterator extends Readonly<IForwardIterator<Key, InputIterator>>>
(first: InputIterator, last: InputIterator): void;
public insert(...args: any[]): any
{
if (args.length === 1)
return this._Insert_by_key(args[0]);
else if (args[0].next instanceof Function && args[1].next instanceof Function)
return this._Insert_by_range(args[0], args[1]);
else
return this._Insert_by_hint(args[0], args[1]);
}
protected abstract _Insert_by_key(key: Key): SetContainer.InsertRet<Key, Unique, SourceT, IteratorT, ReverseT>;
protected abstract _Insert_by_hint(hint: IteratorT, key: Key): IteratorT;
protected abstract _Insert_by_range<InputIterator extends Readonly<IForwardIterator<Key, InputIterator>>>
(begin: InputIterator, end: InputIterator): void;
/* ---------------------------------------------------------
ERASE
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public erase(key: Key): number;
/**
* @inheritDoc
*/
public erase(pos: IteratorT): IteratorT;
/**
* @inheritDoc
*/
public erase(first: IteratorT, last: IteratorT): IteratorT;
public erase(...args: any[]): any
{
if (args.length === 1 && !(args[0] instanceof this.end().constructor && (args[0] as IteratorT).source() === <any>this))
return this._Erase_by_val(args[0]);
else if (args.length === 1)
return this._Erase_by_range(args[0]);
else
return this._Erase_by_range(args[0], args[1]);
}
protected abstract _Erase_by_val(key: Key): number;
protected _Erase_by_range(first: IteratorT, last: IteratorT = first.next()): IteratorT
{
// ERASE
let it = this.data_.erase(first, last);
// POST-PROCESS
this._Handle_erase(first, last);
return it;
}
/* ---------------------------------------------------------
UTILITY
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public abstract swap(obj: SourceT): void;
/**
* @inheritDoc
*/
public abstract merge(source: SourceT): void;
/* ---------------------------------------------------------
POST-PROCESS
--------------------------------------------------------- */
protected abstract _Handle_insert(first: IteratorT, last: IteratorT): void;
protected abstract _Handle_erase(first: IteratorT, last: IteratorT): void;
}
export namespace SetContainer
{
/**
* Return type of {@link SetContainer.insert}
*/
export type InsertRet<Key,
Unique extends boolean,
Source extends SetContainer<Key, Unique, Source, IteratorT, ReverseT>,
IteratorT extends Iterator<Key, Unique, Source, IteratorT, ReverseT>,
ReverseT extends ReverseIterator<Key, Unique, Source, IteratorT, ReverseT>>
= Unique extends true
? Pair<IteratorT, boolean>
: IteratorT;
/**
* Iterator of {@link SetContainer}
*
* @author Jenogho Nam <http://samchon.org>
*/
export type Iterator<Key,
Unique extends boolean,
SourceT extends SetContainer<Key, Unique, SourceT, IteratorT, ReverseT>,
IteratorT extends Iterator<Key, Unique, SourceT, IteratorT, ReverseT>,
ReverseT extends ReverseIterator<Key, Unique, SourceT, IteratorT, ReverseT>>
= Readonly<IContainer.Iterator<Key, SourceT, IteratorT, ReverseT, Key>>;
/**
* Reverse iterator of {@link SetContainer}
*
* @author Jenogho Nam <http://samchon.org>
*/
export type ReverseIterator<Key,
Unique extends boolean,
SourceT extends SetContainer<Key, Unique, SourceT, IteratorT, ReverseT>,
IteratorT extends Iterator<Key, Unique, SourceT, IteratorT, ReverseT>,
ReverseT extends ReverseIterator<Key, Unique, SourceT, IteratorT, ReverseT>>
= Readonly<IContainer.ReverseIterator<Key, SourceT, IteratorT, ReverseT, Key>>;
}