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 74 kB

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