2022-03-09 14:29:47 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2022-03-09 14:29:47 +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-11 22:50:04 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2022-03-09 14:29:47 +01:00
|
|
|
|
|
|
|
#include <new>
|
2022-04-19 01:33:12 +02:00
|
|
|
#include <type_traits>
|
2022-03-11 22:50:04 +01:00
|
|
|
#include <utility>
|
2022-03-09 14:29:47 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2024-03-21 16:43:47 +01:00
|
|
|
template <class KeyT, class ValueT, class EqT, class Enable = void>
|
2022-03-09 14:29:47 +01:00
|
|
|
struct MapNode {
|
|
|
|
using first_type = KeyT;
|
|
|
|
using second_type = ValueT;
|
|
|
|
using public_key_type = KeyT;
|
|
|
|
using public_type = MapNode;
|
|
|
|
|
|
|
|
KeyT first{};
|
|
|
|
union {
|
|
|
|
ValueT second;
|
|
|
|
};
|
|
|
|
|
|
|
|
const KeyT &key() const {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapNode &get_public() {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-12 13:27:14 +01:00
|
|
|
const MapNode &get_public() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:29:47 +01:00
|
|
|
MapNode() {
|
|
|
|
}
|
|
|
|
MapNode(KeyT key, ValueT value) : first(std::move(key)) {
|
|
|
|
new (&second) ValueT(std::move(value));
|
|
|
|
DCHECK(!empty());
|
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
MapNode(const MapNode &) = delete;
|
|
|
|
MapNode &operator=(const MapNode &) = delete;
|
2022-03-09 14:29:47 +01:00
|
|
|
MapNode(MapNode &&other) noexcept {
|
|
|
|
*this = std::move(other);
|
|
|
|
}
|
|
|
|
void operator=(MapNode &&other) noexcept {
|
|
|
|
DCHECK(empty());
|
|
|
|
DCHECK(!other.empty());
|
|
|
|
first = std::move(other.first);
|
2022-07-23 13:53:32 +02:00
|
|
|
other.first = KeyT();
|
2022-03-09 14:29:47 +01:00
|
|
|
new (&second) ValueT(std::move(other.second));
|
|
|
|
other.second.~ValueT();
|
|
|
|
}
|
|
|
|
~MapNode() {
|
|
|
|
if (!empty()) {
|
|
|
|
second.~ValueT();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_from(const MapNode &other) {
|
|
|
|
DCHECK(empty());
|
|
|
|
DCHECK(!other.empty());
|
|
|
|
first = other.first;
|
|
|
|
new (&second) ValueT(other.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
2024-03-21 16:43:47 +01:00
|
|
|
return is_hash_table_key_empty<EqT>(first);
|
2022-03-09 14:29:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
DCHECK(!empty());
|
|
|
|
first = KeyT();
|
|
|
|
second.~ValueT();
|
|
|
|
DCHECK(empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void emplace(KeyT key, ArgsT &&...args) {
|
|
|
|
DCHECK(empty());
|
|
|
|
first = std::move(key);
|
|
|
|
new (&second) ValueT(std::forward<ArgsT>(args)...);
|
|
|
|
DCHECK(!empty());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-21 16:43:47 +01:00
|
|
|
template <class KeyT, class ValueT, class EqT>
|
|
|
|
struct MapNode<KeyT, ValueT, EqT, typename std::enable_if_t<(sizeof(KeyT) + sizeof(ValueT) > 28 * sizeof(void *))>> {
|
2022-04-19 01:33:12 +02:00
|
|
|
struct Impl {
|
|
|
|
using first_type = KeyT;
|
|
|
|
using second_type = ValueT;
|
|
|
|
|
|
|
|
KeyT first{};
|
|
|
|
union {
|
|
|
|
ValueT second;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class InputKeyT, class... ArgsT>
|
|
|
|
Impl(InputKeyT &&key, ArgsT &&...args) : first(std::forward<InputKeyT>(key)) {
|
|
|
|
new (&second) ValueT(std::forward<ArgsT>(args)...);
|
2024-03-21 16:43:47 +01:00
|
|
|
DCHECK(!is_hash_table_key_empty<EqT>(first));
|
2022-04-19 01:33:12 +02:00
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
Impl(const Impl &) = delete;
|
|
|
|
Impl &operator=(const Impl &) = delete;
|
|
|
|
Impl(Impl &&) = delete;
|
2023-05-05 13:31:55 +02:00
|
|
|
Impl &operator=(Impl &&) = delete;
|
2022-04-19 01:33:12 +02:00
|
|
|
~Impl() {
|
|
|
|
second.~ValueT();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using first_type = KeyT;
|
|
|
|
using second_type = ValueT;
|
|
|
|
using public_key_type = KeyT;
|
|
|
|
using public_type = Impl;
|
|
|
|
|
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
|
|
|
|
const KeyT &key() const {
|
|
|
|
DCHECK(!empty());
|
|
|
|
return impl_->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
Impl &get_public() {
|
|
|
|
return *impl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Impl &get_public() const {
|
|
|
|
return *impl_;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapNode() {
|
|
|
|
}
|
|
|
|
MapNode(KeyT key, ValueT value) : impl_(td::make_unique<Impl>(std::move(key), std::move(value))) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void copy_from(const MapNode &other) {
|
|
|
|
DCHECK(empty());
|
|
|
|
DCHECK(!other.empty());
|
|
|
|
impl_ = td::make_unique<Impl>(other.impl_->first, other.impl_->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return impl_ == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
DCHECK(!empty());
|
|
|
|
impl_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... ArgsT>
|
|
|
|
void emplace(KeyT key, ArgsT &&...args) {
|
|
|
|
DCHECK(empty());
|
|
|
|
impl_ = td::make_unique<Impl>(std::move(key), std::forward<ArgsT>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-09 14:29:47 +01:00
|
|
|
} // namespace td
|