|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <vec/common/hash_table/hash.h> |
| 4 | +#include <vec/common/hash_table/hash_table.h> |
| 5 | +#include <vec/common/hash_table/hash_table_allocator.h> |
| 6 | + |
| 7 | +// #include <IO/WriteBuffer.h> |
| 8 | +// #include <IO/WriteHelpers.h> |
| 9 | +// #include <IO/ReadBuffer.h> |
| 10 | +// #include <IO/ReadHelpers.h> |
| 11 | +// #include <IO/VarInt.h> |
| 12 | + |
| 13 | +/** NOTE HashSet could only be used for memmoveable (position independent) types. |
| 14 | + * Example: std::string is not position independent in libstdc++ with C++11 ABI or in libc++. |
| 15 | + * Also, key must be of type, that zero bytes is compared equals to zero key. |
| 16 | + */ |
| 17 | + |
| 18 | +template <typename Key, typename TCell, typename Hash = DefaultHash<Key>, |
| 19 | + typename Grower = HashTableGrower<>, typename Allocator = HashTableAllocator> |
| 20 | +class HashSetTable : public HashTable<Key, TCell, Hash, Grower, Allocator> { |
| 21 | +public: |
| 22 | + using Self = HashSetTable; |
| 23 | + using Cell = TCell; |
| 24 | + |
| 25 | + using Base = HashTable<Key, TCell, Hash, Grower, Allocator>; |
| 26 | + using typename Base::LookupResult; |
| 27 | + |
| 28 | + void merge(const Self& rhs) { |
| 29 | + if (!this->hasZero() && rhs.hasZero()) { |
| 30 | + this->setHasZero(); |
| 31 | + ++this->m_size; |
| 32 | + } |
| 33 | + |
| 34 | + for (size_t i = 0; i < rhs.grower.bufSize(); ++i) |
| 35 | + if (!rhs.buf[i].isZero(*this)) this->insert(Cell::getKey(rhs.buf[i].getValue())); |
| 36 | + } |
| 37 | + |
| 38 | + // void readAndMerge(DB::ReadBuffer & rb) |
| 39 | + // { |
| 40 | + // Cell::State::read(rb); |
| 41 | + |
| 42 | + // size_t new_size = 0; |
| 43 | + // DB::readVarUInt(new_size, rb); |
| 44 | + |
| 45 | + // this->resize(new_size); |
| 46 | + |
| 47 | + // for (size_t i = 0; i < new_size; ++i) |
| 48 | + // { |
| 49 | + // Cell x; |
| 50 | + // x.read(rb); |
| 51 | + // this->insert(Cell::getKey(x.getValue())); |
| 52 | + // } |
| 53 | + // } |
| 54 | +}; |
| 55 | + |
| 56 | +template <typename Key, typename Hash, typename TState = HashTableNoState> |
| 57 | +struct HashSetCellWithSavedHash : public HashTableCell<Key, Hash, TState> { |
| 58 | + using Base = HashTableCell<Key, Hash, TState>; |
| 59 | + |
| 60 | + size_t saved_hash; |
| 61 | + |
| 62 | + HashSetCellWithSavedHash() : Base() {} |
| 63 | + HashSetCellWithSavedHash(const Key& key_, const typename Base::State& state) |
| 64 | + : Base(key_, state) {} |
| 65 | + |
| 66 | + bool keyEquals(const Key& key_) const { return this->key == key_; } |
| 67 | + bool keyEquals(const Key& key_, size_t hash_) const { |
| 68 | + return saved_hash == hash_ && this->key == key_; |
| 69 | + } |
| 70 | + bool keyEquals(const Key& key_, size_t hash_, const typename Base::State&) const { |
| 71 | + return keyEquals(key_, hash_); |
| 72 | + } |
| 73 | + |
| 74 | + void setHash(size_t hash_value) { saved_hash = hash_value; } |
| 75 | + size_t getHash(const Hash& /*hash_function*/) const { return saved_hash; } |
| 76 | +}; |
| 77 | + |
| 78 | +template <typename Key, typename Hash, typename State> |
| 79 | +ALWAYS_INLINE inline auto lookupResultGetKey(HashSetCellWithSavedHash<Key, Hash, State>* cell) { |
| 80 | + return &cell->key; |
| 81 | +} |
| 82 | + |
| 83 | +template <typename Key, typename Hash, typename State> |
| 84 | +ALWAYS_INLINE inline void* lookupResultGetMapped(HashSetCellWithSavedHash<Key, Hash, State>*) { |
| 85 | + return nullptr; |
| 86 | +} |
| 87 | + |
| 88 | +template <typename Key, typename Hash = DefaultHash<Key>, typename Grower = HashTableGrower<>, |
| 89 | + typename Allocator = HashTableAllocator> |
| 90 | +using HashSet = HashSetTable<Key, HashTableCell<Key, Hash>, Hash, Grower, Allocator>; |
| 91 | + |
| 92 | +template <typename Key, typename Hash = DefaultHash<Key>, typename Grower = HashTableGrower<>, |
| 93 | + typename Allocator = HashTableAllocator> |
| 94 | +using HashSetWithSavedHash = |
| 95 | + HashSetTable<Key, HashSetCellWithSavedHash<Key, Hash>, Hash, Grower, Allocator>; |
0 commit comments