-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
VectorBoolean.ts
461 lines (397 loc) · 13 KB
/
VectorBoolean.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//================================================================
/**
* @packageDocumentation
* @module std
*/
//================================================================
import { IArrayContainer } from "../base/container/IArrayContainer";
import { ArrayContainer } from "../internal/container/linear/ArrayContainer";
import { ArrayIterator } from "../internal/iterator/ArrayIterator";
import { ArrayReverseIterator } from "../internal/iterator/ArrayReverseIterator";
import { TreeMap } from "./TreeMap";
import { IForwardIterator } from "../iterator/IForwardIterator";
import { NativeArrayIterator } from "../internal/iterator/disposable/NativeArrayIterator";
import { Pair } from "../utility/Pair";
import { not_equal_to } from "../functional/comparators";
/**
* Vector only for `boolean`.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export class VectorBoolean
extends ArrayContainer<boolean, VectorBoolean, VectorBoolean, VectorBoolean.Iterator, VectorBoolean.ReverseIterator, boolean>
implements IArrayContainer<boolean, VectorBoolean, VectorBoolean.Iterator, VectorBoolean.ReverseIterator>
{
/**
* Store not full elements, but their sequence.
*
* - first: index
* - second: value
*/
private data_!: TreeMap<number, boolean>;
/**
* Number of elements
*/
private size_!: number;
/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
- CONSTRUCTORS
- ASSIGN & CLEAR
============================================================
CONSTURCTORS
--------------------------------------------------------- */
/**
* Default Constructor.
*/
public constructor();
/**
* Initializer Constructor.
*
* @param items Items to assign.
*/
public constructor(array: boolean[]);
/**
* Copy Constructor
*
* @param obj Object to copy.
*/
public constructor(obj: VectorBoolean);
/**
* Fill Constructor.
*
* @param size Initial size.
* @param val Value to fill.
*/
public constructor(n: number, val: boolean);
/**
* Range Constructor.
*
* @param first Input iterator of the first position.
* @param last Input iteartor of the last position.
*/
public constructor(first: Readonly<IForwardIterator<boolean>>, last: Readonly<IForwardIterator<boolean>>);
public constructor(...args: any[])
{
super();
if (args.length === 1 && args[0] instanceof VectorBoolean)
{
// COPY CONSTRUCTOR
let obj: VectorBoolean = args[0];
this.data_ = new TreeMap(obj.data_.begin(), obj.data_.end());
this.size_ = obj.size_;
}
else if (args.length === 1 && args[0] instanceof Array)
{
// INITIALIZER
this.clear();
this.push(...args[0]);
}
else if (args.length === 2)
{
// ASSIGNER
this.assign(args[0], args[1]);
}
else // DEFAULT CONSTRUCTOR
this.clear();
}
/* ---------------------------------------------------------
ASSIGN & CLEAR
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public assign(n: number, val: boolean): void;
/**
* @inheritDoc
*/
public assign<InputIterator extends Readonly<IForwardIterator<boolean, InputIterator>>>
(first: InputIterator, last: InputIterator): void;
public assign(first: any, last: any): void
{
this.clear();
this.insert(this.end(), first, last);
}
/**
* @inheritDoc
*/
public clear(): void
{
this.data_ = new TreeMap();
this.size_ = 0;
}
/**
* @inheritDoc
*/
public resize(n: number): void
{
let expansion: number = n - this.size();
if (expansion > 0)
this.insert(this.end(), expansion, false);
else if (expansion < 0)
this.erase(this.end().advance(-expansion), this.end());
}
/**
* Flip all values.
*/
public flip(): void
{
for (let entry of this.data_)
entry.second = !entry.second;
}
/**
* @inheritDoc
*/
public swap(obj: VectorBoolean): void
{
[this.data_, obj.data_] = [obj.data_, this.data_];
[this.size_, obj.size_] = [obj.size_, this.size_];
}
/* =========================================================
ACCESSORS
========================================================= */
protected source(): VectorBoolean
{
return this;
}
/**
* @inheritDoc
*/
public size(): number
{
return this.size_;
}
protected _At(index: number): boolean
{
// FIND THE NEAREST NODE OF LEFT
let it = this._Find_node(index);
return it.second; // RETURNS
}
protected _Set(index: number, val: boolean): void
{
val = !!val; // SIFT
// FIND THE NEAREAST NODE OF LEFT
let it = this._Find_node(index);
if (it.second === val)
return; // NO NEED TO CHANGE
//----
// CHANGE VALUE
//----
if (it.first === index)
{
// CHANGE VALUE DIRECTLY
it.second = val;
}
else
{
// EMPLACE NEW NODE
it = this.data_.emplace(index, val).first;
}
//----
// POST-PROCESS
//----
// THE LAST ELEMENT, NO POST-PROCESS REQUIRED
if (index === this.size() - 1)
return;
// LIST UP NEIGHBORS
let prev = it.prev();
let next = it.next();
// ARRANGE LEFT SIDE
if (not_equal_to(prev, this.data_.end()) && prev.second === it.second)
this.data_.erase(it);
// ARRANGE RIGHT SIDE
if (next.equals(this.data_.end()) === true
|| (next.first !== index + 1 || next.second !== val))
{
// 1) IT'S THE LAST NODE
// 2) NEXT NODE DOES NOT POINT THE INDEX + 1 (NEAREST NEIGHBOR)
// 3) NEXT NODE'S VALUE IS DIFFERENT WITH THE CHANGED VALUE
//----
// EMPLACE NEW NODE WITH OLD
this.data_.emplace(index + 1, !val);
}
else
{
// NEXT NODE'S VALUE IS SAME WITH THE CHANGED VALUE
//----
// ERASE THE NEXT NODE
this.data_.erase(next);
}
}
/**
* @inheritDoc
*/
public nth(index: number): VectorBoolean.Iterator
{
return new VectorBoolean.Iterator(this as VectorBoolean, index);
}
private _Find_node(index: number): TreeMap.Iterator<number, boolean>
{
return this.data_.upper_bound(index).prev();
}
/* =========================================================
ELEMENTS I/O
- PUSH & POP
- INSERT
- ERASE
============================================================
PUSH & POP
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public push(...items: boolean[]): number
{
if (items.length === 0)
return this.size();
let first = new NativeArrayIterator(items, 0);
let last = new NativeArrayIterator(items, items.length);
this._Insert_by_range(this.end(), first, last);
return this.size();
}
/**
* @inheritDoc
*/
public push_back(val: boolean): void
{
let it = this.data_.rbegin();
let index: number = this.size_++;
val = !!val; // SIFT
// EMPLACE OR NOT
if (this.data_.empty() || it.second !== val)
this.data_.emplace(index, val);
}
protected _Pop_back(): void
{
let it: TreeMap.ReverseIterator<number, boolean> = this.data_.rbegin();
let index: number = --this.size_;
// ERASE OR NOT
if (it.first === index)
this.data_.erase(it.base());
}
/* ---------------------------------------------------------
INSERT
--------------------------------------------------------- */
protected _Insert_by_repeating_val(pos: VectorBoolean.Iterator, n: number, val: boolean): VectorBoolean.Iterator
{
// RESERVE ELEMENTS -> THE REPEATED COUNT AND VALUE
let elements: Pair<number, boolean>[] = [];
elements.push(new Pair(n, val));
// DO INSERT
if (pos.equals(this.end()) === true)
return this._Insert_to_end(elements);
else
return this._Insert_to_middle(pos, elements);
}
protected _Insert_by_range<InputIterator extends Readonly<IForwardIterator<boolean, InputIterator>>>
(pos: VectorBoolean.Iterator, first: InputIterator, last: InputIterator): VectorBoolean.Iterator
{
// RESERVE ELEMENTS -> REPEATED SIZE & VALUE
let elements: Pair<number, boolean>[] = [];
for (let it = first; !it.equals(last); it = it.next())
{
if (elements.length === 0 || elements[elements.length - 1].second !== it.value)
elements.push(new Pair(1, it.value));
else
++elements[elements.length - 1].first;
}
if (pos.equals(this.end()) === true)
return this._Insert_to_end(elements);
else
return this._Insert_to_middle(pos, elements);
}
private _Insert_to_middle(pos: VectorBoolean.Iterator, elements: Pair<number, boolean>[]): VectorBoolean.Iterator
{
let first = this._Find_node(pos.index());
for (let it = first; !it.equals(this.data_.end()); it = it.next())
{
// COMPUTE SIZE TO ENROLL
let next: TreeMap.Iterator<number, boolean> = it.next();
let sx: number = (it.first < pos.index())
? pos.index() // POSITION TO INSERT
: it.first; // CURRENT POINT
let sy: number = next.equals(this.data_.end())
? this.size() // IT'S THE LAST ELEMENT
: next.first; // TO NEXT ELEMENT
// DO ENROLL
let size: number = sy - sx;
let value: boolean = !!it.second;
elements.push(new Pair(size, value));
}
// ERASE BACK-SIDE ELEMENTS FOR THE POST-INSERTION
this.size_ = pos.index();
this.data_.erase
(
first.first === pos.index()
? first
: first.next(),
this.data_.end()
);
// DO POST-INSERTION
return this._Insert_to_end(elements);
}
private _Insert_to_end(elements: Pair<number, boolean>[]): VectorBoolean.Iterator
{
let old_size: number = this.size();
let last_value: boolean | null = this.data_.empty()
? null
: this.data_.rbegin().second;
for (let i: number = 0; i < elements.length; ++i)
{
let p: Pair<number, boolean> = elements[i];
// INDEXING
let index: number = this.size();
let value: boolean = !!p.second;
this.size_ += p.first;
// NEED NOT TO EMPLACE, JUST SKIP
if (i === 0 && value === last_value)
continue;
// DO EMPLACE
this.data_.emplace(index, value);
}
return this.begin().advance(old_size);
}
/* ---------------------------------------------------------
ERASE
--------------------------------------------------------- */
protected _Erase_by_range(first: VectorBoolean.Iterator, last: VectorBoolean.Iterator): VectorBoolean.Iterator
{
let elements: Pair<number, boolean>[] = [];
if (last.equals(this.end()) === false)
{
let last_index: number = Math.min(this.size(), last.index());
for (let it = this._Find_node(last_index); !it.equals(this.data_.end()); it = it.next())
{
let next: TreeMap.Iterator<number, boolean> = it.next();
let sx: number = Math.max(it.first, last_index);
let sy: number = next.equals(this.data_.end())
? this.size() // IT'S THE LAST ELEMENT
: next.first; // TO NEXT ELEMENT
let size: number = sy - sx;
let value: boolean = it.second;
elements.push(new Pair(size, value));
}
}
this.size_ = first.index();
this.data_.erase
(
this.data_.lower_bound(this.size_),
this.data_.end()
);
return this._Insert_to_end(elements);
}
}
export namespace VectorBoolean
{
// HEAD
/**
* Iterator of {@link VectorBoolean}
*/
export type Iterator = ArrayIterator<boolean, VectorBoolean>;
/**
* Reverse iterator of {@link VectorBoolean}
*/
export type ReverseIterator = ArrayReverseIterator<boolean, VectorBoolean>;
// BODY
export const Iterator = ArrayIterator;
export const ReverseIterator = ArrayReverseIterator;
}