2011-12-12 10:05:35 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
#include <malloc.h>
|
2013-09-15 10:53:10 +00:00
|
|
|
|
#include <math.h>
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
2013-10-24 12:53:24 +00:00
|
|
|
|
#include <utility>
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
|
2013-05-05 16:23:54 +00:00
|
|
|
|
#include <Yandex/likely.h>
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
2012-09-22 02:49:28 +00:00
|
|
|
|
#include <stats/IntHash.h>
|
|
|
|
|
|
2011-12-19 04:26:27 +00:00
|
|
|
|
#include <DB/Core/Types.h>
|
2013-01-20 23:47:23 +00:00
|
|
|
|
#include <DB/Core/Exception.h>
|
2013-02-03 18:07:55 +00:00
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
#ifdef DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <statdaemons/Stopwatch.h>
|
|
|
|
|
#endif
|
2011-12-19 04:26:27 +00:00
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Очень простая хэш-таблица. Предназначена для быстрой агрегации. Есть только необходимый минимум возможностей.
|
2011-12-19 02:00:40 +00:00
|
|
|
|
* Требования:
|
|
|
|
|
* - Key и Mapped - position independent типы (для перемещения значений которых достаточно сделать memcpy).
|
|
|
|
|
*
|
|
|
|
|
* Желательно, чтобы Key был числом, или маленьким агрегатом (типа UInt128).
|
|
|
|
|
*
|
2011-12-12 10:05:35 +00:00
|
|
|
|
* Сценарий работы:
|
|
|
|
|
* - вставлять в хэш-таблицу значения;
|
|
|
|
|
* - проитерироваться по имеющимся в ней значениям.
|
|
|
|
|
*
|
|
|
|
|
* Open addressing.
|
2011-12-19 07:00:15 +00:00
|
|
|
|
* Linear probing (подходит, если хэш функция хорошая!).
|
2011-12-12 10:05:35 +00:00
|
|
|
|
* Значение с нулевым ключём хранится отдельно.
|
|
|
|
|
* Удаления элементов нет.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2011-12-19 07:00:15 +00:00
|
|
|
|
/** Хэш функции, которые лучше чем тривиальная функция std::tr1::hash.
|
2011-12-19 07:14:00 +00:00
|
|
|
|
* (при агрегации по идентификатору посетителя, прирост производительности более чем в 5 раз)
|
2011-12-19 07:00:15 +00:00
|
|
|
|
*/
|
|
|
|
|
template <typename T> struct default_hash;
|
|
|
|
|
|
2013-10-24 12:53:24 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
inline size_t default_hash_64(T key)
|
2011-12-19 07:00:15 +00:00
|
|
|
|
{
|
2013-10-24 12:53:24 +00:00
|
|
|
|
union
|
2011-12-19 07:00:15 +00:00
|
|
|
|
{
|
2013-10-24 12:53:24 +00:00
|
|
|
|
T in;
|
|
|
|
|
UInt64 out;
|
|
|
|
|
} u;
|
|
|
|
|
u.out = 0;
|
|
|
|
|
u.in = key;
|
|
|
|
|
return intHash32<0>(u.out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_HASH_64(T) \
|
|
|
|
|
template <> struct default_hash<T>\
|
|
|
|
|
{\
|
|
|
|
|
size_t operator() (T key) const\
|
|
|
|
|
{\
|
|
|
|
|
return default_hash_64<T>(key);\
|
|
|
|
|
}\
|
2011-12-19 07:00:15 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-24 12:53:24 +00:00
|
|
|
|
DEFAULT_HASH_64(UInt8)
|
|
|
|
|
DEFAULT_HASH_64(UInt16)
|
|
|
|
|
DEFAULT_HASH_64(UInt32)
|
|
|
|
|
DEFAULT_HASH_64(UInt64)
|
|
|
|
|
DEFAULT_HASH_64(Int8)
|
|
|
|
|
DEFAULT_HASH_64(Int16)
|
|
|
|
|
DEFAULT_HASH_64(Int32)
|
|
|
|
|
DEFAULT_HASH_64(Int64)
|
|
|
|
|
DEFAULT_HASH_64(Float32)
|
|
|
|
|
DEFAULT_HASH_64(Float64)
|
|
|
|
|
|
|
|
|
|
#undef DEFAULT_HASH_64
|
|
|
|
|
|
2011-12-19 07:00:15 +00:00
|
|
|
|
|
2011-12-19 02:00:40 +00:00
|
|
|
|
/** Способ проверить, что ключ нулевой,
|
2013-01-20 23:47:23 +00:00
|
|
|
|
* а также способ установить значение ключа в ноль.
|
2013-01-21 06:09:56 +00:00
|
|
|
|
* При этом, нулевой ключ всё-равно должен быть представлен только нулевыми байтами
|
|
|
|
|
* (кроме, возможно, мусора из-за выравнивания).
|
2011-12-19 02:00:40 +00:00
|
|
|
|
*/
|
|
|
|
|
template <typename T> struct default_zero_traits
|
2011-12-12 10:05:35 +00:00
|
|
|
|
{
|
|
|
|
|
static inline bool check(T x) { return 0 == x; }
|
|
|
|
|
static inline void set(T & x) { x = 0; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-01-20 23:47:23 +00:00
|
|
|
|
/** Описание, как хэш-таблица будет расти.
|
|
|
|
|
*/
|
|
|
|
|
struct default_growth_traits
|
|
|
|
|
{
|
|
|
|
|
/** Изначально выделить кусок памяти для 64K элементов.
|
|
|
|
|
* Уменьшите значение для лучшей кэш-локальности в случае маленького количества уникальных ключей.
|
|
|
|
|
*/
|
|
|
|
|
static const int INITIAL_SIZE_DEGREE = 16;
|
|
|
|
|
|
|
|
|
|
/** Степень роста хэш таблицы, пока не превышен порог размера. (В 4 раза.)
|
|
|
|
|
*/
|
|
|
|
|
static const int FAST_GROWTH_DEGREE = 2;
|
|
|
|
|
|
|
|
|
|
/** Порог размера, после которого степень роста уменьшается (до роста в 2 раза) - 8 миллионов элементов.
|
|
|
|
|
* После этого порога, максимально возможный оверхед по памяти будет всего лишь в 4, а не в 8 раз.
|
|
|
|
|
*/
|
|
|
|
|
static const int GROWTH_CHANGE_THRESHOLD = 23;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
template
|
|
|
|
|
<
|
|
|
|
|
typename Key,
|
|
|
|
|
typename Mapped,
|
2011-12-19 07:00:15 +00:00
|
|
|
|
typename Hash = default_hash<Key>,
|
2011-12-19 02:00:40 +00:00
|
|
|
|
typename ZeroTraits = default_zero_traits<Key>,
|
2013-01-20 23:47:23 +00:00
|
|
|
|
typename GrowthTraits = default_growth_traits
|
2011-12-12 10:05:35 +00:00
|
|
|
|
>
|
|
|
|
|
class HashMap : private boost::noncopyable
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
friend class const_iterator;
|
|
|
|
|
friend class iterator;
|
|
|
|
|
|
2011-12-19 02:00:40 +00:00
|
|
|
|
typedef std::pair<Key, Mapped> Value; /// Без const Key для простоты.
|
2011-12-12 10:05:35 +00:00
|
|
|
|
typedef size_t HashValue;
|
2013-01-20 23:47:23 +00:00
|
|
|
|
typedef HashMap<Key, Mapped, Hash, ZeroTraits, GrowthTraits> Self;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
size_t m_size; /// Количество элементов
|
|
|
|
|
UInt8 size_degree; /// Размер таблицы в виде степени двух
|
|
|
|
|
bool has_zero; /// Хэш-таблица содержит элемент со значением ключа = 0.
|
2011-12-19 04:26:27 +00:00
|
|
|
|
Value * buf; /// Кусок памяти для всех элементов кроме элемента с ключём 0.
|
|
|
|
|
char zero_value_storage[sizeof(Value)]; /// Кусок памяти для элемента с ключём 0.
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
Hash hash;
|
|
|
|
|
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
mutable size_t collisions;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
inline size_t buf_size() const { return 1 << size_degree; }
|
2013-01-20 23:47:23 +00:00
|
|
|
|
inline size_t buf_size_bytes() const { return buf_size() * sizeof(Value); }
|
2011-12-12 10:05:35 +00:00
|
|
|
|
inline size_t max_fill() const { return 1 << (size_degree - 1); }
|
|
|
|
|
inline size_t mask() const { return buf_size() - 1; }
|
|
|
|
|
inline size_t place(HashValue x) const { return x & mask(); }
|
|
|
|
|
|
2011-12-19 04:26:27 +00:00
|
|
|
|
inline Value * zero_value() { return reinterpret_cast<Value*>(zero_value_storage); }
|
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
2013-01-20 23:47:23 +00:00
|
|
|
|
/// Увеличить размер буфера в 2 ^ N раз
|
2011-12-12 10:05:35 +00:00
|
|
|
|
void resize()
|
|
|
|
|
{
|
2013-01-20 23:47:23 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
|
Stopwatch watch;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
2013-01-20 23:47:23 +00:00
|
|
|
|
size_t old_size = buf_size();
|
|
|
|
|
size_t old_size_bytes = buf_size_bytes();
|
|
|
|
|
|
|
|
|
|
size_degree += size_degree >= GrowthTraits::GROWTH_CHANGE_THRESHOLD
|
|
|
|
|
? 1
|
|
|
|
|
: GrowthTraits::FAST_GROWTH_DEGREE;
|
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
/// Расширим пространство.
|
|
|
|
|
buf = reinterpret_cast<Value *>(realloc(reinterpret_cast<void *>(buf), buf_size_bytes()));
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
if (NULL == buf)
|
|
|
|
|
throwFromErrno("HashMap: Cannot realloc.", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
|
|
|
|
|
|
|
|
|
/// Очистим новый кусок памяти.
|
|
|
|
|
memset(buf + old_size, 0, buf_size_bytes() - old_size_bytes);
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
/** Теперь некоторые элементы может потребоваться переместить на новое место.
|
|
|
|
|
* Элемент может остаться на месте, или переместиться в новое место "справа",
|
|
|
|
|
* или переместиться левее по цепочке разрешения коллизий, из-за того, что элементы левее него были перемещены в новое место "справа".
|
|
|
|
|
*/
|
|
|
|
|
for (size_t i = 0; i < old_size; ++i)
|
|
|
|
|
if (!ZeroTraits::check(buf[i].first))
|
|
|
|
|
reinsert(buf[i]);
|
|
|
|
|
|
|
|
|
|
#ifdef DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
|
watch.stop();
|
|
|
|
|
std::cerr << std::fixed << std::setprecision(3)
|
|
|
|
|
<< "Resize from " << old_size << " to " << buf_size() << " took " << watch.elapsedSeconds() << " sec."
|
|
|
|
|
<< std::endl;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Вставить в новый буфер значение, которое было в старом буфере.
|
|
|
|
|
* Используется при увеличении размера буфера.
|
|
|
|
|
*/
|
2013-01-20 23:47:23 +00:00
|
|
|
|
void reinsert(Value & x)
|
2011-12-12 10:05:35 +00:00
|
|
|
|
{
|
|
|
|
|
size_t place_value = place(hash(x.first));
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
/// Если элемент на своём месте.
|
|
|
|
|
if (&x == &buf[place_value])
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/// Вычисление нового места, с учётом цепочки разрешения коллизий.
|
2011-12-12 10:05:35 +00:00
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first))
|
|
|
|
|
{
|
2011-12-19 07:00:15 +00:00
|
|
|
|
++place_value;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
place_value &= mask();
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
|
|
|
|
/// Копирование на новое место и зануление старого.
|
2011-12-19 02:00:40 +00:00
|
|
|
|
memcpy(&buf[place_value], &x, sizeof(x));
|
2013-01-20 23:47:23 +00:00
|
|
|
|
ZeroTraits::set(x.first);
|
|
|
|
|
|
|
|
|
|
/// Потом на старое место могут переместиться элементы, которые раньше были в коллизии с этим.
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2011-12-19 02:00:40 +00:00
|
|
|
|
typedef Key key_type;
|
|
|
|
|
typedef Mapped mapped_type;
|
|
|
|
|
typedef Value value_type;
|
|
|
|
|
|
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
HashMap() :
|
|
|
|
|
m_size(0),
|
2013-01-20 23:47:23 +00:00
|
|
|
|
size_degree(GrowthTraits::INITIAL_SIZE_DEGREE),
|
2011-12-12 10:05:35 +00:00
|
|
|
|
has_zero(false)
|
|
|
|
|
{
|
2011-12-19 04:26:27 +00:00
|
|
|
|
ZeroTraits::set(zero_value()->first);
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
buf = reinterpret_cast<Value *>(calloc(buf_size_bytes(), 1));
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
if (NULL == buf)
|
|
|
|
|
throwFromErrno("HashMap: Cannot calloc.", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
2013-10-24 12:53:24 +00:00
|
|
|
|
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
collisions = 0;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~HashMap()
|
|
|
|
|
{
|
2013-11-02 03:56:17 +00:00
|
|
|
|
if (!__has_trivial_destructor(Key) || !__has_trivial_destructor(Mapped))
|
|
|
|
|
for (iterator it = begin(); it != end(); ++it)
|
|
|
|
|
it->~Value();
|
2013-01-20 23:47:23 +00:00
|
|
|
|
|
2013-02-03 18:07:55 +00:00
|
|
|
|
free(reinterpret_cast<void *>(buf));
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class iterator
|
|
|
|
|
{
|
|
|
|
|
Self * container;
|
|
|
|
|
Value * ptr;
|
|
|
|
|
|
|
|
|
|
friend class HashMap;
|
|
|
|
|
|
|
|
|
|
iterator(Self * container_, Value * ptr_) : container(container_), ptr(ptr_) {}
|
|
|
|
|
|
|
|
|
|
public:
|
2011-12-19 02:00:40 +00:00
|
|
|
|
iterator() {}
|
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
bool operator== (const iterator & rhs) const { return ptr == rhs.ptr; }
|
|
|
|
|
bool operator!= (const iterator & rhs) const { return ptr != rhs.ptr; }
|
|
|
|
|
|
|
|
|
|
iterator & operator++()
|
|
|
|
|
{
|
2011-12-19 02:00:40 +00:00
|
|
|
|
if (unlikely(ZeroTraits::check(ptr->first)))
|
2011-12-12 10:05:35 +00:00
|
|
|
|
ptr = container->buf;
|
|
|
|
|
else
|
|
|
|
|
++ptr;
|
|
|
|
|
|
2012-03-05 06:04:18 +00:00
|
|
|
|
while (ptr < container->buf + container->buf_size() && ZeroTraits::check(ptr->first))
|
2011-12-12 10:05:35 +00:00
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Value & operator* () const { return *ptr; }
|
|
|
|
|
Value * operator->() const { return ptr; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class const_iterator
|
|
|
|
|
{
|
|
|
|
|
const Self * container;
|
|
|
|
|
const Value * ptr;
|
|
|
|
|
|
|
|
|
|
friend class HashMap;
|
|
|
|
|
|
2012-08-23 22:27:10 +00:00
|
|
|
|
const_iterator(const Self * container_, const Value * ptr_) : container(container_), ptr(ptr_) {}
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2011-12-19 02:00:40 +00:00
|
|
|
|
const_iterator() {}
|
2011-12-12 10:05:35 +00:00
|
|
|
|
const_iterator(const iterator & rhs) : container(rhs.container), ptr(rhs.ptr) {}
|
|
|
|
|
|
|
|
|
|
bool operator== (const const_iterator & rhs) const { return ptr == rhs.ptr; }
|
|
|
|
|
bool operator!= (const const_iterator & rhs) const { return ptr != rhs.ptr; }
|
|
|
|
|
|
|
|
|
|
const_iterator & operator++()
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(ZeroTraits::check(ptr->first)))
|
|
|
|
|
ptr = container->buf;
|
|
|
|
|
else
|
|
|
|
|
++ptr;
|
|
|
|
|
|
2012-03-05 06:04:18 +00:00
|
|
|
|
while (ptr < container->buf + container->buf_size() && ZeroTraits::check(ptr->first))
|
2011-12-12 10:05:35 +00:00
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Value & operator* () const { return *ptr; }
|
|
|
|
|
const Value * operator->() const { return ptr; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
if (has_zero)
|
2011-12-19 04:26:27 +00:00
|
|
|
|
return const_iterator(this, zero_value());
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
const Value * ptr = buf;
|
2012-03-05 06:04:18 +00:00
|
|
|
|
while (ptr < buf + buf_size() && ZeroTraits::check(ptr->first))
|
2011-12-12 10:05:35 +00:00
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
|
|
return const_iterator(this, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator begin()
|
|
|
|
|
{
|
|
|
|
|
if (has_zero)
|
2011-12-19 04:26:27 +00:00
|
|
|
|
return iterator(this, zero_value());
|
2011-12-12 10:05:35 +00:00
|
|
|
|
|
|
|
|
|
Value * ptr = buf;
|
2012-03-05 06:04:18 +00:00
|
|
|
|
while (ptr < buf + buf_size() && ZeroTraits::check(ptr->first))
|
2011-12-12 10:05:35 +00:00
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
|
|
return iterator(this, ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const_iterator end() const { return const_iterator(this, buf + buf_size()); }
|
|
|
|
|
iterator end() { return iterator(this, buf + buf_size()); }
|
|
|
|
|
|
|
|
|
|
|
2011-12-19 02:00:40 +00:00
|
|
|
|
/// Вставить значение. В случае хоть сколько-нибудь сложных значений, лучше используйте функцию emplace.
|
2011-12-12 10:05:35 +00:00
|
|
|
|
std::pair<iterator, bool> insert(const Value & x)
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x.first))
|
|
|
|
|
{
|
|
|
|
|
if (!has_zero)
|
|
|
|
|
{
|
|
|
|
|
++m_size;
|
|
|
|
|
has_zero = true;
|
2011-12-19 04:26:27 +00:00
|
|
|
|
zero_value()->second = x.second;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
return std::make_pair(begin(), true);
|
|
|
|
|
}
|
|
|
|
|
return std::make_pair(begin(), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash(x.first));
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x.first)
|
|
|
|
|
{
|
2011-12-19 07:00:15 +00:00
|
|
|
|
++place_value;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
place_value &= mask();
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator res(this, &buf[place_value]);
|
|
|
|
|
|
2011-12-19 04:26:27 +00:00
|
|
|
|
if (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first == x.first)
|
2011-12-12 10:05:35 +00:00
|
|
|
|
return std::make_pair(res, false);
|
|
|
|
|
|
|
|
|
|
buf[place_value] = x;
|
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
|
|
if (unlikely(m_size > max_fill()))
|
|
|
|
|
{
|
|
|
|
|
resize();
|
|
|
|
|
return std::make_pair(find(x.first), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_pair(res, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-12-19 02:00:40 +00:00
|
|
|
|
/** Вставить ключ,
|
|
|
|
|
* вернуть итератор на позицию, которую можно использовать для placement new значения,
|
|
|
|
|
* а также флаг - был ли вставлен новый ключ.
|
|
|
|
|
*
|
2011-12-19 04:26:27 +00:00
|
|
|
|
* Вы обязаны сделать placement new значения, если был вставлен новый ключ,
|
|
|
|
|
* так как при уничтожении хэш-таблицы для него будет вызываться деструктор!
|
|
|
|
|
*
|
2011-12-19 02:00:40 +00:00
|
|
|
|
* Пример использования:
|
|
|
|
|
*
|
|
|
|
|
* Map::iterator it;
|
|
|
|
|
* bool inserted;
|
|
|
|
|
* map.emplace(key, it, inserted);
|
|
|
|
|
* if (inserted)
|
|
|
|
|
* new(&it->second) Value(value);
|
|
|
|
|
*/
|
|
|
|
|
void emplace(Key x, iterator & it, bool & inserted)
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x))
|
|
|
|
|
{
|
|
|
|
|
if (!has_zero)
|
|
|
|
|
{
|
|
|
|
|
++m_size;
|
|
|
|
|
has_zero = true;
|
|
|
|
|
inserted = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
inserted = false;
|
2011-12-19 04:26:27 +00:00
|
|
|
|
|
|
|
|
|
it = begin();
|
2011-12-19 02:00:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash(x));
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x)
|
|
|
|
|
{
|
2011-12-19 07:00:15 +00:00
|
|
|
|
++place_value;
|
2011-12-19 02:00:40 +00:00
|
|
|
|
place_value &= mask();
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
2011-12-19 02:00:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it = iterator(this, &buf[place_value]);
|
|
|
|
|
|
2011-12-19 04:26:27 +00:00
|
|
|
|
if (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first == x)
|
2011-12-19 02:00:40 +00:00
|
|
|
|
{
|
|
|
|
|
inserted = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-19 04:26:27 +00:00
|
|
|
|
new(&buf[place_value].first) Key(x);
|
2011-12-19 02:00:40 +00:00
|
|
|
|
inserted = true;
|
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
|
|
if (unlikely(m_size > max_fill()))
|
|
|
|
|
{
|
|
|
|
|
resize();
|
|
|
|
|
it = find(x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-15 10:53:10 +00:00
|
|
|
|
/// То же самое, но с заранее вычисленным значением хэш-функции.
|
|
|
|
|
void emplace(Key x, iterator & it, bool & inserted, size_t hash_value)
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x))
|
|
|
|
|
{
|
|
|
|
|
if (!has_zero)
|
|
|
|
|
{
|
|
|
|
|
++m_size;
|
|
|
|
|
has_zero = true;
|
|
|
|
|
inserted = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
inserted = false;
|
|
|
|
|
|
|
|
|
|
it = begin();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash_value);
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x)
|
|
|
|
|
{
|
|
|
|
|
++place_value;
|
|
|
|
|
place_value &= mask();
|
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it = iterator(this, &buf[place_value]);
|
|
|
|
|
|
|
|
|
|
if (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first == x)
|
|
|
|
|
{
|
|
|
|
|
inserted = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new(&buf[place_value].first) Key(x);
|
|
|
|
|
inserted = true;
|
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
|
|
if (unlikely(m_size > max_fill()))
|
|
|
|
|
{
|
|
|
|
|
resize();
|
|
|
|
|
it = find(x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
iterator find(Key x)
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x))
|
|
|
|
|
return has_zero ? begin() : end();
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash(x));
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x)
|
|
|
|
|
{
|
2011-12-19 07:00:15 +00:00
|
|
|
|
++place_value;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
place_value &= mask();
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !ZeroTraits::check(buf[place_value].first) ? iterator(this, &buf[place_value]) : end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const_iterator find(Key x) const
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x))
|
|
|
|
|
return has_zero ? begin() : end();
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash(x.first));
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x)
|
|
|
|
|
{
|
2011-12-19 07:00:15 +00:00
|
|
|
|
++place_value;
|
2011-12-12 10:05:35 +00:00
|
|
|
|
place_value &= mask();
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !ZeroTraits::check(buf[place_value].first) ? const_iterator(this, &buf[place_value]) : end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
|
|
|
|
return m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
|
{
|
|
|
|
|
return 0 == m_size;
|
|
|
|
|
}
|
2011-12-19 07:00:15 +00:00
|
|
|
|
|
2013-10-24 12:53:24 +00:00
|
|
|
|
Mapped & operator[](Key x)
|
|
|
|
|
{
|
|
|
|
|
if (ZeroTraits::check(x))
|
|
|
|
|
{
|
|
|
|
|
if (!has_zero)
|
|
|
|
|
{
|
|
|
|
|
++m_size;
|
|
|
|
|
has_zero = true;
|
|
|
|
|
}
|
|
|
|
|
return zero_value()->first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t place_value = place(hash(x));
|
|
|
|
|
while (!ZeroTraits::check(buf[place_value].first) && buf[place_value].first != x)
|
|
|
|
|
{
|
|
|
|
|
++place_value;
|
|
|
|
|
place_value &= mask();
|
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
++collisions;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ZeroTraits::check(buf[place_value].first))
|
|
|
|
|
return buf[place_value].second;
|
|
|
|
|
|
|
|
|
|
new(&buf[place_value].first) Key(x);
|
|
|
|
|
new(&buf[place_value].second) Value();
|
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
|
|
if (unlikely(m_size > max_fill()))
|
|
|
|
|
{
|
|
|
|
|
resize();
|
|
|
|
|
return (*this)[x];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf[place_value].second;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-19 07:00:15 +00:00
|
|
|
|
#ifdef DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
size_t getCollisions() const
|
|
|
|
|
{
|
|
|
|
|
return collisions;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-12-12 10:05:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-09-15 10:53:10 +00:00
|
|
|
|
|
2011-12-12 10:05:35 +00:00
|
|
|
|
}
|