2014-05-02 12:49:39 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/Common/HashTable/HashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Двухуровневая хэш-таблица.
|
|
|
|
|
* Представляет собой 256 маленьких хэш-таблиц (bucket-ов первого уровня).
|
|
|
|
|
* Для определения, какую из них использовать, берётся один из байтов хэш-функции.
|
|
|
|
|
*
|
|
|
|
|
* Обычно работает чуть-чуть медленнее простой хэш-таблицы.
|
|
|
|
|
* Тем не менее, обладает преимуществами в некоторых случаях:
|
|
|
|
|
* - если надо мерджить две хэш-таблицы вместе, то это можно легко распараллелить по bucket-ам;
|
|
|
|
|
* - лаг при ресайзах размазан, так как маленькие хэш-таблицы ресайзятся по-отдельности;
|
|
|
|
|
* - по идее, ресайзы кэш-локальны в большем диапазоне размеров.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename Key,
|
|
|
|
|
typename Cell,
|
|
|
|
|
typename Hash,
|
|
|
|
|
typename Grower,
|
|
|
|
|
typename Allocator,
|
|
|
|
|
typename ImplTable = HashTable<Key, Cell, Hash, Grower, Allocator>
|
|
|
|
|
>
|
|
|
|
|
class TwoLevelHashTable :
|
|
|
|
|
private boost::noncopyable,
|
|
|
|
|
protected Hash /// empty base optimization
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
friend class const_iterator;
|
|
|
|
|
friend class iterator;
|
|
|
|
|
|
|
|
|
|
typedef size_t HashValue;
|
2014-05-11 20:48:39 +00:00
|
|
|
|
typedef TwoLevelHashTable<Key, Cell, Hash, Grower, Allocator, ImplTable> Self;
|
2014-05-02 15:48:03 +00:00
|
|
|
|
public:
|
2014-05-02 12:49:39 +00:00
|
|
|
|
typedef ImplTable Impl;
|
|
|
|
|
|
|
|
|
|
size_t hash(const Key & x) const { return Hash::operator()(x); }
|
2014-05-12 03:11:45 +00:00
|
|
|
|
size_t getBucketFromHash(size_t hash_value) const { return hash_value >> 56; }
|
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:
|
|
|
|
|
typedef typename Impl::key_type key_type;
|
|
|
|
|
typedef typename Impl::value_type value_type;
|
|
|
|
|
|
|
|
|
|
static constexpr size_t NUM_BUCKETS = 256;
|
2014-05-10 20:47:12 +00:00
|
|
|
|
static constexpr size_t MAX_BUCKET = NUM_BUCKETS - 1;
|
2014-05-02 12:49:39 +00:00
|
|
|
|
Impl impls[NUM_BUCKETS];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
std::pair<iterator, bool> insert(const value_type & x)
|
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(Cell::getKey(x));
|
|
|
|
|
|
|
|
|
|
std::pair<iterator, bool> res;
|
|
|
|
|
emplace(Cell::getKey(x), res.first, res.second, hash_value);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Вставить ключ,
|
|
|
|
|
* вернуть итератор на позицию, которую можно использовать для placement new значения,
|
|
|
|
|
* а также флаг - был ли вставлен новый ключ.
|
|
|
|
|
*
|
|
|
|
|
* Вы обязаны сделать placement new значения, если был вставлен новый ключ,
|
|
|
|
|
* так как при уничтожении хэш-таблицы для него будет вызываться деструктор!
|
|
|
|
|
*
|
|
|
|
|
* Пример использования:
|
|
|
|
|
*
|
|
|
|
|
* Map::iterator it;
|
|
|
|
|
* bool inserted;
|
|
|
|
|
* map.emplace(key, it, inserted);
|
|
|
|
|
* if (inserted)
|
|
|
|
|
* new(&it->second) Mapped(value);
|
|
|
|
|
*/
|
|
|
|
|
void emplace(Key x, iterator & it, bool & inserted)
|
|
|
|
|
{
|
|
|
|
|
size_t hash_value = hash(x);
|
|
|
|
|
emplace(x, it, inserted, hash_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// То же самое, но с заранее вычисленным значением хэш-функции.
|
|
|
|
|
void emplace(Key x, iterator & it, bool & inserted, size_t hash_value)
|
|
|
|
|
{
|
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;
|
|
|
|
|
impls[buck].emplace(x, impl_it, inserted);
|
2014-05-10 20:47:12 +00:00
|
|
|
|
it = iterator(this, buck, impl_it);
|
2014-05-02 12:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator find(Key x)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
typename Impl::iterator found = impls[buck].find(x);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator find(Key x) const
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
typename Impl::const_iterator found = impls[buck].find(x);
|
|
|
|
|
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)
|
|
|
|
|
DB::assertString(",", rb);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|