2022-02-17 19:31:58 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2022-02-17 19:31:58 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-03-09 14:29:47 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2022-02-17 19:31:58 +01:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2022-02-23 14:27:19 +01:00
|
|
|
|
2022-03-09 15:00:22 +01:00
|
|
|
namespace detail {
|
2022-03-09 16:16:59 +01:00
|
|
|
uint32 normalize_flat_hash_table_size(uint32 size);
|
|
|
|
uint32 get_random_flat_hash_table_bucket(uint32 bucket_count_mask);
|
2022-03-09 15:00:22 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2022-02-17 19:31:58 +01:00
|
|
|
template <class NodeT, class HashT, class EqT>
|
|
|
|
class FlatHashTable {
|
2022-02-24 22:24:27 +01:00
|
|
|
static constexpr uint32 INVALID_BUCKET = 0xFFFFFFFF;
|
2022-02-23 20:13:40 +01:00
|
|
|
|
2022-03-09 13:17:29 +01:00
|
|
|
void allocate_nodes(uint32 size) {
|
2022-02-23 20:13:40 +01:00
|
|
|
DCHECK(size >= 8);
|
|
|
|
DCHECK((size & (size - 1)) == 0);
|
2022-03-09 13:17:29 +01:00
|
|
|
CHECK(size <= min(static_cast<uint32>(1) << 29, static_cast<uint32>(0x7FFFFFFF / sizeof(NodeT))));
|
|
|
|
nodes_ = new NodeT[size];
|
|
|
|
// used_node_count_ = 0;
|
|
|
|
bucket_count_mask_ = size - 1;
|
|
|
|
bucket_count_ = size;
|
|
|
|
begin_bucket_ = INVALID_BUCKET;
|
2022-02-23 20:13:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 12:30:02 +01:00
|
|
|
static void clear_nodes(NodeT *nodes) {
|
2022-03-09 13:17:29 +01:00
|
|
|
delete[] nodes;
|
2022-02-23 20:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2022-02-23 14:34:42 +01:00
|
|
|
using KeyT = typename NodeT::public_key_type;
|
|
|
|
using key_type = typename NodeT::public_key_type;
|
|
|
|
using value_type = typename NodeT::public_type;
|
2022-02-17 19:31:58 +01:00
|
|
|
|
2022-03-13 21:14:18 +01:00
|
|
|
// TODO use EndSentinel for end() after switching to C++17
|
|
|
|
// struct EndSentinel {};
|
2022-03-12 11:26:03 +01:00
|
|
|
|
2022-02-17 19:31:58 +01:00
|
|
|
struct Iterator {
|
2022-03-09 16:16:59 +01:00
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2022-02-17 19:31:58 +01:00
|
|
|
using difference_type = std::ptrdiff_t;
|
2023-04-19 00:40:12 +02:00
|
|
|
using value_type = typename NodeT::public_type;
|
2022-02-21 00:22:01 +01:00
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
2022-02-17 19:31:58 +01:00
|
|
|
|
|
|
|
Iterator &operator++() {
|
2022-02-24 22:04:32 +01:00
|
|
|
DCHECK(it_ != nullptr);
|
2022-02-17 19:31:58 +01:00
|
|
|
do {
|
2022-02-24 22:04:32 +01:00
|
|
|
if (unlikely(++it_ == end_)) {
|
|
|
|
it_ = begin_;
|
|
|
|
}
|
|
|
|
if (unlikely(it_ == start_)) {
|
|
|
|
it_ = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
} while (it_->empty());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
reference operator*() {
|
|
|
|
return it_->get_public();
|
|
|
|
}
|
2022-03-11 19:38:48 +01:00
|
|
|
const value_type &operator*() const {
|
|
|
|
return it_->get_public();
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
pointer operator->() {
|
2022-03-11 11:34:36 +01:00
|
|
|
return &it_->get_public();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-11 19:38:48 +01:00
|
|
|
const value_type *operator->() const {
|
|
|
|
return &it_->get_public();
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:29:47 +01:00
|
|
|
NodeT *get() {
|
|
|
|
return it_;
|
|
|
|
}
|
|
|
|
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator==(const Iterator &other) const {
|
|
|
|
DCHECK(other.it_ == nullptr);
|
2022-03-12 11:26:03 +01:00
|
|
|
return it_ == nullptr;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator!=(const Iterator &other) const {
|
|
|
|
DCHECK(other.it_ == nullptr);
|
2022-03-12 11:26:03 +01:00
|
|
|
return it_ != nullptr;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator() = default;
|
2022-03-09 13:44:14 +01:00
|
|
|
Iterator(NodeT *it, NodeT *begin, NodeT *end) : it_(it), begin_(begin), start_(it), end_(end) {
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-24 22:04:32 +01:00
|
|
|
NodeT *it_ = nullptr;
|
|
|
|
NodeT *begin_ = nullptr;
|
|
|
|
NodeT *start_ = nullptr;
|
|
|
|
NodeT *end_ = nullptr;
|
2022-02-17 19:31:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstIterator {
|
2022-03-09 16:16:59 +01:00
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2022-02-17 19:31:58 +01:00
|
|
|
using difference_type = std::ptrdiff_t;
|
2023-04-19 00:40:12 +02:00
|
|
|
using value_type = typename NodeT::public_type;
|
2022-02-17 19:31:58 +01:00
|
|
|
using pointer = const value_type *;
|
|
|
|
using reference = const value_type &;
|
|
|
|
|
|
|
|
ConstIterator &operator++() {
|
|
|
|
++it_;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-03-11 19:38:48 +01:00
|
|
|
reference operator*() const {
|
2022-02-17 19:31:58 +01:00
|
|
|
return *it_;
|
|
|
|
}
|
2022-03-11 19:38:48 +01:00
|
|
|
pointer operator->() const {
|
2022-02-17 19:31:58 +01:00
|
|
|
return &*it_;
|
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator==(const ConstIterator &other) const {
|
|
|
|
return it_ == other.it_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator!=(const ConstIterator &other) const {
|
|
|
|
return it_ != other.it_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator() = default;
|
|
|
|
ConstIterator(Iterator it) : it_(std::move(it)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Iterator it_;
|
|
|
|
};
|
|
|
|
using iterator = Iterator;
|
|
|
|
using const_iterator = ConstIterator;
|
|
|
|
|
2022-03-12 13:27:14 +01:00
|
|
|
struct NodePointer {
|
|
|
|
value_type &operator*() {
|
|
|
|
return it_->get_public();
|
|
|
|
}
|
|
|
|
const value_type &operator*() const {
|
|
|
|
return it_->get_public();
|
|
|
|
}
|
|
|
|
value_type *operator->() {
|
|
|
|
return &it_->get_public();
|
|
|
|
}
|
|
|
|
const value_type *operator->() const {
|
|
|
|
return &it_->get_public();
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeT *get() {
|
|
|
|
return it_;
|
|
|
|
}
|
|
|
|
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator==(const Iterator &) const {
|
2022-03-12 13:27:14 +01:00
|
|
|
return it_ == nullptr;
|
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator!=(const Iterator &) const {
|
2022-03-12 13:27:14 +01:00
|
|
|
return it_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit NodePointer(NodeT *it) : it_(it) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NodeT *it_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstNodePointer {
|
|
|
|
const value_type &operator*() const {
|
|
|
|
return it_->get_public();
|
|
|
|
}
|
|
|
|
const value_type *operator->() const {
|
|
|
|
return &it_->get_public();
|
|
|
|
}
|
|
|
|
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator==(const ConstIterator &) const {
|
2022-03-12 13:27:14 +01:00
|
|
|
return it_ == nullptr;
|
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
bool operator!=(const ConstIterator &) const {
|
2022-03-12 13:27:14 +01:00
|
|
|
return it_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const NodeT *get() const {
|
|
|
|
return it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit ConstNodePointer(const NodeT *it) : it_(it) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const NodeT *it_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2022-02-17 19:31:58 +01:00
|
|
|
FlatHashTable() = default;
|
2022-07-23 10:13:17 +02:00
|
|
|
FlatHashTable(const FlatHashTable &other) = delete;
|
|
|
|
FlatHashTable &operator=(const FlatHashTable &other) = delete;
|
2022-02-17 19:31:58 +01:00
|
|
|
|
2022-02-23 14:34:42 +01:00
|
|
|
FlatHashTable(std::initializer_list<NodeT> nodes) {
|
2022-02-20 21:03:34 +01:00
|
|
|
if (nodes.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
reserve(nodes.size());
|
2022-02-23 20:13:40 +01:00
|
|
|
uint32 used_nodes = 0;
|
2022-02-19 09:34:43 +01:00
|
|
|
for (auto &new_node : nodes) {
|
|
|
|
CHECK(!new_node.empty());
|
2022-02-20 21:03:34 +01:00
|
|
|
auto bucket = calc_bucket(new_node.key());
|
2022-02-17 19:31:58 +01:00
|
|
|
while (true) {
|
2022-02-19 09:34:43 +01:00
|
|
|
auto &node = nodes_[bucket];
|
|
|
|
if (node.empty()) {
|
2022-02-20 21:03:34 +01:00
|
|
|
node.copy_from(new_node);
|
2022-02-23 20:13:40 +01:00
|
|
|
used_nodes++;
|
2022-02-17 19:31:58 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-20 21:03:34 +01:00
|
|
|
if (EqT()(node.key(), new_node.key())) {
|
|
|
|
break;
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
next_bucket(bucket);
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 13:34:56 +01:00
|
|
|
used_node_count_ = used_nodes;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-09 13:17:29 +01:00
|
|
|
FlatHashTable(FlatHashTable &&other) noexcept
|
|
|
|
: nodes_(other.nodes_)
|
|
|
|
, used_node_count_(other.used_node_count_)
|
|
|
|
, bucket_count_mask_(other.bucket_count_mask_)
|
|
|
|
, bucket_count_(other.bucket_count_)
|
|
|
|
, begin_bucket_(other.begin_bucket_) {
|
|
|
|
other.drop();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-02-23 13:50:58 +01:00
|
|
|
void operator=(FlatHashTable &&other) noexcept {
|
2022-02-23 20:13:40 +01:00
|
|
|
clear();
|
|
|
|
nodes_ = other.nodes_;
|
2022-03-09 13:17:29 +01:00
|
|
|
used_node_count_ = other.used_node_count_;
|
|
|
|
bucket_count_mask_ = other.bucket_count_mask_;
|
|
|
|
bucket_count_ = other.bucket_count_;
|
|
|
|
begin_bucket_ = other.begin_bucket_;
|
|
|
|
other.drop();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-09 21:01:10 +01:00
|
|
|
~FlatHashTable() {
|
2022-07-23 10:13:17 +02:00
|
|
|
clear_nodes(nodes_);
|
2022-03-09 21:01:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 19:31:58 +01:00
|
|
|
void swap(FlatHashTable &other) noexcept {
|
2022-02-23 20:13:40 +01:00
|
|
|
std::swap(nodes_, other.nodes_);
|
2022-03-09 13:17:29 +01:00
|
|
|
std::swap(used_node_count_, other.used_node_count_);
|
|
|
|
std::swap(bucket_count_mask_, other.bucket_count_mask_);
|
|
|
|
std::swap(bucket_count_, other.bucket_count_);
|
|
|
|
std::swap(begin_bucket_, other.begin_bucket_);
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:51:43 +01:00
|
|
|
uint32 bucket_count() const {
|
2022-03-09 13:34:56 +01:00
|
|
|
return bucket_count_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 13:27:14 +01:00
|
|
|
NodePointer find(const KeyT &key) {
|
|
|
|
return NodePointer(find_impl(key));
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 13:27:14 +01:00
|
|
|
ConstNodePointer find(const KeyT &key) const {
|
|
|
|
return ConstNodePointer(const_cast<FlatHashTable *>(this)->find_impl(key));
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const {
|
2022-03-09 13:34:56 +01:00
|
|
|
return used_node_count_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
2022-03-09 13:34:56 +01:00
|
|
|
return used_node_count_ == 0;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator begin() {
|
2022-03-11 12:10:10 +01:00
|
|
|
return create_iterator(begin_impl());
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
Iterator end() {
|
|
|
|
return Iterator();
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
ConstIterator begin() const {
|
2022-02-23 14:34:42 +01:00
|
|
|
return ConstIterator(const_cast<FlatHashTable *>(this)->begin());
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-13 21:14:18 +01:00
|
|
|
ConstIterator end() const {
|
|
|
|
return ConstIterator();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void reserve(size_t size) {
|
2022-02-23 20:13:40 +01:00
|
|
|
if (size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-23 20:51:43 +01:00
|
|
|
CHECK(size <= (1u << 29));
|
2022-03-09 15:00:22 +01:00
|
|
|
uint32 want_size = detail::normalize_flat_hash_table_size(static_cast<uint32>(size) * 5 / 3 + 1);
|
2022-02-23 16:20:25 +01:00
|
|
|
if (want_size > bucket_count()) {
|
2022-02-17 19:31:58 +01:00
|
|
|
resize(want_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
2022-03-12 13:27:14 +01:00
|
|
|
std::pair<NodePointer, bool> emplace(KeyT key, ArgsT &&...args) {
|
2022-03-09 14:40:50 +01:00
|
|
|
CHECK(!is_hash_table_key_empty(key));
|
2022-04-18 20:27:38 +02:00
|
|
|
if (unlikely(bucket_count_mask_ == 0)) {
|
|
|
|
CHECK(used_node_count_ == 0);
|
|
|
|
resize(8);
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
auto bucket = calc_bucket(key);
|
|
|
|
while (true) {
|
2022-02-19 09:34:43 +01:00
|
|
|
auto &node = nodes_[bucket];
|
|
|
|
if (node.empty()) {
|
2022-04-18 20:27:38 +02:00
|
|
|
if (unlikely(used_node_count_ * 5 >= bucket_count_mask_ * 3)) {
|
|
|
|
resize(2 * bucket_count_);
|
|
|
|
CHECK(used_node_count_ * 5 < bucket_count_mask_ * 3);
|
|
|
|
return emplace(std::move(key), std::forward<ArgsT>(args)...);
|
|
|
|
}
|
|
|
|
invalidate_iterators();
|
|
|
|
|
2022-02-19 09:34:43 +01:00
|
|
|
node.emplace(std::move(key), std::forward<ArgsT>(args)...);
|
2022-03-09 13:34:56 +01:00
|
|
|
used_node_count_++;
|
2022-03-12 13:27:14 +01:00
|
|
|
return {NodePointer(&node), true};
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-04-19 01:33:12 +02:00
|
|
|
if (EqT()(node.key(), key)) {
|
|
|
|
return {NodePointer(&node), false};
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
next_bucket(bucket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 13:27:14 +01:00
|
|
|
std::pair<NodePointer, bool> insert(KeyT key) {
|
2022-02-17 19:31:58 +01:00
|
|
|
return emplace(std::move(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ItT>
|
|
|
|
void insert(ItT begin, ItT end) {
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
emplace(*begin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:34:42 +01:00
|
|
|
template <class T = typename NodeT::second_type>
|
2022-02-21 00:22:01 +01:00
|
|
|
T &operator[](const KeyT &key) {
|
|
|
|
return emplace(key).first->second;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t erase(const KeyT &key) {
|
2022-03-11 12:10:10 +01:00
|
|
|
auto *node = find_impl(key);
|
|
|
|
if (node == nullptr) {
|
2022-02-17 19:31:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2022-03-11 12:10:10 +01:00
|
|
|
erase_node(node);
|
|
|
|
try_shrink();
|
2022-02-17 19:31:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count(const KeyT &key) const {
|
2022-03-11 12:10:10 +01:00
|
|
|
return const_cast<FlatHashTable *>(this)->find_impl(key) != nullptr;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2022-02-23 20:13:40 +01:00
|
|
|
if (nodes_ != nullptr) {
|
2022-02-25 12:30:02 +01:00
|
|
|
clear_nodes(nodes_);
|
2022-03-09 13:17:29 +01:00
|
|
|
drop();
|
2022-02-23 20:13:40 +01:00
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void erase(Iterator it) {
|
|
|
|
DCHECK(it != end());
|
2022-03-09 14:29:47 +01:00
|
|
|
erase_node(it.get());
|
2022-02-23 16:05:56 +01:00
|
|
|
try_shrink();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-03-12 13:27:14 +01:00
|
|
|
|
|
|
|
void erase(NodePointer it) {
|
|
|
|
DCHECK(it != end());
|
|
|
|
erase_node(it.get());
|
|
|
|
try_shrink();
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
|
|
|
|
template <class F>
|
|
|
|
void remove_if(F &&f) {
|
2022-02-23 20:13:40 +01:00
|
|
|
if (empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-11 12:10:10 +01:00
|
|
|
auto it = begin_impl();
|
2022-02-23 20:13:40 +01:00
|
|
|
auto end = nodes_ + bucket_count();
|
|
|
|
while (it != end && !it->empty()) {
|
2022-02-17 19:31:58 +01:00
|
|
|
++it;
|
|
|
|
}
|
2022-02-24 22:04:32 +01:00
|
|
|
if (it == end) {
|
|
|
|
do {
|
|
|
|
--it;
|
|
|
|
} while (!it->empty());
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
auto first_empty = it;
|
2022-02-23 20:13:40 +01:00
|
|
|
while (it != end) {
|
2022-02-17 19:31:58 +01:00
|
|
|
if (!it->empty() && f(it->get_public())) {
|
|
|
|
erase_node(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 20:13:40 +01:00
|
|
|
for (it = nodes_; it != first_empty;) {
|
2022-02-17 19:31:58 +01:00
|
|
|
if (!it->empty() && f(it->get_public())) {
|
|
|
|
erase_node(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try_shrink();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-23 20:13:40 +01:00
|
|
|
NodeT *nodes_ = nullptr;
|
2022-03-09 13:17:29 +01:00
|
|
|
uint32 used_node_count_ = 0;
|
|
|
|
uint32 bucket_count_mask_ = 0;
|
|
|
|
uint32 bucket_count_ = 0;
|
|
|
|
uint32 begin_bucket_ = 0;
|
|
|
|
|
|
|
|
void drop() {
|
|
|
|
nodes_ = nullptr;
|
|
|
|
used_node_count_ = 0;
|
|
|
|
bucket_count_mask_ = 0;
|
|
|
|
bucket_count_ = 0;
|
|
|
|
begin_bucket_ = 0;
|
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
|
2022-03-11 12:10:10 +01:00
|
|
|
NodeT *begin_impl() {
|
|
|
|
if (empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (begin_bucket_ == INVALID_BUCKET) {
|
|
|
|
begin_bucket_ = detail::get_random_flat_hash_table_bucket(bucket_count_mask_);
|
|
|
|
while (nodes_[begin_bucket_].empty()) {
|
|
|
|
next_bucket(begin_bucket_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes_ + begin_bucket_;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeT *find_impl(const KeyT &key) {
|
|
|
|
if (unlikely(nodes_ == nullptr) || is_hash_table_key_empty(key)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto bucket = calc_bucket(key);
|
|
|
|
while (true) {
|
|
|
|
auto &node = nodes_[bucket];
|
|
|
|
if (node.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-19 01:33:12 +02:00
|
|
|
if (EqT()(node.key(), key)) {
|
|
|
|
return &node;
|
|
|
|
}
|
2022-03-11 12:10:10 +01:00
|
|
|
next_bucket(bucket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 19:31:58 +01:00
|
|
|
void try_shrink() {
|
2022-02-23 20:13:40 +01:00
|
|
|
DCHECK(nodes_ != nullptr);
|
2022-03-09 13:34:56 +01:00
|
|
|
if (unlikely(used_node_count_ * 10 < bucket_count_mask_ && bucket_count_mask_ > 7)) {
|
2022-03-09 15:00:22 +01:00
|
|
|
resize(detail::normalize_flat_hash_table_size((used_node_count_ + 1) * 5 / 3 + 1));
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-02-24 22:24:27 +01:00
|
|
|
invalidate_iterators();
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:13:40 +01:00
|
|
|
uint32 calc_bucket(const KeyT &key) const {
|
2022-11-23 17:37:32 +01:00
|
|
|
return HashT()(key) & bucket_count_mask_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:13:40 +01:00
|
|
|
inline void next_bucket(uint32 &bucket) const {
|
2022-03-09 13:34:56 +01:00
|
|
|
bucket = (bucket + 1) & bucket_count_mask_;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:51:43 +01:00
|
|
|
void resize(uint32 new_size) {
|
|
|
|
if (unlikely(nodes_ == nullptr)) {
|
2022-03-09 13:17:29 +01:00
|
|
|
allocate_nodes(new_size);
|
2022-03-09 13:34:56 +01:00
|
|
|
used_node_count_ = 0;
|
2022-02-23 20:51:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-23 20:13:40 +01:00
|
|
|
auto old_nodes = nodes_;
|
2022-03-09 13:34:56 +01:00
|
|
|
uint32 old_size = used_node_count_;
|
|
|
|
uint32 old_bucket_count = bucket_count_;
|
2022-03-09 13:17:29 +01:00
|
|
|
allocate_nodes(new_size);
|
2022-03-09 13:34:56 +01:00
|
|
|
used_node_count_ = old_size;
|
2022-02-23 20:13:40 +01:00
|
|
|
|
2022-02-25 12:30:02 +01:00
|
|
|
auto old_nodes_end = old_nodes + old_bucket_count;
|
|
|
|
for (NodeT *old_node = old_nodes; old_node != old_nodes_end; ++old_node) {
|
|
|
|
if (old_node->empty()) {
|
2022-02-17 19:31:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-25 12:30:02 +01:00
|
|
|
auto bucket = calc_bucket(old_node->key());
|
2022-02-17 19:31:58 +01:00
|
|
|
while (!nodes_[bucket].empty()) {
|
|
|
|
next_bucket(bucket);
|
|
|
|
}
|
2022-02-25 12:30:02 +01:00
|
|
|
nodes_[bucket] = std::move(*old_node);
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
2022-02-25 12:30:02 +01:00
|
|
|
clear_nodes(old_nodes);
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:13:40 +01:00
|
|
|
void erase_node(NodeT *it) {
|
2022-02-24 19:38:57 +01:00
|
|
|
DCHECK(nodes_ <= it && static_cast<size_t>(it - nodes_) < bucket_count());
|
2022-02-25 11:53:13 +01:00
|
|
|
it->clear();
|
2022-03-09 13:34:56 +01:00
|
|
|
used_node_count_--;
|
2022-02-17 19:31:58 +01:00
|
|
|
|
2022-03-09 13:34:56 +01:00
|
|
|
const auto bucket_count = bucket_count_;
|
2022-02-25 12:15:32 +01:00
|
|
|
const auto *end = nodes_ + bucket_count;
|
|
|
|
for (auto *test_node = it + 1; test_node != end; test_node++) {
|
2022-02-25 12:30:02 +01:00
|
|
|
if (likely(test_node->empty())) {
|
2022-02-25 11:53:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:15:32 +01:00
|
|
|
auto want_node = nodes_ + calc_bucket(test_node->key());
|
|
|
|
if (want_node <= it || want_node > test_node) {
|
|
|
|
*it = std::move(*test_node);
|
|
|
|
it = test_node;
|
2022-02-25 11:53:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:15:32 +01:00
|
|
|
auto empty_i = static_cast<uint32>(it - nodes_);
|
|
|
|
auto empty_bucket = empty_i;
|
2022-02-25 11:53:13 +01:00
|
|
|
for (uint32 test_i = bucket_count;; test_i++) {
|
2022-03-09 13:34:56 +01:00
|
|
|
auto test_bucket = test_i - bucket_count_;
|
2022-02-25 11:53:13 +01:00
|
|
|
if (nodes_[test_bucket].empty()) {
|
|
|
|
return;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto want_i = calc_bucket(nodes_[test_bucket].key());
|
|
|
|
if (want_i < empty_i) {
|
2022-02-25 11:53:13 +01:00
|
|
|
want_i += bucket_count;
|
2022-02-17 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (want_i <= empty_i || want_i > test_i) {
|
|
|
|
nodes_[empty_bucket] = std::move(nodes_[test_bucket]);
|
|
|
|
empty_i = test_i;
|
|
|
|
empty_bucket = test_bucket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 22:24:27 +01:00
|
|
|
|
2022-03-09 13:44:14 +01:00
|
|
|
Iterator create_iterator(NodeT *node) {
|
|
|
|
return Iterator(node, nodes_, nodes_ + bucket_count());
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:34:56 +01:00
|
|
|
void invalidate_iterators() {
|
2022-03-09 13:17:29 +01:00
|
|
|
begin_bucket_ = INVALID_BUCKET;
|
2022-02-24 22:24:27 +01:00
|
|
|
}
|
2022-02-17 19:31:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|