|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GOOGLE_PROTOBUF_MAP_H__
|
|
|
#define GOOGLE_PROTOBUF_MAP_H__
|
|
|
|
|
|
#include <functional>
|
|
|
#include <initializer_list>
|
|
|
#include <iterator>
|
|
|
#include <limits>
|
|
|
#include <map>
|
|
|
#include <string>
|
|
|
#include <type_traits>
|
|
|
#include <utility>
|
|
|
|
|
|
#if defined(__cpp_lib_string_view)
|
|
|
#include <string_view>
|
|
|
#endif
|
|
|
|
|
|
#include <google/protobuf/stubs/common.h>
|
|
|
#include <google/protobuf/arena.h>
|
|
|
#include <google/protobuf/generated_enum_util.h>
|
|
|
#include <google/protobuf/map_type_handler.h>
|
|
|
#include <google/protobuf/stubs/hash.h>
|
|
|
|
|
|
#ifdef SWIG
|
|
|
#error "You cannot SWIG proto headers"
|
|
|
#endif
|
|
|
|
|
|
#include <google/protobuf/port_def.inc>
|
|
|
|
|
|
namespace google {
|
|
|
namespace protobuf {
|
|
|
|
|
|
template <typename Key, typename T>
|
|
|
class Map;
|
|
|
|
|
|
class MapIterator;
|
|
|
|
|
|
template <typename Enum>
|
|
|
struct is_proto_enum;
|
|
|
|
|
|
namespace internal {
|
|
|
template <typename Derived, typename Key, typename T,
|
|
|
WireFormatLite::FieldType key_wire_type,
|
|
|
WireFormatLite::FieldType value_wire_type, int default_enum_value>
|
|
|
class MapFieldLite;
|
|
|
|
|
|
template <typename Derived, typename Key, typename T,
|
|
|
WireFormatLite::FieldType key_wire_type,
|
|
|
WireFormatLite::FieldType value_wire_type, int default_enum_value>
|
|
|
class MapField;
|
|
|
|
|
|
template <typename Key, typename T>
|
|
|
class TypeDefinedMapFieldBase;
|
|
|
|
|
|
class DynamicMapField;
|
|
|
|
|
|
class GeneratedMessageReflection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
class MapAllocator {
|
|
|
public:
|
|
|
using value_type = U;
|
|
|
using pointer = value_type*;
|
|
|
using const_pointer = const value_type*;
|
|
|
using reference = value_type&;
|
|
|
using const_reference = const value_type&;
|
|
|
using size_type = size_t;
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
|
MapAllocator() : arena_(nullptr) {}
|
|
|
explicit MapAllocator(Arena* arena) : arena_(arena) {}
|
|
|
template <typename X>
|
|
|
MapAllocator(const MapAllocator<X>& allocator)
|
|
|
: arena_(allocator.arena()) {}
|
|
|
|
|
|
pointer allocate(size_type n, const void* = nullptr) {
|
|
|
|
|
|
|
|
|
if (arena_ == nullptr) {
|
|
|
return static_cast<pointer>(::operator new(n * sizeof(value_type)));
|
|
|
} else {
|
|
|
return reinterpret_cast<pointer>(
|
|
|
Arena::CreateArray<uint8>(arena_, n * sizeof(value_type)));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void deallocate(pointer p, size_type n) {
|
|
|
if (arena_ == nullptr) {
|
|
|
#if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
|
|
|
::operator delete(p, n * sizeof(value_type));
|
|
|
#else
|
|
|
(void)n;
|
|
|
::operator delete(p);
|
|
|
#endif
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#if __cplusplus >= 201103L && !defined(GOOGLE_PROTOBUF_OS_APPLE) && \
|
|
|
!defined(GOOGLE_PROTOBUF_OS_NACL) && \
|
|
|
!defined(GOOGLE_PROTOBUF_OS_EMSCRIPTEN)
|
|
|
template <class NodeType, class... Args>
|
|
|
void construct(NodeType* p, Args&&... args) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new (const_cast<void*>(static_cast<const void*>(p)))
|
|
|
NodeType(std::forward<Args>(args)...);
|
|
|
}
|
|
|
|
|
|
template <class NodeType>
|
|
|
void destroy(NodeType* p) {
|
|
|
p->~NodeType();
|
|
|
}
|
|
|
#else
|
|
|
void construct(pointer p, const_reference t) { new (p) value_type(t); }
|
|
|
|
|
|
void destroy(pointer p) { p->~value_type(); }
|
|
|
#endif
|
|
|
|
|
|
template <typename X>
|
|
|
struct rebind {
|
|
|
using other = MapAllocator<X>;
|
|
|
};
|
|
|
|
|
|
template <typename X>
|
|
|
bool operator==(const MapAllocator<X>& other) const {
|
|
|
return arena_ == other.arena_;
|
|
|
}
|
|
|
|
|
|
template <typename X>
|
|
|
bool operator!=(const MapAllocator<X>& other) const {
|
|
|
return arena_ != other.arena_;
|
|
|
}
|
|
|
|
|
|
|
|
|
size_type max_size() const {
|
|
|
|
|
|
return (std::numeric_limits<size_type>::max)();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Arena* arena() const { return arena_; }
|
|
|
|
|
|
private:
|
|
|
using DestructorSkippable_ = void;
|
|
|
Arena* const arena_;
|
|
|
};
|
|
|
|
|
|
template <typename T>
|
|
|
using KeyForTree =
|
|
|
typename std::conditional<std::is_scalar<T>::value, T,
|
|
|
std::reference_wrapper<const T>>::type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename key_type>
|
|
|
struct TransparentSupport {
|
|
|
using hash = std::hash<key_type>;
|
|
|
using less = std::less<key_type>;
|
|
|
|
|
|
static bool Equals(const key_type& a, const key_type& b) { return a == b; }
|
|
|
|
|
|
template <typename K>
|
|
|
using key_arg = key_type;
|
|
|
};
|
|
|
|
|
|
#if defined(__cpp_lib_string_view)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
struct TransparentSupport<std::string> {
|
|
|
static std::string_view ImplicitConvert(std::string_view str) { return str; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename = void>
|
|
|
static std::string_view ImplicitConvert(const std::string& str) {
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
struct hash : private std::hash<std::string_view> {
|
|
|
using is_transparent = void;
|
|
|
|
|
|
template <typename T>
|
|
|
size_t operator()(const T& str) const {
|
|
|
return base()(ImplicitConvert(str));
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
const std::hash<std::string_view>& base() const { return *this; }
|
|
|
};
|
|
|
struct less {
|
|
|
using is_transparent = void;
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
bool operator()(const T& t, const U& u) const {
|
|
|
return ImplicitConvert(t) < ImplicitConvert(u);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
static bool Equals(const T& t, const U& u) {
|
|
|
return ImplicitConvert(t) == ImplicitConvert(u);
|
|
|
}
|
|
|
|
|
|
template <typename K>
|
|
|
using key_arg = K;
|
|
|
};
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Key, typename T>
|
|
|
struct MapPair {
|
|
|
using first_type = const Key;
|
|
|
using second_type = T;
|
|
|
|
|
|
MapPair(const Key& other_first, const T& other_second)
|
|
|
: first(other_first), second(other_second) {}
|
|
|
explicit MapPair(const Key& other_first) : first(other_first), second() {}
|
|
|
MapPair(const MapPair& other) : first(other.first), second(other.second) {}
|
|
|
|
|
|
~MapPair() {}
|
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
operator std::pair<T1, T2>() const {
|
|
|
return std::pair<T1, T2>(first, second);
|
|
|
}
|
|
|
|
|
|
const Key first;
|
|
|
T second;
|
|
|
|
|
|
private:
|
|
|
friend class Arena;
|
|
|
friend class Map<Key, T>;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Key, typename T>
|
|
|
class Map {
|
|
|
public:
|
|
|
using key_type = Key;
|
|
|
using mapped_type = T;
|
|
|
using value_type = MapPair<Key, T>;
|
|
|
|
|
|
using pointer = value_type*;
|
|
|
using const_pointer = const value_type*;
|
|
|
using reference = value_type&;
|
|
|
using const_reference = const value_type&;
|
|
|
|
|
|
using size_type = size_t;
|
|
|
using hasher = typename internal::TransparentSupport<Key>::hash;
|
|
|
|
|
|
Map() : arena_(nullptr), default_enum_value_(0) { Init(); }
|
|
|
explicit Map(Arena* arena) : arena_(arena), default_enum_value_(0) { Init(); }
|
|
|
|
|
|
Map(const Map& other)
|
|
|
: arena_(nullptr), default_enum_value_(other.default_enum_value_) {
|
|
|
Init();
|
|
|
insert(other.begin(), other.end());
|
|
|
}
|
|
|
|
|
|
Map(Map&& other) noexcept : Map() {
|
|
|
if (other.arena_) {
|
|
|
*this = other;
|
|
|
} else {
|
|
|
swap(other);
|
|
|
}
|
|
|
}
|
|
|
Map& operator=(Map&& other) noexcept {
|
|
|
if (this != &other) {
|
|
|
if (arena_ != other.arena_) {
|
|
|
*this = other;
|
|
|
} else {
|
|
|
swap(other);
|
|
|
}
|
|
|
}
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
template <class InputIt>
|
|
|
Map(const InputIt& first, const InputIt& last)
|
|
|
: arena_(nullptr), default_enum_value_(0) {
|
|
|
Init();
|
|
|
insert(first, last);
|
|
|
}
|
|
|
|
|
|
~Map() {
|
|
|
if (arena_ == nullptr) {
|
|
|
clear();
|
|
|
delete elements_;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
void Init() { elements_ = Arena::CreateMessage<InnerMap>(arena_, 0); }
|
|
|
|
|
|
using Allocator = internal::MapAllocator<void*>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InnerMap : private hasher {
|
|
|
public:
|
|
|
explicit InnerMap(size_type n) : InnerMap(nullptr, n) {}
|
|
|
InnerMap(Arena* arena, size_type n)
|
|
|
: hasher(),
|
|
|
num_elements_(0),
|
|
|
seed_(Seed()),
|
|
|
table_(nullptr),
|
|
|
alloc_(arena) {
|
|
|
n = TableSize(n);
|
|
|
table_ = CreateEmptyTable(n);
|
|
|
num_buckets_ = index_of_first_non_null_ = n;
|
|
|
}
|
|
|
|
|
|
~InnerMap() {
|
|
|
if (table_ != nullptr) {
|
|
|
clear();
|
|
|
Dealloc<void*>(table_, num_buckets_);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
enum { kMinTableSize = 8 };
|
|
|
|
|
|
|
|
|
struct Node {
|
|
|
value_type kv;
|
|
|
Node* next;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using TreeAllocator = typename Allocator::template rebind<
|
|
|
std::pair<const internal::KeyForTree<Key>, void*>>::other;
|
|
|
using Tree = std::map<internal::KeyForTree<Key>, void*,
|
|
|
typename internal::TransparentSupport<Key>::less,
|
|
|
TreeAllocator>;
|
|
|
using TreeIterator = typename Tree::iterator;
|
|
|
|
|
|
static Node* NodeFromTreeIterator(TreeIterator it) {
|
|
|
return static_cast<Node*>(it->second);
|
|
|
}
|
|
|
|
|
|
|
|
|
template <typename KeyValueType>
|
|
|
class iterator_base {
|
|
|
public:
|
|
|
using reference = KeyValueType&;
|
|
|
using pointer = KeyValueType*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator_base() : node_(nullptr), m_(nullptr), bucket_index_(0) {}
|
|
|
|
|
|
explicit iterator_base(const InnerMap* m) : m_(m) {
|
|
|
SearchFrom(m->index_of_first_non_null_);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
explicit iterator_base(const iterator_base<U>& it)
|
|
|
: node_(it.node_), m_(it.m_), bucket_index_(it.bucket_index_) {}
|
|
|
|
|
|
iterator_base(Node* n, const InnerMap* m, size_type index)
|
|
|
: node_(n), m_(m), bucket_index_(index) {}
|
|
|
|
|
|
iterator_base(TreeIterator tree_it, const InnerMap* m, size_type index)
|
|
|
: node_(NodeFromTreeIterator(tree_it)), m_(m), bucket_index_(index) {
|
|
|
|
|
|
|
|
|
GOOGLE_DCHECK_EQ(bucket_index_ % 2, 0u);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SearchFrom(size_type start_bucket) {
|
|
|
GOOGLE_DCHECK(m_->index_of_first_non_null_ == m_->num_buckets_ ||
|
|
|
m_->table_[m_->index_of_first_non_null_] != nullptr);
|
|
|
node_ = nullptr;
|
|
|
for (bucket_index_ = start_bucket; bucket_index_ < m_->num_buckets_;
|
|
|
bucket_index_++) {
|
|
|
if (m_->TableEntryIsNonEmptyList(bucket_index_)) {
|
|
|
node_ = static_cast<Node*>(m_->table_[bucket_index_]);
|
|
|
break;
|
|
|
} else if (m_->TableEntryIsTree(bucket_index_)) {
|
|
|
Tree* tree = static_cast<Tree*>(m_->table_[bucket_index_]);
|
|
|
GOOGLE_DCHECK(!tree->empty());
|
|
|
node_ = NodeFromTreeIterator(tree->begin());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
reference operator*() const { return node_->kv; }
|
|
|
pointer operator->() const { return &(operator*()); }
|
|
|
|
|
|
friend bool operator==(const iterator_base& a, const iterator_base& b) {
|
|
|
return a.node_ == b.node_;
|
|
|
}
|
|
|
friend bool operator!=(const iterator_base& a, const iterator_base& b) {
|
|
|
return a.node_ != b.node_;
|
|
|
}
|
|
|
|
|
|
iterator_base& operator++() {
|
|
|
if (node_->next == nullptr) {
|
|
|
TreeIterator tree_it;
|
|
|
const bool is_list = revalidate_if_necessary(&tree_it);
|
|
|
if (is_list) {
|
|
|
SearchFrom(bucket_index_ + 1);
|
|
|
} else {
|
|
|
GOOGLE_DCHECK_EQ(bucket_index_ & 1, 0u);
|
|
|
Tree* tree = static_cast<Tree*>(m_->table_[bucket_index_]);
|
|
|
if (++tree_it == tree->end()) {
|
|
|
SearchFrom(bucket_index_ + 2);
|
|
|
} else {
|
|
|
node_ = NodeFromTreeIterator(tree_it);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
node_ = node_->next;
|
|
|
}
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
iterator_base operator++(int ) {
|
|
|
iterator_base tmp = *this;
|
|
|
++*this;
|
|
|
return tmp;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool revalidate_if_necessary(TreeIterator* it) {
|
|
|
GOOGLE_DCHECK(node_ != nullptr && m_ != nullptr);
|
|
|
|
|
|
bucket_index_ &= (m_->num_buckets_ - 1);
|
|
|
|
|
|
if (m_->table_[bucket_index_] == static_cast<void*>(node_)) return true;
|
|
|
|
|
|
|
|
|
if (m_->TableEntryIsNonEmptyList(bucket_index_)) {
|
|
|
Node* l = static_cast<Node*>(m_->table_[bucket_index_]);
|
|
|
while ((l = l->next) != nullptr) {
|
|
|
if (l == node_) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator_base i(m_->find(node_->kv.first, it));
|
|
|
bucket_index_ = i.bucket_index_;
|
|
|
return m_->TableEntryIsList(bucket_index_);
|
|
|
}
|
|
|
|
|
|
Node* node_;
|
|
|
const InnerMap* m_;
|
|
|
size_type bucket_index_;
|
|
|
};
|
|
|
|
|
|
public:
|
|
|
using iterator = iterator_base<value_type>;
|
|
|
using const_iterator = iterator_base<const value_type>;
|
|
|
|
|
|
iterator begin() { return iterator(this); }
|
|
|
iterator end() { return iterator(); }
|
|
|
const_iterator begin() const { return const_iterator(this); }
|
|
|
const_iterator end() const { return const_iterator(); }
|
|
|
|
|
|
void clear() {
|
|
|
for (size_type b = 0; b < num_buckets_; b++) {
|
|
|
if (TableEntryIsNonEmptyList(b)) {
|
|
|
Node* node = static_cast<Node*>(table_[b]);
|
|
|
table_[b] = nullptr;
|
|
|
do {
|
|
|
Node* next = node->next;
|
|
|
DestroyNode(node);
|
|
|
node = next;
|
|
|
} while (node != nullptr);
|
|
|
} else if (TableEntryIsTree(b)) {
|
|
|
Tree* tree = static_cast<Tree*>(table_[b]);
|
|
|
GOOGLE_DCHECK(table_[b] == table_[b + 1] && (b & 1) == 0);
|
|
|
table_[b] = table_[b + 1] = nullptr;
|
|
|
typename Tree::iterator tree_it = tree->begin();
|
|
|
do {
|
|
|
Node* node = NodeFromTreeIterator(tree_it);
|
|
|
typename Tree::iterator next = tree_it;
|
|
|
++next;
|
|
|
tree->erase(tree_it);
|
|
|
DestroyNode(node);
|
|
|
tree_it = next;
|
|
|
} while (tree_it != tree->end());
|
|
|
DestroyTree(tree);
|
|
|
b++;
|
|
|
}
|
|
|
}
|
|
|
num_elements_ = 0;
|
|
|
index_of_first_non_null_ = num_buckets_;
|
|
|
}
|
|
|
|
|
|
const hasher& hash_function() const { return *this; }
|
|
|
|
|
|
static size_type max_size() {
|
|
|
return static_cast<size_type>(1) << (sizeof(void**) >= 8 ? 60 : 28);
|
|
|
}
|
|
|
size_type size() const { return num_elements_; }
|
|
|
bool empty() const { return size() == 0; }
|
|
|
|
|
|
template <typename K>
|
|
|
iterator find(const K& k) {
|
|
|
return iterator(FindHelper(k).first);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<iterator, bool> insert(const Key& k) {
|
|
|
std::pair<const_iterator, size_type> p = FindHelper(k);
|
|
|
|
|
|
if (p.first.node_ != nullptr)
|
|
|
return std::make_pair(iterator(p.first), false);
|
|
|
|
|
|
if (ResizeIfLoadIsOutOfRange(num_elements_ + 1)) {
|
|
|
p = FindHelper(k);
|
|
|
}
|
|
|
const size_type b = p.second;
|
|
|
Node* node;
|
|
|
if (alloc_.arena() == nullptr) {
|
|
|
node = new Node{value_type(k), nullptr};
|
|
|
} else {
|
|
|
node = Alloc<Node>(1);
|
|
|
Arena::CreateInArenaStorage(const_cast<Key*>(&node->kv.first),
|
|
|
alloc_.arena(), k);
|
|
|
Arena::CreateInArenaStorage(&node->kv.second, alloc_.arena());
|
|
|
}
|
|
|
|
|
|
iterator result = InsertUnique(b, node);
|
|
|
++num_elements_;
|
|
|
return std::make_pair(result, true);
|
|
|
}
|
|
|
|
|
|
value_type& operator[](const Key& k) { return *insert(k).first; }
|
|
|
|
|
|
void erase(iterator it) {
|
|
|
GOOGLE_DCHECK_EQ(it.m_, this);
|
|
|
typename Tree::iterator tree_it;
|
|
|
const bool is_list = it.revalidate_if_necessary(&tree_it);
|
|
|
size_type b = it.bucket_index_;
|
|
|
Node* const item = it.node_;
|
|
|
if (is_list) {
|
|
|
GOOGLE_DCHECK(TableEntryIsNonEmptyList(b));
|
|
|
Node* head = static_cast<Node*>(table_[b]);
|
|
|
head = EraseFromLinkedList(item, head);
|
|
|
table_[b] = static_cast<void*>(head);
|
|
|
} else {
|
|
|
GOOGLE_DCHECK(TableEntryIsTree(b));
|
|
|
Tree* tree = static_cast<Tree*>(table_[b]);
|
|
|
tree->erase(tree_it);
|
|
|
if (tree->empty()) {
|
|
|
|
|
|
|
|
|
b &= ~static_cast<size_type>(1);
|
|
|
DestroyTree(tree);
|
|
|
table_[b] = table_[b + 1] = nullptr;
|
|
|
}
|
|
|
}
|
|
|
DestroyNode(item);
|
|
|
--num_elements_;
|
|
|
if (PROTOBUF_PREDICT_FALSE(b == index_of_first_non_null_)) {
|
|
|
while (index_of_first_non_null_ < num_buckets_ &&
|
|
|
table_[index_of_first_non_null_] == nullptr) {
|
|
|
++index_of_first_non_null_;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
const_iterator find(const Key& k, TreeIterator* it) const {
|
|
|
return FindHelper(k, it).first;
|
|
|
}
|
|
|
template <typename K>
|
|
|
std::pair<const_iterator, size_type> FindHelper(const K& k) const {
|
|
|
return FindHelper(k, nullptr);
|
|
|
}
|
|
|
template <typename K>
|
|
|
std::pair<const_iterator, size_type> FindHelper(const K& k,
|
|
|
TreeIterator* it) const {
|
|
|
size_type b = BucketNumber(k);
|
|
|
if (TableEntryIsNonEmptyList(b)) {
|
|
|
Node* node = static_cast<Node*>(table_[b]);
|
|
|
do {
|
|
|
if (internal::TransparentSupport<Key>::Equals(node->kv.first, k)) {
|
|
|
return std::make_pair(const_iterator(node, this, b), b);
|
|
|
} else {
|
|
|
node = node->next;
|
|
|
}
|
|
|
} while (node != nullptr);
|
|
|
} else if (TableEntryIsTree(b)) {
|
|
|
GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]);
|
|
|
b &= ~static_cast<size_t>(1);
|
|
|
Tree* tree = static_cast<Tree*>(table_[b]);
|
|
|
auto tree_it = tree->find(k);
|
|
|
if (tree_it != tree->end()) {
|
|
|
if (it != nullptr) *it = tree_it;
|
|
|
return std::make_pair(const_iterator(tree_it, this, b), b);
|
|
|
}
|
|
|
}
|
|
|
return std::make_pair(end(), b);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator InsertUnique(size_type b, Node* node) {
|
|
|
GOOGLE_DCHECK(index_of_first_non_null_ == num_buckets_ ||
|
|
|
table_[index_of_first_non_null_] != nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iterator result;
|
|
|
GOOGLE_DCHECK(find(node->kv.first) == end());
|
|
|
if (TableEntryIsEmpty(b)) {
|
|
|
result = InsertUniqueInList(b, node);
|
|
|
} else if (TableEntryIsNonEmptyList(b)) {
|
|
|
if (PROTOBUF_PREDICT_FALSE(TableEntryIsTooLong(b))) {
|
|
|
TreeConvert(b);
|
|
|
result = InsertUniqueInTree(b, node);
|
|
|
GOOGLE_DCHECK_EQ(result.bucket_index_, b & ~static_cast<size_type>(1));
|
|
|
} else {
|
|
|
|
|
|
|
|
|
return InsertUniqueInList(b, node);
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
|
|
|
return InsertUniqueInTree(b, node);
|
|
|
}
|
|
|
|
|
|
index_of_first_non_null_ =
|
|
|
(std::min)(index_of_first_non_null_, result.bucket_index_);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ShouldInsertAfterHead(void* node) {
|
|
|
#ifdef NDEBUG
|
|
|
return false;
|
|
|
#else
|
|
|
|
|
|
return (reinterpret_cast<uintptr_t>(node) ^ seed_) % 13 > 6;
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterator InsertUniqueInList(size_type b, Node* node) {
|
|
|
if (table_[b] != nullptr && ShouldInsertAfterHead(node)) {
|
|
|
Node* first = static_cast<Node*>(table_[b]);
|
|
|
node->next = first->next;
|
|
|
first->next = node;
|
|
|
return iterator(node, this, b);
|
|
|
}
|
|
|
|
|
|
node->next = static_cast<Node*>(table_[b]);
|
|
|
table_[b] = static_cast<void*>(node);
|
|
|
return iterator(node, this, b);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterator InsertUniqueInTree(size_type b, Node* node) {
|
|
|
GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]);
|
|
|
|
|
|
node->next = nullptr;
|
|
|
return iterator(
|
|
|
static_cast<Tree*>(table_[b])->insert({node->kv.first, node}).first,
|
|
|
this, b & ~static_cast<size_t>(1));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ResizeIfLoadIsOutOfRange(size_type new_size) {
|
|
|
const size_type kMaxMapLoadTimes16 = 12;
|
|
|
const size_type hi_cutoff = num_buckets_ * kMaxMapLoadTimes16 / 16;
|
|
|
const size_type lo_cutoff = hi_cutoff / 4;
|
|
|
|
|
|
|
|
|
|
|
|
if (PROTOBUF_PREDICT_FALSE(new_size >= hi_cutoff)) {
|
|
|
if (num_buckets_ <= max_size() / 2) {
|
|
|
Resize(num_buckets_ * 2);
|
|
|
return true;
|
|
|
}
|
|
|
} else if (PROTOBUF_PREDICT_FALSE(new_size <= lo_cutoff &&
|
|
|
num_buckets_ > kMinTableSize)) {
|
|
|
size_type lg2_of_size_reduction_factor = 1;
|
|
|
|
|
|
|
|
|
|
|
|
const size_type hypothetical_size = new_size * 5 / 4 + 1;
|
|
|
while ((hypothetical_size << lg2_of_size_reduction_factor) <
|
|
|
hi_cutoff) {
|
|
|
++lg2_of_size_reduction_factor;
|
|
|
}
|
|
|
size_type new_num_buckets = std::max<size_type>(
|
|
|
kMinTableSize, num_buckets_ >> lg2_of_size_reduction_factor);
|
|
|
if (new_num_buckets != num_buckets_) {
|
|
|
Resize(new_num_buckets);
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
void Resize(size_t new_num_buckets) {
|
|
|
GOOGLE_DCHECK_GE(new_num_buckets, kMinTableSize);
|
|
|
void** const old_table = table_;
|
|
|
const size_type old_table_size = num_buckets_;
|
|
|
num_buckets_ = new_num_buckets;
|
|
|
table_ = CreateEmptyTable(num_buckets_);
|
|
|
const size_type start = index_of_first_non_null_;
|
|
|
index_of_first_non_null_ = num_buckets_;
|
|
|
for (size_type i = start; i < old_table_size; i++) {
|
|
|
if (TableEntryIsNonEmptyList(old_table, i)) {
|
|
|
TransferList(old_table, i);
|
|
|
} else if (TableEntryIsTree(old_table, i)) {
|
|
|
TransferTree(old_table, i++);
|
|
|
}
|
|
|
}
|
|
|
Dealloc<void*>(old_table, old_table_size);
|
|
|
}
|
|
|
|
|
|
void TransferList(void* const* table, size_type index) {
|
|
|
Node* node = static_cast<Node*>(table[index]);
|
|
|
do {
|
|
|
Node* next = node->next;
|
|
|
InsertUnique(BucketNumber(node->kv.first), node);
|
|
|
node = next;
|
|
|
} while (node != nullptr);
|
|
|
}
|
|
|
|
|
|
void TransferTree(void* const* table, size_type index) {
|
|
|
Tree* tree = static_cast<Tree*>(table[index]);
|
|
|
typename Tree::iterator tree_it = tree->begin();
|
|
|
do {
|
|
|
InsertUnique(BucketNumber(std::cref(tree_it->first).get()),
|
|
|
NodeFromTreeIterator(tree_it));
|
|
|
} while (++tree_it != tree->end());
|
|
|
DestroyTree(tree);
|
|
|
}
|
|
|
|
|
|
Node* EraseFromLinkedList(Node* item, Node* head) {
|
|
|
if (head == item) {
|
|
|
return head->next;
|
|
|
} else {
|
|
|
head->next = EraseFromLinkedList(item, head->next);
|
|
|
return head;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool TableEntryIsEmpty(size_type b) const {
|
|
|
return TableEntryIsEmpty(table_, b);
|
|
|
}
|
|
|
bool TableEntryIsNonEmptyList(size_type b) const {
|
|
|
return TableEntryIsNonEmptyList(table_, b);
|
|
|
}
|
|
|
bool TableEntryIsTree(size_type b) const {
|
|
|
return TableEntryIsTree(table_, b);
|
|
|
}
|
|
|
bool TableEntryIsList(size_type b) const {
|
|
|
return TableEntryIsList(table_, b);
|
|
|
}
|
|
|
static bool TableEntryIsEmpty(void* const* table, size_type b) {
|
|
|
return table[b] == nullptr;
|
|
|
}
|
|
|
static bool TableEntryIsNonEmptyList(void* const* table, size_type b) {
|
|
|
return table[b] != nullptr && table[b] != table[b ^ 1];
|
|
|
}
|
|
|
static bool TableEntryIsTree(void* const* table, size_type b) {
|
|
|
return !TableEntryIsEmpty(table, b) &&
|
|
|
!TableEntryIsNonEmptyList(table, b);
|
|
|
}
|
|
|
static bool TableEntryIsList(void* const* table, size_type b) {
|
|
|
return !TableEntryIsTree(table, b);
|
|
|
}
|
|
|
|
|
|
void TreeConvert(size_type b) {
|
|
|
GOOGLE_DCHECK(!TableEntryIsTree(b) && !TableEntryIsTree(b ^ 1));
|
|
|
Tree* tree =
|
|
|
Arena::Create<Tree>(alloc_.arena(), typename Tree::key_compare(),
|
|
|
typename Tree::allocator_type(alloc_));
|
|
|
size_type count = CopyListToTree(b, tree) + CopyListToTree(b ^ 1, tree);
|
|
|
GOOGLE_DCHECK_EQ(count, tree->size());
|
|
|
table_[b] = table_[b ^ 1] = static_cast<void*>(tree);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_type CopyListToTree(size_type b, Tree* tree) {
|
|
|
size_type count = 0;
|
|
|
Node* node = static_cast<Node*>(table_[b]);
|
|
|
while (node != nullptr) {
|
|
|
tree->insert({node->kv.first, node});
|
|
|
++count;
|
|
|
Node* next = node->next;
|
|
|
node->next = nullptr;
|
|
|
node = next;
|
|
|
}
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TableEntryIsTooLong(size_type b) {
|
|
|
const size_type kMaxLength = 8;
|
|
|
size_type count = 0;
|
|
|
Node* node = static_cast<Node*>(table_[b]);
|
|
|
do {
|
|
|
++count;
|
|
|
node = node->next;
|
|
|
} while (node != nullptr);
|
|
|
|
|
|
GOOGLE_DCHECK_LE(count, kMaxLength);
|
|
|
return count >= kMaxLength;
|
|
|
}
|
|
|
|
|
|
template <typename K>
|
|
|
size_type BucketNumber(const K& k) const {
|
|
|
|
|
|
|
|
|
uint64 h = hash_function()(k) ^ seed_;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uint64 kPhi = uint64{0x9e3779b97f4a7c15};
|
|
|
return ((kPhi * h) >> 32) & (num_buckets_ - 1);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_type TableSize(size_type n) {
|
|
|
return n < static_cast<size_type>(kMinTableSize)
|
|
|
? static_cast<size_type>(kMinTableSize)
|
|
|
: n;
|
|
|
}
|
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
U* Alloc(size_type n) {
|
|
|
using alloc_type = typename Allocator::template rebind<U>::other;
|
|
|
return alloc_type(alloc_).allocate(n);
|
|
|
}
|
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
void Dealloc(U* t, size_type n) {
|
|
|
using alloc_type = typename Allocator::template rebind<U>::other;
|
|
|
alloc_type(alloc_).deallocate(t, n);
|
|
|
}
|
|
|
|
|
|
void DestroyNode(Node* node) {
|
|
|
if (alloc_.arena() == nullptr) {
|
|
|
delete node;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void DestroyTree(Tree* tree) {
|
|
|
if (alloc_.arena() == nullptr) {
|
|
|
delete tree;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void** CreateEmptyTable(size_type n) {
|
|
|
GOOGLE_DCHECK(n >= kMinTableSize);
|
|
|
GOOGLE_DCHECK_EQ(n & (n - 1), 0);
|
|
|
void** result = Alloc<void*>(n);
|
|
|
memset(result, 0, n * sizeof(result[0]));
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
size_type Seed() const {
|
|
|
|
|
|
|
|
|
|
|
|
size_type s = reinterpret_cast<uintptr_t>(this) >> 12;
|
|
|
#if defined(__x86_64__) && defined(__GNUC__) && \
|
|
|
!defined(GOOGLE_PROTOBUF_NO_RDTSC)
|
|
|
uint32 hi, lo;
|
|
|
asm("rdtsc" : "=a"(lo), "=d"(hi));
|
|
|
s += ((static_cast<uint64>(hi) << 32) | lo);
|
|
|
#endif
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
friend class Arena;
|
|
|
using InternalArenaConstructable_ = void;
|
|
|
using DestructorSkippable_ = void;
|
|
|
|
|
|
size_type num_elements_;
|
|
|
size_type num_buckets_;
|
|
|
size_type seed_;
|
|
|
size_type index_of_first_non_null_;
|
|
|
void** table_;
|
|
|
Allocator alloc_;
|
|
|
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(InnerMap);
|
|
|
};
|
|
|
|
|
|
template <typename LookupKey>
|
|
|
using key_arg = typename internal::TransparentSupport<
|
|
|
key_type>::template key_arg<LookupKey>;
|
|
|
|
|
|
public:
|
|
|
|
|
|
class const_iterator {
|
|
|
using InnerIt = typename InnerMap::const_iterator;
|
|
|
|
|
|
public:
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
using value_type = typename Map::value_type;
|
|
|
using difference_type = ptrdiff_t;
|
|
|
using pointer = const value_type*;
|
|
|
using reference = const value_type&;
|
|
|
|
|
|
const_iterator() {}
|
|
|
explicit const_iterator(const InnerIt& it) : it_(it) {}
|
|
|
|
|
|
const_reference operator*() const { return *it_; }
|
|
|
const_pointer operator->() const { return &(operator*()); }
|
|
|
|
|
|
const_iterator& operator++() {
|
|
|
++it_;
|
|
|
return *this;
|
|
|
}
|
|
|
const_iterator operator++(int) { return const_iterator(it_++); }
|
|
|
|
|
|
friend bool operator==(const const_iterator& a, const const_iterator& b) {
|
|
|
return a.it_ == b.it_;
|
|
|
}
|
|
|
friend bool operator!=(const const_iterator& a, const const_iterator& b) {
|
|
|
return !(a == b);
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
InnerIt it_;
|
|
|
};
|
|
|
|
|
|
class iterator {
|
|
|
using InnerIt = typename InnerMap::iterator;
|
|
|
|
|
|
public:
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
using value_type = typename Map::value_type;
|
|
|
using difference_type = ptrdiff_t;
|
|
|
using pointer = value_type*;
|
|
|
using reference = value_type&;
|
|
|
|
|
|
iterator() {}
|
|
|
explicit iterator(const InnerIt& it) : it_(it) {}
|
|
|
|
|
|
reference operator*() const { return *it_; }
|
|
|
pointer operator->() const { return &(operator*()); }
|
|
|
|
|
|
iterator& operator++() {
|
|
|
++it_;
|
|
|
return *this;
|
|
|
}
|
|
|
iterator operator++(int) { return iterator(it_++); }
|
|
|
|
|
|
|
|
|
operator const_iterator() const {
|
|
|
return const_iterator(typename InnerMap::const_iterator(it_));
|
|
|
}
|
|
|
|
|
|
friend bool operator==(const iterator& a, const iterator& b) {
|
|
|
return a.it_ == b.it_;
|
|
|
}
|
|
|
friend bool operator!=(const iterator& a, const iterator& b) {
|
|
|
return !(a == b);
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
friend class Map;
|
|
|
|
|
|
InnerIt it_;
|
|
|
};
|
|
|
|
|
|
iterator begin() { return iterator(elements_->begin()); }
|
|
|
iterator end() { return iterator(elements_->end()); }
|
|
|
const_iterator begin() const {
|
|
|
return const_iterator(iterator(elements_->begin()));
|
|
|
}
|
|
|
const_iterator end() const {
|
|
|
return const_iterator(iterator(elements_->end()));
|
|
|
}
|
|
|
const_iterator cbegin() const { return begin(); }
|
|
|
const_iterator cend() const { return end(); }
|
|
|
|
|
|
|
|
|
size_type size() const { return elements_->size(); }
|
|
|
bool empty() const { return size() == 0; }
|
|
|
|
|
|
|
|
|
T& operator[](const key_type& key) { return (*elements_)[key].second; }
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
const T& at(const key_arg<K>& key) const {
|
|
|
const_iterator it = find(key);
|
|
|
GOOGLE_CHECK(it != end()) << "key not found: " << static_cast<Key>(key);
|
|
|
return it->second;
|
|
|
}
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
T& at(const key_arg<K>& key) {
|
|
|
iterator it = find(key);
|
|
|
GOOGLE_CHECK(it != end()) << "key not found: " << static_cast<Key>(key);
|
|
|
return it->second;
|
|
|
}
|
|
|
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
size_type count(const key_arg<K>& key) const {
|
|
|
return find(key) == end() ? 0 : 1;
|
|
|
}
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
const_iterator find(const key_arg<K>& key) const {
|
|
|
return const_iterator(iterator(elements_->find(key)));
|
|
|
}
|
|
|
template <typename K = key_type>
|
|
|
iterator find(const key_arg<K>& key) {
|
|
|
return iterator(elements_->find(key));
|
|
|
}
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
bool contains(const key_arg<K>& key) const {
|
|
|
return find(key) != end();
|
|
|
}
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
std::pair<const_iterator, const_iterator> equal_range(
|
|
|
const key_arg<K>& key) const {
|
|
|
const_iterator it = find(key);
|
|
|
if (it == end()) {
|
|
|
return std::pair<const_iterator, const_iterator>(it, it);
|
|
|
} else {
|
|
|
const_iterator begin = it++;
|
|
|
return std::pair<const_iterator, const_iterator>(begin, it);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
std::pair<iterator, iterator> equal_range(const key_arg<K>& key) {
|
|
|
iterator it = find(key);
|
|
|
if (it == end()) {
|
|
|
return std::pair<iterator, iterator>(it, it);
|
|
|
} else {
|
|
|
iterator begin = it++;
|
|
|
return std::pair<iterator, iterator>(begin, it);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
std::pair<iterator, bool> insert(const value_type& value) {
|
|
|
std::pair<typename InnerMap::iterator, bool> p =
|
|
|
elements_->insert(value.first);
|
|
|
if (p.second) {
|
|
|
p.first->second = value.second;
|
|
|
}
|
|
|
return std::pair<iterator, bool>(iterator(p.first), p.second);
|
|
|
}
|
|
|
template <class InputIt>
|
|
|
void insert(InputIt first, InputIt last) {
|
|
|
for (InputIt it = first; it != last; ++it) {
|
|
|
iterator exist_it = find(it->first);
|
|
|
if (exist_it == end()) {
|
|
|
operator[](it->first) = it->second;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void insert(std::initializer_list<value_type> values) {
|
|
|
insert(values.begin(), values.end());
|
|
|
}
|
|
|
|
|
|
|
|
|
template <typename K = key_type>
|
|
|
size_type erase(const key_arg<K>& key) {
|
|
|
iterator it = find(key);
|
|
|
if (it == end()) {
|
|
|
return 0;
|
|
|
} else {
|
|
|
erase(it);
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
iterator erase(iterator pos) {
|
|
|
iterator i = pos++;
|
|
|
elements_->erase(i.it_);
|
|
|
return pos;
|
|
|
}
|
|
|
void erase(iterator first, iterator last) {
|
|
|
while (first != last) {
|
|
|
first = erase(first);
|
|
|
}
|
|
|
}
|
|
|
void clear() { elements_->clear(); }
|
|
|
|
|
|
|
|
|
Map& operator=(const Map& other) {
|
|
|
if (this != &other) {
|
|
|
clear();
|
|
|
insert(other.begin(), other.end());
|
|
|
}
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
void swap(Map& other) {
|
|
|
if (arena_ == other.arena_) {
|
|
|
std::swap(default_enum_value_, other.default_enum_value_);
|
|
|
std::swap(elements_, other.elements_);
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
Map copy = *this;
|
|
|
*this = other;
|
|
|
other = copy;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hasher hash_function() const { return elements_->hash_function(); }
|
|
|
|
|
|
private:
|
|
|
|
|
|
void SetDefaultEnumValue(int default_enum_value) {
|
|
|
default_enum_value_ = default_enum_value;
|
|
|
}
|
|
|
|
|
|
Arena* arena_;
|
|
|
int default_enum_value_;
|
|
|
InnerMap* elements_;
|
|
|
|
|
|
friend class Arena;
|
|
|
using InternalArenaConstructable_ = void;
|
|
|
using DestructorSkippable_ = void;
|
|
|
template <typename Derived, typename K, typename V,
|
|
|
internal::WireFormatLite::FieldType key_wire_type,
|
|
|
internal::WireFormatLite::FieldType value_wire_type,
|
|
|
int default_enum_value>
|
|
|
friend class internal::MapFieldLite;
|
|
|
};
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#include <google/protobuf/port_undef.inc>
|
|
|
|
|
|
#endif
|
|
|
|