You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

map.h 52 kB

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