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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
#ifndef CORE_LIB_DICTIONARY_H
#define CORE_LIB_DICTIONARY_H
#include "list.h"
#include "common.h"
#include "int-set.h"
#include "exception.h"
#include "slang-math.h"
#include "hash.h"
namespace Slang
{
template<typename TKey, typename TValue>
class KeyValuePair
{
public:
TKey Key;
TValue Value;
KeyValuePair()
{}
KeyValuePair(const TKey & key, const TValue & value)
{
Key = key;
Value = value;
}
KeyValuePair(TKey && key, TValue && value)
{
Key = _Move(key);
Value = _Move(value);
}
KeyValuePair(TKey && key, const TValue & value)
{
Key = _Move(key);
Value = value;
}
KeyValuePair(const KeyValuePair<TKey, TValue> & _that)
{
Key = _that.Key;
Value = _that.Value;
}
KeyValuePair(KeyValuePair<TKey, TValue> && _that)
{
operator=(_Move(_that));
}
KeyValuePair & operator=(KeyValuePair<TKey, TValue> && that)
{
Key = _Move(that.Key);
Value = _Move(that.Value);
return *this;
}
KeyValuePair & operator=(const KeyValuePair<TKey, TValue> & that)
{
Key = that.Key;
Value = that.Value;
return *this;
}
int GetHashCode()
{
return GetHashCode(Key);
}
};
template<typename TKey, typename TValue>
inline KeyValuePair<TKey, TValue> KVPair(const TKey & k, const TValue & v)
{
return KeyValuePair<TKey, TValue>(k, v);
}
const float MaxLoadFactor = 0.7f;
template<typename TKey, typename TValue>
class Dictionary
{
friend class Iterator;
friend class ItemProxy;
private:
inline int GetProbeOffset(int /*probeId*/) const
{
// quadratic probing
return 1;
}
private:
int bucketSizeMinusOne, shiftBits;
int _count;
IntSet marks;
KeyValuePair<TKey, TValue>* hashMap;
void Free()
{
if (hashMap)
delete[] hashMap;
hashMap = 0;
}
inline bool IsDeleted(int pos) const
{
return marks.Contains((pos << 1) + 1);
}
inline bool IsEmpty(int pos) const
{
return !marks.Contains((pos << 1));
}
inline void SetDeleted(int pos, bool val)
{
if (val)
marks.Add((pos << 1) + 1);
else
marks.Remove((pos << 1) + 1);
}
inline void SetEmpty(int pos, bool val)
{
if (val)
marks.Remove((pos << 1));
else
marks.Add((pos << 1));
}
struct FindPositionResult
{
int ObjectPosition;
int InsertionPosition;
FindPositionResult()
{
ObjectPosition = -1;
InsertionPosition = -1;
}
FindPositionResult(int objPos, int insertPos)
{
ObjectPosition = objPos;
InsertionPosition = insertPos;
}
};
template<typename T>
inline int GetHashPos(T & key) const
{
return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits;
}
template<typename T>
FindPositionResult FindPosition(const T & key) const
{
int hashPos = GetHashPos((T&)key);
int insertPos = -1;
int numProbes = 0;
while (numProbes <= bucketSizeMinusOne)
{
if (IsEmpty(hashPos))
{
if (insertPos == -1)
return FindPositionResult(-1, hashPos);
else
return FindPositionResult(-1, insertPos);
}
else if (IsDeleted(hashPos))
{
if (insertPos == -1)
insertPos = hashPos;
}
else if (hashMap[hashPos].Key == key)
{
return FindPositionResult(hashPos, -1);
}
numProbes++;
hashPos = (hashPos + GetProbeOffset(numProbes)) & bucketSizeMinusOne;
}
if (insertPos != -1)
return FindPositionResult(-1, insertPos);
throw InvalidOperationException("Hash map is full. This indicates an error in Key::Equal or Key::GetHashCode.");
}
TValue & _Insert(KeyValuePair<TKey, TValue> && kvPair, int pos)
{
hashMap[pos] = _Move(kvPair);
SetEmpty(pos, false);
SetDeleted(pos, false);
return hashMap[pos].Value;
}
void Rehash()
{
if (bucketSizeMinusOne == -1 || _count / (float)bucketSizeMinusOne >= MaxLoadFactor)
{
int newSize = (bucketSizeMinusOne + 1) * 2;
int newShiftBits = shiftBits - 1;
if (newSize == 0)
{
newSize = 16;
newShiftBits = 28;
}
Dictionary<TKey, TValue> newDict;
newDict.shiftBits = newShiftBits;
newDict.bucketSizeMinusOne = newSize - 1;
newDict.hashMap = new KeyValuePair<TKey, TValue>[newSize];
newDict.marks.SetMax(newSize * 2);
if (hashMap)
{
for (auto & kvPair : *this)
{
newDict.Add(_Move(kvPair));
}
}
*this = _Move(newDict);
}
}
bool AddIfNotExists(KeyValuePair<TKey, TValue> && kvPair)
{
Rehash();
auto pos = FindPosition(kvPair.Key);
if (pos.ObjectPosition != -1)
return false;
else if (pos.InsertionPosition != -1)
{
_count++;
_Insert(_Move(kvPair), pos.InsertionPosition);
return true;
}
else
throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
}
void Add(KeyValuePair<TKey, TValue> && kvPair)
{
if (!AddIfNotExists(_Move(kvPair)))
throw KeyExistsException("The key already exists in Dictionary.");
}
TValue & Set(KeyValuePair<TKey, TValue> && kvPair)
{
Rehash();
auto pos = FindPosition(kvPair.Key);
if (pos.ObjectPosition != -1)
return _Insert(_Move(kvPair), pos.ObjectPosition);
else if (pos.InsertionPosition != -1)
{
_count++;
return _Insert(_Move(kvPair), pos.InsertionPosition);
}
else
throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
}
public:
class Iterator
{
private:
const Dictionary<TKey, TValue> * dict;
int pos;
public:
KeyValuePair<TKey, TValue> & operator *() const
{
return dict->hashMap[pos];
}
KeyValuePair<TKey, TValue> * operator ->() const
{
return dict->hashMap + pos;
}
Iterator & operator ++()
{
if (pos > dict->bucketSizeMinusOne)
return *this;
pos++;
while (pos <= dict->bucketSizeMinusOne && (dict->IsDeleted(pos) || dict->IsEmpty(pos)))
{
pos++;
}
return *this;
}
Iterator operator ++(int)
{
Iterator rs = *this;
operator++();
return rs;
}
bool operator != (const Iterator & _that) const
{
return pos != _that.pos || dict != _that.dict;
}
bool operator == (const Iterator & _that) const
{
return pos == _that.pos && dict == _that.dict;
}
Iterator(const Dictionary<TKey, TValue> * _dict, int _pos)
{
this->dict = _dict;
this->pos = _pos;
}
Iterator()
{
this->dict = 0;
this->pos = 0;
}
};
Iterator begin() const
{
int pos = 0;
while (pos < bucketSizeMinusOne + 1)
{
if (IsEmpty(pos) || IsDeleted(pos))
pos++;
else
break;
}
return Iterator(this, pos);
}
Iterator end() const
{
return Iterator(this, bucketSizeMinusOne + 1);
}
public:
void Add(const TKey & key, const TValue & value)
{
Add(KeyValuePair<TKey, TValue>(key, value));
}
void Add(TKey && key, TValue && value)
{
Add(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
}
bool AddIfNotExists(const TKey & key, const TValue & value)
{
return AddIfNotExists(KeyValuePair<TKey, TValue>(key, value));
}
bool AddIfNotExists(TKey && key, TValue && value)
{
return AddIfNotExists(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
}
void Remove(const TKey & key)
{
if (_count == 0)
return;
auto pos = FindPosition(key);
if (pos.ObjectPosition != -1)
{
SetDeleted(pos.ObjectPosition, true);
_count--;
}
}
void Clear()
{
_count = 0;
marks.Clear();
}
template<typename T>
bool ContainsKey(const T & key) const
{
if (bucketSizeMinusOne == -1)
return false;
auto pos = FindPosition(key);
return pos.ObjectPosition != -1;
}
template<typename T>
bool TryGetValue(const T & key, TValue & value) const
{
if (bucketSizeMinusOne == -1)
return false;
auto pos = FindPosition(key);
if (pos.ObjectPosition != -1)
{
value = hashMap[pos.ObjectPosition].Value;
return true;
}
return false;
}
template<typename T>
TValue * TryGetValue(const T & key) const
{
if (bucketSizeMinusOne == -1)
return nullptr;
auto pos = FindPosition(key);
if (pos.ObjectPosition != -1)
{
return &hashMap[pos.ObjectPosition].Value;
}
return nullptr;
}
class ItemProxy
{
private:
const Dictionary<TKey, TValue> * dict;
TKey key;
public:
ItemProxy(const TKey & _key, const Dictionary<TKey, TValue> * _dict)
{
this->dict = _dict;
this->key = _key;
}
ItemProxy(TKey && _key, const Dictionary<TKey, TValue> * _dict)
{
this->dict = _dict;
this->key = _Move(_key);
}
TValue & GetValue() const
{
auto pos = dict->FindPosition(key);
if (pos.ObjectPosition != -1)
{
return dict->hashMap[pos.ObjectPosition].Value;
}
else
throw KeyNotFoundException("The key does not exists in dictionary.");
}
inline TValue & operator()() const
{
return GetValue();
}
operator TValue&() const
{
return GetValue();
}
TValue & operator = (const TValue & val) const
{
return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), val));
}
TValue & operator = (TValue && val) const
{
return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), _Move(val)));
}
};
ItemProxy operator [](const TKey & key) const
{
return ItemProxy(key, this);
}
ItemProxy operator [](TKey && key) const
{
return ItemProxy(_Move(key), this);
}
int Count() const
{
return _count;
}
private:
template<typename... Args>
void Init(const KeyValuePair<TKey, TValue> & kvPair, Args... args)
{
Add(kvPair);
Init(args...);
}
public:
Dictionary()
{
bucketSizeMinusOne = -1;
shiftBits = 32;
_count = 0;
hashMap = 0;
}
template<typename Arg, typename... Args>
Dictionary(Arg arg, Args... args)
{
Init(arg, args...);
}
Dictionary(const Dictionary<TKey, TValue> & other)
: bucketSizeMinusOne(-1), _count(0), hashMap(0)
{
*this = other;
}
Dictionary(Dictionary<TKey, TValue> && other)
: bucketSizeMinusOne(-1), _count(0), hashMap(0)
{
*this = (_Move(other));
}
Dictionary<TKey, TValue> & operator = (const Dictionary<TKey, TValue> & other)
{
if (this == &other)
return *this;
Free();
bucketSizeMinusOne = other.bucketSizeMinusOne;
_count = other._count;
shiftBits = other.shiftBits;
hashMap = new KeyValuePair<TKey, TValue>[other.bucketSizeMinusOne + 1];
marks = other.marks;
for (int i = 0; i <= bucketSizeMinusOne; i++)
hashMap[i] = other.hashMap[i];
return *this;
}
Dictionary<TKey, TValue> & operator = (Dictionary<TKey, TValue> && other)
{
if (this == &other)
return *this;
Free();
bucketSizeMinusOne = other.bucketSizeMinusOne;
_count = other._count;
hashMap = other.hashMap;
shiftBits = other.shiftBits;
marks = _Move(other.marks);
other.hashMap = 0;
other._count = 0;
other.bucketSizeMinusOne = -1;
return *this;
}
~Dictionary()
{
Free();
}
};
class _DummyClass
{};
template<typename T, typename DictionaryType>
class HashSetBase
{
protected:
DictionaryType dict;
private:
template<typename... Args>
void Init(const T & v, Args... args)
{
Add(v);
Init(args...);
}
public:
HashSetBase()
{}
template<typename Arg, typename... Args>
HashSetBase(Arg arg, Args... args)
{
Init(arg, args...);
}
HashSetBase(const HashSetBase & set)
{
operator=(set);
}
HashSetBase(HashSetBase && set)
{
operator=(_Move(set));
}
HashSetBase & operator = (const HashSetBase & set)
{
dict = set.dict;
return *this;
}
HashSetBase & operator = (HashSetBase && set)
{
dict = _Move(set.dict);
return *this;
}
public:
class Iterator
{
private:
typename DictionaryType::Iterator iter;
public:
Iterator() = default;
T & operator *() const
{
return (*iter).Key;
}
T * operator ->() const
{
return &(*iter).Key;
}
Iterator & operator ++()
{
++iter;
return *this;
}
Iterator operator ++(int)
{
Iterator rs = *this;
operator++();
return rs;
}
bool operator != (const Iterator & _that) const
{
return iter != _that.iter;
}
bool operator == (const Iterator & _that) const
{
return iter == _that.iter;
}
Iterator(const typename DictionaryType::Iterator & _iter)
{
this->iter = _iter;
}
};
Iterator begin() const
{
return Iterator(dict.begin());
}
Iterator end() const
{
return Iterator(dict.end());
}
public:
int Count() const
{
return dict.Count();
}
void Clear()
{
dict.Clear();
}
bool Add(const T& obj)
{
return dict.AddIfNotExists(obj, _DummyClass());
}
bool Add(T && obj)
{
return dict.AddIfNotExists(_Move(obj), _DummyClass());
}
void Remove(const T & obj)
{
dict.Remove(obj);
}
bool Contains(const T & obj) const
{
return dict.ContainsKey(obj);
}
};
template <typename T>
class HashSet : public HashSetBase<T, Dictionary<T, _DummyClass>>
{};
}
#endif
|