F14Set.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. /**
  18. * F14NodeSet, F14ValueSet, and F14VectorSet
  19. *
  20. * F14FastSet conditionally inherits from F14ValueSet or F14VectorSet
  21. *
  22. * See F14.md
  23. *
  24. * @author Nathan Bronson <ngbronson@fb.com>
  25. * @author Xiao Shi <xshi@fb.com>
  26. */
  27. #include <tuple>
  28. #include <folly/lang/SafeAssert.h>
  29. #include <folly/container/F14Set-fwd.h>
  30. #include <folly/container/detail/F14Policy.h>
  31. #include <folly/container/detail/F14Table.h>
  32. #if !FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
  33. //////// Compatibility for unsupported platforms (not x86_64 and not aarch64)
  34. #include <unordered_set>
  35. namespace folly {
  36. namespace f14 {
  37. namespace detail {
  38. template <typename K, typename H, typename E, typename A>
  39. class F14BasicSet : public std::unordered_set<K, H, E, A> {
  40. using Super = std::unordered_set<K, H, E, A>;
  41. public:
  42. using typename Super::pointer;
  43. using typename Super::value_type;
  44. F14BasicSet() = default;
  45. using Super::Super;
  46. //// PUBLIC - F14 Extensions
  47. // exact for libstdc++, approximate for others
  48. std::size_t getAllocatedMemorySize() const {
  49. std::size_t rv = 0;
  50. visitAllocationClasses(
  51. [&](std::size_t bytes, std::size_t n) { rv += bytes * n; });
  52. return rv;
  53. }
  54. // exact for libstdc++, approximate for others
  55. template <typename V>
  56. void visitAllocationClasses(V&& visitor) const {
  57. auto bc = this->bucket_count();
  58. if (bc > 1) {
  59. visitor(bc * sizeof(pointer), 1);
  60. }
  61. if (this->size() > 0) {
  62. visitor(sizeof(StdNodeReplica<K, value_type, H>), this->size());
  63. }
  64. }
  65. template <typename V>
  66. void visitContiguousRanges(V&& visitor) const {
  67. for (value_type const& entry : *this) {
  68. value_type const* b = std::addressof(entry);
  69. visitor(b, b + 1);
  70. }
  71. }
  72. };
  73. } // namespace detail
  74. } // namespace f14
  75. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  76. class F14NodeSet
  77. : public f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc> {
  78. using Super = f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc>;
  79. public:
  80. using typename Super::value_type;
  81. F14NodeSet() = default;
  82. using Super::Super;
  83. F14NodeSet& operator=(std::initializer_list<value_type> ilist) {
  84. Super::operator=(ilist);
  85. return *this;
  86. }
  87. };
  88. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  89. class F14ValueSet
  90. : public f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc> {
  91. using Super = f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc>;
  92. public:
  93. using typename Super::value_type;
  94. F14ValueSet() : Super() {}
  95. using Super::Super;
  96. F14ValueSet& operator=(std::initializer_list<value_type> ilist) {
  97. Super::operator=(ilist);
  98. return *this;
  99. }
  100. };
  101. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  102. class F14VectorSet
  103. : public f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc> {
  104. using Super = f14::detail::F14BasicSet<Key, Hasher, KeyEqual, Alloc>;
  105. public:
  106. using typename Super::value_type;
  107. F14VectorSet() = default;
  108. using Super::Super;
  109. F14VectorSet& operator=(std::initializer_list<value_type> ilist) {
  110. Super::operator=(ilist);
  111. return *this;
  112. }
  113. };
  114. } // namespace folly
  115. #else // FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
  116. //////// Common case for supported platforms
  117. namespace folly {
  118. namespace f14 {
  119. namespace detail {
  120. template <typename Policy>
  121. class F14BasicSet {
  122. template <typename K, typename T>
  123. using EnableHeterogeneousFind = std::enable_if_t<
  124. EligibleForHeterogeneousFind<
  125. typename Policy::Value,
  126. typename Policy::Hasher,
  127. typename Policy::KeyEqual,
  128. K>::value,
  129. T>;
  130. template <typename K, typename T>
  131. using EnableHeterogeneousInsert = std::enable_if_t<
  132. EligibleForHeterogeneousInsert<
  133. typename Policy::Value,
  134. typename Policy::Hasher,
  135. typename Policy::KeyEqual,
  136. K>::value,
  137. T>;
  138. template <typename K, typename T>
  139. using EnableHeterogeneousErase = std::enable_if_t<
  140. EligibleForHeterogeneousFind<
  141. typename Policy::Value,
  142. typename Policy::Hasher,
  143. typename Policy::KeyEqual,
  144. K>::value &&
  145. !std::is_same<typename Policy::Iter, remove_cvref_t<K>>::value,
  146. T>;
  147. public:
  148. //// PUBLIC - Member types
  149. using key_type = typename Policy::Value;
  150. using value_type = key_type;
  151. using size_type = std::size_t;
  152. using difference_type = std::ptrdiff_t;
  153. using hasher = typename Policy::Hasher;
  154. using key_equal = typename Policy::KeyEqual;
  155. using allocator_type = typename Policy::Alloc;
  156. using reference = value_type&;
  157. using const_reference = value_type const&;
  158. using pointer = typename Policy::AllocTraits::pointer;
  159. using const_pointer = typename Policy::AllocTraits::const_pointer;
  160. using iterator = typename Policy::Iter;
  161. using const_iterator = iterator;
  162. private:
  163. using ItemIter = typename Policy::ItemIter;
  164. public:
  165. //// PUBLIC - Member functions
  166. F14BasicSet() noexcept(Policy::kDefaultConstructIsNoexcept)
  167. : F14BasicSet(0) {}
  168. explicit F14BasicSet(
  169. std::size_t initialCapacity,
  170. hasher const& hash = hasher{},
  171. key_equal const& eq = key_equal{},
  172. allocator_type const& alloc = allocator_type{})
  173. : table_{initialCapacity, hash, eq, alloc} {}
  174. explicit F14BasicSet(std::size_t initialCapacity, allocator_type const& alloc)
  175. : F14BasicSet(initialCapacity, hasher{}, key_equal{}, alloc) {}
  176. explicit F14BasicSet(
  177. std::size_t initialCapacity,
  178. hasher const& hash,
  179. allocator_type const& alloc)
  180. : F14BasicSet(initialCapacity, hash, key_equal{}, alloc) {}
  181. explicit F14BasicSet(allocator_type const& alloc)
  182. : F14BasicSet(0, hasher{}, key_equal{}, alloc) {}
  183. template <typename InputIt>
  184. F14BasicSet(
  185. InputIt first,
  186. InputIt last,
  187. std::size_t initialCapacity = 0,
  188. hasher const& hash = hasher{},
  189. key_equal const& eq = key_equal{},
  190. allocator_type const& alloc = allocator_type{})
  191. : table_{initialCapacity, hash, eq, alloc} {
  192. initialInsert(first, last, initialCapacity);
  193. }
  194. template <typename InputIt>
  195. F14BasicSet(
  196. InputIt first,
  197. InputIt last,
  198. std::size_t initialCapacity,
  199. allocator_type const& alloc)
  200. : table_{initialCapacity, hasher{}, key_equal{}, alloc} {
  201. initialInsert(first, last, initialCapacity);
  202. }
  203. template <typename InputIt>
  204. F14BasicSet(
  205. InputIt first,
  206. InputIt last,
  207. std::size_t initialCapacity,
  208. hasher const& hash,
  209. allocator_type const& alloc)
  210. : table_{initialCapacity, hash, key_equal{}, alloc} {
  211. initialInsert(first, last, initialCapacity);
  212. }
  213. F14BasicSet(F14BasicSet const& rhs) = default;
  214. F14BasicSet(F14BasicSet const& rhs, allocator_type const& alloc)
  215. : table_(rhs.table_, alloc) {}
  216. F14BasicSet(F14BasicSet&& rhs) = default;
  217. F14BasicSet(F14BasicSet&& rhs, allocator_type const& alloc) noexcept(
  218. Policy::kAllocIsAlwaysEqual)
  219. : table_{std::move(rhs.table_), alloc} {}
  220. F14BasicSet(
  221. std::initializer_list<value_type> init,
  222. std::size_t initialCapacity = 0,
  223. hasher const& hash = hasher{},
  224. key_equal const& eq = key_equal{},
  225. allocator_type const& alloc = allocator_type{})
  226. : table_{initialCapacity, hash, eq, alloc} {
  227. initialInsert(init.begin(), init.end(), initialCapacity);
  228. }
  229. F14BasicSet(
  230. std::initializer_list<value_type> init,
  231. std::size_t initialCapacity,
  232. allocator_type const& alloc)
  233. : table_{initialCapacity, hasher{}, key_equal{}, alloc} {
  234. initialInsert(init.begin(), init.end(), initialCapacity);
  235. }
  236. F14BasicSet(
  237. std::initializer_list<value_type> init,
  238. std::size_t initialCapacity,
  239. hasher const& hash,
  240. allocator_type const& alloc)
  241. : table_{initialCapacity, hash, key_equal{}, alloc} {
  242. initialInsert(init.begin(), init.end(), initialCapacity);
  243. }
  244. F14BasicSet& operator=(F14BasicSet const&) = default;
  245. F14BasicSet& operator=(F14BasicSet&&) = default;
  246. F14BasicSet& operator=(std::initializer_list<value_type> ilist) {
  247. clear();
  248. bulkInsert(ilist.begin(), ilist.end(), false);
  249. return *this;
  250. }
  251. allocator_type get_allocator() const noexcept {
  252. return table_.alloc();
  253. }
  254. //// PUBLIC - Iterators
  255. iterator begin() noexcept {
  256. return cbegin();
  257. }
  258. const_iterator begin() const noexcept {
  259. return cbegin();
  260. }
  261. const_iterator cbegin() const noexcept {
  262. return table_.makeIter(table_.begin());
  263. }
  264. iterator end() noexcept {
  265. return cend();
  266. }
  267. const_iterator end() const noexcept {
  268. return cend();
  269. }
  270. const_iterator cend() const noexcept {
  271. return table_.makeIter(table_.end());
  272. }
  273. //// PUBLIC - Capacity
  274. bool empty() const noexcept {
  275. return table_.empty();
  276. }
  277. std::size_t size() const noexcept {
  278. return table_.size();
  279. }
  280. std::size_t max_size() const noexcept {
  281. return table_.max_size();
  282. }
  283. //// PUBLIC - Modifiers
  284. void clear() noexcept {
  285. table_.clear();
  286. }
  287. std::pair<iterator, bool> insert(value_type const& value) {
  288. return emplace(value);
  289. }
  290. std::pair<iterator, bool> insert(value_type&& value) {
  291. return emplace(std::move(value));
  292. }
  293. // std::unordered_set's hinted insertion API is misleading. No
  294. // implementation I've seen actually uses the hint. Code restructuring
  295. // by the caller to use the hinted API is at best unnecessary, and at
  296. // worst a pessimization. It is used, however, so we provide it.
  297. iterator insert(const_iterator /*hint*/, value_type const& value) {
  298. return insert(value).first;
  299. }
  300. iterator insert(const_iterator /*hint*/, value_type&& value) {
  301. return insert(std::move(value)).first;
  302. }
  303. template <typename K>
  304. EnableHeterogeneousInsert<K, std::pair<iterator, bool>> insert(K&& value) {
  305. return emplace(std::forward<K>(value));
  306. }
  307. private:
  308. template <class InputIt>
  309. FOLLY_ALWAYS_INLINE void
  310. bulkInsert(InputIt first, InputIt last, bool autoReserve) {
  311. if (autoReserve) {
  312. table_.reserveForInsert(std::distance(first, last));
  313. }
  314. while (first != last) {
  315. insert(*first);
  316. ++first;
  317. }
  318. }
  319. template <class InputIt>
  320. void initialInsert(InputIt first, InputIt last, std::size_t initialCapacity) {
  321. FOLLY_SAFE_DCHECK(empty() && bucket_count() >= initialCapacity, "");
  322. // It's possible that there are a lot of duplicates in first..last and
  323. // so we will oversize ourself. The common case, however, is that
  324. // we can avoid a lot of rehashing if we pre-expand. The behavior
  325. // is easy to disable at a particular call site by asking for an
  326. // initialCapacity of 1.
  327. bool autoReserve =
  328. std::is_same<
  329. typename std::iterator_traits<InputIt>::iterator_category,
  330. std::random_access_iterator_tag>::value &&
  331. initialCapacity == 0;
  332. bulkInsert(first, last, autoReserve);
  333. }
  334. public:
  335. template <class InputIt>
  336. void insert(InputIt first, InputIt last) {
  337. // Bulk reserve is a heuristic choice, so it can backfire. We restrict
  338. // ourself to situations that mimic bulk construction without an
  339. // explicit initialCapacity.
  340. bool autoReserve =
  341. std::is_same<
  342. typename std::iterator_traits<InputIt>::iterator_category,
  343. std::random_access_iterator_tag>::value &&
  344. bucket_count() == 0;
  345. bulkInsert(first, last, autoReserve);
  346. }
  347. void insert(std::initializer_list<value_type> ilist) {
  348. insert(ilist.begin(), ilist.end());
  349. }
  350. template <class... Args>
  351. std::pair<iterator, bool> emplace(Args&&... args) {
  352. using K = KeyTypeForEmplace<key_type, hasher, key_equal, Args...>;
  353. // If args is a single arg that can be emplaced directly (either
  354. // key_type or a heterogeneous find + conversion to key_type) key will
  355. // just be a reference to that arg, otherwise this will construct an
  356. // intermediate key.
  357. K key(std::forward<Args>(args)...);
  358. auto rv = table_.tryEmplaceValue(key, std::forward<K>(key));
  359. return std::make_pair(table_.makeIter(rv.first), rv.second);
  360. }
  361. template <class... Args>
  362. iterator emplace_hint(const_iterator /*hint*/, Args&&... args) {
  363. return emplace(std::forward<Args>(args)...).first;
  364. }
  365. FOLLY_ALWAYS_INLINE iterator erase(const_iterator pos) {
  366. return eraseInto(pos, [](value_type&&) {});
  367. }
  368. iterator erase(const_iterator first, const_iterator last) {
  369. return eraseInto(first, last, [](value_type&&) {});
  370. }
  371. size_type erase(key_type const& key) {
  372. return eraseInto(key, [](value_type&&) {});
  373. }
  374. template <typename K>
  375. EnableHeterogeneousErase<K, size_type> erase(K const& key) {
  376. return eraseInto(key, [](value_type&&) {});
  377. }
  378. // eraseInto contains the same overloads as erase but provides
  379. // an additional callback argument which is called with an rvalue
  380. // reference to the item directly before it is destroyed. This can be
  381. // used to extract an item out of a F14Set while avoiding a copy.
  382. template <typename BeforeDestroy>
  383. FOLLY_ALWAYS_INLINE iterator
  384. eraseInto(const_iterator pos, BeforeDestroy&& beforeDestroy) {
  385. auto itemPos = table_.unwrapIter(pos);
  386. table_.eraseIterInto(itemPos, beforeDestroy);
  387. // If we are inlined then gcc and clang can optimize away all of the
  388. // work of ++pos if the caller discards it.
  389. itemPos.advanceLikelyDead();
  390. return table_.makeIter(itemPos);
  391. }
  392. template <typename BeforeDestroy>
  393. iterator eraseInto(
  394. const_iterator first,
  395. const_iterator last,
  396. BeforeDestroy&& beforeDestroy) {
  397. while (first != last) {
  398. first = eraseInto(first, beforeDestroy);
  399. }
  400. return first;
  401. }
  402. template <typename BeforeDestroy>
  403. size_type eraseInto(key_type const& key, BeforeDestroy&& beforeDestroy) {
  404. return table_.eraseKeyInto(key, beforeDestroy);
  405. }
  406. template <typename K, typename BeforeDestroy>
  407. EnableHeterogeneousErase<K, size_type> eraseInto(
  408. K const& key,
  409. BeforeDestroy&& beforeDestroy) {
  410. return table_.eraseKeyInto(key, beforeDestroy);
  411. }
  412. //// PUBLIC - Lookup
  413. FOLLY_ALWAYS_INLINE std::size_t count(key_type const& key) const {
  414. return table_.find(key).atEnd() ? 0 : 1;
  415. }
  416. template <typename K>
  417. FOLLY_ALWAYS_INLINE EnableHeterogeneousFind<K, size_type> count(
  418. K const& key) const {
  419. return table_.find(key).atEnd() ? 0 : 1;
  420. }
  421. // prehash(key) does the work of evaluating hash_function()(key)
  422. // (including additional bit-mixing for non-avalanching hash functions),
  423. // wraps the result of that work in a token for later reuse, and
  424. // begins prefetching of the first steps of looking for key into the
  425. // local CPU cache.
  426. //
  427. // The returned token may be used at any time, may be used more than
  428. // once, and may be used in other F14 sets and maps. Tokens are
  429. // transferrable between any F14 containers (maps and sets) with the
  430. // same key_type and equal hash_function()s.
  431. //
  432. // Hash tokens are not hints -- it is a bug to call any method on this
  433. // class with a token t and key k where t isn't the result of a call
  434. // to prehash(k2) with k2 == k.
  435. F14HashToken prehash(key_type const& key) const {
  436. return table_.prehash(key);
  437. }
  438. template <typename K>
  439. EnableHeterogeneousFind<K, F14HashToken> prehash(K const& key) const {
  440. return table_.prehash(key);
  441. }
  442. FOLLY_ALWAYS_INLINE iterator find(key_type const& key) {
  443. return const_cast<F14BasicSet const*>(this)->find(key);
  444. }
  445. FOLLY_ALWAYS_INLINE const_iterator find(key_type const& key) const {
  446. return table_.makeIter(table_.find(key));
  447. }
  448. FOLLY_ALWAYS_INLINE iterator
  449. find(F14HashToken const& token, key_type const& key) {
  450. return const_cast<F14BasicSet const*>(this)->find(token, key);
  451. }
  452. FOLLY_ALWAYS_INLINE const_iterator
  453. find(F14HashToken const& token, key_type const& key) const {
  454. return table_.makeIter(table_.find(token, key));
  455. }
  456. template <typename K>
  457. FOLLY_ALWAYS_INLINE EnableHeterogeneousFind<K, iterator> find(K const& key) {
  458. return const_cast<F14BasicSet const*>(this)->find(key);
  459. }
  460. template <typename K>
  461. FOLLY_ALWAYS_INLINE EnableHeterogeneousFind<K, const_iterator> find(
  462. K const& key) const {
  463. return table_.makeIter(table_.find(key));
  464. }
  465. template <typename K>
  466. FOLLY_ALWAYS_INLINE EnableHeterogeneousFind<K, iterator> find(
  467. F14HashToken const& token,
  468. K const& key) {
  469. return const_cast<F14BasicSet const*>(this)->find(token, key);
  470. }
  471. template <typename K>
  472. FOLLY_ALWAYS_INLINE EnableHeterogeneousFind<K, const_iterator> find(
  473. F14HashToken const& token,
  474. K const& key) const {
  475. return table_.makeIter(table_.find(token, key));
  476. }
  477. std::pair<iterator, iterator> equal_range(key_type const& key) {
  478. return equal_range(*this, key);
  479. }
  480. std::pair<const_iterator, const_iterator> equal_range(
  481. key_type const& key) const {
  482. return equal_range(*this, key);
  483. }
  484. template <typename K>
  485. EnableHeterogeneousFind<K, std::pair<iterator, iterator>> equal_range(
  486. K const& key) {
  487. return equal_range(*this, key);
  488. }
  489. template <typename K>
  490. EnableHeterogeneousFind<K, std::pair<const_iterator, const_iterator>>
  491. equal_range(K const& key) const {
  492. return equal_range(*this, key);
  493. }
  494. //// PUBLIC - Bucket interface
  495. std::size_t bucket_count() const noexcept {
  496. return table_.bucket_count();
  497. }
  498. std::size_t max_bucket_count() const noexcept {
  499. return table_.max_bucket_count();
  500. }
  501. //// PUBLIC - Hash policy
  502. float load_factor() const noexcept {
  503. return table_.load_factor();
  504. }
  505. float max_load_factor() const noexcept {
  506. return table_.max_load_factor();
  507. }
  508. void max_load_factor(float v) {
  509. table_.max_load_factor(v);
  510. }
  511. void rehash(std::size_t bucketCapacity) {
  512. // The standard's rehash() requires understanding the max load factor,
  513. // which is easy to get wrong. Since we don't actually allow adjustment
  514. // of max_load_factor there is no difference.
  515. reserve(bucketCapacity);
  516. }
  517. void reserve(std::size_t capacity) {
  518. table_.reserve(capacity);
  519. }
  520. //// PUBLIC - Observers
  521. hasher hash_function() const {
  522. return table_.hasher();
  523. }
  524. key_equal key_eq() const {
  525. return table_.keyEqual();
  526. }
  527. //// PUBLIC - F14 Extensions
  528. // Get memory footprint, not including sizeof(*this).
  529. std::size_t getAllocatedMemorySize() const {
  530. return table_.getAllocatedMemorySize();
  531. }
  532. // Enumerates classes of allocated memory blocks currently owned
  533. // by this table, calling visitor(allocationSize, allocationCount).
  534. // This can be used to get a more accurate indication of memory footprint
  535. // than getAllocatedMemorySize() if you have some way of computing the
  536. // internal fragmentation of the allocator, such as JEMalloc's nallocx.
  537. // The visitor might be called twice with the same allocationSize. The
  538. // visitor's computation should produce the same result for visitor(8,
  539. // 2) as for two calls to visitor(8, 1), for example. The visitor may
  540. // be called with a zero allocationCount.
  541. template <typename V>
  542. void visitAllocationClasses(V&& visitor) const {
  543. return table_.visitAllocationClasses(visitor);
  544. }
  545. // Calls visitor with two value_type const*, b and e, such that every
  546. // entry in the table is included in exactly one of the ranges [b,e).
  547. // This can be used to efficiently iterate elements in bulk when crossing
  548. // an API boundary that supports contiguous blocks of items.
  549. template <typename V>
  550. void visitContiguousRanges(V&& visitor) const;
  551. F14TableStats computeStats() const noexcept {
  552. return table_.computeStats();
  553. }
  554. private:
  555. template <typename Self, typename K>
  556. static auto equal_range(Self& self, K const& key) {
  557. auto first = self.find(key);
  558. auto last = first;
  559. if (last != self.end()) {
  560. ++last;
  561. }
  562. return std::make_pair(first, last);
  563. }
  564. protected:
  565. F14Table<Policy> table_;
  566. };
  567. template <typename S>
  568. bool setsEqual(S const& lhs, S const& rhs) {
  569. if (lhs.size() != rhs.size()) {
  570. return false;
  571. }
  572. for (auto& k : lhs) {
  573. auto iter = rhs.find(k);
  574. if (iter == rhs.end()) {
  575. return false;
  576. }
  577. if (!std::is_same<
  578. typename S::key_equal,
  579. std::equal_to<typename S::value_type>>::value) {
  580. // spec says we compare key with == as well as with key_eq()
  581. if (!(k == *iter)) {
  582. return false;
  583. }
  584. }
  585. }
  586. return true;
  587. }
  588. } // namespace detail
  589. } // namespace f14
  590. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  591. class F14ValueSet
  592. : public f14::detail::F14BasicSet<f14::detail::SetPolicyWithDefaults<
  593. f14::detail::ValueContainerPolicy,
  594. Key,
  595. Hasher,
  596. KeyEqual,
  597. Alloc>> {
  598. using Policy = f14::detail::SetPolicyWithDefaults<
  599. f14::detail::ValueContainerPolicy,
  600. Key,
  601. Hasher,
  602. KeyEqual,
  603. Alloc>;
  604. using Super = f14::detail::F14BasicSet<Policy>;
  605. public:
  606. using typename Super::value_type;
  607. F14ValueSet() = default;
  608. using Super::Super;
  609. F14ValueSet& operator=(std::initializer_list<value_type> ilist) {
  610. Super::operator=(ilist);
  611. return *this;
  612. }
  613. void swap(F14ValueSet& rhs) noexcept(Policy::kSwapIsNoexcept) {
  614. this->table_.swap(rhs.table_);
  615. }
  616. template <typename V>
  617. void visitContiguousRanges(V&& visitor) const {
  618. this->table_.visitContiguousItemRanges(std::forward<V>(visitor));
  619. }
  620. };
  621. template <typename K, typename H, typename E, typename A>
  622. bool operator==(
  623. F14ValueSet<K, H, E, A> const& lhs,
  624. F14ValueSet<K, H, E, A> const& rhs) {
  625. return setsEqual(lhs, rhs);
  626. }
  627. template <typename K, typename H, typename E, typename A>
  628. bool operator!=(
  629. F14ValueSet<K, H, E, A> const& lhs,
  630. F14ValueSet<K, H, E, A> const& rhs) {
  631. return !(lhs == rhs);
  632. }
  633. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  634. class F14NodeSet
  635. : public f14::detail::F14BasicSet<f14::detail::SetPolicyWithDefaults<
  636. f14::detail::NodeContainerPolicy,
  637. Key,
  638. Hasher,
  639. KeyEqual,
  640. Alloc>> {
  641. using Policy = f14::detail::SetPolicyWithDefaults<
  642. f14::detail::NodeContainerPolicy,
  643. Key,
  644. Hasher,
  645. KeyEqual,
  646. Alloc>;
  647. using Super = f14::detail::F14BasicSet<Policy>;
  648. public:
  649. using typename Super::value_type;
  650. F14NodeSet() = default;
  651. using Super::Super;
  652. F14NodeSet& operator=(std::initializer_list<value_type> ilist) {
  653. Super::operator=(ilist);
  654. return *this;
  655. }
  656. void swap(F14NodeSet& rhs) noexcept(Policy::kSwapIsNoexcept) {
  657. this->table_.swap(rhs.table_);
  658. }
  659. template <typename V>
  660. void visitContiguousRanges(V&& visitor) const {
  661. this->table_.visitItems([&](typename Policy::Item ptr) {
  662. value_type const* b = std::addressof(*ptr);
  663. visitor(b, b + 1);
  664. });
  665. }
  666. };
  667. template <typename K, typename H, typename E, typename A>
  668. bool operator==(
  669. F14NodeSet<K, H, E, A> const& lhs,
  670. F14NodeSet<K, H, E, A> const& rhs) {
  671. return setsEqual(lhs, rhs);
  672. }
  673. template <typename K, typename H, typename E, typename A>
  674. bool operator!=(
  675. F14NodeSet<K, H, E, A> const& lhs,
  676. F14NodeSet<K, H, E, A> const& rhs) {
  677. return !(lhs == rhs);
  678. }
  679. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  680. class F14VectorSet
  681. : public f14::detail::F14BasicSet<f14::detail::SetPolicyWithDefaults<
  682. f14::detail::VectorContainerPolicy,
  683. Key,
  684. Hasher,
  685. KeyEqual,
  686. Alloc>> {
  687. using Policy = f14::detail::SetPolicyWithDefaults<
  688. f14::detail::VectorContainerPolicy,
  689. Key,
  690. Hasher,
  691. KeyEqual,
  692. Alloc>;
  693. using Super = f14::detail::F14BasicSet<Policy>;
  694. template <typename K, typename T>
  695. using EnableHeterogeneousVectorErase = std::enable_if_t<
  696. f14::detail::EligibleForHeterogeneousFind<
  697. typename Policy::Value,
  698. typename Policy::Hasher,
  699. typename Policy::KeyEqual,
  700. K>::value &&
  701. !std::is_same<typename Policy::Iter, remove_cvref_t<K>>::value &&
  702. !std::is_same<typename Policy::ReverseIter, remove_cvref_t<K>>::value,
  703. T>;
  704. public:
  705. using typename Super::const_iterator;
  706. using typename Super::iterator;
  707. using typename Super::key_type;
  708. using typename Super::value_type;
  709. using reverse_iterator = typename Policy::ReverseIter;
  710. using const_reverse_iterator = reverse_iterator;
  711. F14VectorSet() = default;
  712. using Super::Super;
  713. F14VectorSet& operator=(std::initializer_list<value_type> ilist) {
  714. Super::operator=(ilist);
  715. return *this;
  716. }
  717. void swap(F14VectorSet& rhs) noexcept(Policy::kSwapIsNoexcept) {
  718. this->table_.swap(rhs.table_);
  719. }
  720. // ITERATION ORDER
  721. //
  722. // Deterministic iteration order for insert-only workloads is part of
  723. // F14VectorSet's supported API: iterator is LIFO and reverse_iterator
  724. // is FIFO.
  725. //
  726. // If there have been no calls to erase() then iterator and
  727. // const_iterator enumerate entries in the opposite of insertion order.
  728. // begin()->first is the key most recently inserted. reverse_iterator
  729. // and reverse_const_iterator, therefore, enumerate in LIFO (insertion)
  730. // order for insert-only workloads. Deterministic iteration order is
  731. // only guaranteed if no keys were removed since the last time the
  732. // set was empty. Iteration order is preserved across rehashes and
  733. // F14VectorSet copies and moves.
  734. //
  735. // iterator uses LIFO order so that erasing while iterating with begin()
  736. // and end() is safe using the erase(it++) idiom, which is supported
  737. // by std::set and std::unordered_set. erase(iter) invalidates iter
  738. // and all iterators before iter in the non-reverse iteration order.
  739. // Every successful erase invalidates all reverse iterators.
  740. iterator begin() {
  741. return cbegin();
  742. }
  743. const_iterator begin() const {
  744. return cbegin();
  745. }
  746. const_iterator cbegin() const {
  747. return this->table_.linearBegin(this->size());
  748. }
  749. iterator end() {
  750. return cend();
  751. }
  752. const_iterator end() const {
  753. return cend();
  754. }
  755. const_iterator cend() const {
  756. return this->table_.linearEnd();
  757. }
  758. reverse_iterator rbegin() {
  759. return this->table_.values_;
  760. }
  761. const_reverse_iterator rbegin() const {
  762. return crbegin();
  763. }
  764. const_reverse_iterator crbegin() const {
  765. return this->table_.values_;
  766. }
  767. reverse_iterator rend() {
  768. return this->table_.values_ + this->table_.size();
  769. }
  770. const_reverse_iterator rend() const {
  771. return crend();
  772. }
  773. const_reverse_iterator crend() const {
  774. return this->table_.values_ + this->table_.size();
  775. }
  776. // explicit conversions between iterator and reverse_iterator
  777. iterator iter(reverse_iterator riter) {
  778. return this->table_.iter(riter);
  779. }
  780. const_iterator iter(const_reverse_iterator riter) const {
  781. return this->table_.iter(riter);
  782. }
  783. reverse_iterator riter(iterator it) {
  784. return this->table_.riter(it);
  785. }
  786. const_reverse_iterator riter(const_iterator it) const {
  787. return this->table_.riter(it);
  788. }
  789. private:
  790. template <typename BeforeDestroy>
  791. void eraseUnderlying(
  792. typename Policy::ItemIter underlying,
  793. BeforeDestroy&& beforeDestroy) {
  794. Alloc& a = this->table_.alloc();
  795. auto values = this->table_.values_;
  796. // destroy the value and remove the ptr from the base table
  797. auto index = underlying.item();
  798. this->table_.eraseIterInto(underlying, beforeDestroy);
  799. Policy::AllocTraits::destroy(a, std::addressof(values[index]));
  800. // move the last element in values_ down and fix up the inbound index
  801. auto tailIndex = this->size();
  802. if (tailIndex != index) {
  803. auto tail = this->table_.find(f14::detail::VectorContainerIndexSearch{
  804. static_cast<uint32_t>(tailIndex)});
  805. tail.item() = index;
  806. auto p = std::addressof(values[index]);
  807. assume(p != nullptr);
  808. this->table_.transfer(a, std::addressof(values[tailIndex]), p, 1);
  809. }
  810. }
  811. template <typename K, typename BeforeDestroy>
  812. std::size_t eraseUnderlyingKey(K const& key, BeforeDestroy&& beforeDestroy) {
  813. auto underlying = this->table_.find(key);
  814. if (underlying.atEnd()) {
  815. return 0;
  816. } else {
  817. eraseUnderlying(underlying, beforeDestroy);
  818. return 1;
  819. }
  820. }
  821. public:
  822. FOLLY_ALWAYS_INLINE iterator erase(const_iterator pos) {
  823. return eraseInto(pos, [](value_type&&) {});
  824. }
  825. iterator erase(const_iterator first, const_iterator last) {
  826. return eraseInto(first, last, [](value_type&&) {});
  827. }
  828. // No erase is provided for reverse_iterator (AKA const_reverse_iterator)
  829. // to make it harder to shoot yourself in the foot by erasing while
  830. // reverse-iterating. You can write that as set.erase(set.iter(riter)).
  831. std::size_t erase(key_type const& key) {
  832. return eraseInto(key, [](value_type&&) {});
  833. }
  834. template <typename K>
  835. EnableHeterogeneousVectorErase<K, std::size_t> erase(K const& key) {
  836. return eraseInto(key, [](value_type&&) {});
  837. }
  838. template <typename BeforeDestroy>
  839. FOLLY_ALWAYS_INLINE iterator
  840. eraseInto(const_iterator pos, BeforeDestroy&& beforeDestroy) {
  841. auto underlying = this->table_.find(
  842. f14::detail::VectorContainerIndexSearch{this->table_.iterToIndex(pos)});
  843. eraseUnderlying(underlying, beforeDestroy);
  844. return ++pos;
  845. }
  846. template <typename BeforeDestroy>
  847. iterator eraseInto(
  848. const_iterator first,
  849. const_iterator last,
  850. BeforeDestroy&& beforeDestroy) {
  851. while (first != last) {
  852. first = eraseInto(first, beforeDestroy);
  853. }
  854. return first;
  855. }
  856. template <typename BeforeDestroy>
  857. std::size_t eraseInto(key_type const& key, BeforeDestroy&& beforeDestroy) {
  858. return eraseUnderlyingKey(key, beforeDestroy);
  859. }
  860. template <typename K, typename BeforeDestroy>
  861. EnableHeterogeneousVectorErase<K, std::size_t> eraseInto(
  862. K const& key,
  863. BeforeDestroy&& beforeDestroy) {
  864. return eraseUnderlyingKey(key, beforeDestroy);
  865. }
  866. template <typename V>
  867. void visitContiguousRanges(V&& visitor) const {
  868. auto n = this->table_.size();
  869. if (n > 0) {
  870. value_type const* b = std::addressof(this->table_.values_[0]);
  871. visitor(b, b + n);
  872. }
  873. }
  874. };
  875. template <typename K, typename H, typename E, typename A>
  876. bool operator==(
  877. F14VectorSet<K, H, E, A> const& lhs,
  878. F14VectorSet<K, H, E, A> const& rhs) {
  879. return setsEqual(lhs, rhs);
  880. }
  881. template <typename K, typename H, typename E, typename A>
  882. bool operator!=(
  883. F14VectorSet<K, H, E, A> const& lhs,
  884. F14VectorSet<K, H, E, A> const& rhs) {
  885. return !(lhs == rhs);
  886. }
  887. } // namespace folly
  888. #endif // FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
  889. namespace folly {
  890. template <typename Key, typename Hasher, typename KeyEqual, typename Alloc>
  891. class F14FastSet : public std::conditional_t<
  892. sizeof(Key) < 24,
  893. F14ValueSet<Key, Hasher, KeyEqual, Alloc>,
  894. F14VectorSet<Key, Hasher, KeyEqual, Alloc>> {
  895. using Super = std::conditional_t<
  896. sizeof(Key) < 24,
  897. F14ValueSet<Key, Hasher, KeyEqual, Alloc>,
  898. F14VectorSet<Key, Hasher, KeyEqual, Alloc>>;
  899. public:
  900. using typename Super::value_type;
  901. F14FastSet() = default;
  902. using Super::Super;
  903. F14FastSet& operator=(std::initializer_list<value_type> ilist) {
  904. Super::operator=(ilist);
  905. return *this;
  906. }
  907. };
  908. template <typename K, typename H, typename E, typename A>
  909. void swap(F14ValueSet<K, H, E, A>& lhs, F14ValueSet<K, H, E, A>& rhs) noexcept(
  910. noexcept(lhs.swap(rhs))) {
  911. lhs.swap(rhs);
  912. }
  913. template <typename K, typename H, typename E, typename A>
  914. void swap(F14NodeSet<K, H, E, A>& lhs, F14NodeSet<K, H, E, A>& rhs) noexcept(
  915. noexcept(lhs.swap(rhs))) {
  916. lhs.swap(rhs);
  917. }
  918. template <typename K, typename H, typename E, typename A>
  919. void swap(
  920. F14VectorSet<K, H, E, A>& lhs,
  921. F14VectorSet<K, H, E, A>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
  922. lhs.swap(rhs);
  923. }
  924. template <typename K, typename H, typename E, typename A>
  925. void swap(F14FastSet<K, H, E, A>& lhs, F14FastSet<K, H, E, A>& rhs) noexcept(
  926. noexcept(lhs.swap(rhs))) {
  927. lhs.swap(rhs);
  928. }
  929. } // namespace folly