map.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // This file defines the map container and its helpers to support protobuf maps.
  31. //
  32. // The Map and MapIterator types are provided by this header file.
  33. // Please avoid using other types defined here, unless they are public
  34. // types within Map or MapIterator, such as Map::value_type.
  35. #ifndef GOOGLE_PROTOBUF_MAP_H__
  36. #define GOOGLE_PROTOBUF_MAP_H__
  37. #include <functional>
  38. #include <initializer_list>
  39. #include <iterator>
  40. #include <limits> // To support Visual Studio 2008
  41. #include <map>
  42. #include <string>
  43. #include <type_traits>
  44. #include <utility>
  45. #if defined(__cpp_lib_string_view)
  46. #include <string_view>
  47. #endif // defined(__cpp_lib_string_view)
  48. #if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__)
  49. #include <mach/mach_time.h>
  50. #endif
  51. #include <google/protobuf/stubs/common.h>
  52. #include <google/protobuf/arena.h>
  53. #include <google/protobuf/generated_enum_util.h>
  54. #include <google/protobuf/map_type_handler.h>
  55. #include <google/protobuf/stubs/hash.h>
  56. #ifdef SWIG
  57. #error "You cannot SWIG proto headers"
  58. #endif
  59. #include <google/protobuf/port_def.inc>
  60. namespace google {
  61. namespace protobuf {
  62. template <typename Key, typename T>
  63. class Map;
  64. class MapIterator;
  65. template <typename Enum>
  66. struct is_proto_enum;
  67. namespace internal {
  68. template <typename Derived, typename Key, typename T,
  69. WireFormatLite::FieldType key_wire_type,
  70. WireFormatLite::FieldType value_wire_type>
  71. class MapFieldLite;
  72. template <typename Derived, typename Key, typename T,
  73. WireFormatLite::FieldType key_wire_type,
  74. WireFormatLite::FieldType value_wire_type>
  75. class MapField;
  76. template <typename Key, typename T>
  77. class TypeDefinedMapFieldBase;
  78. class DynamicMapField;
  79. class GeneratedMessageReflection;
  80. // re-implement std::allocator to use arena allocator for memory allocation.
  81. // Used for Map implementation. Users should not use this class
  82. // directly.
  83. template <typename U>
  84. class MapAllocator {
  85. public:
  86. using value_type = U;
  87. using pointer = value_type*;
  88. using const_pointer = const value_type*;
  89. using reference = value_type&;
  90. using const_reference = const value_type&;
  91. using size_type = size_t;
  92. using difference_type = ptrdiff_t;
  93. constexpr MapAllocator() : arena_(nullptr) {}
  94. explicit constexpr MapAllocator(Arena* arena) : arena_(arena) {}
  95. template <typename X>
  96. MapAllocator(const MapAllocator<X>& allocator) // NOLINT(runtime/explicit)
  97. : arena_(allocator.arena()) {}
  98. pointer allocate(size_type n, const void* /* hint */ = nullptr) {
  99. // If arena is not given, malloc needs to be called which doesn't
  100. // construct element object.
  101. if (arena_ == nullptr) {
  102. return static_cast<pointer>(::operator new(n * sizeof(value_type)));
  103. } else {
  104. return reinterpret_cast<pointer>(
  105. Arena::CreateArray<uint8_t>(arena_, n * sizeof(value_type)));
  106. }
  107. }
  108. void deallocate(pointer p, size_type n) {
  109. if (arena_ == nullptr) {
  110. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  111. ::operator delete(p, n * sizeof(value_type));
  112. #else
  113. (void)n;
  114. ::operator delete(p);
  115. #endif
  116. }
  117. }
  118. #if !defined(GOOGLE_PROTOBUF_OS_APPLE) && !defined(GOOGLE_PROTOBUF_OS_NACL) && \
  119. !defined(GOOGLE_PROTOBUF_OS_EMSCRIPTEN)
  120. template <class NodeType, class... Args>
  121. void construct(NodeType* p, Args&&... args) {
  122. // Clang 3.6 doesn't compile static casting to void* directly. (Issue
  123. // #1266) According C++ standard 5.2.9/1: "The static_cast operator shall
  124. // not cast away constness". So first the maybe const pointer is casted to
  125. // const void* and after the const void* is const casted.
  126. new (const_cast<void*>(static_cast<const void*>(p)))
  127. NodeType(std::forward<Args>(args)...);
  128. }
  129. template <class NodeType>
  130. void destroy(NodeType* p) {
  131. p->~NodeType();
  132. }
  133. #else
  134. void construct(pointer p, const_reference t) { new (p) value_type(t); }
  135. void destroy(pointer p) { p->~value_type(); }
  136. #endif
  137. template <typename X>
  138. struct rebind {
  139. using other = MapAllocator<X>;
  140. };
  141. template <typename X>
  142. bool operator==(const MapAllocator<X>& other) const {
  143. return arena_ == other.arena_;
  144. }
  145. template <typename X>
  146. bool operator!=(const MapAllocator<X>& other) const {
  147. return arena_ != other.arena_;
  148. }
  149. // To support Visual Studio 2008
  150. size_type max_size() const {
  151. // parentheses around (std::...:max) prevents macro warning of max()
  152. return (std::numeric_limits<size_type>::max)();
  153. }
  154. // To support gcc-4.4, which does not properly
  155. // support templated friend classes
  156. Arena* arena() const { return arena_; }
  157. private:
  158. using DestructorSkippable_ = void;
  159. Arena* arena_;
  160. };
  161. template <typename T>
  162. using KeyForTree =
  163. typename std::conditional<std::is_scalar<T>::value, T,
  164. std::reference_wrapper<const T>>::type;
  165. // Default case: Not transparent.
  166. // We use std::hash<key_type>/std::less<key_type> and all the lookup functions
  167. // only accept `key_type`.
  168. template <typename key_type>
  169. struct TransparentSupport {
  170. using hash = std::hash<key_type>;
  171. using less = std::less<key_type>;
  172. static bool Equals(const key_type& a, const key_type& b) { return a == b; }
  173. template <typename K>
  174. using key_arg = key_type;
  175. };
  176. #if defined(__cpp_lib_string_view)
  177. // If std::string_view is available, we add transparent support for std::string
  178. // keys. We use std::hash<std::string_view> as it supports the input types we
  179. // care about. The lookup functions accept arbitrary `K`. This will include any
  180. // key type that is convertible to std::string_view.
  181. template <>
  182. struct TransparentSupport<std::string> {
  183. static std::string_view ImplicitConvert(std::string_view str) { return str; }
  184. // If the element is not convertible to std::string_view, try to convert to
  185. // std::string first.
  186. // The template makes this overload lose resolution when both have the same
  187. // rank otherwise.
  188. template <typename = void>
  189. static std::string_view ImplicitConvert(const std::string& str) {
  190. return str;
  191. }
  192. struct hash : private std::hash<std::string_view> {
  193. using is_transparent = void;
  194. template <typename T>
  195. size_t operator()(const T& str) const {
  196. return base()(ImplicitConvert(str));
  197. }
  198. private:
  199. const std::hash<std::string_view>& base() const { return *this; }
  200. };
  201. struct less {
  202. using is_transparent = void;
  203. template <typename T, typename U>
  204. bool operator()(const T& t, const U& u) const {
  205. return ImplicitConvert(t) < ImplicitConvert(u);
  206. }
  207. };
  208. template <typename T, typename U>
  209. static bool Equals(const T& t, const U& u) {
  210. return ImplicitConvert(t) == ImplicitConvert(u);
  211. }
  212. template <typename K>
  213. using key_arg = K;
  214. };
  215. #endif // defined(__cpp_lib_string_view)
  216. template <typename Key>
  217. using TreeForMap =
  218. std::map<KeyForTree<Key>, void*, typename TransparentSupport<Key>::less,
  219. MapAllocator<std::pair<const KeyForTree<Key>, void*>>>;
  220. inline bool TableEntryIsEmpty(void* const* table, size_t b) {
  221. return table[b] == nullptr;
  222. }
  223. inline bool TableEntryIsNonEmptyList(void* const* table, size_t b) {
  224. return table[b] != nullptr && table[b] != table[b ^ 1];
  225. }
  226. inline bool TableEntryIsTree(void* const* table, size_t b) {
  227. return !TableEntryIsEmpty(table, b) && !TableEntryIsNonEmptyList(table, b);
  228. }
  229. inline bool TableEntryIsList(void* const* table, size_t b) {
  230. return !TableEntryIsTree(table, b);
  231. }
  232. // This captures all numeric types.
  233. inline size_t MapValueSpaceUsedExcludingSelfLong(bool) { return 0; }
  234. inline size_t MapValueSpaceUsedExcludingSelfLong(const std::string& str) {
  235. return StringSpaceUsedExcludingSelfLong(str);
  236. }
  237. template <typename T,
  238. typename = decltype(std::declval<const T&>().SpaceUsedLong())>
  239. size_t MapValueSpaceUsedExcludingSelfLong(const T& message) {
  240. return message.SpaceUsedLong() - sizeof(T);
  241. }
  242. constexpr size_t kGlobalEmptyTableSize = 1;
  243. PROTOBUF_EXPORT extern void* const kGlobalEmptyTable[kGlobalEmptyTableSize];
  244. // Space used for the table, trees, and nodes.
  245. // Does not include the indirect space used. Eg the data of a std::string.
  246. template <typename Key>
  247. PROTOBUF_NOINLINE size_t SpaceUsedInTable(void** table, size_t num_buckets,
  248. size_t num_elements,
  249. size_t sizeof_node) {
  250. size_t size = 0;
  251. // The size of the table.
  252. size += sizeof(void*) * num_buckets;
  253. // All the nodes.
  254. size += sizeof_node * num_elements;
  255. // For each tree, count the overhead of the those nodes.
  256. // Two buckets at a time because we only care about trees.
  257. for (size_t b = 0; b < num_buckets; b += 2) {
  258. if (internal::TableEntryIsTree(table, b)) {
  259. using Tree = TreeForMap<Key>;
  260. Tree* tree = static_cast<Tree*>(table[b]);
  261. // Estimated cost of the red-black tree nodes, 3 pointers plus a
  262. // bool (plus alignment, so 4 pointers).
  263. size += tree->size() *
  264. (sizeof(typename Tree::value_type) + sizeof(void*) * 4);
  265. }
  266. }
  267. return size;
  268. }
  269. template <typename Map,
  270. typename = typename std::enable_if<
  271. !std::is_scalar<typename Map::key_type>::value ||
  272. !std::is_scalar<typename Map::mapped_type>::value>::type>
  273. size_t SpaceUsedInValues(const Map* map) {
  274. size_t size = 0;
  275. for (const auto& v : *map) {
  276. size += internal::MapValueSpaceUsedExcludingSelfLong(v.first) +
  277. internal::MapValueSpaceUsedExcludingSelfLong(v.second);
  278. }
  279. return size;
  280. }
  281. inline size_t SpaceUsedInValues(const void*) { return 0; }
  282. } // namespace internal
  283. // This is the class for Map's internal value_type. Instead of using
  284. // std::pair as value_type, we use this class which provides us more control of
  285. // its process of construction and destruction.
  286. template <typename Key, typename T>
  287. struct MapPair {
  288. using first_type = const Key;
  289. using second_type = T;
  290. MapPair(const Key& other_first, const T& other_second)
  291. : first(other_first), second(other_second) {}
  292. explicit MapPair(const Key& other_first) : first(other_first), second() {}
  293. explicit MapPair(Key&& other_first)
  294. : first(std::move(other_first)), second() {}
  295. MapPair(const MapPair& other) : first(other.first), second(other.second) {}
  296. ~MapPair() {}
  297. // Implicitly convertible to std::pair of compatible types.
  298. template <typename T1, typename T2>
  299. operator std::pair<T1, T2>() const { // NOLINT(runtime/explicit)
  300. return std::pair<T1, T2>(first, second);
  301. }
  302. const Key first;
  303. T second;
  304. private:
  305. friend class Arena;
  306. friend class Map<Key, T>;
  307. };
  308. // Map is an associative container type used to store protobuf map
  309. // fields. Each Map instance may or may not use a different hash function, a
  310. // different iteration order, and so on. E.g., please don't examine
  311. // implementation details to decide if the following would work:
  312. // Map<int, int> m0, m1;
  313. // m0[0] = m1[0] = m0[1] = m1[1] = 0;
  314. // assert(m0.begin()->first == m1.begin()->first); // Bug!
  315. //
  316. // Map's interface is similar to std::unordered_map, except that Map is not
  317. // designed to play well with exceptions.
  318. template <typename Key, typename T>
  319. class Map {
  320. public:
  321. using key_type = Key;
  322. using mapped_type = T;
  323. using value_type = MapPair<Key, T>;
  324. using pointer = value_type*;
  325. using const_pointer = const value_type*;
  326. using reference = value_type&;
  327. using const_reference = const value_type&;
  328. using size_type = size_t;
  329. using hasher = typename internal::TransparentSupport<Key>::hash;
  330. constexpr Map() : elements_(nullptr) {}
  331. explicit Map(Arena* arena) : elements_(arena) {}
  332. Map(const Map& other) : Map() { insert(other.begin(), other.end()); }
  333. Map(Map&& other) noexcept : Map() {
  334. if (other.arena() != nullptr) {
  335. *this = other;
  336. } else {
  337. swap(other);
  338. }
  339. }
  340. Map& operator=(Map&& other) noexcept {
  341. if (this != &other) {
  342. if (arena() != other.arena()) {
  343. *this = other;
  344. } else {
  345. swap(other);
  346. }
  347. }
  348. return *this;
  349. }
  350. template <class InputIt>
  351. Map(const InputIt& first, const InputIt& last) : Map() {
  352. insert(first, last);
  353. }
  354. ~Map() {}
  355. private:
  356. using Allocator = internal::MapAllocator<void*>;
  357. // InnerMap is a generic hash-based map. It doesn't contain any
  358. // protocol-buffer-specific logic. It is a chaining hash map with the
  359. // additional feature that some buckets can be converted to use an ordered
  360. // container. This ensures O(lg n) bounds on find, insert, and erase, while
  361. // avoiding the overheads of ordered containers most of the time.
  362. //
  363. // The implementation doesn't need the full generality of unordered_map,
  364. // and it doesn't have it. More bells and whistles can be added as needed.
  365. // Some implementation details:
  366. // 1. The hash function has type hasher and the equality function
  367. // equal_to<Key>. We inherit from hasher to save space
  368. // (empty-base-class optimization).
  369. // 2. The number of buckets is a power of two.
  370. // 3. Buckets are converted to trees in pairs: if we convert bucket b then
  371. // buckets b and b^1 will share a tree. Invariant: buckets b and b^1 have
  372. // the same non-null value iff they are sharing a tree. (An alternative
  373. // implementation strategy would be to have a tag bit per bucket.)
  374. // 4. As is typical for hash_map and such, the Keys and Values are always
  375. // stored in linked list nodes. Pointers to elements are never invalidated
  376. // until the element is deleted.
  377. // 5. The trees' payload type is pointer to linked-list node. Tree-converting
  378. // a bucket doesn't copy Key-Value pairs.
  379. // 6. Once we've tree-converted a bucket, it is never converted back. However,
  380. // the items a tree contains may wind up assigned to trees or lists upon a
  381. // rehash.
  382. // 7. The code requires no C++ features from C++14 or later.
  383. // 8. Mutations to a map do not invalidate the map's iterators, pointers to
  384. // elements, or references to elements.
  385. // 9. Except for erase(iterator), any non-const method can reorder iterators.
  386. // 10. InnerMap uses KeyForTree<Key> when using the Tree representation, which
  387. // is either `Key`, if Key is a scalar, or `reference_wrapper<const Key>`
  388. // otherwise. This avoids unnecessary copies of string keys, for example.
  389. class InnerMap : private hasher {
  390. public:
  391. explicit constexpr InnerMap(Arena* arena)
  392. : hasher(),
  393. num_elements_(0),
  394. num_buckets_(internal::kGlobalEmptyTableSize),
  395. seed_(0),
  396. index_of_first_non_null_(internal::kGlobalEmptyTableSize),
  397. table_(const_cast<void**>(internal::kGlobalEmptyTable)),
  398. alloc_(arena) {}
  399. ~InnerMap() {
  400. if (alloc_.arena() == nullptr &&
  401. num_buckets_ != internal::kGlobalEmptyTableSize) {
  402. clear();
  403. Dealloc<void*>(table_, num_buckets_);
  404. }
  405. }
  406. private:
  407. enum { kMinTableSize = 8 };
  408. // Linked-list nodes, as one would expect for a chaining hash table.
  409. struct Node {
  410. value_type kv;
  411. Node* next;
  412. };
  413. // Trees. The payload type is a copy of Key, so that we can query the tree
  414. // with Keys that are not in any particular data structure.
  415. // The value is a void* pointing to Node. We use void* instead of Node* to
  416. // avoid code bloat. That way there is only one instantiation of the tree
  417. // class per key type.
  418. using Tree = internal::TreeForMap<Key>;
  419. using TreeIterator = typename Tree::iterator;
  420. static Node* NodeFromTreeIterator(TreeIterator it) {
  421. return static_cast<Node*>(it->second);
  422. }
  423. // iterator and const_iterator are instantiations of iterator_base.
  424. template <typename KeyValueType>
  425. class iterator_base {
  426. public:
  427. using reference = KeyValueType&;
  428. using pointer = KeyValueType*;
  429. // Invariants:
  430. // node_ is always correct. This is handy because the most common
  431. // operations are operator* and operator-> and they only use node_.
  432. // When node_ is set to a non-null value, all the other non-const fields
  433. // are updated to be correct also, but those fields can become stale
  434. // if the underlying map is modified. When those fields are needed they
  435. // are rechecked, and updated if necessary.
  436. iterator_base() : node_(nullptr), m_(nullptr), bucket_index_(0) {}
  437. explicit iterator_base(const InnerMap* m) : m_(m) {
  438. SearchFrom(m->index_of_first_non_null_);
  439. }
  440. // Any iterator_base can convert to any other. This is overkill, and we
  441. // rely on the enclosing class to use it wisely. The standard "iterator
  442. // can convert to const_iterator" is OK but the reverse direction is not.
  443. template <typename U>
  444. explicit iterator_base(const iterator_base<U>& it)
  445. : node_(it.node_), m_(it.m_), bucket_index_(it.bucket_index_) {}
  446. iterator_base(Node* n, const InnerMap* m, size_type index)
  447. : node_(n), m_(m), bucket_index_(index) {}
  448. iterator_base(TreeIterator tree_it, const InnerMap* m, size_type index)
  449. : node_(NodeFromTreeIterator(tree_it)), m_(m), bucket_index_(index) {
  450. // Invariant: iterators that use buckets with trees have an even
  451. // bucket_index_.
  452. GOOGLE_DCHECK_EQ(bucket_index_ % 2, 0u);
  453. }
  454. // Advance through buckets, looking for the first that isn't empty.
  455. // If nothing non-empty is found then leave node_ == nullptr.
  456. void SearchFrom(size_type start_bucket) {
  457. GOOGLE_DCHECK(m_->index_of_first_non_null_ == m_->num_buckets_ ||
  458. m_->table_[m_->index_of_first_non_null_] != nullptr);
  459. node_ = nullptr;
  460. for (bucket_index_ = start_bucket; bucket_index_ < m_->num_buckets_;
  461. bucket_index_++) {
  462. if (m_->TableEntryIsNonEmptyList(bucket_index_)) {
  463. node_ = static_cast<Node*>(m_->table_[bucket_index_]);
  464. break;
  465. } else if (m_->TableEntryIsTree(bucket_index_)) {
  466. Tree* tree = static_cast<Tree*>(m_->table_[bucket_index_]);
  467. GOOGLE_DCHECK(!tree->empty());
  468. node_ = NodeFromTreeIterator(tree->begin());
  469. break;
  470. }
  471. }
  472. }
  473. reference operator*() const { return node_->kv; }
  474. pointer operator->() const { return &(operator*()); }
  475. friend bool operator==(const iterator_base& a, const iterator_base& b) {
  476. return a.node_ == b.node_;
  477. }
  478. friend bool operator!=(const iterator_base& a, const iterator_base& b) {
  479. return a.node_ != b.node_;
  480. }
  481. iterator_base& operator++() {
  482. if (node_->next == nullptr) {
  483. TreeIterator tree_it;
  484. const bool is_list = revalidate_if_necessary(&tree_it);
  485. if (is_list) {
  486. SearchFrom(bucket_index_ + 1);
  487. } else {
  488. GOOGLE_DCHECK_EQ(bucket_index_ & 1, 0u);
  489. Tree* tree = static_cast<Tree*>(m_->table_[bucket_index_]);
  490. if (++tree_it == tree->end()) {
  491. SearchFrom(bucket_index_ + 2);
  492. } else {
  493. node_ = NodeFromTreeIterator(tree_it);
  494. }
  495. }
  496. } else {
  497. node_ = node_->next;
  498. }
  499. return *this;
  500. }
  501. iterator_base operator++(int /* unused */) {
  502. iterator_base tmp = *this;
  503. ++*this;
  504. return tmp;
  505. }
  506. // Assumes node_ and m_ are correct and non-null, but other fields may be
  507. // stale. Fix them as needed. Then return true iff node_ points to a
  508. // Node in a list. If false is returned then *it is modified to be
  509. // a valid iterator for node_.
  510. bool revalidate_if_necessary(TreeIterator* it) {
  511. GOOGLE_DCHECK(node_ != nullptr && m_ != nullptr);
  512. // Force bucket_index_ to be in range.
  513. bucket_index_ &= (m_->num_buckets_ - 1);
  514. // Common case: the bucket we think is relevant points to node_.
  515. if (m_->table_[bucket_index_] == static_cast<void*>(node_)) return true;
  516. // Less common: the bucket is a linked list with node_ somewhere in it,
  517. // but not at the head.
  518. if (m_->TableEntryIsNonEmptyList(bucket_index_)) {
  519. Node* l = static_cast<Node*>(m_->table_[bucket_index_]);
  520. while ((l = l->next) != nullptr) {
  521. if (l == node_) {
  522. return true;
  523. }
  524. }
  525. }
  526. // Well, bucket_index_ still might be correct, but probably
  527. // not. Revalidate just to be sure. This case is rare enough that we
  528. // don't worry about potential optimizations, such as having a custom
  529. // find-like method that compares Node* instead of the key.
  530. iterator_base i(m_->find(node_->kv.first, it));
  531. bucket_index_ = i.bucket_index_;
  532. return m_->TableEntryIsList(bucket_index_);
  533. }
  534. Node* node_;
  535. const InnerMap* m_;
  536. size_type bucket_index_;
  537. };
  538. public:
  539. using iterator = iterator_base<value_type>;
  540. using const_iterator = iterator_base<const value_type>;
  541. Arena* arena() const { return alloc_.arena(); }
  542. void Swap(InnerMap* other) {
  543. std::swap(num_elements_, other->num_elements_);
  544. std::swap(num_buckets_, other->num_buckets_);
  545. std::swap(seed_, other->seed_);
  546. std::swap(index_of_first_non_null_, other->index_of_first_non_null_);
  547. std::swap(table_, other->table_);
  548. std::swap(alloc_, other->alloc_);
  549. }
  550. iterator begin() { return iterator(this); }
  551. iterator end() { return iterator(); }
  552. const_iterator begin() const { return const_iterator(this); }
  553. const_iterator end() const { return const_iterator(); }
  554. void clear() {
  555. for (size_type b = 0; b < num_buckets_; b++) {
  556. if (TableEntryIsNonEmptyList(b)) {
  557. Node* node = static_cast<Node*>(table_[b]);
  558. table_[b] = nullptr;
  559. do {
  560. Node* next = node->next;
  561. DestroyNode(node);
  562. node = next;
  563. } while (node != nullptr);
  564. } else if (TableEntryIsTree(b)) {
  565. Tree* tree = static_cast<Tree*>(table_[b]);
  566. GOOGLE_DCHECK(table_[b] == table_[b + 1] && (b & 1) == 0);
  567. table_[b] = table_[b + 1] = nullptr;
  568. typename Tree::iterator tree_it = tree->begin();
  569. do {
  570. Node* node = NodeFromTreeIterator(tree_it);
  571. typename Tree::iterator next = tree_it;
  572. ++next;
  573. tree->erase(tree_it);
  574. DestroyNode(node);
  575. tree_it = next;
  576. } while (tree_it != tree->end());
  577. DestroyTree(tree);
  578. b++;
  579. }
  580. }
  581. num_elements_ = 0;
  582. index_of_first_non_null_ = num_buckets_;
  583. }
  584. const hasher& hash_function() const { return *this; }
  585. static size_type max_size() {
  586. return static_cast<size_type>(1) << (sizeof(void**) >= 8 ? 60 : 28);
  587. }
  588. size_type size() const { return num_elements_; }
  589. bool empty() const { return size() == 0; }
  590. template <typename K>
  591. iterator find(const K& k) {
  592. return iterator(FindHelper(k).first);
  593. }
  594. template <typename K>
  595. const_iterator find(const K& k) const {
  596. return FindHelper(k).first;
  597. }
  598. // Insert the key into the map, if not present. In that case, the value will
  599. // be value initialized.
  600. template <typename K>
  601. std::pair<iterator, bool> insert(K&& k) {
  602. std::pair<const_iterator, size_type> p = FindHelper(k);
  603. // Case 1: key was already present.
  604. if (p.first.node_ != nullptr)
  605. return std::make_pair(iterator(p.first), false);
  606. // Case 2: insert.
  607. if (ResizeIfLoadIsOutOfRange(num_elements_ + 1)) {
  608. p = FindHelper(k);
  609. }
  610. const size_type b = p.second; // bucket number
  611. // If K is not key_type, make the conversion to key_type explicit.
  612. using TypeToInit = typename std::conditional<
  613. std::is_same<typename std::decay<K>::type, key_type>::value, K&&,
  614. key_type>::type;
  615. Node* node = Alloc<Node>(1);
  616. // Even when arena is nullptr, CreateInArenaStorage is still used to
  617. // ensure the arena of submessage will be consistent. Otherwise,
  618. // submessage may have its own arena when message-owned arena is enabled.
  619. Arena::CreateInArenaStorage(const_cast<Key*>(&node->kv.first),
  620. alloc_.arena(),
  621. static_cast<TypeToInit>(std::forward<K>(k)));
  622. Arena::CreateInArenaStorage(&node->kv.second, alloc_.arena());
  623. iterator result = InsertUnique(b, node);
  624. ++num_elements_;
  625. return std::make_pair(result, true);
  626. }
  627. template <typename K>
  628. value_type& operator[](K&& k) {
  629. return *insert(std::forward<K>(k)).first;
  630. }
  631. void erase(iterator it) {
  632. GOOGLE_DCHECK_EQ(it.m_, this);
  633. typename Tree::iterator tree_it;
  634. const bool is_list = it.revalidate_if_necessary(&tree_it);
  635. size_type b = it.bucket_index_;
  636. Node* const item = it.node_;
  637. if (is_list) {
  638. GOOGLE_DCHECK(TableEntryIsNonEmptyList(b));
  639. Node* head = static_cast<Node*>(table_[b]);
  640. head = EraseFromLinkedList(item, head);
  641. table_[b] = static_cast<void*>(head);
  642. } else {
  643. GOOGLE_DCHECK(TableEntryIsTree(b));
  644. Tree* tree = static_cast<Tree*>(table_[b]);
  645. tree->erase(tree_it);
  646. if (tree->empty()) {
  647. // Force b to be the minimum of b and b ^ 1. This is important
  648. // only because we want index_of_first_non_null_ to be correct.
  649. b &= ~static_cast<size_type>(1);
  650. DestroyTree(tree);
  651. table_[b] = table_[b + 1] = nullptr;
  652. }
  653. }
  654. DestroyNode(item);
  655. --num_elements_;
  656. if (PROTOBUF_PREDICT_FALSE(b == index_of_first_non_null_)) {
  657. while (index_of_first_non_null_ < num_buckets_ &&
  658. table_[index_of_first_non_null_] == nullptr) {
  659. ++index_of_first_non_null_;
  660. }
  661. }
  662. }
  663. size_t SpaceUsedInternal() const {
  664. return internal::SpaceUsedInTable<Key>(table_, num_buckets_,
  665. num_elements_, sizeof(Node));
  666. }
  667. private:
  668. const_iterator find(const Key& k, TreeIterator* it) const {
  669. return FindHelper(k, it).first;
  670. }
  671. template <typename K>
  672. std::pair<const_iterator, size_type> FindHelper(const K& k) const {
  673. return FindHelper(k, nullptr);
  674. }
  675. template <typename K>
  676. std::pair<const_iterator, size_type> FindHelper(const K& k,
  677. TreeIterator* it) const {
  678. size_type b = BucketNumber(k);
  679. if (TableEntryIsNonEmptyList(b)) {
  680. Node* node = static_cast<Node*>(table_[b]);
  681. do {
  682. if (internal::TransparentSupport<Key>::Equals(node->kv.first, k)) {
  683. return std::make_pair(const_iterator(node, this, b), b);
  684. } else {
  685. node = node->next;
  686. }
  687. } while (node != nullptr);
  688. } else if (TableEntryIsTree(b)) {
  689. GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]);
  690. b &= ~static_cast<size_t>(1);
  691. Tree* tree = static_cast<Tree*>(table_[b]);
  692. auto tree_it = tree->find(k);
  693. if (tree_it != tree->end()) {
  694. if (it != nullptr) *it = tree_it;
  695. return std::make_pair(const_iterator(tree_it, this, b), b);
  696. }
  697. }
  698. return std::make_pair(end(), b);
  699. }
  700. // Insert the given Node in bucket b. If that would make bucket b too big,
  701. // and bucket b is not a tree, create a tree for buckets b and b^1 to share.
  702. // Requires count(*KeyPtrFromNodePtr(node)) == 0 and that b is the correct
  703. // bucket. num_elements_ is not modified.
  704. iterator InsertUnique(size_type b, Node* node) {
  705. GOOGLE_DCHECK(index_of_first_non_null_ == num_buckets_ ||
  706. table_[index_of_first_non_null_] != nullptr);
  707. // In practice, the code that led to this point may have already
  708. // determined whether we are inserting into an empty list, a short list,
  709. // or whatever. But it's probably cheap enough to recompute that here;
  710. // it's likely that we're inserting into an empty or short list.
  711. iterator result;
  712. GOOGLE_DCHECK(find(node->kv.first) == end());
  713. if (TableEntryIsEmpty(b)) {
  714. result = InsertUniqueInList(b, node);
  715. } else if (TableEntryIsNonEmptyList(b)) {
  716. if (PROTOBUF_PREDICT_FALSE(TableEntryIsTooLong(b))) {
  717. TreeConvert(b);
  718. result = InsertUniqueInTree(b, node);
  719. GOOGLE_DCHECK_EQ(result.bucket_index_, b & ~static_cast<size_type>(1));
  720. } else {
  721. // Insert into a pre-existing list. This case cannot modify
  722. // index_of_first_non_null_, so we skip the code to update it.
  723. return InsertUniqueInList(b, node);
  724. }
  725. } else {
  726. // Insert into a pre-existing tree. This case cannot modify
  727. // index_of_first_non_null_, so we skip the code to update it.
  728. return InsertUniqueInTree(b, node);
  729. }
  730. // parentheses around (std::min) prevents macro expansion of min(...)
  731. index_of_first_non_null_ =
  732. (std::min)(index_of_first_non_null_, result.bucket_index_);
  733. return result;
  734. }
  735. // Returns whether we should insert after the head of the list. For
  736. // non-optimized builds, we randomly decide whether to insert right at the
  737. // head of the list or just after the head. This helps add a little bit of
  738. // non-determinism to the map ordering.
  739. bool ShouldInsertAfterHead(void* node) {
  740. #ifdef NDEBUG
  741. (void)node;
  742. return false;
  743. #else
  744. // Doing modulo with a prime mixes the bits more.
  745. return (reinterpret_cast<uintptr_t>(node) ^ seed_) % 13 > 6;
  746. #endif
  747. }
  748. // Helper for InsertUnique. Handles the case where bucket b is a
  749. // not-too-long linked list.
  750. iterator InsertUniqueInList(size_type b, Node* node) {
  751. if (table_[b] != nullptr && ShouldInsertAfterHead(node)) {
  752. Node* first = static_cast<Node*>(table_[b]);
  753. node->next = first->next;
  754. first->next = node;
  755. return iterator(node, this, b);
  756. }
  757. node->next = static_cast<Node*>(table_[b]);
  758. table_[b] = static_cast<void*>(node);
  759. return iterator(node, this, b);
  760. }
  761. // Helper for InsertUnique. Handles the case where bucket b points to a
  762. // Tree.
  763. iterator InsertUniqueInTree(size_type b, Node* node) {
  764. GOOGLE_DCHECK_EQ(table_[b], table_[b ^ 1]);
  765. // Maintain the invariant that node->next is null for all Nodes in Trees.
  766. node->next = nullptr;
  767. return iterator(
  768. static_cast<Tree*>(table_[b])->insert({node->kv.first, node}).first,
  769. this, b & ~static_cast<size_t>(1));
  770. }
  771. // Returns whether it did resize. Currently this is only used when
  772. // num_elements_ increases, though it could be used in other situations.
  773. // It checks for load too low as well as load too high: because any number
  774. // of erases can occur between inserts, the load could be as low as 0 here.
  775. // Resizing to a lower size is not always helpful, but failing to do so can
  776. // destroy the expected big-O bounds for some operations. By having the
  777. // policy that sometimes we resize down as well as up, clients can easily
  778. // keep O(size()) = O(number of buckets) if they want that.
  779. bool ResizeIfLoadIsOutOfRange(size_type new_size) {
  780. const size_type kMaxMapLoadTimes16 = 12; // controls RAM vs CPU tradeoff
  781. const size_type hi_cutoff = num_buckets_ * kMaxMapLoadTimes16 / 16;
  782. const size_type lo_cutoff = hi_cutoff / 4;
  783. // We don't care how many elements are in trees. If a lot are,
  784. // we may resize even though there are many empty buckets. In
  785. // practice, this seems fine.
  786. if (PROTOBUF_PREDICT_FALSE(new_size >= hi_cutoff)) {
  787. if (num_buckets_ <= max_size() / 2) {
  788. Resize(num_buckets_ * 2);
  789. return true;
  790. }
  791. } else if (PROTOBUF_PREDICT_FALSE(new_size <= lo_cutoff &&
  792. num_buckets_ > kMinTableSize)) {
  793. size_type lg2_of_size_reduction_factor = 1;
  794. // It's possible we want to shrink a lot here... size() could even be 0.
  795. // So, estimate how much to shrink by making sure we don't shrink so
  796. // much that we would need to grow the table after a few inserts.
  797. const size_type hypothetical_size = new_size * 5 / 4 + 1;
  798. while ((hypothetical_size << lg2_of_size_reduction_factor) <
  799. hi_cutoff) {
  800. ++lg2_of_size_reduction_factor;
  801. }
  802. size_type new_num_buckets = std::max<size_type>(
  803. kMinTableSize, num_buckets_ >> lg2_of_size_reduction_factor);
  804. if (new_num_buckets != num_buckets_) {
  805. Resize(new_num_buckets);
  806. return true;
  807. }
  808. }
  809. return false;
  810. }
  811. // Resize to the given number of buckets.
  812. void Resize(size_t new_num_buckets) {
  813. if (num_buckets_ == internal::kGlobalEmptyTableSize) {
  814. // This is the global empty array.
  815. // Just overwrite with a new one. No need to transfer or free anything.
  816. num_buckets_ = index_of_first_non_null_ = kMinTableSize;
  817. table_ = CreateEmptyTable(num_buckets_);
  818. seed_ = Seed();
  819. return;
  820. }
  821. GOOGLE_DCHECK_GE(new_num_buckets, kMinTableSize);
  822. void** const old_table = table_;
  823. const size_type old_table_size = num_buckets_;
  824. num_buckets_ = new_num_buckets;
  825. table_ = CreateEmptyTable(num_buckets_);
  826. const size_type start = index_of_first_non_null_;
  827. index_of_first_non_null_ = num_buckets_;
  828. for (size_type i = start; i < old_table_size; i++) {
  829. if (internal::TableEntryIsNonEmptyList(old_table, i)) {
  830. TransferList(old_table, i);
  831. } else if (internal::TableEntryIsTree(old_table, i)) {
  832. TransferTree(old_table, i++);
  833. }
  834. }
  835. Dealloc<void*>(old_table, old_table_size);
  836. }
  837. void TransferList(void* const* table, size_type index) {
  838. Node* node = static_cast<Node*>(table[index]);
  839. do {
  840. Node* next = node->next;
  841. InsertUnique(BucketNumber(node->kv.first), node);
  842. node = next;
  843. } while (node != nullptr);
  844. }
  845. void TransferTree(void* const* table, size_type index) {
  846. Tree* tree = static_cast<Tree*>(table[index]);
  847. typename Tree::iterator tree_it = tree->begin();
  848. do {
  849. InsertUnique(BucketNumber(std::cref(tree_it->first).get()),
  850. NodeFromTreeIterator(tree_it));
  851. } while (++tree_it != tree->end());
  852. DestroyTree(tree);
  853. }
  854. Node* EraseFromLinkedList(Node* item, Node* head) {
  855. if (head == item) {
  856. return head->next;
  857. } else {
  858. head->next = EraseFromLinkedList(item, head->next);
  859. return head;
  860. }
  861. }
  862. bool TableEntryIsEmpty(size_type b) const {
  863. return internal::TableEntryIsEmpty(table_, b);
  864. }
  865. bool TableEntryIsNonEmptyList(size_type b) const {
  866. return internal::TableEntryIsNonEmptyList(table_, b);
  867. }
  868. bool TableEntryIsTree(size_type b) const {
  869. return internal::TableEntryIsTree(table_, b);
  870. }
  871. bool TableEntryIsList(size_type b) const {
  872. return internal::TableEntryIsList(table_, b);
  873. }
  874. void TreeConvert(size_type b) {
  875. GOOGLE_DCHECK(!TableEntryIsTree(b) && !TableEntryIsTree(b ^ 1));
  876. Tree* tree =
  877. Arena::Create<Tree>(alloc_.arena(), typename Tree::key_compare(),
  878. typename Tree::allocator_type(alloc_));
  879. size_type count = CopyListToTree(b, tree) + CopyListToTree(b ^ 1, tree);
  880. GOOGLE_DCHECK_EQ(count, tree->size());
  881. table_[b] = table_[b ^ 1] = static_cast<void*>(tree);
  882. }
  883. // Copy a linked list in the given bucket to a tree.
  884. // Returns the number of things it copied.
  885. size_type CopyListToTree(size_type b, Tree* tree) {
  886. size_type count = 0;
  887. Node* node = static_cast<Node*>(table_[b]);
  888. while (node != nullptr) {
  889. tree->insert({node->kv.first, node});
  890. ++count;
  891. Node* next = node->next;
  892. node->next = nullptr;
  893. node = next;
  894. }
  895. return count;
  896. }
  897. // Return whether table_[b] is a linked list that seems awfully long.
  898. // Requires table_[b] to point to a non-empty linked list.
  899. bool TableEntryIsTooLong(size_type b) {
  900. const size_type kMaxLength = 8;
  901. size_type count = 0;
  902. Node* node = static_cast<Node*>(table_[b]);
  903. do {
  904. ++count;
  905. node = node->next;
  906. } while (node != nullptr);
  907. // Invariant: no linked list ever is more than kMaxLength in length.
  908. GOOGLE_DCHECK_LE(count, kMaxLength);
  909. return count >= kMaxLength;
  910. }
  911. template <typename K>
  912. size_type BucketNumber(const K& k) const {
  913. // We xor the hash value against the random seed so that we effectively
  914. // have a random hash function.
  915. uint64_t h = hash_function()(k) ^ seed_;
  916. // We use the multiplication method to determine the bucket number from
  917. // the hash value. The constant kPhi (suggested by Knuth) is roughly
  918. // (sqrt(5) - 1) / 2 * 2^64.
  919. constexpr uint64_t kPhi = uint64_t{0x9e3779b97f4a7c15};
  920. return ((kPhi * h) >> 32) & (num_buckets_ - 1);
  921. }
  922. // Return a power of two no less than max(kMinTableSize, n).
  923. // Assumes either n < kMinTableSize or n is a power of two.
  924. size_type TableSize(size_type n) {
  925. return n < static_cast<size_type>(kMinTableSize)
  926. ? static_cast<size_type>(kMinTableSize)
  927. : n;
  928. }
  929. // Use alloc_ to allocate an array of n objects of type U.
  930. template <typename U>
  931. U* Alloc(size_type n) {
  932. using alloc_type = typename Allocator::template rebind<U>::other;
  933. return alloc_type(alloc_).allocate(n);
  934. }
  935. // Use alloc_ to deallocate an array of n objects of type U.
  936. template <typename U>
  937. void Dealloc(U* t, size_type n) {
  938. using alloc_type = typename Allocator::template rebind<U>::other;
  939. alloc_type(alloc_).deallocate(t, n);
  940. }
  941. void DestroyNode(Node* node) {
  942. if (alloc_.arena() == nullptr) {
  943. delete node;
  944. }
  945. }
  946. void DestroyTree(Tree* tree) {
  947. if (alloc_.arena() == nullptr) {
  948. delete tree;
  949. }
  950. }
  951. void** CreateEmptyTable(size_type n) {
  952. GOOGLE_DCHECK(n >= kMinTableSize);
  953. GOOGLE_DCHECK_EQ(n & (n - 1), 0u);
  954. void** result = Alloc<void*>(n);
  955. memset(result, 0, n * sizeof(result[0]));
  956. return result;
  957. }
  958. // Return a randomish value.
  959. size_type Seed() const {
  960. // We get a little bit of randomness from the address of the map. The
  961. // lower bits are not very random, due to alignment, so we discard them
  962. // and shift the higher bits into their place.
  963. size_type s = reinterpret_cast<uintptr_t>(this) >> 4;
  964. #if !defined(GOOGLE_PROTOBUF_NO_RDTSC)
  965. #if defined(__APPLE__)
  966. // Use a commpage-based fast time function on Apple environments (MacOS,
  967. // iOS, tvOS, watchOS, etc).
  968. s += mach_absolute_time();
  969. #elif defined(__x86_64__) && defined(__GNUC__)
  970. uint32_t hi, lo;
  971. asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  972. s += ((static_cast<uint64_t>(hi) << 32) | lo);
  973. #elif defined(__aarch64__) && defined(__GNUC__)
  974. // There is no rdtsc on ARMv8. CNTVCT_EL0 is the virtual counter of the
  975. // system timer. It runs at a different frequency than the CPU's, but is
  976. // the best source of time-based entropy we get.
  977. uint64_t virtual_timer_value;
  978. asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
  979. s += virtual_timer_value;
  980. #endif
  981. #endif // !defined(GOOGLE_PROTOBUF_NO_RDTSC)
  982. return s;
  983. }
  984. friend class Arena;
  985. using InternalArenaConstructable_ = void;
  986. using DestructorSkippable_ = void;
  987. size_type num_elements_;
  988. size_type num_buckets_;
  989. size_type seed_;
  990. size_type index_of_first_non_null_;
  991. void** table_; // an array with num_buckets_ entries
  992. Allocator alloc_;
  993. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(InnerMap);
  994. }; // end of class InnerMap
  995. template <typename LookupKey>
  996. using key_arg = typename internal::TransparentSupport<
  997. key_type>::template key_arg<LookupKey>;
  998. public:
  999. // Iterators
  1000. class const_iterator {
  1001. using InnerIt = typename InnerMap::const_iterator;
  1002. public:
  1003. using iterator_category = std::forward_iterator_tag;
  1004. using value_type = typename Map::value_type;
  1005. using difference_type = ptrdiff_t;
  1006. using pointer = const value_type*;
  1007. using reference = const value_type&;
  1008. const_iterator() {}
  1009. explicit const_iterator(const InnerIt& it) : it_(it) {}
  1010. const_reference operator*() const { return *it_; }
  1011. const_pointer operator->() const { return &(operator*()); }
  1012. const_iterator& operator++() {
  1013. ++it_;
  1014. return *this;
  1015. }
  1016. const_iterator operator++(int) { return const_iterator(it_++); }
  1017. friend bool operator==(const const_iterator& a, const const_iterator& b) {
  1018. return a.it_ == b.it_;
  1019. }
  1020. friend bool operator!=(const const_iterator& a, const const_iterator& b) {
  1021. return !(a == b);
  1022. }
  1023. private:
  1024. InnerIt it_;
  1025. };
  1026. class iterator {
  1027. using InnerIt = typename InnerMap::iterator;
  1028. public:
  1029. using iterator_category = std::forward_iterator_tag;
  1030. using value_type = typename Map::value_type;
  1031. using difference_type = ptrdiff_t;
  1032. using pointer = value_type*;
  1033. using reference = value_type&;
  1034. iterator() {}
  1035. explicit iterator(const InnerIt& it) : it_(it) {}
  1036. reference operator*() const { return *it_; }
  1037. pointer operator->() const { return &(operator*()); }
  1038. iterator& operator++() {
  1039. ++it_;
  1040. return *this;
  1041. }
  1042. iterator operator++(int) { return iterator(it_++); }
  1043. // Allow implicit conversion to const_iterator.
  1044. operator const_iterator() const { // NOLINT(runtime/explicit)
  1045. return const_iterator(typename InnerMap::const_iterator(it_));
  1046. }
  1047. friend bool operator==(const iterator& a, const iterator& b) {
  1048. return a.it_ == b.it_;
  1049. }
  1050. friend bool operator!=(const iterator& a, const iterator& b) {
  1051. return !(a == b);
  1052. }
  1053. private:
  1054. friend class Map;
  1055. InnerIt it_;
  1056. };
  1057. iterator begin() { return iterator(elements_.begin()); }
  1058. iterator end() { return iterator(elements_.end()); }
  1059. const_iterator begin() const { return const_iterator(elements_.begin()); }
  1060. const_iterator end() const { return const_iterator(elements_.end()); }
  1061. const_iterator cbegin() const { return begin(); }
  1062. const_iterator cend() const { return end(); }
  1063. // Capacity
  1064. size_type size() const { return elements_.size(); }
  1065. bool empty() const { return size() == 0; }
  1066. // Element access
  1067. template <typename K = key_type>
  1068. T& operator[](const key_arg<K>& key) {
  1069. return elements_[key].second;
  1070. }
  1071. template <
  1072. typename K = key_type,
  1073. // Disable for integral types to reduce code bloat.
  1074. typename = typename std::enable_if<!std::is_integral<K>::value>::type>
  1075. T& operator[](key_arg<K>&& key) {
  1076. return elements_[std::forward<K>(key)].second;
  1077. }
  1078. template <typename K = key_type>
  1079. const T& at(const key_arg<K>& key) const {
  1080. const_iterator it = find(key);
  1081. GOOGLE_CHECK(it != end()) << "key not found: " << static_cast<Key>(key);
  1082. return it->second;
  1083. }
  1084. template <typename K = key_type>
  1085. T& at(const key_arg<K>& key) {
  1086. iterator it = find(key);
  1087. GOOGLE_CHECK(it != end()) << "key not found: " << static_cast<Key>(key);
  1088. return it->second;
  1089. }
  1090. // Lookup
  1091. template <typename K = key_type>
  1092. size_type count(const key_arg<K>& key) const {
  1093. return find(key) == end() ? 0 : 1;
  1094. }
  1095. template <typename K = key_type>
  1096. const_iterator find(const key_arg<K>& key) const {
  1097. return const_iterator(elements_.find(key));
  1098. }
  1099. template <typename K = key_type>
  1100. iterator find(const key_arg<K>& key) {
  1101. return iterator(elements_.find(key));
  1102. }
  1103. template <typename K = key_type>
  1104. bool contains(const key_arg<K>& key) const {
  1105. return find(key) != end();
  1106. }
  1107. template <typename K = key_type>
  1108. std::pair<const_iterator, const_iterator> equal_range(
  1109. const key_arg<K>& key) const {
  1110. const_iterator it = find(key);
  1111. if (it == end()) {
  1112. return std::pair<const_iterator, const_iterator>(it, it);
  1113. } else {
  1114. const_iterator begin = it++;
  1115. return std::pair<const_iterator, const_iterator>(begin, it);
  1116. }
  1117. }
  1118. template <typename K = key_type>
  1119. std::pair<iterator, iterator> equal_range(const key_arg<K>& key) {
  1120. iterator it = find(key);
  1121. if (it == end()) {
  1122. return std::pair<iterator, iterator>(it, it);
  1123. } else {
  1124. iterator begin = it++;
  1125. return std::pair<iterator, iterator>(begin, it);
  1126. }
  1127. }
  1128. // insert
  1129. std::pair<iterator, bool> insert(const value_type& value) {
  1130. std::pair<typename InnerMap::iterator, bool> p =
  1131. elements_.insert(value.first);
  1132. if (p.second) {
  1133. p.first->second = value.second;
  1134. }
  1135. return std::pair<iterator, bool>(iterator(p.first), p.second);
  1136. }
  1137. template <class InputIt>
  1138. void insert(InputIt first, InputIt last) {
  1139. for (InputIt it = first; it != last; ++it) {
  1140. iterator exist_it = find(it->first);
  1141. if (exist_it == end()) {
  1142. operator[](it->first) = it->second;
  1143. }
  1144. }
  1145. }
  1146. void insert(std::initializer_list<value_type> values) {
  1147. insert(values.begin(), values.end());
  1148. }
  1149. // Erase and clear
  1150. template <typename K = key_type>
  1151. size_type erase(const key_arg<K>& key) {
  1152. iterator it = find(key);
  1153. if (it == end()) {
  1154. return 0;
  1155. } else {
  1156. erase(it);
  1157. return 1;
  1158. }
  1159. }
  1160. iterator erase(iterator pos) {
  1161. iterator i = pos++;
  1162. elements_.erase(i.it_);
  1163. return pos;
  1164. }
  1165. void erase(iterator first, iterator last) {
  1166. while (first != last) {
  1167. first = erase(first);
  1168. }
  1169. }
  1170. void clear() { elements_.clear(); }
  1171. // Assign
  1172. Map& operator=(const Map& other) {
  1173. if (this != &other) {
  1174. clear();
  1175. insert(other.begin(), other.end());
  1176. }
  1177. return *this;
  1178. }
  1179. void swap(Map& other) {
  1180. if (arena() == other.arena()) {
  1181. InternalSwap(other);
  1182. } else {
  1183. // TODO(zuguang): optimize this. The temporary copy can be allocated
  1184. // in the same arena as the other message, and the "other = copy" can
  1185. // be replaced with the fast-path swap above.
  1186. Map copy = *this;
  1187. *this = other;
  1188. other = copy;
  1189. }
  1190. }
  1191. void InternalSwap(Map& other) { elements_.Swap(&other.elements_); }
  1192. // Access to hasher. Currently this returns a copy, but it may
  1193. // be modified to return a const reference in the future.
  1194. hasher hash_function() const { return elements_.hash_function(); }
  1195. size_t SpaceUsedExcludingSelfLong() const {
  1196. if (empty()) return 0;
  1197. return elements_.SpaceUsedInternal() + internal::SpaceUsedInValues(this);
  1198. }
  1199. private:
  1200. Arena* arena() const { return elements_.arena(); }
  1201. InnerMap elements_;
  1202. friend class Arena;
  1203. using InternalArenaConstructable_ = void;
  1204. using DestructorSkippable_ = void;
  1205. template <typename Derived, typename K, typename V,
  1206. internal::WireFormatLite::FieldType key_wire_type,
  1207. internal::WireFormatLite::FieldType value_wire_type>
  1208. friend class internal::MapFieldLite;
  1209. };
  1210. } // namespace protobuf
  1211. } // namespace google
  1212. #include <google/protobuf/port_undef.inc>
  1213. #endif // GOOGLE_PROTOBUF_MAP_H__