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