/usr/include/kcmap.h is in libkyotocabinet-dev 1.2.76-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 | /*************************************************************************************************
* 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];
char** entp = buckets_ + bidx;
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) {
*entp = rec.child_;
delete[] rbuf;
count_--;
return true;
}
entp = (char**)rbuf;
rbuf = rec.child_;
}
return false;
}
/**
* Retrieve the value of a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @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 corresponding record, or NULL on failure.
*/
const char* get(const char* kbuf, size_t ksiz, size_t* sp) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && sp);
size_t bidx = hash_record(kbuf, ksiz) % bnum_;
char* rbuf = buckets_[bidx];
while (rbuf) {
Record rec(rbuf);
if (rec.ksiz_ == ksiz && !std::memcmp(rec.kbuf_, kbuf, ksiz)) {
*sp = rec.vsiz_;
return rec.vbuf_;
}
rbuf = rec.child_;
}
return NULL;
}
/**
* Remove all records.
*/
void clear() {
_assert_(true);
if (count_ < 1) return;
for (size_t i = 0; i < bnum_; i++) {
char* rbuf = buckets_[i];
while (rbuf) {
Record rec(rbuf);
char* child = rec.child_;
delete[] rbuf;
rbuf = child;
}
buckets_[i] = NULL;
}
count_ = 0;
}
/**
* Get the number of records.
* @return the number of records.
*/
size_t count() {
_assert_(true);
return count_;
}
/**
* Get the hash value of a record.
* @param kbuf the pointer to the key region.
* @param ksiz the size of the key region.
* @return the hash value.
*/
static size_t hash_record(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ);
return hashmurmur(kbuf, ksiz);
}
private:
/**
* Record data.
*/
struct Record {
/** constructor */
Record(char* child, const char* kbuf, uint64_t ksiz,
const char* vbuf, uint64_t vsiz, uint64_t psiz) :
child_(child), kbuf_(kbuf), ksiz_(ksiz), vbuf_(vbuf), vsiz_(vsiz), psiz_(psiz) {
_assert_(kbuf && ksiz <= MEMMAXSIZ && vbuf && vsiz <= MEMMAXSIZ && psiz <= MEMMAXSIZ);
}
/** constructor */
Record(const char* rbuf) :
child_(NULL), kbuf_(NULL), ksiz_(0), vbuf_(NULL), vsiz_(0), psiz_(0) {
_assert_(rbuf);
deserialize(rbuf);
}
/** overwrite the buffer */
void overwrite(char* rbuf, const char* vbuf, size_t vsiz, size_t psiz) {
_assert_(rbuf && vbuf && vsiz <= MEMMAXSIZ && psiz <= MEMMAXSIZ);
char* wp = rbuf + sizeof(child_) + sizevarnum(ksiz_) + ksiz_;
wp += writevarnum(wp, vsiz);
std::memcpy(wp, vbuf, vsiz);
wp += vsiz;
writevarnum(wp, psiz);
}
/** append a value */
void append(char* rbuf, int32_t oh, const char* vbuf, size_t vsiz, size_t psiz) {
_assert_(rbuf && vbuf && vsiz <= MEMMAXSIZ && psiz <= MEMMAXSIZ);
char* wp = rbuf + sizeof(child_) + sizevarnum(ksiz_) + ksiz_;
if (oh > 0) {
char* pv = wp + sizevarnum(vsiz_);
std::memmove(pv + oh, pv, vsiz_);
wp += writevarnum(wp, vsiz_ + vsiz);
wp = pv + oh + vsiz_;
} else {
wp += writevarnum(wp, vsiz_ + vsiz);
wp += vsiz_;
}
std::memcpy(wp, vbuf, vsiz);
wp += vsiz;
writevarnum(wp, psiz);
}
/** concatenate two values */
void concatenate(char* rbuf, const char* ovbuf, size_t ovsiz,
const char* nvbuf, size_t nvsiz, size_t psiz) {
_assert_(rbuf && ovbuf && ovsiz <= MEMMAXSIZ && nvbuf && nvsiz <= MEMMAXSIZ);
char* wp = rbuf + sizeof(child_) + sizevarnum(ksiz_) + ksiz_;
wp += writevarnum(wp, ovsiz + nvsiz);
std::memcpy(wp, ovbuf, ovsiz);
wp += ovsiz;
std::memcpy(wp, nvbuf, nvsiz);
wp += nvsiz;
writevarnum(wp, psiz);
}
/** serialize data into a buffer */
char* serialize() {
_assert_(true);
uint64_t rsiz = sizeof(child_) + sizevarnum(ksiz_) + ksiz_ + sizevarnum(vsiz_) + vsiz_ +
sizevarnum(psiz_) + psiz_;
char* rbuf = new char[rsiz];
char* wp = rbuf;
*(char**)wp = child_;
wp += sizeof(child_);
wp += writevarnum(wp, ksiz_);
std::memcpy(wp, kbuf_, ksiz_);
wp += ksiz_;
wp += writevarnum(wp, vsiz_);
std::memcpy(wp, vbuf_, vsiz_);
wp += vsiz_;
writevarnum(wp, psiz_);
return rbuf;
}
/** deserialize a buffer into object */
void deserialize(const char* rbuf) {
_assert_(rbuf);
const char* rp = rbuf;
child_ = *(char**)rp;
rp += sizeof(child_);
rp += readvarnum(rp, sizeof(ksiz_), &ksiz_);
kbuf_ = rp;
rp += ksiz_;
rp += readvarnum(rp, sizeof(vsiz_), &vsiz_);
vbuf_ = rp;
rp += vsiz_;
readvarnum(rp, sizeof(psiz_), &psiz_);
}
char* child_; ///< region of the child
const char* kbuf_; ///< region of the key
uint64_t ksiz_; ///< size of the key
const char* vbuf_; ///< region of the value
uint64_t vsiz_; ///< size of the key
uint64_t psiz_; ///< size of the padding
};
/**
* Comparator for records.
*/
struct RecordComparator {
/** comparing operator */
bool operator ()(char* const& abuf, char* const& bbuf) {
const char* akbuf = abuf + sizeof(char**);
uint64_t aksiz;
akbuf += readvarnum(akbuf, sizeof(aksiz), &aksiz);
const char* bkbuf = bbuf + sizeof(char**);
uint64_t bksiz;
bkbuf += readvarnum(bkbuf, sizeof(bksiz), &bksiz);
uint64_t msiz = aksiz < bksiz ? aksiz : bksiz;
for (uint64_t i = 0; i < msiz; i++) {
if (((uint8_t*)akbuf)[i] != ((uint8_t*)bkbuf)[i])
return ((uint8_t*)akbuf)[i] < ((uint8_t*)bkbuf)[i];
}
return (int32_t)aksiz < (int32_t)bksiz;
}
};
/**
* Initialize fields.
*/
void initialize() {
_assert_(true);
if (bnum_ >= MAPZMAPBNUM) {
buckets_ = (char**)mapalloc(sizeof(*buckets_) * bnum_);
} else {
buckets_ = new char*[bnum_];
for (size_t i = 0; i < bnum_; i++) {
buckets_[i] = NULL;
}
}
}
/**
* Clean up fields.
*/
void destroy() {
_assert_(true);
for (size_t i = 0; i < bnum_; i++) {
char* rbuf = buckets_[i];
while (rbuf) {
Record rec(rbuf);
char* child = rec.child_;
delete[] rbuf;
rbuf = child;
}
}
if (bnum_ >= MAPZMAPBNUM) {
mapfree(buckets_);
} else {
delete[] buckets_;
}
}
/** Dummy constructor to forbid the use. */
TinyHashMap(const TinyHashMap&);
/** Dummy Operator to forbid the use. */
TinyHashMap& operator =(const TinyHashMap&);
/** The bucket array. */
char** buckets_;
/** The number of buckets. */
size_t bnum_;
/** The number of records. */
size_t count_;
};
/**
* Memory-saving string array list.
*/
class TinyArrayList {
public:
/**
* Default constructor.
*/
explicit TinyArrayList() : recs_() {
_assert_(true);
}
/**
* Destructor.
*/
~TinyArrayList() {
_assert_(true);
std::deque<char*>::iterator it = recs_.begin();
std::deque<char*>::iterator itend = recs_.end();
while (it != itend) {
delete[] *it;
++it;
}
}
/**
* Insert a record at the bottom of the list.
* @param buf the pointer to the record region.
* @param size the size of the record region.
*/
void push(const char* buf, size_t size) {
_assert_(buf && size <= MEMMAXSIZ);
size_t rsiz = sizevarnum(size) + size;
char* rbuf = new char[rsiz];
char* wp = rbuf + writevarnum(rbuf, size);
std::memcpy(wp, buf, size);
recs_.push_back(rbuf);
}
/**
* Remove a record at the bottom of the list.
* @return true if the operation success, or false if there is no record in the list.
*/
bool pop() {
_assert_(true);
if (recs_.empty()) return false;
delete[] recs_.back();
recs_.pop_back();
return true;
}
/**
* Insert a record at the top of the list.
* @param buf the pointer to the record region.
* @param size the size of the record region.
*/
void unshift(const char* buf, size_t size) {
_assert_(buf && size <= MEMMAXSIZ);
size_t rsiz = sizevarnum(size) + size;
char* rbuf = new char[rsiz];
char* wp = rbuf + writevarnum(rbuf, size);
std::memcpy(wp, buf, size);
recs_.push_front(rbuf);
}
/**
* Remove a record at the top of the list.
* @return true if the operation success, or false if there is no record in the list.
*/
bool shift() {
_assert_(true);
if (recs_.empty()) return false;
delete[] recs_.front();
recs_.pop_front();
return true;
}
/**
* Insert a record at the position of the given index of the list.
* @param buf the pointer to the record region.
* @param size the size of the record region.
* @param idx the index of the position. It must be equal to or less than the number of
* records.
*/
void insert(const char* buf, size_t size, size_t idx) {
size_t rsiz = sizevarnum(size) + size;
char* rbuf = new char[rsiz];
char* wp = rbuf + writevarnum(rbuf, size);
std::memcpy(wp, buf, size);
recs_.insert(recs_.begin() + idx, rbuf);
}
/**
* Remove a record at the position of the given index of the list.
* @param idx the index of the position. It must be less than the number of records.
*/
void remove(size_t idx) {
_assert_(true);
std::deque<char*>::iterator it = recs_.begin() + idx;
delete[] *it;
recs_.erase(it);
}
/**
* Retrieve a record at the position of the given index of the list.
* @param idx the index of the position. It must be less than the number of records.
* @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 region of the retrieved record.
*/
const char* get(size_t idx, size_t* sp) {
_assert_(sp);
const char* rbuf = recs_[idx];
uint64_t rsiz;
const char* rp = rbuf + readvarnum(rbuf, sizeof(uint64_t), &rsiz);
*sp = rsiz;
return rp;
}
/**
* Remove all records.
*/
void clear() {
_assert_(true);
std::deque<char*>::iterator it = recs_.begin();
std::deque<char*>::iterator itend = recs_.end();
while (it != itend) {
delete[] *it;
++it;
}
recs_.clear();
}
/**
* Get the number of records.
* @return the number of records.
*/
size_t count() {
_assert_(true);
return recs_.size();
}
private:
/** Dummy constructor to forbid the use. */
TinyArrayList(const TinyArrayList&);
/** Dummy Operator to forbid the use. */
TinyArrayList& operator =(const TinyArrayList&);
/** The record list. */
std::deque<char*> recs_;
};
} // common namespace
#endif // duplication check
// END OF FILE
|