2016-06-07 08:23:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/HashTable/HashMap.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
2018-11-22 21:19:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int NO_AVAILABLE_DATA;
|
2018-11-22 21:19:58 +00:00
|
|
|
extern const int INCORRECT_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** Replacement of the hash table for a small number (<10) of keys.
|
|
|
|
* Implemented as an array with linear search.
|
|
|
|
* The array is located inside the object.
|
|
|
|
* The interface is a subset of the HashTable interface.
|
2016-06-07 08:23:15 +00:00
|
|
|
*
|
2017-05-07 20:25:26 +00:00
|
|
|
* Insert is possible only if the `full` method returns false.
|
|
|
|
* With an unknown number of different keys,
|
|
|
|
* you should check if the table is not full,
|
|
|
|
* and do a `fallback` in this case (for example, use a real hash table).
|
2016-06-07 08:23:15 +00:00
|
|
|
*/
|
|
|
|
template
|
|
|
|
<
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Key,
|
|
|
|
typename Cell,
|
|
|
|
size_t capacity
|
2016-06-07 08:23:15 +00:00
|
|
|
>
|
|
|
|
class SmallTable :
|
2017-04-01 07:20:54 +00:00
|
|
|
private boost::noncopyable,
|
|
|
|
protected Cell::State
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class const_iterator;
|
|
|
|
friend class iterator;
|
|
|
|
friend class Reader;
|
|
|
|
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = SmallTable;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
size_t m_size = 0; /// Amount of elements.
|
|
|
|
Cell buf[capacity]; /// A piece of memory for all elements.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Find a cell with the same key or an empty cell, starting from the specified position and then by the collision resolution chain.
|
2017-04-01 07:20:54 +00:00
|
|
|
const Cell * ALWAYS_INLINE findCell(const Key & x) const
|
|
|
|
{
|
|
|
|
const Cell * it = buf;
|
|
|
|
while (it < buf + m_size)
|
|
|
|
{
|
|
|
|
if (it->keyEquals(x))
|
|
|
|
break;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell * ALWAYS_INLINE findCell(const Key & x)
|
|
|
|
{
|
|
|
|
Cell * it = buf;
|
|
|
|
while (it < buf + m_size)
|
|
|
|
{
|
|
|
|
if (it->keyEquals(x))
|
|
|
|
break;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using key_type = Key;
|
2019-10-29 15:16:51 +00:00
|
|
|
using mapped_type = typename Cell::mapped_type;
|
2017-04-01 07:20:54 +00:00
|
|
|
using value_type = typename Cell::value_type;
|
2019-10-29 15:16:51 +00:00
|
|
|
using cell_type = Cell;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
class Reader final : private Cell::State
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Reader(DB::ReadBuffer & in_)
|
|
|
|
: in(in_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader(const Reader &) = delete;
|
|
|
|
Reader & operator=(const Reader &) = delete;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool next()
|
|
|
|
{
|
|
|
|
if (!is_initialized)
|
|
|
|
{
|
|
|
|
Cell::State::read(in);
|
|
|
|
DB::readVarUInt(size, in);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size > capacity)
|
2018-11-22 21:19:58 +00:00
|
|
|
throw DB::Exception("Illegal size", DB::ErrorCodes::INCORRECT_DATA);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
is_initialized = true;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (read_count == size)
|
|
|
|
{
|
|
|
|
is_eof = true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
cell.read(in);
|
|
|
|
++read_count;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
inline const value_type & get() const
|
|
|
|
{
|
|
|
|
if (!is_initialized || is_eof)
|
|
|
|
throw DB::Exception("No available data", DB::ErrorCodes::NO_AVAILABLE_DATA);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return cell.getValue();
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
private:
|
|
|
|
DB::ReadBuffer & in;
|
|
|
|
Cell cell;
|
|
|
|
size_t read_count = 0;
|
|
|
|
size_t size;
|
|
|
|
bool is_eof = false;
|
|
|
|
bool is_initialized = false;
|
|
|
|
};
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
Self * container;
|
|
|
|
Cell * ptr;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class SmallTable;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
public:
|
|
|
|
iterator() {}
|
|
|
|
iterator(Self * container_, Cell * ptr_) : container(container_), ptr(ptr_) {}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator== (const iterator & rhs) const { return ptr == rhs.ptr; }
|
|
|
|
bool operator!= (const iterator & rhs) const { return ptr != rhs.ptr; }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
iterator & operator++()
|
|
|
|
{
|
|
|
|
++ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2019-02-28 09:35:38 +00:00
|
|
|
Cell & operator* () const { return *ptr; }
|
|
|
|
Cell * operator->() const { return ptr; }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Cell * getPtr() const { return ptr; }
|
|
|
|
};
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
class const_iterator
|
|
|
|
{
|
|
|
|
const Self * container;
|
|
|
|
const Cell * ptr;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class SmallTable;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
public:
|
|
|
|
const_iterator() {}
|
|
|
|
const_iterator(const Self * container_, const Cell * ptr_) : container(container_), ptr(ptr_) {}
|
|
|
|
const_iterator(const iterator & rhs) : container(rhs.container), ptr(rhs.ptr) {}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator== (const const_iterator & rhs) const { return ptr == rhs.ptr; }
|
|
|
|
bool operator!= (const const_iterator & rhs) const { return ptr != rhs.ptr; }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const_iterator & operator++()
|
|
|
|
{
|
|
|
|
++ptr;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2019-02-28 09:35:38 +00:00
|
|
|
const Cell & operator* () const { return *ptr; }
|
|
|
|
const Cell * operator->() const { return ptr; }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const Cell * getPtr() const { return ptr; }
|
|
|
|
};
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const_iterator begin() const { return iteratorTo(buf); }
|
|
|
|
iterator begin() { return iteratorTo(buf); }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const_iterator end() const { return iteratorTo(buf + m_size); }
|
|
|
|
iterator end() { return iteratorTo(buf + m_size); }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const_iterator iteratorTo(const Cell * ptr) const { return const_iterator(this, ptr); }
|
|
|
|
iterator iteratorTo(Cell * ptr) { return iterator(this, ptr); }
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
2017-05-07 20:25:26 +00:00
|
|
|
/** The table is full.
|
|
|
|
* You can not insert anything into the full table.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
bool full()
|
|
|
|
{
|
|
|
|
return m_size == capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Insert the value. In the case of any more complex values, it is better to use the `emplace` function.
|
2017-04-01 07:20:54 +00:00
|
|
|
std::pair<iterator, bool> ALWAYS_INLINE insert(const value_type & x)
|
|
|
|
{
|
|
|
|
std::pair<iterator, bool> res;
|
|
|
|
|
|
|
|
emplace(Cell::getKey(x), res.first, res.second);
|
|
|
|
|
|
|
|
if (res.second)
|
|
|
|
res.first.ptr->setMapped(x);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/** Insert the key,
|
2017-05-10 04:00:19 +00:00
|
|
|
* return an iterator to a position that can be used for `placement new` of value,
|
2017-05-07 20:25:26 +00:00
|
|
|
* as well as the flag - whether a new key was inserted.
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-05-09 19:07:35 +00:00
|
|
|
* You have to make `placement new` of value if you inserted a new key,
|
2017-05-07 20:25:26 +00:00
|
|
|
* since when destroying a hash table, a destructor will be called for it!
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-05-07 20:25:26 +00:00
|
|
|
* Example usage:
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
|
|
|
* Map::iterator it;
|
|
|
|
* bool inserted;
|
|
|
|
* map.emplace(key, it, inserted);
|
|
|
|
* if (inserted)
|
|
|
|
* new(&it->second) Mapped(value);
|
|
|
|
*/
|
|
|
|
void ALWAYS_INLINE emplace(Key x, iterator & it, bool & inserted)
|
|
|
|
{
|
|
|
|
Cell * res = findCell(x);
|
|
|
|
it = iteratorTo(res);
|
|
|
|
inserted = res == buf + m_size;
|
|
|
|
if (inserted)
|
|
|
|
{
|
|
|
|
new(res) Cell(x, *this);
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Same, but return false if it's full.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool ALWAYS_INLINE tryEmplace(Key x, iterator & it, bool & inserted)
|
|
|
|
{
|
|
|
|
Cell * res = findCell(x);
|
|
|
|
it = iteratorTo(res);
|
|
|
|
inserted = res == buf + m_size;
|
|
|
|
if (inserted)
|
|
|
|
{
|
|
|
|
if (res == buf + capacity)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
new(res) Cell(x, *this);
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-07 20:25:26 +00:00
|
|
|
/// Copy the cell from another hash table. It is assumed that there was no such key in the table yet.
|
2017-04-01 07:20:54 +00:00
|
|
|
void ALWAYS_INLINE insertUnique(const Cell * cell)
|
|
|
|
{
|
|
|
|
memcpy(&buf[m_size], cell, sizeof(*cell));
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ALWAYS_INLINE insertUnique(Key x)
|
|
|
|
{
|
|
|
|
new(&buf[m_size]) Cell(x, *this);
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterator ALWAYS_INLINE find(Key x) { return iteratorTo(findCell(x)); }
|
|
|
|
const_iterator ALWAYS_INLINE find(Key x) const { return iteratorTo(findCell(x)); }
|
|
|
|
|
|
|
|
|
|
|
|
void write(DB::WriteBuffer & wb) const
|
|
|
|
{
|
|
|
|
Cell::State::write(wb);
|
|
|
|
DB::writeVarUInt(m_size, wb);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
|
|
buf[i].write(wb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeText(DB::WriteBuffer & wb) const
|
|
|
|
{
|
|
|
|
Cell::State::writeText(wb);
|
|
|
|
DB::writeText(m_size, wb);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
|
|
{
|
|
|
|
DB::writeChar(',', wb);
|
|
|
|
buf[i].writeText(wb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void read(DB::ReadBuffer & rb)
|
|
|
|
{
|
|
|
|
Cell::State::read(rb);
|
|
|
|
|
|
|
|
m_size = 0;
|
|
|
|
|
|
|
|
size_t new_size = 0;
|
|
|
|
DB::readVarUInt(new_size, rb);
|
|
|
|
|
|
|
|
if (new_size > capacity)
|
2018-11-22 21:19:58 +00:00
|
|
|
throw DB::Exception("Illegal size", DB::ErrorCodes::INCORRECT_DATA);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < new_size; ++i)
|
|
|
|
buf[i].read(rb);
|
|
|
|
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void readText(DB::ReadBuffer & rb)
|
|
|
|
{
|
|
|
|
Cell::State::readText(rb);
|
|
|
|
|
|
|
|
m_size = 0;
|
|
|
|
|
|
|
|
size_t new_size = 0;
|
|
|
|
DB::readText(new_size, rb);
|
|
|
|
|
|
|
|
if (new_size > capacity)
|
2018-11-22 21:19:58 +00:00
|
|
|
throw DB::Exception("Illegal size", DB::ErrorCodes::INCORRECT_DATA);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < new_size; ++i)
|
|
|
|
{
|
|
|
|
DB::assertChar(',', rb);
|
|
|
|
buf[i].readText(rb);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return 0 == m_size;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void clear()
|
|
|
|
{
|
2017-12-25 04:01:46 +00:00
|
|
|
if (!std::is_trivially_destructible_v<Cell>)
|
2017-04-01 07:20:54 +00:00
|
|
|
for (iterator it = begin(); it != end(); ++it)
|
|
|
|
it.ptr->~Cell();
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
m_size = 0;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getBufferSizeInBytes() const
|
|
|
|
{
|
|
|
|
return sizeof(buf);
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct HashUnused {};
|
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Key,
|
|
|
|
size_t capacity
|
2016-06-07 08:23:15 +00:00
|
|
|
>
|
|
|
|
using SmallSet = SmallTable<Key, HashTableCell<Key, HashUnused>, capacity>;
|
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Key,
|
|
|
|
typename Cell,
|
|
|
|
size_t capacity
|
2016-06-07 08:23:15 +00:00
|
|
|
>
|
|
|
|
class SmallMapTable : public SmallTable<Key, Cell, capacity>
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using key_type = Key;
|
2019-10-29 15:16:51 +00:00
|
|
|
using mapped_type = typename Cell::mapped_type;
|
2017-04-01 07:20:54 +00:00
|
|
|
using value_type = typename Cell::value_type;
|
2019-10-29 15:16:51 +00:00
|
|
|
using cell_type = Cell;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
mapped_type & ALWAYS_INLINE operator[](Key x)
|
|
|
|
{
|
|
|
|
typename SmallMapTable::iterator it;
|
|
|
|
bool inserted;
|
|
|
|
this->emplace(x, it, inserted);
|
2019-10-29 15:16:51 +00:00
|
|
|
new (&it->getMapped()) mapped_type();
|
|
|
|
return it->getMapped();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Key,
|
|
|
|
typename Mapped,
|
|
|
|
size_t capacity
|
2016-06-07 08:23:15 +00:00
|
|
|
>
|
|
|
|
using SmallMap = SmallMapTable<Key, HashMapCell<Key, Mapped, HashUnused>, capacity>;
|