2014-03-17 02:01:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Common/HashTable/Hash.h>
|
|
|
|
#include <DB/Common/HashTable/HashTable.h>
|
|
|
|
#include <DB/Common/HashTable/HashTableAllocator.h>
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/IO/ReadBuffer.h>
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
#include <DB/IO/VarInt.h>
|
|
|
|
|
2016-12-08 04:56:52 +00:00
|
|
|
/** NOTE HashSet could only be used for memmoveable (position independent) types.
|
|
|
|
* Example: std::string is not position independent in libstdc++ with C++11 ABI or in libc++.
|
|
|
|
* Also, key must be of type, that zero bytes is compared equals to zero key.
|
|
|
|
*/
|
|
|
|
|
2014-03-17 02:01:03 +00:00
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename Key,
|
2014-05-10 02:42:45 +00:00
|
|
|
typename TCell,
|
2014-03-17 02:01:03 +00:00
|
|
|
typename Hash = DefaultHash<Key>,
|
2014-05-03 16:03:49 +00:00
|
|
|
typename Grower = HashTableGrower<>,
|
2014-03-17 02:01:03 +00:00
|
|
|
typename Allocator = HashTableAllocator
|
|
|
|
>
|
2014-05-10 02:42:45 +00:00
|
|
|
class HashSetTable : public HashTable<Key, TCell, Hash, Grower, Allocator>
|
2014-03-17 02:01:03 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
using Self = HashSetTable<Key, TCell, Hash, Grower, Allocator>;
|
|
|
|
using Cell = TCell;
|
2014-03-17 02:01:03 +00:00
|
|
|
|
|
|
|
void merge(const Self & rhs)
|
|
|
|
{
|
2014-04-28 01:48:24 +00:00
|
|
|
if (!this->hasZero() && rhs.hasZero())
|
2014-03-17 02:01:03 +00:00
|
|
|
{
|
2014-04-28 01:48:24 +00:00
|
|
|
this->setHasZero();
|
2014-03-17 02:01:03 +00:00
|
|
|
++this->m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rhs.grower.bufSize(); ++i)
|
2014-04-28 01:48:24 +00:00
|
|
|
if (!rhs.buf[i].isZero(*this))
|
|
|
|
this->insert(Cell::getKey(rhs.buf[i].getValue()));
|
2014-03-17 02:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void readAndMerge(DB::ReadBuffer & rb)
|
|
|
|
{
|
2014-04-28 01:48:24 +00:00
|
|
|
Cell::State::read(rb);
|
|
|
|
|
2014-03-17 02:01:03 +00:00
|
|
|
size_t new_size = 0;
|
|
|
|
DB::readVarUInt(new_size, rb);
|
|
|
|
|
|
|
|
this->resize(new_size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < new_size; ++i)
|
|
|
|
{
|
|
|
|
Cell x;
|
|
|
|
x.read(rb);
|
2014-04-28 01:48:24 +00:00
|
|
|
this->insert(Cell::getKey(x.getValue()));
|
2014-03-17 02:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-05-10 02:42:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Key, typename Hash, typename TState = HashTableNoState>
|
|
|
|
struct HashSetCellWithSavedHash : public HashTableCell<Key, Hash, TState>
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
using Base = HashTableCell<Key, Hash, TState>;
|
2014-05-10 02:42:45 +00:00
|
|
|
|
|
|
|
size_t saved_hash;
|
|
|
|
|
|
|
|
HashSetCellWithSavedHash() : Base() {}
|
|
|
|
HashSetCellWithSavedHash(const Key & key_, const typename Base::State & state) : Base(key_, state) {}
|
|
|
|
|
|
|
|
bool keyEquals(const Key & key_) const { return this->key == key_; }
|
2014-11-09 08:39:18 +00:00
|
|
|
bool keyEquals(const Key & key_, size_t hash_) const { return saved_hash == hash_ && this->key == key_; }
|
2014-05-10 02:42:45 +00:00
|
|
|
|
|
|
|
void setHash(size_t hash_value) { saved_hash = hash_value; }
|
|
|
|
size_t getHash(const Hash & hash) const { return saved_hash; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename Key,
|
|
|
|
typename Hash = DefaultHash<Key>,
|
|
|
|
typename Grower = HashTableGrower<>,
|
|
|
|
typename Allocator = HashTableAllocator
|
|
|
|
>
|
|
|
|
using HashSet = HashSetTable<Key, HashTableCell<Key, Hash>, Hash, Grower, Allocator>;
|
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename Key,
|
|
|
|
typename Hash = DefaultHash<Key>,
|
|
|
|
typename Grower = HashTableGrower<>,
|
|
|
|
typename Allocator = HashTableAllocator
|
|
|
|
>
|
|
|
|
using HashSetWithSavedHash = HashSetTable<Key, HashSetCellWithSavedHash<Key, Hash>, Hash, Grower, Allocator>;
|