flat_tree.h 35 KB

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