flat_tree.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CONTAINERS_FLAT_TREE_H_
  5. #define BASE_CONTAINERS_FLAT_TREE_H_
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/stl_util.h"
  12. #include "base/template_util.h"
  13. namespace base {
  14. namespace internal {
  15. // This is a convenience method returning true if Iterator is at least a
  16. // ForwardIterator and thus supports multiple passes over a range.
  17. template <class Iterator>
  18. constexpr bool is_multipass() {
  19. return std::is_base_of<
  20. std::forward_iterator_tag,
  21. typename std::iterator_traits<Iterator>::iterator_category>::value;
  22. }
  23. // Uses SFINAE to detect whether type has is_transparent member.
  24. template <typename T, typename = void>
  25. struct IsTransparentCompare : std::false_type {};
  26. template <typename T>
  27. struct IsTransparentCompare<T, void_t<typename T::is_transparent>>
  28. : std::true_type {};
  29. // Implementation -------------------------------------------------------------
  30. // Implementation of a sorted vector for backing flat_set and flat_map. Do not
  31. // use directly.
  32. //
  33. // The use of "value" in this is like std::map uses, meaning it's the thing
  34. // contained (in the case of map it's a <Kay, Mapped> pair). The Key is how
  35. // things are looked up. In the case of a set, Key == Value. In the case of
  36. // a map, the Key is a component of a Value.
  37. //
  38. // The helper class GetKeyFromValue provides the means to extract a key from a
  39. // value for comparison purposes. It should implement:
  40. // const Key& operator()(const Value&).
  41. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  42. class flat_tree {
  43. protected:
  44. using underlying_type = std::vector<Value>;
  45. public:
  46. // --------------------------------------------------------------------------
  47. // Types.
  48. //
  49. using key_type = Key;
  50. using key_compare = KeyCompare;
  51. using value_type = Value;
  52. // Wraps the templated key comparison to compare values.
  53. class value_compare : public key_compare {
  54. public:
  55. value_compare() = default;
  56. template <class Cmp>
  57. explicit value_compare(Cmp&& compare_arg)
  58. : KeyCompare(std::forward<Cmp>(compare_arg)) {}
  59. bool operator()(const value_type& left, const value_type& right) const {
  60. GetKeyFromValue extractor;
  61. return key_compare::operator()(extractor(left), extractor(right));
  62. }
  63. };
  64. using pointer = typename underlying_type::pointer;
  65. using const_pointer = typename underlying_type::const_pointer;
  66. using reference = typename underlying_type::reference;
  67. using const_reference = typename underlying_type::const_reference;
  68. using size_type = typename underlying_type::size_type;
  69. using difference_type = typename underlying_type::difference_type;
  70. using iterator = typename underlying_type::iterator;
  71. using const_iterator = typename underlying_type::const_iterator;
  72. using reverse_iterator = typename underlying_type::reverse_iterator;
  73. using const_reverse_iterator =
  74. typename underlying_type::const_reverse_iterator;
  75. // --------------------------------------------------------------------------
  76. // Lifetime.
  77. //
  78. // Constructors that take range guarantee O(N * log^2(N)) + O(N) complexity
  79. // and take O(N * log(N)) + O(N) if extra memory is available (N is a range
  80. // length).
  81. //
  82. // Assume that move constructors invalidate iterators and references.
  83. //
  84. // The constructors that take ranges, lists, and vectors do not require that
  85. // the input be sorted.
  86. flat_tree();
  87. explicit flat_tree(const key_compare& comp);
  88. template <class InputIterator>
  89. flat_tree(InputIterator first,
  90. InputIterator last,
  91. const key_compare& comp = key_compare());
  92. flat_tree(const flat_tree&);
  93. flat_tree(flat_tree&&) noexcept = default;
  94. flat_tree(const underlying_type& items,
  95. const key_compare& comp = key_compare());
  96. flat_tree(underlying_type&& items, const key_compare& comp = key_compare());
  97. flat_tree(std::initializer_list<value_type> ilist,
  98. const key_compare& comp = key_compare());
  99. ~flat_tree();
  100. // --------------------------------------------------------------------------
  101. // Assignments.
  102. //
  103. // Assume that move assignment invalidates iterators and references.
  104. flat_tree& operator=(const flat_tree&);
  105. flat_tree& operator=(flat_tree&&) noexcept(
  106. std::is_nothrow_move_assignable<underlying_type>::value);
  107. // Takes the first if there are duplicates in the initializer list.
  108. flat_tree& operator=(std::initializer_list<value_type> ilist);
  109. // --------------------------------------------------------------------------
  110. // Memory management.
  111. //
  112. // Beware that shrink_to_fit() simply forwards the request to the
  113. // underlying_type and its implementation is free to optimize otherwise and
  114. // leave capacity() to be greater that its size.
  115. //
  116. // reserve() and shrink_to_fit() invalidate iterators and references.
  117. void reserve(size_type new_capacity);
  118. size_type capacity() const;
  119. void shrink_to_fit();
  120. // --------------------------------------------------------------------------
  121. // Size management.
  122. //
  123. // clear() leaves the capacity() of the flat_tree unchanged.
  124. void clear();
  125. size_type size() const;
  126. size_type max_size() const;
  127. bool empty() const;
  128. // --------------------------------------------------------------------------
  129. // Iterators.
  130. iterator begin();
  131. const_iterator begin() const;
  132. const_iterator cbegin() const;
  133. iterator end();
  134. const_iterator end() const;
  135. const_iterator cend() const;
  136. reverse_iterator rbegin();
  137. const_reverse_iterator rbegin() const;
  138. const_reverse_iterator crbegin() const;
  139. reverse_iterator rend();
  140. const_reverse_iterator rend() const;
  141. const_reverse_iterator crend() const;
  142. // --------------------------------------------------------------------------
  143. // Insert operations.
  144. //
  145. // Assume that every operation invalidates iterators and references.
  146. // Insertion of one element can take O(size). Capacity of flat_tree grows in
  147. // an implementation-defined manner.
  148. //
  149. // NOTE: Prefer to build a new flat_tree from a std::vector (or similar)
  150. // instead of calling insert() repeatedly.
  151. std::pair<iterator, bool> insert(const value_type& val);
  152. std::pair<iterator, bool> insert(value_type&& val);
  153. iterator insert(const_iterator position_hint, const value_type& x);
  154. iterator insert(const_iterator position_hint, value_type&& x);
  155. // This method inserts the values from the range [first, last) into the
  156. // current tree.
  157. template <class InputIterator>
  158. void insert(InputIterator first, InputIterator last);
  159. template <class... Args>
  160. std::pair<iterator, bool> emplace(Args&&... args);
  161. template <class... Args>
  162. iterator emplace_hint(const_iterator position_hint, Args&&... args);
  163. // --------------------------------------------------------------------------
  164. // Underlying type operations.
  165. //
  166. // Assume that either operation invalidates iterators and references.
  167. // Extracts the underlying_type and returns it to the caller. Ensures that
  168. // `this` is `empty()` afterwards.
  169. underlying_type extract() &&;
  170. // Replaces the underlying_type with `body`. Expects that `body` is sorted
  171. // and has no repeated elements with regard to value_comp().
  172. void replace(underlying_type&& body);
  173. // --------------------------------------------------------------------------
  174. // Erase operations.
  175. //
  176. // Assume that every operation invalidates iterators and references.
  177. //
  178. // erase(position), erase(first, last) can take O(size).
  179. // erase(key) may take O(size) + O(log(size)).
  180. //
  181. // Prefer base::EraseIf() or some other variation on erase(remove(), end())
  182. // idiom when deleting multiple non-consecutive elements.
  183. iterator erase(iterator position);
  184. iterator erase(const_iterator position);
  185. iterator erase(const_iterator first, const_iterator last);
  186. template <typename K>
  187. size_type erase(const K& key);
  188. // --------------------------------------------------------------------------
  189. // Comparators.
  190. key_compare key_comp() const;
  191. value_compare value_comp() const;
  192. // --------------------------------------------------------------------------
  193. // Search operations.
  194. //
  195. // Search operations have O(log(size)) complexity.
  196. template <typename K>
  197. size_type count(const K& key) const;
  198. template <typename K>
  199. iterator find(const K& key);
  200. template <typename K>
  201. const_iterator find(const K& key) const;
  202. template <typename K>
  203. bool contains(const K& key) const;
  204. template <typename K>
  205. std::pair<iterator, iterator> equal_range(const K& key);
  206. template <typename K>
  207. std::pair<const_iterator, const_iterator> equal_range(const K& key) const;
  208. template <typename K>
  209. iterator lower_bound(const K& key);
  210. template <typename K>
  211. const_iterator lower_bound(const K& key) const;
  212. template <typename K>
  213. iterator upper_bound(const K& key);
  214. template <typename K>
  215. const_iterator upper_bound(const K& key) const;
  216. // --------------------------------------------------------------------------
  217. // General operations.
  218. //
  219. // Assume that swap invalidates iterators and references.
  220. //
  221. // Implementation note: currently we use operator==() and operator<() on
  222. // std::vector, because they have the same contract we need, so we use them
  223. // directly for brevity and in case it is more optimal than calling equal()
  224. // and lexicograhpical_compare(). If the underlying container type is changed,
  225. // this code may need to be modified.
  226. void swap(flat_tree& other) noexcept;
  227. friend bool operator==(const flat_tree& lhs, const flat_tree& rhs) {
  228. return lhs.impl_.body_ == rhs.impl_.body_;
  229. }
  230. friend bool operator!=(const flat_tree& lhs, const flat_tree& rhs) {
  231. return !(lhs == rhs);
  232. }
  233. friend bool operator<(const flat_tree& lhs, const flat_tree& rhs) {
  234. return lhs.impl_.body_ < rhs.impl_.body_;
  235. }
  236. friend bool operator>(const flat_tree& lhs, const flat_tree& rhs) {
  237. return rhs < lhs;
  238. }
  239. friend bool operator>=(const flat_tree& lhs, const flat_tree& rhs) {
  240. return !(lhs < rhs);
  241. }
  242. friend bool operator<=(const flat_tree& lhs, const flat_tree& rhs) {
  243. return !(lhs > rhs);
  244. }
  245. friend void swap(flat_tree& lhs, flat_tree& rhs) noexcept { lhs.swap(rhs); }
  246. protected:
  247. // Emplaces a new item into the tree that is known not to be in it. This
  248. // is for implementing map operator[].
  249. template <class... Args>
  250. iterator unsafe_emplace(const_iterator position, Args&&... args);
  251. // Attempts to emplace a new element with key |key|. Only if |key| is not yet
  252. // present, construct value_type from |args| and insert it. Returns an
  253. // iterator to the element with key |key| and a bool indicating whether an
  254. // insertion happened.
  255. template <class K, class... Args>
  256. std::pair<iterator, bool> emplace_key_args(const K& key, Args&&... args);
  257. // Similar to |emplace_key_args|, but checks |hint| first as a possible
  258. // insertion position.
  259. template <class K, class... Args>
  260. std::pair<iterator, bool> emplace_hint_key_args(const_iterator hint,
  261. const K& key,
  262. Args&&... args);
  263. private:
  264. // Helper class for e.g. lower_bound that can compare a value on the left
  265. // to a key on the right.
  266. struct KeyValueCompare {
  267. // The key comparison object must outlive this class.
  268. explicit KeyValueCompare(const key_compare& key_comp)
  269. : key_comp_(key_comp) {}
  270. template <typename T, typename U>
  271. bool operator()(const T& lhs, const U& rhs) const {
  272. return key_comp_(extract_if_value_type(lhs), extract_if_value_type(rhs));
  273. }
  274. private:
  275. const key_type& extract_if_value_type(const value_type& v) const {
  276. GetKeyFromValue extractor;
  277. return extractor(v);
  278. }
  279. template <typename K>
  280. const K& extract_if_value_type(const K& k) const {
  281. return k;
  282. }
  283. const key_compare& key_comp_;
  284. };
  285. iterator const_cast_it(const_iterator c_it) {
  286. auto distance = std::distance(cbegin(), c_it);
  287. return std::next(begin(), distance);
  288. }
  289. // This method is inspired by both std::map::insert(P&&) and
  290. // std::map::insert_or_assign(const K&, V&&). It inserts val if an equivalent
  291. // element is not present yet, otherwise it overwrites. It returns an iterator
  292. // to the modified element and a flag indicating whether insertion or
  293. // assignment happened.
  294. template <class V>
  295. std::pair<iterator, bool> insert_or_assign(V&& val) {
  296. auto position = lower_bound(GetKeyFromValue()(val));
  297. if (position == end() || value_comp()(val, *position))
  298. return {impl_.body_.emplace(position, std::forward<V>(val)), true};
  299. *position = std::forward<V>(val);
  300. return {position, false};
  301. }
  302. // This method is similar to insert_or_assign, with the following differences:
  303. // - Instead of searching [begin(), end()) it only searches [first, last).
  304. // - In case no equivalent element is found, val is appended to the end of the
  305. // underlying body and an iterator to the next bigger element in [first,
  306. // last) is returned.
  307. template <class V>
  308. std::pair<iterator, bool> append_or_assign(iterator first,
  309. iterator last,
  310. V&& val) {
  311. auto position = std::lower_bound(first, last, val, value_comp());
  312. if (position == last || value_comp()(val, *position)) {
  313. // emplace_back might invalidate position, which is why distance needs to
  314. // be cached.
  315. const difference_type distance = std::distance(begin(), position);
  316. impl_.body_.emplace_back(std::forward<V>(val));
  317. return {std::next(begin(), distance), true};
  318. }
  319. *position = std::forward<V>(val);
  320. return {position, false};
  321. }
  322. // This method is similar to insert, with the following differences:
  323. // - Instead of searching [begin(), end()) it only searches [first, last).
  324. // - In case no equivalent element is found, val is appended to the end of the
  325. // underlying body and an iterator to the next bigger element in [first,
  326. // last) is returned.
  327. template <class V>
  328. std::pair<iterator, bool> append_unique(iterator first,
  329. iterator last,
  330. V&& val) {
  331. auto position = std::lower_bound(first, last, val, value_comp());
  332. if (position == last || value_comp()(val, *position)) {
  333. // emplace_back might invalidate position, which is why distance needs to
  334. // be cached.
  335. const difference_type distance = std::distance(begin(), position);
  336. impl_.body_.emplace_back(std::forward<V>(val));
  337. return {std::next(begin(), distance), true};
  338. }
  339. return {position, false};
  340. }
  341. void sort_and_unique(iterator first, iterator last) {
  342. // Preserve stability for the unique code below.
  343. std::stable_sort(first, last, value_comp());
  344. auto equal_comp = [this](const value_type& lhs, const value_type& rhs) {
  345. // lhs is already <= rhs due to sort, therefore
  346. // !(lhs < rhs) <=> lhs == rhs.
  347. return !value_comp()(lhs, rhs);
  348. };
  349. erase(std::unique(first, last, equal_comp), last);
  350. }
  351. // To support comparators that may not be possible to default-construct, we
  352. // have to store an instance of Compare. Using this to store all internal
  353. // state of flat_tree and using private inheritance to store compare lets us
  354. // take advantage of an empty base class optimization to avoid extra space in
  355. // the common case when Compare has no state.
  356. struct Impl : private value_compare {
  357. Impl() = default;
  358. template <class Cmp, class... Body>
  359. explicit Impl(Cmp&& compare_arg, Body&&... underlying_type_args)
  360. : value_compare(std::forward<Cmp>(compare_arg)),
  361. body_(std::forward<Body>(underlying_type_args)...) {}
  362. const value_compare& get_value_comp() const { return *this; }
  363. const key_compare& get_key_comp() const { return *this; }
  364. underlying_type body_;
  365. } impl_;
  366. // If the compare is not transparent we want to construct key_type once.
  367. template <typename K>
  368. using KeyTypeOrK = typename std::
  369. conditional<IsTransparentCompare<key_compare>::value, K, key_type>::type;
  370. };
  371. // ----------------------------------------------------------------------------
  372. // Lifetime.
  373. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  374. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree() = default;
  375. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  376. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  377. const KeyCompare& comp)
  378. : impl_(comp) {}
  379. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  380. template <class InputIterator>
  381. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  382. InputIterator first,
  383. InputIterator last,
  384. const KeyCompare& comp)
  385. : impl_(comp, first, last) {
  386. sort_and_unique(begin(), end());
  387. }
  388. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  389. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  390. const flat_tree&) = default;
  391. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  392. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  393. const underlying_type& items,
  394. const KeyCompare& comp)
  395. : impl_(comp, items) {
  396. sort_and_unique(begin(), end());
  397. }
  398. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  399. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  400. underlying_type&& items,
  401. const KeyCompare& comp)
  402. : impl_(comp, std::move(items)) {
  403. sort_and_unique(begin(), end());
  404. }
  405. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  406. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
  407. std::initializer_list<value_type> ilist,
  408. const KeyCompare& comp)
  409. : flat_tree(std::begin(ilist), std::end(ilist), comp) {}
  410. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  411. flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::~flat_tree() = default;
  412. // ----------------------------------------------------------------------------
  413. // Assignments.
  414. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  415. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=(
  416. const flat_tree&) -> flat_tree& = default;
  417. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  418. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::
  419. operator=(flat_tree&&) noexcept(
  420. std::is_nothrow_move_assignable<underlying_type>::value)
  421. -> flat_tree& = default;
  422. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  423. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=(
  424. std::initializer_list<value_type> ilist) -> flat_tree& {
  425. impl_.body_ = ilist;
  426. sort_and_unique(begin(), end());
  427. return *this;
  428. }
  429. // ----------------------------------------------------------------------------
  430. // Memory management.
  431. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  432. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::reserve(
  433. size_type new_capacity) {
  434. impl_.body_.reserve(new_capacity);
  435. }
  436. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  437. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::capacity() const
  438. -> size_type {
  439. return impl_.body_.capacity();
  440. }
  441. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  442. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::shrink_to_fit() {
  443. impl_.body_.shrink_to_fit();
  444. }
  445. // ----------------------------------------------------------------------------
  446. // Size management.
  447. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  448. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::clear() {
  449. impl_.body_.clear();
  450. }
  451. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  452. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::size() const
  453. -> size_type {
  454. return impl_.body_.size();
  455. }
  456. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  457. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::max_size() const
  458. -> size_type {
  459. return impl_.body_.max_size();
  460. }
  461. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  462. bool flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::empty() const {
  463. return impl_.body_.empty();
  464. }
  465. // ----------------------------------------------------------------------------
  466. // Iterators.
  467. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  468. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::begin() -> iterator {
  469. return impl_.body_.begin();
  470. }
  471. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  472. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::begin() const
  473. -> const_iterator {
  474. return impl_.body_.begin();
  475. }
  476. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  477. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::cbegin() const
  478. -> const_iterator {
  479. return impl_.body_.cbegin();
  480. }
  481. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  482. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::end() -> iterator {
  483. return impl_.body_.end();
  484. }
  485. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  486. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::end() const
  487. -> const_iterator {
  488. return impl_.body_.end();
  489. }
  490. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  491. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::cend() const
  492. -> const_iterator {
  493. return impl_.body_.cend();
  494. }
  495. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  496. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rbegin()
  497. -> reverse_iterator {
  498. return impl_.body_.rbegin();
  499. }
  500. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  501. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rbegin() const
  502. -> const_reverse_iterator {
  503. return impl_.body_.rbegin();
  504. }
  505. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  506. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::crbegin() const
  507. -> const_reverse_iterator {
  508. return impl_.body_.crbegin();
  509. }
  510. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  511. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rend()
  512. -> reverse_iterator {
  513. return impl_.body_.rend();
  514. }
  515. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  516. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rend() const
  517. -> const_reverse_iterator {
  518. return impl_.body_.rend();
  519. }
  520. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  521. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::crend() const
  522. -> const_reverse_iterator {
  523. return impl_.body_.crend();
  524. }
  525. // ----------------------------------------------------------------------------
  526. // Insert operations.
  527. //
  528. // Currently we use position_hint the same way as eastl or boost:
  529. // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set.h#L493
  530. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  531. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
  532. const value_type& val) -> std::pair<iterator, bool> {
  533. return emplace_key_args(GetKeyFromValue()(val), val);
  534. }
  535. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  536. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
  537. value_type&& val) -> std::pair<iterator, bool> {
  538. return emplace_key_args(GetKeyFromValue()(val), std::move(val));
  539. }
  540. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  541. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
  542. const_iterator position_hint,
  543. const value_type& val) -> iterator {
  544. return emplace_hint_key_args(position_hint, GetKeyFromValue()(val), val)
  545. .first;
  546. }
  547. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  548. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
  549. const_iterator position_hint,
  550. value_type&& val) -> iterator {
  551. return emplace_hint_key_args(position_hint, GetKeyFromValue()(val),
  552. std::move(val))
  553. .first;
  554. }
  555. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  556. template <class InputIterator>
  557. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
  558. InputIterator first,
  559. InputIterator last) {
  560. if (first == last)
  561. return;
  562. // Dispatch to single element insert if the input range contains a single
  563. // element.
  564. if (is_multipass<InputIterator>() && std::next(first) == last) {
  565. insert(end(), *first);
  566. return;
  567. }
  568. // Provide a convenience lambda to obtain an iterator pointing past the last
  569. // old element. This needs to be dymanic due to possible re-allocations.
  570. auto middle = [this, size = size()] { return std::next(begin(), size); };
  571. // For batch updates initialize the first insertion point.
  572. difference_type pos_first_new = size();
  573. // Loop over the input range while appending new values and overwriting
  574. // existing ones, if applicable. Keep track of the first insertion point.
  575. for (; first != last; ++first) {
  576. std::pair<iterator, bool> result = append_unique(begin(), middle(), *first);
  577. if (result.second) {
  578. pos_first_new =
  579. std::min(pos_first_new, std::distance(begin(), result.first));
  580. }
  581. }
  582. // The new elements might be unordered and contain duplicates, so post-process
  583. // the just inserted elements and merge them with the rest, inserting them at
  584. // the previously found spot.
  585. sort_and_unique(middle(), end());
  586. std::inplace_merge(std::next(begin(), pos_first_new), middle(), end(),
  587. value_comp());
  588. }
  589. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  590. template <class... Args>
  591. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args)
  592. -> std::pair<iterator, bool> {
  593. return insert(value_type(std::forward<Args>(args)...));
  594. }
  595. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  596. template <class... Args>
  597. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint(
  598. const_iterator position_hint,
  599. Args&&... args) -> iterator {
  600. return insert(position_hint, value_type(std::forward<Args>(args)...));
  601. }
  602. // ----------------------------------------------------------------------------
  603. // Underlying type operations.
  604. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  605. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::
  606. extract() && -> underlying_type {
  607. return std::exchange(impl_.body_, underlying_type());
  608. }
  609. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  610. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::replace(
  611. underlying_type&& body) {
  612. // Ensure that |body| is sorted and has no repeated elements.
  613. DCHECK(std::is_sorted(body.begin(), body.end(), value_comp()));
  614. DCHECK(std::adjacent_find(body.begin(), body.end(),
  615. [this](const auto& lhs, const auto& rhs) {
  616. return !value_comp()(lhs, rhs);
  617. }) == body.end());
  618. impl_.body_ = std::move(body);
  619. }
  620. // ----------------------------------------------------------------------------
  621. // Erase operations.
  622. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  623. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase(
  624. iterator position) -> iterator {
  625. CHECK(position != impl_.body_.end());
  626. return impl_.body_.erase(position);
  627. }
  628. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  629. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase(
  630. const_iterator position) -> iterator {
  631. CHECK(position != impl_.body_.end());
  632. return impl_.body_.erase(position);
  633. }
  634. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  635. template <typename K>
  636. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase(const K& val)
  637. -> size_type {
  638. auto eq_range = equal_range(val);
  639. auto res = std::distance(eq_range.first, eq_range.second);
  640. erase(eq_range.first, eq_range.second);
  641. return res;
  642. }
  643. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  644. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase(
  645. const_iterator first,
  646. const_iterator last) -> iterator {
  647. return impl_.body_.erase(first, last);
  648. }
  649. // ----------------------------------------------------------------------------
  650. // Comparators.
  651. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  652. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::key_comp() const
  653. -> key_compare {
  654. return impl_.get_key_comp();
  655. }
  656. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  657. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::value_comp() const
  658. -> value_compare {
  659. return impl_.get_value_comp();
  660. }
  661. // ----------------------------------------------------------------------------
  662. // Search operations.
  663. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  664. template <typename K>
  665. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::count(
  666. const K& key) const -> size_type {
  667. auto eq_range = equal_range(key);
  668. return std::distance(eq_range.first, eq_range.second);
  669. }
  670. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  671. template <typename K>
  672. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::find(const K& key)
  673. -> iterator {
  674. return const_cast_it(base::as_const(*this).find(key));
  675. }
  676. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  677. template <typename K>
  678. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::find(
  679. const K& key) const -> const_iterator {
  680. auto eq_range = equal_range(key);
  681. return (eq_range.first == eq_range.second) ? end() : eq_range.first;
  682. }
  683. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  684. template <typename K>
  685. bool flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::contains(
  686. const K& key) const {
  687. auto lower = lower_bound(key);
  688. return lower != end() && !key_comp()(key, GetKeyFromValue()(*lower));
  689. }
  690. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  691. template <typename K>
  692. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::equal_range(
  693. const K& key) -> std::pair<iterator, iterator> {
  694. auto res = base::as_const(*this).equal_range(key);
  695. return {const_cast_it(res.first), const_cast_it(res.second)};
  696. }
  697. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  698. template <typename K>
  699. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::equal_range(
  700. const K& key) const -> std::pair<const_iterator, const_iterator> {
  701. auto lower = lower_bound(key);
  702. GetKeyFromValue extractor;
  703. if (lower == end() || impl_.get_key_comp()(key, extractor(*lower)))
  704. return {lower, lower};
  705. return {lower, std::next(lower)};
  706. }
  707. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  708. template <typename K>
  709. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::lower_bound(
  710. const K& key) -> iterator {
  711. return const_cast_it(base::as_const(*this).lower_bound(key));
  712. }
  713. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  714. template <typename K>
  715. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::lower_bound(
  716. const K& key) const -> const_iterator {
  717. static_assert(std::is_convertible<const KeyTypeOrK<K>&, const K&>::value,
  718. "Requested type cannot be bound to the container's key_type "
  719. "which is required for a non-transparent compare.");
  720. const KeyTypeOrK<K>& key_ref = key;
  721. KeyValueCompare key_value(impl_.get_key_comp());
  722. return std::lower_bound(begin(), end(), key_ref, key_value);
  723. }
  724. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  725. template <typename K>
  726. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::upper_bound(
  727. const K& key) -> iterator {
  728. return const_cast_it(base::as_const(*this).upper_bound(key));
  729. }
  730. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  731. template <typename K>
  732. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::upper_bound(
  733. const K& key) const -> const_iterator {
  734. static_assert(std::is_convertible<const KeyTypeOrK<K>&, const K&>::value,
  735. "Requested type cannot be bound to the container's key_type "
  736. "which is required for a non-transparent compare.");
  737. const KeyTypeOrK<K>& key_ref = key;
  738. KeyValueCompare key_value(impl_.get_key_comp());
  739. return std::upper_bound(begin(), end(), key_ref, key_value);
  740. }
  741. // ----------------------------------------------------------------------------
  742. // General operations.
  743. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  744. void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::swap(
  745. flat_tree& other) noexcept {
  746. std::swap(impl_, other.impl_);
  747. }
  748. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  749. template <class... Args>
  750. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::unsafe_emplace(
  751. const_iterator position,
  752. Args&&... args) -> iterator {
  753. return impl_.body_.emplace(position, std::forward<Args>(args)...);
  754. }
  755. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  756. template <class K, class... Args>
  757. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_key_args(
  758. const K& key,
  759. Args&&... args) -> std::pair<iterator, bool> {
  760. auto lower = lower_bound(key);
  761. if (lower == end() || key_comp()(key, GetKeyFromValue()(*lower)))
  762. return {unsafe_emplace(lower, std::forward<Args>(args)...), true};
  763. return {lower, false};
  764. }
  765. template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
  766. template <class K, class... Args>
  767. auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint_key_args(
  768. const_iterator hint,
  769. const K& key,
  770. Args&&... args) -> std::pair<iterator, bool> {
  771. GetKeyFromValue extractor;
  772. if ((hint == begin() || key_comp()(extractor(*std::prev(hint)), key))) {
  773. if (hint == end() || key_comp()(key, extractor(*hint))) {
  774. // *(hint - 1) < key < *hint => key did not exist and hint is correct.
  775. return {unsafe_emplace(hint, std::forward<Args>(args)...), true};
  776. }
  777. if (!key_comp()(extractor(*hint), key)) {
  778. // key == *hint => no-op, return correct hint.
  779. return {const_cast_it(hint), false};
  780. }
  781. }
  782. // hint was not helpful, dispatch to hintless version.
  783. return emplace_key_args(key, std::forward<Args>(args)...);
  784. }
  785. // For containers like sets, the key is the same as the value. This implements
  786. // the GetKeyFromValue template parameter to flat_tree for this case.
  787. template <class Key>
  788. struct GetKeyFromValueIdentity {
  789. const Key& operator()(const Key& k) const { return k; }
  790. };
  791. } // namespace internal
  792. // ----------------------------------------------------------------------------
  793. // Free functions.
  794. // Erases all elements that match predicate. It has O(size) complexity.
  795. template <class Key,
  796. class Value,
  797. class GetKeyFromValue,
  798. class KeyCompare,
  799. typename Predicate>
  800. size_t EraseIf(
  801. base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>&
  802. container,
  803. Predicate pred) {
  804. auto it = std::remove_if(container.begin(), container.end(), pred);
  805. size_t removed = std::distance(it, container.end());
  806. container.erase(it, container.end());
  807. return removed;
  808. }
  809. } // namespace base
  810. #endif // BASE_CONTAINERS_FLAT_TREE_H_