2014-05-02 12:49:39 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/Common/HashTable/HashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Двухуровневая хэш-таблица.
|
2014-12-27 06:29:20 +00:00
|
|
|
|
* Представляет собой 256 (или 1 << BITS_FOR_BUCKET) маленьких хэш-таблиц (bucket-ов первого уровня).
|
2014-05-02 12:49:39 +00:00
|
|
|
|
* Для определения, какую из них использовать, берётся один из байтов хэш-функции.
|
|
|
|
|
*
|
|
|
|
|
* Обычно работает чуть-чуть медленнее простой хэш-таблицы.
|
|
|
|
|
* Тем не менее, обладает преимуществами в некоторых случаях:
|
|
|
|
|
* - если надо мерджить две хэш-таблицы вместе, то это можно легко распараллелить по bucket-ам;
|
|
|
|
|
* - лаг при ресайзах размазан, так как маленькие хэш-таблицы ресайзятся по-отдельности;
|
|
|
|
|
* - по идее, ресайзы кэш-локальны в большем диапазоне размеров.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-12-26 03:00:51 +00:00
|
|
|
|
template <size_t initial_size_degree = 8>
|
|
|
|
|
struct TwoLevelHashTableGrower : public HashTableGrower<initial_size_degree>
|
|
|
|
|
{
|
|
|
|
|
/// Увеличить размер хэш-таблицы.
|
|
|
|
|
void increaseSize()
|
|
|
|
|
{
|
|
|
|
|
this->size_degree += this->size_degree >= 15 ? 1 : 2;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-02 12:49:39 +00:00
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename Key,
|
|
|
|
|
typename Cell,
|
|
|
|
|
typename Hash,
|
|
|
|
|
typename Grower,
|
2014-12-26 03:00:51 +00:00
|
|
|
|
typename Allocator, /// TODO WithStackMemory
|
2014-12-27 06:29:20 +00:00
|
|
|
|
typename ImplTable = HashTable<Key, Cell, Hash, Grower, Allocator>,
|
|
|
|
|
size_t BITS_FOR_BUCKET = 8
|
2014-05-02 12:49:39 +00:00
|
|
|
|
>
|
|
|
|
|
class TwoLevelHashTable :
|
|
|
|
|
private boost::noncopyable,
|
|
|
|
|
protected Hash /// empty base optimization
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
friend class const_iterator;
|
|
|
|
|
friend class iterator;
|
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using HashValue = size_t;
|
|
|
|
|
using Self = TwoLevelHashTable<Key, Cell, Hash, Grower, Allocator, ImplTable>;
|
2014-05-02 15:48:03 +00:00
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Impl = ImplTable;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
2014-12-27 06:29:20 +00:00
|
|
|
|
static constexpr size_t NUM_BUCKETS = 1 << BITS_FOR_BUCKET;
|
|
|
|
|
static constexpr size_t MAX_BUCKET = NUM_BUCKETS - 1;
|
|
|
|
|
|
2014-05-02 12:49:39 +00:00
|
|
|
|
size_t hash(const Key & x) const { return Hash::operator()(x); }
|
2014-12-27 06:29:20 +00:00
|
|
|
|
|
|
|
|
|
/// NOTE Плохо для хэш-таблиц больше чем на 2^32 ячеек.
|
2015-09-07 07:40:14 +00:00
|
|
|
|
static size_t getBucketFromHash(size_t hash_value) { return (hash_value >> (32 - BITS_FOR_BUCKET)) & MAX_BUCKET; }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
2014-05-19 04:53:05 +00:00
|
|
|
|
protected:
|
2014-05-02 12:49:39 +00:00
|
|
|
|
typename Impl::iterator beginOfNextNonEmptyBucket(size_t & bucket)
|
|
|
|
|
{
|
2014-05-10 20:47:12 +00:00
|
|
|
|
while (bucket != NUM_BUCKETS && impls[bucket].empty())
|
2014-05-02 12:49:39 +00:00
|
|
|
|
++bucket;
|
|
|
|
|
|
|
|
|
|
if (bucket != NUM_BUCKETS)
|
|
|
|
|
return impls[bucket].begin();
|
|
|
|
|
|
2014-05-12 03:11:45 +00:00
|
|
|
|
--bucket;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
return impls[MAX_BUCKET].end();
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typename Impl::const_iterator beginOfNextNonEmptyBucket(size_t & bucket) const
|
|
|
|
|
{
|
2014-05-10 20:47:12 +00:00
|
|
|
|
while (bucket != NUM_BUCKETS && impls[bucket].empty())
|
2014-05-02 12:49:39 +00:00
|
|
|
|
++bucket;
|
|
|
|
|
|
|
|
|
|
if (bucket != NUM_BUCKETS)
|
|
|
|
|
return impls[bucket].begin();
|
|
|
|
|
|
2014-05-12 03:11:45 +00:00
|
|
|
|
--bucket;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
return impls[MAX_BUCKET].end();
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using key_type = typename Impl::key_type;
|
|
|
|
|
using value_type = typename Impl::value_type;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
Impl impls[NUM_BUCKETS];
|
|
|
|
|
|
|
|
|
|
|
2014-12-30 11:27:58 +00:00
|
|
|
|
TwoLevelHashTable() {}
|
|
|
|
|
|
|
|
|
|
/// Скопировать данные из другой (обычной) хэш-таблицы. У неё должна быть такая же хэш-функция.
|
2014-12-30 12:58:02 +00:00
|
|
|
|
template <typename Source>
|
2014-12-30 11:27:58 +00:00
|
|
|
|
TwoLevelHashTable(const Source & src)
|
|
|
|
|
{
|
2014-12-30 12:58:02 +00:00
|
|
|
|
typename Source::const_iterator it = src.begin();
|
|
|
|
|
|
2014-12-30 18:04:53 +00:00
|
|
|
|
/// Предполагается, что нулевой ключ (хранящийся отдельно) при итерировании идёт первым.
|
2014-12-30 12:58:02 +00:00
|
|
|
|
if (it != src.end() && it.getPtr()->isZero(src))
|
2014-12-30 11:27:58 +00:00
|
|
|
|
{
|
2014-12-30 12:58:02 +00:00
|
|
|
|
insert(*it);
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; it != src.end(); ++it)
|
|
|
|
|
{
|
|
|
|
|
const Cell * cell = it.getPtr();
|
|
|
|
|
size_t hash_value = cell->getHash(src);
|
2014-12-30 11:27:58 +00:00
|
|
|
|
size_t buck = getBucketFromHash(hash_value);
|
2014-12-30 12:58:02 +00:00
|
|
|
|
impls[buck].insertUniqueNonZero(cell, hash_value);
|
2014-12-30 11:27:58 +00:00
|
|
|
|
}
|
2014-12-30 12:58:02 +00:00
|
|
|
|
}
|
2014-12-30 11:27:58 +00:00
|
|
|
|
|
|
|
|
|
|
2014-05-02 12:49:39 +00:00
|
|
|
|
class iterator
|
|
|
|
|
{
|
2014-05-10 20:47:12 +00:00
|
|
|
|
Self * container;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
size_t bucket;
|
|
|
|
|
typename Impl::iterator current_it;
|
|
|
|
|
|
|
|
|
|
friend class TwoLevelHashTable;
|
|
|
|
|
|
2014-05-10 20:47:12 +00:00
|
|
|
|
iterator(Self * container_, size_t bucket_, typename Impl::iterator current_it_)
|
|
|
|
|
: container(container_), bucket(bucket_), current_it(current_it_) {}
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
iterator() {}
|
|
|
|
|
|
2014-05-12 03:11:45 +00:00
|
|
|
|
bool operator== (const iterator & rhs) const { return bucket == rhs.bucket && current_it == rhs.current_it; }
|
|
|
|
|
bool operator!= (const iterator & rhs) const { return !(*this == rhs); }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
iterator & operator++()
|
|
|
|
|
{
|
|
|
|
|
++current_it;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
if (current_it == container->impls[bucket].end())
|
|
|
|
|
{
|
|
|
|
|
++bucket;
|
|
|
|
|
current_it = container->beginOfNextNonEmptyBucket(bucket);
|
|
|
|
|
}
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value_type & operator* () const { return *current_it; }
|
|
|
|
|
value_type * operator->() const { return &*current_it; }
|
2014-12-30 18:04:53 +00:00
|
|
|
|
|
|
|
|
|
Cell * getPtr() const { return current_it.getPtr(); }
|
2014-12-30 20:38:05 +00:00
|
|
|
|
size_t getHash() const { return current_it.getHash(); }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class const_iterator
|
|
|
|
|
{
|
2014-05-10 20:47:12 +00:00
|
|
|
|
Self * container;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
size_t bucket;
|
|
|
|
|
typename Impl::const_iterator current_it;
|
|
|
|
|
|
|
|
|
|
friend class TwoLevelHashTable;
|
|
|
|
|
|
2014-05-10 20:47:12 +00:00
|
|
|
|
const_iterator(Self * container_, size_t bucket_, typename Impl::const_iterator current_it_)
|
|
|
|
|
: container(container_), bucket(bucket_), current_it(current_it_) {}
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
const_iterator() {}
|
2014-05-10 20:47:12 +00:00
|
|
|
|
const_iterator(const iterator & rhs) : container(rhs.container), bucket(rhs.bucket), current_it(rhs.current_it) {}
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
2014-05-12 03:11:45 +00:00
|
|
|
|
bool operator== (const const_iterator & rhs) const { return bucket == rhs.bucket && current_it == rhs.current_it; }
|
|
|
|
|
bool operator!= (const const_iterator & rhs) const { return !(*this == rhs); }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
const_iterator & operator++()
|
|
|
|
|
{
|
|
|
|
|
++current_it;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
if (current_it == container->impls[bucket].end())
|
|
|
|
|
{
|
|
|
|
|
++bucket;
|
|
|
|
|
current_it = container->beginOfNextNonEmptyBucket(bucket);
|
|
|
|
|
}
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const value_type & operator* () const { return *current_it; }
|
|
|
|
|
const value_type * operator->() const { return &*current_it; }
|
2014-12-30 18:04:53 +00:00
|
|
|
|
|
|
|
|
|
const Cell * getPtr() const { return current_it.getPtr(); }
|
2014-12-30 20:38:05 +00:00
|
|
|
|
size_t getHash() const { return current_it.getHash(); }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
size_t buck = 0;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
typename Impl::const_iterator impl_it = beginOfNextNonEmptyBucket(buck);
|
|
|
|
|
return { this, buck, impl_it };
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator begin()
|
|
|
|
|
{
|
|
|
|
|
size_t buck = 0;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
typename Impl::iterator impl_it = beginOfNextNonEmptyBucket(buck);
|
|
|
|
|
return { this, buck, impl_it };
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 20:47:12 +00:00
|
|
|
|
const_iterator end() const { return { this, MAX_BUCKET, impls[MAX_BUCKET].end() }; }
|
|
|
|
|
iterator end() { return { this, MAX_BUCKET, impls[MAX_BUCKET].end() }; }
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Вставить значение. В случае хоть сколько-нибудь сложных значений, лучше используйте функцию emplace.
|
2014-12-30 11:27:58 +00:00
|
|
|
|
std::pair<iterator, bool> ALWAYS_INLINE insert(const value_type & x)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(Cell::getKey(x));
|
|
|
|
|
|
|
|
|
|
std::pair<iterator, bool> res;
|
|
|
|
|
emplace(Cell::getKey(x), res.first, res.second, hash_value);
|
2014-12-30 18:04:53 +00:00
|
|
|
|
|
|
|
|
|
if (res.second)
|
|
|
|
|
res.first.getPtr()->setMapped(x);
|
|
|
|
|
|
2014-05-02 12:49:39 +00:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Вставить ключ,
|
|
|
|
|
* вернуть итератор на позицию, которую можно использовать для placement new значения,
|
|
|
|
|
* а также флаг - был ли вставлен новый ключ.
|
|
|
|
|
*
|
|
|
|
|
* Вы обязаны сделать placement new значения, если был вставлен новый ключ,
|
|
|
|
|
* так как при уничтожении хэш-таблицы для него будет вызываться деструктор!
|
|
|
|
|
*
|
|
|
|
|
* Пример использования:
|
|
|
|
|
*
|
|
|
|
|
* Map::iterator it;
|
|
|
|
|
* bool inserted;
|
|
|
|
|
* map.emplace(key, it, inserted);
|
|
|
|
|
* if (inserted)
|
|
|
|
|
* new(&it->second) Mapped(value);
|
|
|
|
|
*/
|
2014-12-30 11:27:58 +00:00
|
|
|
|
void ALWAYS_INLINE emplace(Key x, iterator & it, bool & inserted)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(x);
|
|
|
|
|
emplace(x, it, inserted, hash_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// То же самое, но с заранее вычисленным значением хэш-функции.
|
2014-12-30 11:27:58 +00:00
|
|
|
|
void ALWAYS_INLINE emplace(Key x, iterator & it, bool & inserted, size_t hash_value)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
{
|
2014-05-12 03:11:45 +00:00
|
|
|
|
size_t buck = getBucketFromHash(hash_value);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
typename Impl::iterator impl_it;
|
2014-12-25 21:25:43 +00:00
|
|
|
|
impls[buck].emplace(x, impl_it, inserted, hash_value);
|
2014-05-10 20:47:12 +00:00
|
|
|
|
it = iterator(this, buck, impl_it);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-12-30 11:27:58 +00:00
|
|
|
|
iterator ALWAYS_INLINE find(Key x)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(x);
|
2014-05-12 03:11:45 +00:00
|
|
|
|
size_t buck = getBucketFromHash(hash_value);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
2014-12-25 21:25:43 +00:00
|
|
|
|
typename Impl::iterator found = impls[buck].find(x, hash_value);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
return found != impls[buck].end()
|
2014-05-10 20:47:12 +00:00
|
|
|
|
? iterator(this, buck, found)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
: end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-12-30 11:27:58 +00:00
|
|
|
|
const_iterator ALWAYS_INLINE find(Key x) const
|
2014-05-02 12:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(x);
|
2014-05-12 03:11:45 +00:00
|
|
|
|
size_t buck = getBucketFromHash(hash_value);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
|
2014-12-25 21:25:43 +00:00
|
|
|
|
typename Impl::const_iterator found = impls[buck].find(x, hash_value);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
return found != impls[buck].end()
|
2014-05-10 20:47:12 +00:00
|
|
|
|
? const_iterator(this, buck, found)
|
2014-05-02 12:49:39 +00:00
|
|
|
|
: end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write(DB::WriteBuffer & wb) const
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
impls[i].write(wb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeText(DB::WriteBuffer & wb) const
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (i != 0)
|
|
|
|
|
DB::writeChar(',', wb);
|
|
|
|
|
impls[i].writeText(wb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void read(DB::ReadBuffer & rb)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
impls[i].read(rb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void readText(DB::ReadBuffer & rb)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (i != 0)
|
2016-02-07 08:42:21 +00:00
|
|
|
|
DB::assertChar(',', rb);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
impls[i].readText(rb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
2014-05-19 04:53:05 +00:00
|
|
|
|
size_t res = 0;
|
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
res += impls[i].size();
|
|
|
|
|
|
|
|
|
|
return res;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
|
{
|
2014-05-19 04:53:05 +00:00
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
if (!impls[i].empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t getBufferSizeInBytes() const
|
|
|
|
|
{
|
2014-05-19 04:53:05 +00:00
|
|
|
|
size_t res = 0;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
for (size_t i = 0; i < NUM_BUCKETS; ++i)
|
|
|
|
|
res += impls[i].getBufferSizeInBytes();
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|