| 1 | #include <iostream> |
| 2 | |
| 3 | #define DBMS_HASH_MAP_DEBUG_RESIZES |
| 4 | #define DBMS_HASH_MAP_COUNT_COLLISIONS |
| 5 | |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <Core/Types.h> |
| 14 | #include <Common/Exception.h> |
| 15 | |
| 16 | #include <IO/ReadHelpers.h> |
| 17 | |
| 18 | #include <common/StringRef.h> |
| 19 | |
| 20 | #include <Common/HashTable/HashMap.h> |
| 21 | |
| 22 | |
| 23 | template |
| 24 | < |
| 25 | typename Key, |
| 26 | typename Mapped, |
| 27 | typename Hash = DefaultHash<Key>, |
| 28 | typename Grower = HashTableGrower<>, |
| 29 | typename Allocator = HashTableAllocator |
| 30 | > |
| 31 | class HashMapWithDump : public HashMap<Key, Mapped, Hash, Grower, Allocator> |
| 32 | { |
| 33 | public: |
| 34 | void dump() const |
| 35 | { |
| 36 | for (size_t i = 0; i < this->grower.bufSize(); ++i) |
| 37 | { |
| 38 | if (this->buf[i].isZero(*this)) |
| 39 | std::cerr << "[ ]" ; |
| 40 | else |
| 41 | std::cerr << '[' << this->buf[i].getValue().first.data << ", " << this->buf[i].getValue().second << ']'; |
| 42 | } |
| 43 | std::cerr << std::endl; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | struct SimpleHash |
| 49 | { |
| 50 | size_t operator() (UInt64 x) const { return x; } |
| 51 | size_t operator() (StringRef x) const { return DB::parse<UInt64>(x.data); } |
| 52 | }; |
| 53 | |
| 54 | struct Grower : public HashTableGrower<2> |
| 55 | { |
| 56 | void increaseSize() |
| 57 | { |
| 58 | ++size_degree; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | int main(int, char **) |
| 63 | { |
| 64 | using Map = HashMapWithDump< |
| 65 | StringRef, |
| 66 | UInt64, |
| 67 | SimpleHash, |
| 68 | Grower, |
| 69 | HashTableAllocatorWithStackMemory<4 * 24>>; |
| 70 | |
| 71 | Map map; |
| 72 | |
| 73 | map.dump(); |
| 74 | std::cerr << "size: " << map.size() << std::endl; |
| 75 | map[StringRef("1" , 1)] = 1; |
| 76 | map.dump(); |
| 77 | std::cerr << "size: " << map.size() << std::endl; |
| 78 | map[StringRef("9" , 1)] = 1; |
| 79 | map.dump(); |
| 80 | std::cerr << "size: " << map.size() << std::endl; |
| 81 | std::cerr << "Collisions: " << map.getCollisions() << std::endl; |
| 82 | map[StringRef("3" , 1)] = 2; |
| 83 | map.dump(); |
| 84 | std::cerr << "size: " << map.size() << std::endl; |
| 85 | std::cerr << "Collisions: " << map.getCollisions() << std::endl; |
| 86 | |
| 87 | for (auto x : map) |
| 88 | std::cerr << x.getKey().toString() << " -> " << x.getMapped() << std::endl; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |