forked from cloudflarearchive/kyotocabinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkcmap.h
1379 lines (1365 loc) · 39.5 KB
/
kcmap.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
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
996
997
998
999
1000
/*************************************************************************************************
* Data mapping structures
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Cabinet.
* 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
* 3 of the License, or 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, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#ifndef _KCMAP_H // duplication check
#define _KCMAP_H
#include <kccommon.h>
#include <kcutil.h>
namespace kyotocabinet { // common namespace
/**
* Doubly-linked hash map.
* @param KEY the key type.
* @param VALUE the value type.
* @param HASH the hash functor.
* @param EQUALTO the equality checking functor.
*/
template <class KEY, class VALUE,
class HASH = std::hash<KEY>, class EQUALTO = std::equal_to<KEY> >
class LinkedHashMap {
public:
class Iterator;
private:
struct Record;
/** The default bucket number of hash table. */
static const size_t MAPDEFBNUM = 31;
/** The mininum number of buckets to use mmap. */
static const size_t MAPZMAPBNUM = 32768;
public:
/**
* Iterator of records.
*/
class Iterator {
friend class LinkedHashMap;
public:
/**
* Copy constructor.
* @param src the source object.
*/
Iterator(const Iterator& src) : map_(src.map_), rec_(src.rec_) {
_assert_(true);
}
/**
* Get the key.
*/
const KEY& key() {
_assert_(true);
return rec_->key;
}
/**
* Get the value.
*/
VALUE& value() {
_assert_(true);
return rec_->value;
}
/**
* Assignment operator from the self type.
* @param right the right operand.
* @return the reference to itself.
*/
Iterator& operator =(const Iterator& right) {
_assert_(true);
if (&right == this) return *this;
map_ = right.map_;
rec_ = right.rec_;
return *this;
}
/**
* Equality operator with the self type.
* @param right the right operand.
* @return true if the both are equal, or false if not.
*/
bool operator ==(const Iterator& right) const {
_assert_(true);
return map_ == right.map_ && rec_ == right.rec_;
}
/**
* Non-equality operator with the self type.
* @param right the right operand.
* @return false if the both are equal, or true if not.
*/
bool operator !=(const Iterator& right) const {
_assert_(true);
return map_ != right.map_ || rec_ != right.rec_;
}
/**
* Preposting increment operator.
* @return the iterator itself.
*/
Iterator& operator ++() {
_assert_(true);
rec_ = rec_->next;
return *this;
}
/**
* Postpositive increment operator.
* @return an iterator of the old position.
*/
Iterator operator ++(int) {
_assert_(true);
Iterator old(*this);
rec_ = rec_->next;
return old;
}
/**
* Preposting decrement operator.
* @return the iterator itself.
*/
Iterator& operator --() {
_assert_(true);
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return *this;
}
/**
* Postpositive decrement operator.
* @return an iterator of the old position.
*/
Iterator operator --(int) {
_assert_(true);
Iterator old(*this);
if (rec_) {
rec_ = rec_->prev;
} else {
rec_ = map_->last_;
}
return old;
}
private:
/**
* Constructor.
* @param map the container.
* @param rec the pointer to the current record.
*/
explicit Iterator(LinkedHashMap* map, Record* rec) : map_(map), rec_(rec) {
_assert_(map);
}
/** The container. */
LinkedHashMap* map_;
/** The current record. */
Record* rec_;
};
/**
* Moving Modes.
*/
enum MoveMode {
MCURRENT, ///< keep the current position
MFIRST, ///< move to the first
MLAST ///< move to the last
};
/**
* Default constructor.
*/
explicit LinkedHashMap() :
buckets_(NULL), bnum_(MAPDEFBNUM), first_(NULL), last_(NULL), count_(0) {
_assert_(true);
initialize();
}
/**
* Constructor.
* @param bnum the number of buckets of the hash table.
*/
explicit LinkedHashMap(size_t bnum) :
buckets_(NULL), bnum_(bnum), first_(NULL), last_(NULL), count_(0) {
_assert_(true);
if (bnum_ < 1) bnum_ = MAPDEFBNUM;
initialize();
}
/**
* Destructor.
*/
~LinkedHashMap() {
_assert_(true);
destroy();
}
/**
* Store a record.
* @param key the key.
* @param value the value.
* @param mode the moving mode.
* @return the pointer to the value of the stored record.
*/
VALUE *set(const KEY& key, const VALUE& value, MoveMode mode) {
_assert_(true);
size_t bidx = hash_(key) % bnum_;
Record* rec = buckets_[bidx];
Record** entp = buckets_ + bidx;
while (rec) {
if (equalto_(rec->key, key)) {
rec->value = value;
switch (mode) {
default: {
break;
}
case MFIRST: {
if (first_ != rec) {
if (last_ == rec) last_ = rec->prev;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = NULL;
rec->next = first_;
first_->prev = rec;
first_ = rec;
}
break;
}
case MLAST: {
if (last_ != rec) {
if (first_ == rec) first_ = rec->next;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = last_;
rec->next = NULL;
last_->next = rec;
last_ = rec;
}
break;
}
}
return &rec->value;
} else {
entp = &rec->child;
rec = rec->child;
}
}
rec = new Record(key, value);
switch (mode) {
default: {
rec->prev = last_;
if (!first_) first_ = rec;
if (last_) last_->next = rec;
last_ = rec;
break;
}
case MFIRST: {
rec->next = first_;
if (!last_) last_ = rec;
if (first_) first_->prev = rec;
first_ = rec;
break;
}
}
*entp = rec;
count_++;
return &rec->value;
}
/**
* Remove a record.
* @param key the key.
* @return true on success, or false on failure.
*/
bool remove(const KEY& key) {
_assert_(true);
size_t bidx = hash_(key) % bnum_;
Record* rec = buckets_[bidx];
Record** entp = buckets_ + bidx;
while (rec) {
if (equalto_(rec->key, key)) {
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
if (rec == first_) first_ = rec->next;
if (rec == last_) last_ = rec->prev;
*entp = rec->child;
count_--;
delete rec;
return true;
} else {
entp = &rec->child;
rec = rec->child;
}
}
return false;
}
/**
* Migrate a record to another map.
* @param key the key.
* @param dist the destination map.
* @param mode the moving mode.
* @return the pointer to the value of the migrated record, or NULL on failure.
*/
VALUE* migrate(const KEY& key, LinkedHashMap* dist, MoveMode mode) {
_assert_(dist);
size_t hash = hash_(key);
size_t bidx = hash % bnum_;
Record* rec = buckets_[bidx];
Record** entp = buckets_ + bidx;
while (rec) {
if (equalto_(rec->key, key)) {
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
if (rec == first_) first_ = rec->next;
if (rec == last_) last_ = rec->prev;
*entp = rec->child;
count_--;
rec->child = NULL;
rec->prev = NULL;
rec->next = NULL;
bidx = hash % dist->bnum_;
Record* drec = dist->buckets_[bidx];
entp = dist->buckets_ + bidx;
while (drec) {
if (dist->equalto_(drec->key, key)) {
if (drec->child) rec->child = drec->child;
if (drec->prev) {
rec->prev = drec->prev;
rec->prev->next = rec;
}
if (drec->next) {
rec->next = drec->next;
rec->next->prev = rec;
}
if (dist->first_ == drec) dist->first_ = rec;
if (dist->last_ == drec) dist->last_ = rec;
*entp = rec;
delete drec;
switch (mode) {
default: {
break;
}
case MFIRST: {
if (dist->first_ != rec) {
if (dist->last_ == rec) dist->last_ = rec->prev;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = NULL;
rec->next = dist->first_;
dist->first_->prev = rec;
dist->first_ = rec;
}
break;
}
case MLAST: {
if (dist->last_ != rec) {
if (dist->first_ == rec) dist->first_ = rec->next;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = dist->last_;
rec->next = NULL;
dist->last_->next = rec;
dist->last_ = rec;
}
break;
}
}
return &rec->value;
} else {
entp = &drec->child;
drec = drec->child;
}
}
switch (mode) {
default: {
rec->prev = dist->last_;
if (!dist->first_) dist->first_ = rec;
if (dist->last_) dist->last_->next = rec;
dist->last_ = rec;
break;
}
case MFIRST: {
rec->next = dist->first_;
if (!dist->last_) dist->last_ = rec;
if (dist->first_) dist->first_->prev = rec;
dist->first_ = rec;
break;
}
}
*entp = rec;
dist->count_++;
return &rec->value;
} else {
entp = &rec->child;
rec = rec->child;
}
}
return NULL;
}
/**
* Retrieve a record.
* @param key the key.
* @param mode the moving mode.
* @return the pointer to the value of the corresponding record, or NULL on failure.
*/
VALUE* get(const KEY& key, MoveMode mode) {
_assert_(true);
size_t bidx = hash_(key) % bnum_;
Record* rec = buckets_[bidx];
while (rec) {
if (equalto_(rec->key, key)) {
switch (mode) {
default: {
break;
}
case MFIRST: {
if (first_ != rec) {
if (last_ == rec) last_ = rec->prev;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = NULL;
rec->next = first_;
first_->prev = rec;
first_ = rec;
}
break;
}
case MLAST: {
if (last_ != rec) {
if (first_ == rec) first_ = rec->next;
if (rec->prev) rec->prev->next = rec->next;
if (rec->next) rec->next->prev = rec->prev;
rec->prev = last_;
rec->next = NULL;
last_->next = rec;
last_ = rec;
}
break;
}
}
return &rec->value;
} else {
rec = rec->child;
}
}
return NULL;
}
/**
* Remove all records.
*/
void clear() {
_assert_(true);
if (count_ < 1) return;
Record* rec = last_;
while (rec) {
Record* prev = rec->prev;
delete rec;
rec = prev;
}
for (size_t i = 0; i < bnum_; i++) {
buckets_[i] = NULL;
}
first_ = NULL;
last_ = NULL;
count_ = 0;
}
/**
* Get the number of records.
*/
size_t count() {
_assert_(true);
return count_;
}
/**
* Get an iterator at the first record.
*/
Iterator begin() {
_assert_(true);
return Iterator(this, first_);
}
/**
* Get an iterator of the end sentry.
*/
Iterator end() {
_assert_(true);
return Iterator(this, NULL);
}
/**
* Get an iterator at a record.
* @param key the key.
* @return the pointer to the value of the corresponding record, or NULL on failure.
*/
Iterator find(const KEY& key) {
_assert_(true);
size_t bidx = hash_(key) % bnum_;
Record* rec = buckets_[bidx];
while (rec) {
if (equalto_(rec->key, key)) {
return Iterator(this, rec);
} else {
rec = rec->child;
}
}
return Iterator(this, NULL);
}
/**
* Get the reference of the key of the first record.
* @return the reference of the key of the first record.
*/
const KEY& first_key() {
_assert_(true);
return first_->key;
}
/**
* Get the reference of the value of the first record.
* @return the reference of the value of the first record.
*/
VALUE& first_value() {
_assert_(true);
return first_->value;
}
/**
* Get the reference of the key of the last record.
* @return the reference of the key of the last record.
*/
const KEY& last_key() {
_assert_(true);
return last_->key;
}
/**
* Get the reference of the value of the last record.
* @return the reference of the value of the last record.
*/
VALUE& last_value() {
_assert_(true);
return last_->value;
}
private:
/**
* Record data.
*/
struct Record {
KEY key; ///< key
VALUE value; ///< value
Record* child; ///< child record
Record* prev; ///< previous record
Record* next; ///< next record
/** constructor */
explicit Record(const KEY& k, const VALUE& v) :
key(k), value(v), child(NULL), prev(NULL), next(NULL) {
_assert_(true);
}
};
/**
* Initialize fields.
*/
void initialize() {
_assert_(true);
if (bnum_ >= MAPZMAPBNUM) {
buckets_ = (Record**)mapalloc(sizeof(*buckets_) * bnum_);
} else {
buckets_ = new Record*[bnum_];
for (size_t i = 0; i < bnum_; i++) {
buckets_[i] = NULL;
}
}
}
/**
* Clean up fields.
*/
void destroy() {
_assert_(true);
Record* rec = last_;
while (rec) {
Record* prev = rec->prev;
delete rec;
rec = prev;
}
if (bnum_ >= MAPZMAPBNUM) {
mapfree(buckets_);
} else {
delete[] buckets_;
}
}
/** Dummy constructor to forbid the use. */
LinkedHashMap(const LinkedHashMap&);
/** Dummy Operator to forbid the use. */
LinkedHashMap& operator =(const LinkedHashMap&);
/** The functor of the hash function. */
HASH hash_;
/** The functor of the equalto function. */
EQUALTO equalto_;
/** The bucket array. */
Record** buckets_;
/** The number of buckets. */
size_t bnum_;
/** The first record. */
Record* first_;
/** The last record. */
Record* last_;
/** The number of records. */
size_t count_;
};
/**
* Memory-saving string hash map.
*/
class TinyHashMap {
public:
class Iterator;
private:
struct Record;
struct RecordComparator;
/** The default bucket number of hash table. */
static const size_t MAPDEFBNUM = 31;
/** The mininum number of buckets to use mmap. */
static const size_t MAPZMAPBNUM = 32768;
public:
/**
* Iterator of records.
*/
class Iterator {
friend class TinyHashMap;
public:
/**
* Constructor.
* @param map the container.
* @note This object will not be invalidated even when the map object is updated once.
* However, phantom records may be retrieved if they are removed after creation of each
* iterator.
*/
explicit Iterator(TinyHashMap* map) : map_(map), bidx_(-1), ridx_(0), recs_() {
_assert_(map);
step();
}
/**
* Destructor.
*/
~Iterator() {
_assert_(true);
free_records();
}
/**
* Get the key of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @return the pointer to the key region of the current record, or NULL on failure.
*/
const char* get_key(size_t* sp) {
_assert_(sp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*sp = rec.ksiz_;
return rec.kbuf_;
}
/**
* Get the value of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @return the pointer to the value region of the current record, or NULL on failure.
*/
const char* get_value(size_t* sp) {
_assert_(sp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*sp = rec.vsiz_;
return rec.vbuf_;
}
/**
* Get a pair of the key and the value of the current record.
* @param ksp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param vbp the pointer to the variable into which the pointer to the value region is
* assigned.
* @param vsp the pointer to the variable into which the size of the value region is
* assigned.
* @return the pointer to the key region, or NULL on failure.
*/
const char* get(size_t* ksp, const char** vbp, size_t* vsp) {
_assert_(ksp && vbp && vsp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*ksp = rec.ksiz_;
*vbp = rec.vbuf_;
*vsp = rec.vsiz_;
return rec.kbuf_;
}
/**
* Step the cursor to the next record.
*/
void step() {
_assert_(true);
if (++ridx_ >= recs_.size()) {
ridx_ = 0;
free_records();
while (true) {
bidx_++;
if (bidx_ >= (int64_t)map_->bnum_) return;
read_records();
if (recs_.size() > 0) break;
}
}
}
private:
/**
* Read records of the current bucket.
*/
void read_records() {
char* rbuf = map_->buckets_[bidx_];
while (rbuf) {
Record rec(rbuf);
size_t rsiz = sizeof(rec.child_) + sizevarnum(rec.ksiz_) + rec.ksiz_ +
sizevarnum(rec.vsiz_) + rec.vsiz_ + sizevarnum(rec.psiz_);
char* nbuf = new char[rsiz];
std::memcpy(nbuf, rbuf, rsiz);
recs_.push_back(nbuf);
rbuf = rec.child_;
}
}
/**
* Release recources of the current records.
*/
void free_records() {
std::vector<char*>::iterator it = recs_.begin();
std::vector<char*>::iterator itend = recs_.end();
while (it != itend) {
char* rbuf = *it;
delete[] rbuf;
++it;
}
recs_.clear();
}
/** Dummy constructor to forbid the use. */
Iterator(const Iterator&);
/** Dummy Operator to forbid the use. */
Iterator& operator =(const Iterator&);
/** The container. */
TinyHashMap* map_;
/** The current bucket index. */
int64_t bidx_;
/** The current record index. */
size_t ridx_;
/** The current records. */
std::vector<char*> recs_;
};
/**
* Sorter of records.
*/
class Sorter {
public:
/**
* Constructor.
* @param map the container.
* @note This object will be invalidated when the map object is updated once.
*/
explicit Sorter(TinyHashMap* map) : map_(map), ridx_(0), recs_() {
_assert_(map);
char** buckets = map_->buckets_;
size_t bnum = map_->bnum_;
for (size_t i = 0; i < bnum; i++) {
char* rbuf = buckets[i];
while (rbuf) {
Record rec(rbuf);
recs_.push_back(rbuf);
rbuf = *(char**)rbuf;
}
}
std::sort(recs_.begin(), recs_.end(), RecordComparator());
}
/**
* Destructor.
*/
~Sorter() {
_assert_(true);
}
/**
* Get the key of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @return the pointer to the key region of the current record, or NULL on failure.
*/
const char* get_key(size_t* sp) {
_assert_(sp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*sp = rec.ksiz_;
return rec.kbuf_;
}
/**
* Get the value of the current record.
* @param sp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @return the pointer to the value region of the current record, or NULL on failure.
*/
const char* get_value(size_t* sp) {
_assert_(sp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*sp = rec.vsiz_;
return rec.vbuf_;
}
/**
* Get a pair of the key and the value of the current record.
* @param ksp the pointer to the variable into which the size of the region of the return
* value is assigned.
* @param vbp the pointer to the variable into which the pointer to the value region is
* assigned.
* @param vsp the pointer to the variable into which the size of the value region is
* assigned.
* @return the pointer to the key region, or NULL on failure.
*/
const char* get(size_t* ksp, const char** vbp, size_t* vsp) {
_assert_(ksp && vbp && vsp);
if (ridx_ >= recs_.size()) return NULL;
Record rec(recs_[ridx_]);
*ksp = rec.ksiz_;
*vbp = rec.vbuf_;
*vsp = rec.vsiz_;
return rec.kbuf_;
}
/**
* Step the cursor to the next record.
*/
void step() {
_assert_(true);
ridx_++;
}
/** The container. */
TinyHashMap* map_;
/** The current record index. */
size_t ridx_;
/** The current records. */
std::vector<char*> recs_;
};
/**
* Default constructor.
*/
explicit TinyHashMap() : buckets_(NULL), bnum_(MAPDEFBNUM), count_(0) {
_assert_(true);
initialize();
}
/**
* Constructor.
* @param bnum the number of buckets of the hash table.
*/
explicit TinyHashMap(size_t bnum) : buckets_(NULL), bnum_(bnum), count_(0) {
_assert_(true);
if (bnum_ < 1) bnum_ = MAPDEFBNUM;
initialize();
}
/**
* Destructor.
*/
~TinyHashMap() {
_assert_(true);
destroy();
}
/**
* Set the value of a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @param vbuf the pointer to the value region.
* @param vsiz the size of the value region.
* @note If no record corresponds to the key, a new record is created. If the corresponding
* record exists, the value is overwritten.
*/
void set(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && vbuf && vsiz <= MEMMAXSIZ);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];
char** entp = buckets_ + bidx;
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) {
int32_t oh = (int32_t)sizevarnum(vsiz) - (int32_t)sizevarnum(rec.vsiz_);
int64_t psiz = (int64_t)(rec.vsiz_ + rec.psiz_) - (int64_t)(vsiz + oh);
if (psiz >= 0) {
rec.overwrite(rbuf, vbuf, vsiz, psiz);
} else {
Record nrec(rec.child_, kbuf, ksiz, vbuf, vsiz, 0);
delete[] rbuf;
*entp = nrec.serialize();
}
return;
}
entp = (char**)rbuf;
rbuf = rec.child_;
}
Record nrec(NULL, kbuf, ksiz, vbuf, vsiz, 0);
*entp = nrec.serialize();
count_++;
}
/**
* Add a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @param vbuf the pointer to the value region.
* @param vsiz the size of the value region.
* @return true on success, or false on failure.
* @note If no record corresponds to the key, a new record is created. If the corresponding
* record exists, the record is not modified and false is returned.
*/
bool add(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && vbuf && vsiz <= MEMMAXSIZ);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];
char** entp = buckets_ + bidx;
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) return false;
entp = (char**)rbuf;
rbuf = rec.child_;
}
Record nrec(NULL, kbuf, ksiz, vbuf, vsiz, 0);
*entp = nrec.serialize();
count_++;
return true;
}
/**
* Replace the value of a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @param vbuf the pointer to the value region.
* @param vsiz the size of the value region.
* @return true on success, or false on failure.
* @note If no record corresponds to the key, no new record is created and false is returned.
* If the corresponding record exists, the value is modified.
*/
bool replace(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && vbuf && vsiz <= MEMMAXSIZ);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];
char** entp = buckets_ + bidx;
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) {
int32_t oh = (int32_t)sizevarnum(vsiz) - (int32_t)sizevarnum(rec.vsiz_);
int64_t psiz = (int64_t)(rec.vsiz_ + rec.psiz_) - (int64_t)(vsiz + oh);
if (psiz >= 0) {
rec.overwrite(rbuf, vbuf, vsiz, psiz);
} else {
Record nrec(rec.child_, kbuf, ksiz, vbuf, vsiz, 0);
delete[] rbuf;
*entp = nrec.serialize();
}
return true;
}
entp = (char**)rbuf;
rbuf = rec.child_;
}
return false;
}
/**
* Append the value of a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @param vbuf the pointer to the value region.
* @param vsiz the size of the value region.
* @note If no record corresponds to the key, a new record is created. If the corresponding
* record exists, the given value is appended at the end of the existing value.
*/
void append(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && vbuf && vsiz <= MEMMAXSIZ);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];
char** entp = buckets_ + bidx;
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) {
size_t nsiz = rec.vsiz_ + vsiz;
int32_t oh = (int32_t)sizevarnum(nsiz) - (int32_t)sizevarnum(rec.vsiz_);
int64_t psiz = (int64_t)(rec.vsiz_ + rec.psiz_) - (int64_t)(nsiz + oh);
if (psiz >= 0) {
rec.append(rbuf, oh, vbuf, vsiz, psiz);
} else {
psiz = nsiz + nsiz / 2;
Record nrec(rec.child_, kbuf, ksiz, "", 0, psiz);
char* nbuf = nrec.serialize();
oh = (int32_t)sizevarnum(nsiz) - 1;
psiz = (int64_t)psiz - (int64_t)(nsiz + oh);
rec.concatenate(nbuf, rec.vbuf_, rec.vsiz_, vbuf, vsiz, psiz);
delete[] rbuf;
*entp = nbuf;
}
return;
}
entp = (char**)rbuf;
rbuf = rec.child_;
}
Record nrec(NULL, kbuf, ksiz, vbuf, vsiz, 0);
*entp = nrec.serialize();
count_++;
}
/**
* Remove a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @return true on success, or false on failure.
* @note If no record corresponds to the key, false is returned.
*/
bool remove(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];