-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dictionary.h
887 lines (742 loc) · 20.5 KB
/
Dictionary.h
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#ifndef PFZ_COLLECTIONS_DICTIONARY_H
#define PFZ_COLLECTIONS_DICTIONARY_H
#include "ObjectPool.h"
#include <assert.h>
template<typename T>
class DefaultEqualityComparer
{
};
#define IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(T) \
template<> \
class DefaultEqualityComparer<T> \
{ \
public: \
static size_t GetHashCode(T value) \
{ \
return (size_t)value; \
} \
static bool Equals(T value1, T value2) \
{ \
return value1 == value2; \
} \
}
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(signed char);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(unsigned char);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(signed short);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(unsigned short);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(signed int);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(unsigned int);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(signed long int);
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(unsigned long int);
// I am really considering that if untyped pointers are used as a key, then there's
// nothing inside the "objects" that will generate the hash. It is the pointer
// itself that should be considered as the hash.
IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(void *);
// The long long int aren't implemented by default as this can cause bad hashing in 32-bit
// computers. Instead of trying to give defaults I prefer that users implement it
// the way they choose.
// IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(signed long long int);
// IMPLEMENT_DEFAULT_EQUALITY_COMPARER_AS_DIRECT_COMPARISON_AND_CAST(unsigned long long int);
template<typename T>
class IEnumerator
{
public:
virtual ~IEnumerator(){}
virtual const T *GetNext() = 0;
};
template<typename T>
class ICountAwareEnumerator:
public IEnumerator<T>
{
public:
virtual size_t GetCount() const = 0;
};
template<typename TKey, typename TValue>
class Pair
{
private:
TKey _key;
TValue _value;
public:
Pair(const TKey &key, const TValue &value):
_key(key),
_value(value)
{
}
const TKey &GetKey() const
{
return _key;
}
const TValue &GetValue() const
{
return _value;
}
};
template<typename TKey, typename TValue, class TEqualityComparer=DefaultEqualityComparer<TKey>, class TMemoryAllocator=DefaultMemoryAllocator>
class Dictionary
{
private:
struct _Node
{
_Node *_nextNode;
size_t _hashCode;
Pair<TKey, TValue> _pair;
_Node(_Node *nextNode, size_t hashCode, const TKey &key, const TValue &value):
_nextNode(nextNode),
_hashCode(hashCode),
_pair(key, value)
{
}
};
static inline bool _IsEmpty(_Node *node)
{
return node->_nextNode == _GetEmptyPointer();
}
static inline _Node *_GetEmptyPointer()
{
return (_Node *)(((char *)0)-1);
}
// We try to use only prime numbers as the capacities (number of buckets).
// Yet, for performance reasons, we don't really look for a real prime, only
// a number that's not divisible from the primes up to 31.
static size_t _AdaptSize(size_t size)
{
if (size <= 31)
return 31;
if (size % 2 == 0)
size --;
else
size -= 2;
while(true)
{
size += 2;
if (size % 3 == 0) continue;
if (size % 5 == 0) continue;
if (size % 7 == 0) continue;
if (size % 11 == 0) continue;
if (size % 13 == 0) continue;
if (size % 17 == 0) continue;
if (size % 19 == 0) continue;
if (size % 23 == 0) continue;
if (size % 29 == 0) continue;
if (size % 31 == 0) continue;
return size;
}
}
_Node *_buckets;
size_t _count;
size_t _capacity;
ObjectPool<_Node, TMemoryAllocator> *_pool;
ObjectPool<_Node, TMemoryAllocator> *_GetPool()
{
if (_pool == NULL)
_pool = new ObjectPool<_Node, TMemoryAllocator>();
return _pool;
}
void _Resize()
{
size_t newCapacity = _AdaptSize(_capacity * 2);
if (newCapacity < _capacity)
throw std::out_of_range("The new required capacity is not supported in this environment.");
_ResizeAlreadyAdaptedSize(newCapacity);
}
void _ResizeAlreadyAdaptedSize(size_t newCapacity)
{
ObjectPool<_Node, TMemoryAllocator> *newPool = NULL;
_Node *newBuckets = (_Node *)TMemoryAllocator::Allocate(newCapacity * sizeof(_Node));
if (newBuckets == NULL)
throw std::bad_alloc();
for(size_t i=0; i<newCapacity; i++)
newBuckets[i]._nextNode = _GetEmptyPointer();
if (_count > 0)
{
try
{
size_t newCount = 0;
for(size_t i=0; i<_capacity; i++)
{
_Node *oldNode = &_buckets[i];
if (_IsEmpty(oldNode))
continue;
do
{
size_t hashCode = oldNode->_hashCode;
size_t newBucketIndex = hashCode % newCapacity;
_Node *newFirstNode = &newBuckets[newBucketIndex];
if (_IsEmpty(newFirstNode))
{
new (newFirstNode) _Node(NULL, hashCode, oldNode->_pair.GetKey(), oldNode->_pair.GetValue());
}
else
{
if (newPool == NULL)
newPool = new ObjectPool<_Node, TMemoryAllocator>();
_Node *newNode = newPool->GetNextWithoutInitializing();
new (newNode) _Node(newFirstNode->_nextNode, hashCode, oldNode->_pair.GetKey(), oldNode->_pair.GetValue());
newFirstNode->_nextNode = newNode;
}
oldNode = oldNode->_nextNode;
newCount++;
#ifndef _DEBUG
if (newCount == _count)
goto exitWhileAndFor;
#endif
} while (oldNode);
}
assert(newCount == _count);
#ifndef _DEBUG
exitWhileAndFor:
{
// block needed to avoid compilation errors.
}
#endif
}
catch(...)
{
// if there's an exception we clean up
// our new allocated objects, without touching
// the previous ones. As long as the possible
// assignment operators don't touch the
// old values, the dictionary should not get
// corrupted and no leaks should happen.
// If the copy constructors did move content from one
// place to another, then the dictionary will be corrupted,
// without memory leaks. So, the caller will be responsible
// for dealing with the corrupted data or killing the dictionary.
for(size_t i=0; i<newCapacity; i++)
{
_Node *node = &newBuckets[i];
if (!_IsEmpty(node))
{
do
{
node->~_Node();
node = node->_nextNode;
} while(node);
}
}
delete newPool;
throw;
}
}
_Node *oldBuckets = _buckets;
ObjectPool<_Node, TMemoryAllocator> *oldPool = _pool;
size_t oldCapacity = _capacity;
_capacity = newCapacity;
_buckets = newBuckets;
_pool = newPool;
// destroy all inner nodes and then the oldbuckets and pool.
for(size_t i=0; i<oldCapacity; i++)
{
_Node *node = &oldBuckets[i];
if (!_IsEmpty(node))
{
do
{
node->~_Node();
node = node->_nextNode;
} while(node);
}
}
TMemoryAllocator::Deallocate(oldBuckets, oldCapacity * sizeof(_Node));
delete oldPool;
}
Dictionary(const Dictionary<TKey, TValue, TMemoryAllocator> &source);
void operator = (const Dictionary<TKey, TValue, TMemoryAllocator> &source);
public:
explicit Dictionary(size_t capacity=31):
_buckets(NULL),
_count(0),
_capacity(_AdaptSize(capacity)),
_pool(NULL)
{
_buckets = (_Node *)TMemoryAllocator::Allocate(_capacity * sizeof(_Node));
if (_buckets == NULL)
throw std::bad_alloc();
for(size_t i=0; i<_capacity; i++)
_buckets[i]._nextNode = _GetEmptyPointer();
}
~Dictionary()
{
if (_count == 0)
goto deallocate;
int clearCount = 0;
for(size_t i=0; i<_capacity; i++)
{
_Node *node = &_buckets[i];
if (!_IsEmpty(node))
{
do
{
node->~_Node();
node = node->_nextNode;
clearCount ++;
if (clearCount == _count)
{
assert(node == NULL);
goto deallocate;
}
} while(node);
}
}
assert(clearCount == _count);
deallocate:
TMemoryAllocator::Deallocate(_buckets, _capacity * sizeof(_Node));
delete _pool;
}
// Gets the number of associations done by this dictionary.
size_t GetCount() const
{
return _count;
}
// Gets the number of association slots already allocated, independently
// on how many associations are currently in use.
size_t GetCapacity() const
{
return _capacity;
}
// Tries to set the capacity (number of "association slots") allocated to the given
// value. If such value is less than Count an exception is thrown. This function returns
// false if the capacity is the same as the actual one (or if it becomes the same by the
// prime search logic). It returns true if the capacity was correctly changed. Note
// that the final capacity may be bigger than the one you asked for.
bool SetCapacity(size_t newCapacity)
{
if (newCapacity < _count)
throw std::out_of_range("newCapacity must at least equals count.");
size_t originalParameter = newCapacity;
newCapacity = _AdaptSize(newCapacity);
if (newCapacity < originalParameter)
throw std::out_of_range("The new capacity is not supported in this environment.");
if (newCapacity == _capacity)
return false;
_ResizeAlreadyAdaptedSize(newCapacity);
return true;
}
// Tries to reduce the capacity to the same size as the number of items in
// this dictionary.
bool TrimExcess()
{
return SetCapacity(_count);
}
// Returns a value indicating if the given key exists in this dictionary.
bool ContainsKey(const TKey &key) const
{
return TryGetValue(key) != NULL;
}
// Tries to get the value for a given key.
// Teh return is either the address of the Value or NULL.
TValue *TryGetValue(const TKey &key) const
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
return NULL;
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
return const_cast<TValue *>(&node->_pair.GetValue());
node = node->_nextNode;
} while (node);
return NULL;
}
// Gets the value for a given key.
// Throws an exception if there's no value for such a key.
TValue &GetValue(const TKey &key) const
{
TValue *result = TryGetValue(key);
if (result == NULL)
throw std::invalid_argument("There's no value for the given key.");
return *result;
}
// Gets the value associated with the given key.
// If there's no value associated with the given key, the default TValue()
// is returned.
TValue GetValueOrDefault(const TKey &key) const
{
TValue *result = TryGetValue(key);
if (result == NULL)
return TValue();
return *result;
}
// Gets the value associated with the given key.
// If there's no value associated with the given key, the provided
// defaultValue is returned.
TValue GetValueOrDefault(const TKey &key, const TValue &defaultValue) const
{
TValue *result = TryGetValue(key);
if (result == NULL)
return defaultValue;
return *result;
}
// Adds a key/value pair to this dictionary.
void Add(const TKey &key, const TValue &value)
{
if (!TryAdd(key, value))
throw std::invalid_argument("There's already a value for the given key.");
}
// Tries to add a key/value pair to this dictionary.
// If the pair is added, the return is true.
// If there's an already existing association, nothing is changed
// and the function returns false.
bool TryAdd(const TKey &key, const TValue &value)
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, value);
_count++;
return true;
}
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
return false;
node = node->_nextNode;
} while (node);
if (_count >= _capacity)
{
_Resize();
bucketIndex = hashCode % _capacity;
firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, value);
_count++;
return true;
}
}
node = _GetPool()->GetNextWithoutInitializing();
new (node) _Node(firstNode->_nextNode, hashCode, key, value);
firstNode->_nextNode = node;
_count++;
return true;
}
// Sets a value for the given key, replacing a previous association
// or adding a new one if necessary.
void Set(const TKey &key, const TValue &value)
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, value);
_count++;
return;
}
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
{
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
{
const_cast<TValue &>(node->_pair.GetValue()) = value;
return;
}
}
node = node->_nextNode;
} while (node);
if (_count >= _capacity)
{
_Resize();
bucketIndex = hashCode % _capacity;
firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, value);
_count++;
return;
}
}
node = _GetPool()->GetNextWithoutInitializing();
new (node) _Node(firstNode->_nextNode, hashCode, key, value);
firstNode->_nextNode = node;
_count++;
}
// Gets the value for a given key or creates one using the given
// value creator.
template<class TValueCreator>
TValue &GetOrCreateValue(const TKey &key, const TValueCreator &valueCreator)
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, valueCreator(key));
_count++;
return const_cast<TValue &>(firstNode->_pair.GetValue());
}
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
return const_cast<TValue &>(node->_pair.GetValue());
node = node->_nextNode;
} while (node);
if (_count >= _capacity)
{
_Resize();
bucketIndex = hashCode % _capacity;
firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, valueCreator(key));
_count++;
return const_cast<TValue &>(firstNode->_pair.GetValue());
}
}
node = _GetPool()->GetNextWithoutInitializing();
new (node) _Node(firstNode->_nextNode, hashCode, key, valueCreator(key));
firstNode->_nextNode = node;
_count++;
return const_cast<TValue &>(node->_pair.GetValue());
}
// Gets the value for a given key or creates one using the given
// value creator.
template<typename TContextData>
TValue &GetOrCreateValue(const TKey &key, TValue (*valueCreator)(const TKey &key, TContextData *contextData), TContextData *contextData)
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, valueCreator(key, contextData));
_count++;
return const_cast<TValue &>(firstNode->_pair.GetValue());
}
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
return const_cast<TValue &>(node->_pair.GetValue());
node = node->_nextNode;
} while (node);
if (_count >= _capacity)
{
_Resize();
bucketIndex = hashCode % _capacity;
firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
{
new (firstNode) _Node(NULL, hashCode, key, valueCreator(key, contextData));
_count++;
return const_cast<TValue &>(firstNode->_pair.GetValue());
}
}
node = _GetPool()->GetNextWithoutInitializing();
new (node) _Node(firstNode->_nextNode, hashCode, key, valueCreator(key, contextData));
firstNode->_nextNode = node;
_count++;
return const_cast<TValue &>(node->_pair.GetValue());
}
// Removes all items in this dictionary.
// Note that the Capacity is not reset by this action, so if you really want
// to reduce the memory utilisation, do a TrimExcess or SetCapacity after this call.
void Clear()
{
if (_count == 0)
return;
int clearCount = 0;
for(size_t i=0; i<_capacity; i++)
{
_Node *node = &_buckets[i];
if (!_IsEmpty(node))
{
bool isFirst = true;
do
{
_Node *nextNode = node->_nextNode;
node->~_Node();
if (isFirst)
{
node->_nextNode = _GetEmptyPointer();
isFirst = false;
}
else
{
assert(_pool);
_pool->DeleteWithoutDestroying(node);
}
clearCount ++;
if (clearCount == _count)
{
assert(nextNode == NULL);
_count = 0;
return;
}
node = nextNode;
} while(node);
}
}
assert(clearCount == _count);
_count = 0;
}
// Removes an association of a key/value pair. The search is done by the key only
// and the return is true if such key was found (and removed) or false if it was
// not found (so, nothing changed in the dictionary).
bool Remove(const TKey &key)
{
size_t hashCode = TEqualityComparer::GetHashCode(key);
size_t bucketIndex = hashCode % _capacity;
_Node *firstNode = &_buckets[bucketIndex];
if (_IsEmpty(firstNode))
return false;
_Node *previousNode = NULL;
_Node *node = firstNode;
do
{
if (hashCode == node->_hashCode)
{
if (TEqualityComparer::Equals(key, node->_pair.GetKey()))
{
if (node == firstNode)
{
assert(previousNode == NULL);
_Node *nextNode = node->_nextNode;
node->~_Node();
if (nextNode == NULL)
node->_nextNode = _GetEmptyPointer();
else
{
assert(_pool);
new (node) _Node(nextNode->_nextNode, nextNode->_hashCode, nextNode->_pair.GetKey(), nextNode->_pair.GetValue());
_pool->Delete(nextNode);
}
}
else
{
assert(previousNode != NULL);
assert(_pool);
previousNode->_nextNode = node->_nextNode;
_pool->Delete(node);
}
_count--;
return true;
}
}
previousNode = node;
node = node->_nextNode;
} while (node);
return false;
}
class DictionaryEnumerator:
public ICountAwareEnumerator<Pair<TKey, TValue>>
{
Dictionary<TKey, TValue, TEqualityComparer, TMemoryAllocator> *_dictionary;
size_t _bucketIndex;
_Node *_node;
public:
DictionaryEnumerator(Dictionary<TKey, TValue, TEqualityComparer, TMemoryAllocator> *dictionary):
_dictionary(dictionary),
_bucketIndex(0),
_node(NULL)
{
assert(dictionary);
}
size_t GetCount() const
{
return _dictionary->_count;
}
const Pair<TKey, TValue> *GetNext()
{
if (_node)
{
_node = _node->_nextNode;
if (_node)
return &_node->_pair;
_bucketIndex++;
}
while(true)
{
if (_bucketIndex >= _dictionary->_capacity)
{
_node = NULL;
return NULL;
}
_node = &_dictionary->_buckets[_bucketIndex];
if (!_IsEmpty(_node))
return &_node->_pair;
_bucketIndex++;
}
}
};
// Creates an object that's capable of enumerating all key/value pairs
// that exist in this dictionary. It's up to you to delete the created
// object.
DictionaryEnumerator *CreateEnumerator()
{
return new DictionaryEnumerator(this);
}
class KeysEnumerator:
public ICountAwareEnumerator<TKey>
{
private:
DictionaryEnumerator _enumerator;
public:
KeysEnumerator(Dictionary<TKey, TValue, TEqualityComparer, TMemoryAllocator> *dictionary):
_enumerator(dictionary)
{
}
size_t GetCount() const
{
return _enumerator.GetCount();
}
const TKey *GetNext()
{
const Pair<TKey, TValue> *pair = _enumerator.GetNext();
if (pair)
return &pair->GetKey();
return NULL;
}
};
// Creates an object that's capable of enumerating all keys
// that exist in this dictionary. It's up to you to delete the created
// object.
KeysEnumerator *CreateKeysEnumerator()
{
return new KeysEnumerator(this);
}
class ValuesEnumerator:
public ICountAwareEnumerator<TValue>
{
private:
DictionaryEnumerator _enumerator;
public:
ValuesEnumerator(Dictionary<TKey, TValue, TEqualityComparer, TMemoryAllocator> *dictionary):
_enumerator(dictionary)
{
}
size_t GetCount() const
{
return _enumerator.GetCount();
}
const TValue *GetNext()
{
const Pair<TKey, TValue> *pair = _enumerator.GetNext();
if (pair)
return &pair->GetValue();
return NULL;
}
};
// Creates an object that's capable of enumerating all values
// that exist in this dictionary. It's up to you to delete the created
// object.
ValuesEnumerator *CreateValuesEnumerator()
{
return new ValuesEnumerator(this);
}
};
#endif