/
HashMultiMap.ts
409 lines (362 loc) · 11.1 KB
/
HashMultiMap.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
//================================================================
/**
* @packageDocumentation
* @module std
*/
//================================================================
import { MultiMap } from "../base/container/MultiMap";
import { IHashMap } from "../base/container/IHashMap";
import { IHashContainer } from "../internal/container/associative/IHashContainer";
import { MapElementList } from "../internal/container/associative/MapElementList";
import { MapHashBuckets } from "../internal/hash/MapHashBuckets";
import { NativeArrayIterator } from "../internal/iterator/disposable/NativeArrayIterator";
import { IForwardIterator } from "../iterator/IForwardIterator";
import { IPair } from "../utility/IPair";
import { Entry } from "../utility/Entry";
import { BinaryPredicator } from "../internal/functional/BinaryPredicator";
import { Hasher } from "../internal/functional/Hasher";
import { Temporary } from "../internal/functional/Temporary";
/**
* Multiple-key Map based on Hash buckets.
*
* @author Jeongho Nam - https://github.com/samchon
*/
export class HashMultiMap<Key, T>
extends MultiMap<Key, T,
HashMultiMap<Key, T>,
HashMultiMap.Iterator<Key, T>,
HashMultiMap.ReverseIterator<Key, T>>
implements IHashMap<Key, T, false, HashMultiMap<Key, T>>
{
private buckets_!: MapHashBuckets<Key, T, false, HashMultiMap<Key, T>>;
/* =========================================================
CONSTRUCTORS & SEMI-CONSTRUCTORS
- CONSTRUCTORS
- ASSIGN & CLEAR
============================================================
CONSTURCTORS
--------------------------------------------------------- */
/**
* Default Constructor.
*
* @param hash An unary function returns hash code. Default is {hash}.
* @param equal A binary function predicates two arguments are equal. Default is {@link equal_to}.
*/
public constructor(hash?: Hasher<Key>, equal?: BinaryPredicator<Key>);
/**
* Initializer Constructor.
*
* @param items Items to assign.
* @param hash An unary function returns hash code. Default is {hash}.
* @param equal A binary function predicates two arguments are equal. Default is {@link equal_to}.
*/
public constructor(items: IPair<Key, T>[], hash?: Hasher<Key>, equal?: BinaryPredicator<Key>);
/**
* Copy Constructor.
*
* @param obj Object to copy.
*/
public constructor(obj: HashMultiMap<Key, T>);
/**
* Range Constructor.
*
* @param first Input iterator of the first position.
* @param last Input iterator of the last position.
* @param hash An unary function returns hash code. Default is {hash}.
* @param equal A binary function predicates two arguments are equal. Default is {@link equal_to}.
*/
public constructor
(
first: Readonly<IForwardIterator<IPair<Key, T>>>,
last: Readonly<IForwardIterator<IPair<Key, T>>>,
hash?: Hasher<Key>, equal?: BinaryPredicator<Key>
);
public constructor(...args: any[])
{
super(thisArg => new MapElementList(thisArg));
IHashContainer.construct<Key, Entry<Key, T>,
HashMultiMap<Key, T>,
HashMultiMap.Iterator<Key, T>,
HashMultiMap.ReverseIterator<Key, T>,
IPair<Key, T>>
(
this, HashMultiMap,
(hash, pred) =>
{
this.buckets_ = new MapHashBuckets(this, hash, pred);
},
...args
);
}
/* ---------------------------------------------------------
ASSIGN & CLEAR
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public clear(): void
{
this.buckets_.clear();
super.clear();
}
/**
* @inheritDoc
*/
public swap(obj: HashMultiMap<Key, T>): void
{
// SWAP CONTENTS
[this.data_, obj.data_] = [obj.data_, this.data_];
MapElementList._Swap_associative(this.data_ as Temporary, obj.data_ as Temporary);
// SWAP BUCKETS
MapHashBuckets._Swap_source(this.buckets_, obj.buckets_);
[this.buckets_, obj.buckets_] = [obj.buckets_, this.buckets_];
}
/* =========================================================
ACCESSORS
- MEMBER
- HASH
============================================================
MEMBER
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public find(key: Key): HashMultiMap.Iterator<Key, T>
{
return this.buckets_.find(key);
}
/**
* @inheritDoc
*/
public count(key: Key): number
{
// FIND MATCHED BUCKET
let index = this.bucket(key);
let bucket = this.buckets_.at(index);
// ITERATE THE BUCKET
let cnt: number = 0;
for (let it of bucket)
if (this.buckets_.key_eq()(it.first, key))
++cnt;
return cnt;
}
/**
* @inheritDoc
*/
public begin(): HashMultiMap.Iterator<Key, T>;
/**
* @inheritDoc
*/
public begin(index: number): HashMultiMap.Iterator<Key, T>;
public begin(index: number | null = null): HashMultiMap.Iterator<Key, T>
{
if (index === null)
return super.begin();
else
return this.buckets_.at(index)[0];
}
/**
* @inheritDoc
*/
public end(): HashMultiMap.Iterator<Key, T>;
/**
* @inheritDoc
*/
public end(index: number): HashMultiMap.Iterator<Key, T>
public end(index: number | null = null): HashMultiMap.Iterator<Key, T>
{
if (index === null)
return super.end();
else
{
let bucket = this.buckets_.at(index);
return bucket[bucket.length - 1].next();
}
}
/**
* @inheritDoc
*/
public rbegin(): HashMultiMap.ReverseIterator<Key, T>;
/**
* @inheritDoc
*/
public rbegin(index: number): HashMultiMap.ReverseIterator<Key, T>;
public rbegin(index: number | null = null): HashMultiMap.ReverseIterator<Key, T>
{
return this.end(index!).reverse();
}
/**
* @inheritDoc
*/
public rend(): HashMultiMap.ReverseIterator<Key, T>;
/**
* @inheritDoc
*/
public rend(index: number): HashMultiMap.ReverseIterator<Key, T>;
public rend(index: number | null = null): HashMultiMap.ReverseIterator<Key, T>
{
return this.begin(index!).reverse();
}
/* ---------------------------------------------------------
HASH
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public bucket_count(): number
{
return this.buckets_.size();
}
/**
* @inheritDoc
*/
public bucket_size(index: number): number
{
return this.buckets_.at(index).length;
}
/**
* @inheritDoc
*/
public load_factor(): number
{
return this.buckets_.load_factor();
}
/**
* @inheritDoc
*/
public hash_function(): Hasher<Key>
{
return this.buckets_.hash_function();
}
/**
* @inheritDoc
*/
public key_eq(): BinaryPredicator<Key>
{
return this.buckets_.key_eq();
}
/**
* @inheritDoc
*/
public bucket(key: Key): number
{
return this.hash_function()(key) % this.buckets_.size();
}
/**
* @inheritDoc
*/
public max_load_factor(): number;
/**
* @inheritDoc
*/
public max_load_factor(z: number): void;
public max_load_factor(z: number | null = null): any
{
return this.buckets_.max_load_factor(z!);
}
/**
* @inheritDoc
*/
public reserve(n: number): void
{
this.buckets_.reserve(n);
}
/**
* @inheritDoc
*/
public rehash(n: number): void
{
if (n <= this.bucket_count())
return;
this.buckets_.rehash(n);
}
protected _Key_eq(x: Key, y: Key): boolean
{
return this.key_eq()(x, y);
}
/* =========================================================
ELEMENTS I/O
- INSERT
- POST-PROCESS
============================================================
INSERT
--------------------------------------------------------- */
/**
* @inheritDoc
*/
public emplace(key: Key, val: T): HashMultiMap.Iterator<Key, T>
{
// INSERT
let it = this.data_.insert(this.data_.end(), new Entry(key, val));
this._Handle_insert(it, it.next()); // POST-PROCESS
return it;
}
/**
* @inheritDoc
*/
public emplace_hint(hint: HashMultiMap.Iterator<Key, T>, key: Key, val: T): HashMultiMap.Iterator<Key, T>
{
// INSERT
let it = this.data_.insert(hint, new Entry(key, val));
// POST-PROCESS
this._Handle_insert(it, it.next());
return it;
}
protected _Insert_by_range<InputIterator extends Readonly<IForwardIterator<IPair<Key, T>, InputIterator>>>
(first: InputIterator, last: InputIterator): void
{
//--------
// INSERTIONS
//--------
// PRELIMINARIES
let entries: Array<Entry<Key, T>> = [];
for (let it = first; !it.equals(last); it = it.next())
entries.push(new Entry(it.value.first, it.value.second));
// INSERT ELEMENTS
let my_first = this.data_.insert
(
this.data_.end(),
new NativeArrayIterator(entries, 0),
new NativeArrayIterator(entries, entries.length)
);
//--------
// HASHING INSERTED ITEMS
//--------
// IF NEEDED, HASH_BUCKET TO HAVE SUITABLE SIZE
if (this.size() > this.buckets_.capacity())
this.reserve(Math.max(this.size(), this.buckets_.capacity() * 2));
// POST-PROCESS
this._Handle_insert(my_first, this.end());
}
/* ---------------------------------------------------------
POST-PROCESS
--------------------------------------------------------- */
protected _Handle_insert(first: HashMultiMap.Iterator<Key, T>, last: HashMultiMap.Iterator<Key, T>): void
{
for (; !first.equals(last); first = first.next())
this.buckets_.insert(first);
}
protected _Handle_erase(first: HashMultiMap.Iterator<Key, T>, last: HashMultiMap.Iterator<Key, T>): void
{
for (; !first.equals(last); first = first.next())
this.buckets_.erase(first);
}
}
export namespace HashMultiMap
{
//----
// PASCAL NOTATION
//----
// HEAD
/**
* Iterator of {@link HashMultiMap}
*/
export type Iterator<Key, T> = MapElementList.Iterator<Key, T, false, HashMultiMap<Key, T>>;
/**
* Reverse iterator of {@link HashMultiMap}
*/
export type ReverseIterator<Key, T> = MapElementList.ReverseIterator<Key, T, false, HashMultiMap<Key, T>>;
// BODY
export const Iterator = MapElementList.Iterator;
export const ReverseIterator = MapElementList.ReverseIterator;
}