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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
|
#ifndef _KVI_POINTERHASHTABLE_H_
#define _KVI_POINTERHASHTABLE_H_
//=================================================================================================
//
// File : kvi_pointerhashtable.h
// Creation date : Sat Jan 12 2008 04:53 by Szymon Stefanek
//
// This file is part of the KVirc irc client distribution
// Copyright (C) 2008 Szymon Stefanek (pragma at kvirc dot net)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program is distributed in the HOPE that it will be USEFUL,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=================================================================================================
#include "kvi_settings.h"
#include "kvi_pointerlist.h"
#include "kvi_string.h"
#include "kvi_qstring.h"
#include "kvi_malloc.h"
#include "kvi_memmove.h"
#include <ctype.h>
///
/// Hash functions for various data types
///
inline unsigned int kvi_hash_hash(const char * szKey,bool bCaseSensitive)
{
unsigned int uResult = 0;
if(bCaseSensitive)
{
while(*szKey)
{
uResult += (unsigned char)(*(szKey));
szKey++;
}
} else {
while(*szKey)
{
uResult += (unsigned char)tolower(*(szKey));
szKey++;
}
}
return uResult;
}
inline bool kvi_hash_key_equal(const char * szKey1,const char * szKey2,bool bCaseSensitive)
{
if(bCaseSensitive)
{
while(*szKey1 && *szKey2)
{
if(*szKey1 != *szKey2)
return false;
szKey1++;
szKey2++;
}
} else {
while(*szKey1 && *szKey2)
{
if(tolower(*szKey1) != tolower(*szKey2))
return false;
szKey1++;
szKey2++;
}
}
return true;
}
inline void kvi_hash_key_copy(const char * const &szFrom,const char * &szTo,bool bDeepCopy)
{
if(bDeepCopy)
{
int len = kvi_strLen(szFrom);
char * dst = (char *)kvi_malloc(len+1);
kvi_fastmove(dst,szFrom,len+1);
szTo = dst;
} else {
szTo = szFrom; // we never modify it anyway
}
}
inline void kvi_hash_key_destroy(const char * &szKey,bool bDeepCopy)
{
if(bDeepCopy)
kvi_free(szKey);
}
inline const char * & kvi_hash_key_default(const char **)
{
static const char * static_null = NULL;
return static_null;
}
inline unsigned int kvi_hash_hash(const KviStr &szKey,bool bCaseSensitive)
{
unsigned int uResult = 0;
const char * p = szKey.ptr();
if(bCaseSensitive)
{
while(*p)
{
uResult += *((const unsigned char *)p);
p++;
}
} else {
while(*p)
{
uResult += tolower(*((const unsigned char *)p));
p++;
}
}
return uResult;
}
inline bool kvi_hash_key_equal(const KviStr &szKey1,const KviStr &szKey2)
{
return kvi_hash_key_equal(szKey1.ptr(),szKey2.ptr());
}
inline void kvi_hash_key_copy(const KviStr &szFrom,KviStr &szTo,bool)
{
szTo = szFrom;
}
inline void kvi_hash_key_destroy(KviStr &szKey,bool)
{
}
inline const KviStr & kvi_hash_key_default(KviStr *)
{
return KviStr::emptyString();
}
inline unsigned int kvi_hash_hash(const int &iKey,bool)
{
return (unsigned int)iKey;
}
inline bool kvi_hash_key_equal(const int &iKey1,const int &iKey2,bool)
{
return iKey1 == iKey2;
}
inline void kvi_hash_key_copy(const int &iKeyFrom,int &iKeyTo,bool)
{
iKeyTo = iKeyFrom;
}
inline void kvi_hash_key_destroy(int &iKey,bool)
{
}
inline const int & kvi_hash_key_default(int *)
{
static int static_default = 0;
return static_default;
}
inline unsigned int kvi_hash_hash(const unsigned short &iKey,bool)
{
return (unsigned int)iKey;
}
inline bool kvi_hash_key_equal(const unsigned short &iKey1,const unsigned short &iKey2,bool)
{
return iKey1 == iKey2;
}
inline void kvi_hash_key_copy(const unsigned short &iKeyFrom,unsigned short &iKeyTo,bool)
{
iKeyTo = iKeyFrom;
}
inline void kvi_hash_key_destroy(unsigned short &iKey,bool)
{
}
inline const unsigned short & kvi_hash_key_default(unsigned short *)
{
static unsigned short static_default = 0;
return static_default;
}
inline unsigned int kvi_hash_hash(void * pKey,bool)
{
unsigned char * pBytes = (unsigned char *)&(pKey);
unsigned char * pEnd = pBytes + sizeof(void *);
unsigned int uSum = 0;
while(pBytes < pEnd)
{
uSum += *pBytes;
pBytes++;
}
return uSum;
}
inline bool kvi_hash_key_equal(void *pKey1,void *pKey2,bool)
{
return pKey1 == pKey2;
}
inline void kvi_hash_key_copy(void * const &pKeyFrom,void *&pKeyTo,bool)
{
pKeyTo = pKeyFrom;
}
inline void kvi_hash_key_destroy(void *iKey,bool)
{
}
inline void * & kvi_hash_key_default(void *)
{
static void * static_default = NULL;
return static_default;
}
inline unsigned int kvi_hash_hash(const TQString &szKey,bool bCaseSensitive)
{
unsigned int uResult = 0;
const TQChar * p = KviTQString::nullTerminatedArray(szKey);
if(!p)return 0;
if(bCaseSensitive)
{
while(p->unicode())
{
uResult += p->unicode();
p++;
}
} else {
while(p->unicode())
{
uResult += p->lower().unicode();
p++;
}
}
return uResult;
}
inline bool kvi_hash_key_equal(const TQString &szKey1,const TQString &szKey2,bool bCaseSensitive)
{
if(bCaseSensitive)
return KviTQString::equalCS(szKey1,szKey2);
return KviTQString::equalCI(szKey1,szKey2);
}
inline void kvi_hash_key_copy(const TQString &szFrom,TQString &szTo,bool)
{
szTo = szFrom;
}
inline void kvi_hash_key_destroy(TQString &szKey,bool)
{
}
inline const TQString & kvi_hash_key_default(TQString *)
{
return KviTQString::empty;
}
template<typename Key,typename T> class KviPointerHashTable;
template<typename Key,typename T> class KviPointerHashTableIterator;
template<typename Key,typename T> class KviPointerHashTableEntry
{
friend class KviPointerHashTable<Key,T>;
protected:
T * pData;
Key hKey;
public:
Key & key(){ return hKey; };
T * data(){ return pData; };
};
///
///
/// \class KviPointerHashTable
/// \brief A fast pointer hash table implementation
///
/// A very cool, very fast hash table implementation :P
///
/// To use this hash table you need to provide implementations
/// for the following functions:
///
/// \verbatim
///
/// unsigned int kvi_hash_hash(const Key &hKey,bool bCaseSensitive);
/// bool kvi_hash_key_equal(const Key &hKey1,const Key &hKey2,bool bCaseSensitive);
/// void kvi_hash_key_copy(const Key &hKeyFrom,Key &hKeyTo,bool bDeepCopy);
/// void kvi_hash_key_destroy(Key &hKey,bool bIsDeepCopy);
/// const Key & kvi_hash_key_default(Key *);
///
/// \endverbatim
///
/// Implementations for the most likey Key data types are provided below.
/// KviPointerHashTable will automagically work with const char *,TQString,KviStr
/// and integer types as keys.
///
/// For string Key types, the hash table may or may not be case sensitive.
/// For other Key types the case sensitive flag has no meaning and will
/// (hopefully) be optimized out by the compiler.
///
/// For pointer based keys the hash table may or may not mantain deep copies
/// of Key data. For example, with char * keys, if deep copying is enabled
/// then a private copy of the string data will be mantained. With deep
/// copying disabled only char * pointers will be kept. For types
/// that do not have meaning of deep copy the deep copying code will
/// (hopefully) be optimized out by the compiler.
///
/// The hashtable mantains an array of KviPointerList based buckets.
/// The number of buckets may be specified by the application user
/// and does NOT need to be a prime number. Yet better to have it a power
/// of two so the memory allocation routines will feel better and are
/// less likely to waste space.
///
template<class Key,class T> class KviPointerHashTable
{
friend class KviPointerHashTableIterator<Key,T>;
protected:
KviPointerList<KviPointerHashTableEntry<Key,T> > ** m_pDataArray;
bool m_bAutoDelete;
unsigned int m_uSize;
unsigned int m_uCount;
bool m_bCaseSensitive;
bool m_bDeepCopyKeys;
unsigned int m_uIteratorIdx;
public:
///
/// Returns the item associated to the key hKey
/// or NULL if no such item exists in the hash table.
/// Places the hash table iterator at the position
/// of the item found.
///
T * find(const Key & hKey)
{
m_uIteratorIdx = kvi_hash_hash(hKey,m_bCaseSensitive) % m_uSize;
if(!m_pDataArray[m_uIteratorIdx])return 0;
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->first();e;e = m_pDataArray[m_uIteratorIdx]->next())
{
if(kvi_hash_key_equal(e->hKey,hKey,m_bCaseSensitive))return (T *)e->pData;
}
return 0;
}
///
/// Returns the item associated to the key hKey
/// or NULL if no such item exists in the hash table.
/// Places the hash table iterator at the position
/// of the item found. This is an alias to find().
///
T * operator[](const Key & hKey)
{
return find(hKey);
}
///
/// Returns the number of items in this hash table
///
unsigned int count() const
{
return m_uCount;
}
///
/// Returns true if the hash table is empty
///
bool isEmpty() const
{
return m_uCount == 0;
}
///
/// Inserts the item pData at the position specified by the key hKey.
/// Replaces any previous item with the same key
/// The replaced item is deleted if autodelete is enabled.
/// The hash table iterator is placed at the newly inserted item.
///
void insert(const Key & hKey,T * pData)
{
if(!pData)return;
unsigned int uEntry = kvi_hash_hash(hKey,m_bCaseSensitive) % m_uSize;
if(!m_pDataArray[uEntry])m_pDataArray[uEntry] = new KviPointerList<KviPointerHashTableEntry<Key,T> >(true);
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[uEntry]->first();e;e = m_pDataArray[uEntry]->next())
{
if(kvi_hash_key_equal(e->hKey,hKey,m_bCaseSensitive))
{
if(!m_bCaseSensitive)
{
// must change the key too
kvi_hash_key_destroy(e->hKey,m_bDeepCopyKeys);
kvi_hash_key_copy(hKey,e->hKey,m_bDeepCopyKeys);
}
if(m_bAutoDelete)delete e->pData;
e->pData = pData;
return;
}
}
KviPointerHashTableEntry<Key,T> * n = new KviPointerHashTableEntry<Key,T>;
kvi_hash_key_copy(hKey,n->hKey,m_bDeepCopyKeys);
n->pData = pData;
m_pDataArray[uEntry]->append(n);
m_uCount++;
}
///
/// Inserts the item pData at the position specified by the key hKey.
/// Replaces any previous item with the same key
/// The replaced item is deleted if autodelete is enabled.
/// The hash table iterator is placed at the newly inserted item.
/// This is just an alias to insert() with a different name.
///
void replace(const Key & hKey,T * pData)
{
insert(hKey,pData);
}
///
/// Removes the item pointer associated to the key hKey, if such an item
/// exists in the hash table. The item is deleted if autodeletion
/// is enabled. Returns true if the item was found and removed and false if it wasn't found.
/// Invalidates the hash table iterator.
///
bool remove(const Key & hKey)
{
unsigned int uEntry = kvi_hash_hash(hKey,m_bCaseSensitive) % m_uSize;
if(!m_pDataArray[uEntry])return false;
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[uEntry]->first();e;e = m_pDataArray[uEntry]->next())
{
if(kvi_hash_key_equal(e->hKey,hKey,m_bCaseSensitive))
{
kvi_hash_key_destroy(e->hKey,m_bDeepCopyKeys);
if(m_bAutoDelete)delete ((T *)(e->pData));
m_pDataArray[uEntry]->removeRef(e);
if(m_pDataArray[uEntry]->isEmpty())
{
delete m_pDataArray[uEntry];
m_pDataArray[uEntry] = 0;
}
m_uCount--;
return true;
}
}
return false;
}
///
/// Removes the first occurence of the item pointer pRef. The item is deleted if autodeletion
/// is enabled. Returns true if the pointer was found and false otherwise
/// Invalidates the hash table iterator.
///
bool removeRef(const T * pRef)
{
for(unsigned int i=0;i<m_uSize;i++)
{
if(m_pDataArray[i])
{
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[i]->first();e;e = m_pDataArray[i]->next())
{
if(e->pData == pRef)
{
kvi_hash_key_destroy(e->hKey,m_bDeepCopyKeys);
if(m_bAutoDelete)delete ((T *)(e->pData));
m_pDataArray[i]->removeRef(e);
if(m_pDataArray[i]->isEmpty())
{
delete m_pDataArray[i];
m_pDataArray[i] = 0;
}
m_uCount--;
return true;
}
}
}
}
return false;
}
///
/// Removes all the items from the hash table.
/// The items are deleted if autodeletion is enabled.
/// Invalidates the hash table iterator.
///
void clear()
{
for(unsigned int i=0;i<m_uSize;i++)
{
if(m_pDataArray[i])
{
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[i]->first();e;e = m_pDataArray[i]->next())
{
kvi_hash_key_destroy(e->hKey,m_bDeepCopyKeys);
if(m_bAutoDelete)
delete ((T *)(e->pData));
}
delete m_pDataArray[i];
m_pDataArray[i] = 0;
}
}
m_uCount = 0;
}
///
/// Searches for the item pointer pRef and returns
/// it's hash table entry, if found, and NULL otherwise.
/// The hash table iterator is placed at the item found.
///
KviPointerHashTableEntry<Key,T> * findRef(const T * pRef)
{
for(m_uIteratorIdx = 0;m_uIteratorIdx<m_uSize;m_uIteratorIdx++)
{
if(m_pDataArray[m_uIteratorIdx])
{
for(KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->first();e;e = m_pDataArray[m_uIteratorIdx]->next())
{
if(e->pData == pRef)return e;
}
}
}
return 0;
}
///
/// Returns the entry pointed by the hash table iterator.
/// This function must be preceeded by a call to firstEntry(), first()
/// or findRef().
///
KviPointerHashTableEntry<Key,T> * currentEntry()
{
if(m_uIteratorIdx >= m_uSize)return 0;
if(m_pDataArray[m_uIteratorIdx])return m_pDataArray[m_uIteratorIdx]->current();
return 0;
}
///
/// Places the hash table iterator at the first entry
/// and returns it.
///
KviPointerHashTableEntry<Key,T> * firstEntry()
{
m_uIteratorIdx = 0;
while(m_uIteratorIdx < m_uSize && (!m_pDataArray[m_uIteratorIdx]))
{
m_uIteratorIdx++;
}
if(m_uIteratorIdx == m_uSize)return 0;
return m_pDataArray[m_uIteratorIdx]->first();
}
///
/// Places the hash table iterator at the next entry
/// and returns it.
/// This function must be preceeded by a call to firstEntry(), first()
/// or findRef().
///
KviPointerHashTableEntry<Key,T> * nextEntry()
{
if(m_uIteratorIdx >= m_uSize)return 0;
if(m_uIteratorIdx < m_uSize)
{
KviPointerHashTableEntry<Key,T> * t = m_pDataArray[m_uIteratorIdx]->next();
if(t)return t;
}
m_uIteratorIdx++;
while(m_uIteratorIdx < m_uSize && (!m_pDataArray[m_uIteratorIdx]))
{
m_uIteratorIdx++;
}
if(m_uIteratorIdx == m_uSize)return 0;
return m_pDataArray[m_uIteratorIdx]->first();
}
///
/// Returns the data value pointer pointed by the hash table iterator.
/// This function must be preceeded by a call to firstEntry(), first()
/// or findRef().
///
T * current()
{
if(m_uIteratorIdx >= m_uSize)return 0;
if(m_pDataArray[m_uIteratorIdx])
{
KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->current();
if(!e)return 0;
return e->data();
}
return 0;
}
///
/// Returns the key pointed by the hash table iterator.
/// This function must be preceeded by a call to firstEntry(), first()
/// or findRef().
///
const Key & currentKey()
{
if(m_uIteratorIdx >= m_uSize)return kvi_hash_key_default(((Key *)NULL));
if(m_pDataArray[m_uIteratorIdx])
{
KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->current();
if(!e)return kvi_hash_key_default(((Key *)NULL));
return e->key();
}
return kvi_hash_key_default(((Key *)NULL));
}
///
/// Places the hash table iterator at the first entry
/// and returns the associated data value pointer.
///
T * first()
{
m_uIteratorIdx = 0;
while(m_uIteratorIdx < m_uSize && (!m_pDataArray[m_uIteratorIdx]))
{
m_uIteratorIdx++;
}
if(m_uIteratorIdx == m_uSize)return 0;
KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->first();
if(!e)return 0;
return e->data();
}
///
/// Places the hash table iterator at the next entry
/// and returns the associated data value pointer.
/// This function must be preceeded by a call to firstEntry(), first()
/// or findRef().
///
T * next()
{
if(m_uIteratorIdx >= m_uSize)return 0;
if(m_uIteratorIdx < m_uSize)
{
KviPointerHashTableEntry<Key,T> * t = m_pDataArray[m_uIteratorIdx]->next();
if(t)
{
return t->data();
}
}
m_uIteratorIdx++;
while(m_uIteratorIdx < m_uSize && (!m_pDataArray[m_uIteratorIdx]))
{
m_uIteratorIdx++;
}
if(m_uIteratorIdx == m_uSize)return 0;
KviPointerHashTableEntry<Key,T> * e = m_pDataArray[m_uIteratorIdx]->first();
if(!e)return 0;
return e->data();
}
///
/// Removes all items in the hash table and then
/// makes a complete shallow copy of the data contained in t.
/// The removed items are deleted if autodeletion is enabled.
/// The hash table iterator is invalidated.
/// Does not change autodelete flag: make sure you not delete the items twice :)
///
void copyFrom(KviPointerHashTable<Key,T> &t)
{
clear();
for(KviPointerHashTableEntry<Key,T> * e = t.firstEntry();e;e = t.nextEntry())
insert(e->key(),e->data());
}
///
/// Inserts a complete shallow copy of the data contained in t.
/// The hash table iterator is invalidated.
///
void insert(KviPointerHashTable<Key,T> &t)
{
for(KviPointerHashTableEntry<Key,T> * e = t.firstEntry();e;e = t.nextEntry())
insert(e->key(),e->data());
}
///
/// Enables or disabled the autodeletion feature.
/// Items are deleted upon removal when the feature is enabled.
///
void setAutoDelete(bool bAutoDelete)
{
m_bAutoDelete = bAutoDelete;
}
///
/// Creates an empty hash table.
/// Automatic deletion is enabled.
///
/// \param uSize The number of hash buckets: does NOT necesairly need to be prime
/// \param bCaseSensitive Are the key comparisons case sensitive ?
/// \param Do we need to mantain deep copies of keys ?
///
KviPointerHashTable(unsigned int uSize = 32,bool bCaseSensitive = true,bool bDeepCopyKeys = true)
{
m_uCount = 0;
m_bCaseSensitive = bCaseSensitive;
m_bAutoDelete = true;
m_bDeepCopyKeys = bDeepCopyKeys;
m_uSize = uSize > 0 ? uSize : 32;
m_pDataArray = new KviPointerList<KviPointerHashTableEntry<Key,T> > *[m_uSize];
for(unsigned int i=0;i<m_uSize;i++)m_pDataArray[i] = NULL;
}
///
/// First creates an empty hash table
/// and then inserts a copy of all the item pointers present in t.
/// The autodelete feature is automatically disabled (take care!).
///
KviPointerHashTable(KviPointerHashTable<Key,T> &t)
{
m_uCount = 0;
m_bAutoDelete = false;
m_bCaseSensitive = t.m_bCaseSensitive;
m_bDeepCopyKeys = t.m_bDeepCopyKeys;
m_uSize = t.m_uSize;
m_pDataArray = new KviPointerList<KviPointerHashTableEntry<Key,T> > *[m_uSize];
for(unsigned int i=0;i<m_uSize;i++)m_pDataArray[i] = NULL;
copyFrom(t);
}
///
/// Destroys the hash table and all the items contained within.
/// Items are deleted if autodeletion is enabled.
///
~KviPointerHashTable()
{
clear();
delete [] m_pDataArray;
}
};
template<typename Key,typename T> class KviPointerHashTableIterator
{
protected:
const KviPointerHashTable<Key,T> * m_pHashTable;
unsigned int m_uEntryIndex;
KviPointerListIterator<KviPointerHashTableEntry<Key,T> > * m_pIterator;
public:
///
/// Creates an iterator copy.
/// The new iterator points exactly to the item pointed by src.
///
void operator = (const KviPointerHashTableIterator<Key,T> &src)
{
m_pHashTable = src.m_pHashTable;
m_uEntryIndex = src.m_uEntryIndex;
if(src.m_pIterator)
m_pIterator = new KviPointerListIterator<KviPointerHashTableEntry<Key,T> >(*(src.m_pIterator));
else
m_pIterator = NULL;
}
///
/// Moves the iterator to the first element of the hash table.
/// Returns true in case of success or false if the hash table is empty.
///
bool moveFirst()
{
if(m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
m_uEntryIndex = 0;
while((m_uEntryIndex < m_pHashTable->m_uSize) && (!(m_pHashTable->m_pDataArray[m_uEntryIndex])))
{
m_uEntryIndex++;
}
if(m_uEntryIndex == m_pHashTable->m_uSize)
return false;
m_pIterator = new KviPointerListIterator<KviPointerHashTableEntry<Key,T> >(*(m_pHashTable->m_pDataArray[m_uEntryIndex]));
bool bRet = m_pIterator->moveFirst();
if(!bRet)
{
delete m_pIterator;
m_pIterator = NULL;
}
return bRet;
}
///
/// Moves the iterator to the last element of the hash table.
/// Returns true in case of success or false if the hash table is empty.
///
bool moveLast()
{
if(m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
m_uEntryIndex = m_pHashTable->m_uSize;
while(m_uEntryIndex > 0)
{
m_uEntryIndex--;
if(m_pHashTable->m_pDataArray[m_uEntryIndex])
{
m_pIterator = new KviPointerListIterator<KviPointerHashTableEntry<Key,T> >(*(m_pHashTable->m_pDataArray[m_uEntryIndex]));
bool bRet = m_pIterator->moveLast();
if(!bRet)
{
delete m_pIterator;
m_pIterator = NULL;
}
return bRet;
}
}
return false;
}
///
/// Moves the iterator to the next element of the hash table.
/// The iterator must be actually valid for this function to work.
/// Returns true in case of success or false if there is no next item.
///
bool moveNext()
{
if(!m_pIterator)
return false;
if(m_pIterator->moveNext())
return true;
if(m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
m_uEntryIndex++;
while((m_uEntryIndex < m_pHashTable->m_uSize) && (!(m_pHashTable->m_pDataArray[m_uEntryIndex])))
{
m_uEntryIndex++;
}
if(m_uEntryIndex == m_pHashTable->m_uSize)
return false;
m_pIterator = new KviPointerListIterator<KviPointerHashTableEntry<Key,T> >(*(m_pHashTable->m_pDataArray[m_uEntryIndex]));
bool bRet = m_pIterator->moveFirst();
if(!bRet)
{
delete m_pIterator;
m_pIterator = NULL;
}
return bRet;
}
///
/// Moves the iterator to the next element of the hash table.
/// The iterator must be actually valid for this function to work.
/// Returns true in case of success or false if there is no next item.
/// This is just an alias to moveNext().
///
bool operator ++()
{
return moveNext();
}
///
/// Moves the iterator to the previous element of the hash table.
/// The iterator must be actually valid for this function to work.
/// Returns true in case of success or false if there is no previous item.
///
bool movePrev()
{
if(!m_pIterator)
return false;
if(m_pIterator->movePrev())
return true;
if(m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
if(m_uEntryIndex >= m_pHashTable->m_uSize)
return false;
while(m_uEntryIndex > 0)
{
m_uEntryIndex--;
if(m_pHashTable->m_pDataArray[m_uEntryIndex])
{
m_pIterator = new KviPointerListIterator<KviPointerHashTableEntry<Key,T> >(*(m_pHashTable->m_pDataArray[m_uEntryIndex]));
bool bRet = m_pIterator->moveLast();
if(!bRet)
{
delete m_pIterator;
m_pIterator = NULL;
}
return bRet;
}
}
return false;
}
///
/// Moves the iterator to the previous element of the hash table.
/// The iterator must be actually valid for this function to work.
/// Returns true in case of success or false if there is no previous item.
/// This is just an alias to movePrev() with a different name.
///
bool operator --()
{
return movePrev();
}
///
/// Returs the value pointed by the iterator
/// or a default constructed value if the iterator is not valid.
/// This is an alias to operator *() with just a different name.
///
T * current() const
{
return m_pIterator ? m_pIterator->current()->data() : NULL;
}
///
/// Returs the value pointed by the iterator
/// or a default constructed value if the iterator is not valid.
/// This is an alias to current() with just a different name.
///
T * operator *() const
{
return m_pIterator ? m_pIterator->current()->data() : NULL;
}
///
/// Returs the key pointed by the iterator
/// or a default constructed key if the iterator is not valid.
///
const Key & currentKey() const
{
return m_pIterator ? m_pIterator->current()->key() : kvi_hash_key_default(((Key *)NULL));
}
///
/// Moves the iterator to the first element of the hash table.
/// Returns the first item found or NULL if the hash table is empty.
///
T * toFirst()
{
if(!moveFirst())
return NULL;
return current();
}
public:
///
/// Creates an iterator pointing to the first item in the hash table, if any.
///
KviPointerHashTableIterator(const KviPointerHashTable<Key,T> &hTable)
{
m_pHashTable = &hTable;
m_uEntryIndex = 0;
m_pIterator = NULL;
moveFirst();
}
///
/// Destroys the iterator
///
~KviPointerHashTableIterator()
{
if(m_pIterator)
delete m_pIterator;
}
};
#endif //_KVI_POINTERHASHTABLE_H_
|