flat_set.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <vector>
  9. #include <algorithm>
  10. namespace base {
  11. using std::begin;
  12. using std::end;
  13. template <typename Type, typename Compare = std::less<>>
  14. class flat_set;
  15. template <typename Type, typename Compare = std::less<>>
  16. class flat_multi_set;
  17. template <typename Type, typename iterator_impl>
  18. class flat_multi_set_iterator_impl;
  19. template <typename Type, typename iterator_impl>
  20. class flat_multi_set_iterator_impl {
  21. public:
  22. using iterator_category = typename iterator_impl::iterator_category;
  23. using value_type = Type;
  24. using difference_type = typename iterator_impl::difference_type;
  25. using pointer = const Type*;
  26. using reference = const Type&;
  27. constexpr flat_multi_set_iterator_impl(
  28. iterator_impl impl = iterator_impl()) noexcept
  29. : _impl(impl) {
  30. }
  31. template <typename other_iterator_impl>
  32. constexpr flat_multi_set_iterator_impl(
  33. const flat_multi_set_iterator_impl<
  34. Type,
  35. other_iterator_impl> &other) noexcept
  36. : _impl(other._impl) {
  37. }
  38. constexpr reference operator*() const noexcept {
  39. return *_impl;
  40. }
  41. constexpr pointer operator->() const noexcept {
  42. return std::addressof(**this);
  43. }
  44. constexpr flat_multi_set_iterator_impl &operator++() noexcept {
  45. ++_impl;
  46. return *this;
  47. }
  48. constexpr flat_multi_set_iterator_impl operator++(int) noexcept {
  49. return _impl++;
  50. }
  51. constexpr flat_multi_set_iterator_impl &operator--() noexcept {
  52. --_impl;
  53. return *this;
  54. }
  55. constexpr flat_multi_set_iterator_impl operator--(int) noexcept {
  56. return _impl--;
  57. }
  58. constexpr flat_multi_set_iterator_impl &operator+=(
  59. difference_type offset) noexcept {
  60. _impl += offset;
  61. return *this;
  62. }
  63. constexpr flat_multi_set_iterator_impl operator+(
  64. difference_type offset) const noexcept {
  65. return _impl + offset;
  66. }
  67. constexpr flat_multi_set_iterator_impl &operator-=(
  68. difference_type offset) noexcept {
  69. _impl -= offset;
  70. return *this;
  71. }
  72. constexpr flat_multi_set_iterator_impl operator-(
  73. difference_type offset) const noexcept {
  74. return _impl - offset;
  75. }
  76. template <typename other_iterator_impl>
  77. constexpr difference_type operator-(
  78. const flat_multi_set_iterator_impl<
  79. Type,
  80. other_iterator_impl> &right) const noexcept {
  81. return _impl - right._impl;
  82. }
  83. constexpr reference operator[](difference_type offset) const noexcept {
  84. return _impl[offset];
  85. }
  86. template <typename other_iterator_impl>
  87. constexpr bool operator==(
  88. const flat_multi_set_iterator_impl<
  89. Type,
  90. other_iterator_impl> &right) const noexcept {
  91. return _impl == right._impl;
  92. }
  93. template <typename other_iterator_impl>
  94. constexpr bool operator!=(
  95. const flat_multi_set_iterator_impl<
  96. Type,
  97. other_iterator_impl> &right) const noexcept {
  98. return _impl != right._impl;
  99. }
  100. template <typename other_iterator_impl>
  101. constexpr bool operator<(
  102. const flat_multi_set_iterator_impl<
  103. Type,
  104. other_iterator_impl> &right) const noexcept {
  105. return _impl < right._impl;
  106. }
  107. private:
  108. iterator_impl _impl;
  109. template <typename OtherType, typename OtherCompare>
  110. friend class flat_multi_set;
  111. template <typename OtherType, typename OtherCompare>
  112. friend class flat_set;
  113. template <
  114. typename OtherType,
  115. typename other_iterator_impl>
  116. friend class flat_multi_set_iterator_impl;
  117. constexpr Type &wrapped() noexcept {
  118. return _impl->wrapped();
  119. }
  120. };
  121. template <typename Type>
  122. class flat_multi_set_const_wrap {
  123. public:
  124. constexpr flat_multi_set_const_wrap(const Type &value) noexcept
  125. : _value(value) {
  126. }
  127. constexpr flat_multi_set_const_wrap(Type &&value) noexcept
  128. : _value(std::move(value)) {
  129. }
  130. constexpr operator const Type&() const noexcept {
  131. return _value;
  132. }
  133. constexpr Type &wrapped() noexcept {
  134. return _value;
  135. }
  136. friend inline constexpr bool operator<(
  137. const flat_multi_set_const_wrap &a,
  138. const flat_multi_set_const_wrap &b) noexcept {
  139. return a._value < b._value;
  140. }
  141. friend inline constexpr bool operator>(
  142. const flat_multi_set_const_wrap &a,
  143. const flat_multi_set_const_wrap &b) noexcept {
  144. return a._value > b._value;
  145. }
  146. friend inline constexpr bool operator<=(
  147. const flat_multi_set_const_wrap &a,
  148. const flat_multi_set_const_wrap &b) noexcept {
  149. return a._value <= b._value;
  150. }
  151. friend inline constexpr bool operator>=(
  152. const flat_multi_set_const_wrap &a,
  153. const flat_multi_set_const_wrap &b) noexcept {
  154. return a._value >= b._value;
  155. }
  156. friend inline constexpr bool operator==(
  157. const flat_multi_set_const_wrap &a,
  158. const flat_multi_set_const_wrap &b) noexcept {
  159. return a._value == b._value;
  160. }
  161. friend inline constexpr bool operator!=(
  162. const flat_multi_set_const_wrap &a,
  163. const flat_multi_set_const_wrap &b) noexcept {
  164. return a._value != b._value;
  165. }
  166. private:
  167. Type _value;
  168. };
  169. template <typename Type, typename Compare>
  170. class flat_multi_set {
  171. using const_wrap = flat_multi_set_const_wrap<Type>;
  172. using impl_t = std::vector<const_wrap>;
  173. public:
  174. using value_type = Type;
  175. using size_type = typename impl_t::size_type;
  176. using difference_type = typename impl_t::difference_type;
  177. using pointer = const Type*;
  178. using reference = const Type&;
  179. using iterator = flat_multi_set_iterator_impl<
  180. Type,
  181. typename impl_t::iterator>;
  182. using const_iterator = flat_multi_set_iterator_impl<
  183. Type,
  184. typename impl_t::const_iterator>;
  185. using reverse_iterator = flat_multi_set_iterator_impl<
  186. Type,
  187. typename impl_t::reverse_iterator>;
  188. using const_reverse_iterator = flat_multi_set_iterator_impl<
  189. Type,
  190. typename impl_t::const_reverse_iterator>;
  191. constexpr flat_multi_set() = default;
  192. template <
  193. typename Iterator,
  194. typename = typename std::iterator_traits<Iterator>::iterator_category>
  195. constexpr flat_multi_set(Iterator first, Iterator last) noexcept
  196. : _data(first, last) {
  197. std::sort(std::begin(impl()), std::end(impl()), compare());
  198. }
  199. constexpr flat_multi_set(std::initializer_list<Type> iter) noexcept
  200. : flat_multi_set(iter.begin(), iter.end()) {
  201. }
  202. constexpr size_type size() const noexcept {
  203. return impl().size();
  204. }
  205. constexpr bool empty() const noexcept {
  206. return impl().empty();
  207. }
  208. constexpr void clear() noexcept {
  209. impl().clear();
  210. }
  211. constexpr void reserve(size_type size) noexcept {
  212. impl().reserve(size);
  213. }
  214. constexpr void shrink_to_fit() noexcept {
  215. impl().shrink_to_fit();
  216. }
  217. constexpr iterator begin() noexcept {
  218. return impl().begin();
  219. }
  220. constexpr iterator end() noexcept {
  221. return impl().end();
  222. }
  223. constexpr const_iterator begin() const noexcept {
  224. return impl().begin();
  225. }
  226. constexpr const_iterator end() const noexcept {
  227. return impl().end();
  228. }
  229. constexpr const_iterator cbegin() const noexcept {
  230. return impl().cbegin();
  231. }
  232. constexpr const_iterator cend() const noexcept {
  233. return impl().cend();
  234. }
  235. constexpr reverse_iterator rbegin() noexcept {
  236. return impl().rbegin();
  237. }
  238. constexpr reverse_iterator rend() noexcept {
  239. return impl().rend();
  240. }
  241. constexpr const_reverse_iterator rbegin() const noexcept {
  242. return impl().rbegin();
  243. }
  244. constexpr const_reverse_iterator rend() const noexcept {
  245. return impl().rend();
  246. }
  247. constexpr const_reverse_iterator crbegin() const noexcept {
  248. return impl().crbegin();
  249. }
  250. constexpr const_reverse_iterator crend() const noexcept {
  251. return impl().crend();
  252. }
  253. constexpr reference front() const noexcept {
  254. return *begin();
  255. }
  256. constexpr reference back() const noexcept {
  257. return *(end() - 1);
  258. }
  259. constexpr iterator insert(const Type &value) noexcept {
  260. if (empty() || !compare()(value, back())) {
  261. impl().push_back(value);
  262. return (end() - 1);
  263. }
  264. auto where = getUpperBound(value);
  265. return impl().insert(where, value);
  266. }
  267. constexpr iterator insert(Type &&value) noexcept {
  268. if (empty() || !compare()(value, back())) {
  269. impl().push_back(std::move(value));
  270. return (end() - 1);
  271. }
  272. auto where = getUpperBound(value);
  273. return impl().insert(where, std::move(value));
  274. }
  275. template <typename... Args>
  276. constexpr iterator emplace(Args&&... args) noexcept {
  277. return insert(Type(std::forward<Args>(args)...));
  278. }
  279. template <typename OtherType>
  280. constexpr bool removeOne(const OtherType &value) noexcept {
  281. if (empty()
  282. || compare()(value, front())
  283. || compare()(back(), value)) {
  284. return false;
  285. }
  286. auto where = getLowerBound(value);
  287. if (compare()(value, *where)) {
  288. return false;
  289. }
  290. impl().erase(where);
  291. return true;
  292. }
  293. constexpr bool removeOne(const Type &value) noexcept {
  294. return removeOne<Type>(value);
  295. }
  296. template <typename OtherType>
  297. constexpr int removeAll(const OtherType &value) noexcept {
  298. if (empty()
  299. || compare()(value, front())
  300. || compare()(back(), value)) {
  301. return 0;
  302. }
  303. auto range = getEqualRange(value);
  304. if (range.first == range.second) {
  305. return 0;
  306. }
  307. const auto result = (range.second - range.first);
  308. impl().erase(range.first, range.second);
  309. return result;
  310. }
  311. constexpr int removeAll(const Type &value) noexcept {
  312. return removeAll<Type>(value);
  313. }
  314. constexpr iterator erase(const_iterator where) noexcept {
  315. return impl().erase(where._impl);
  316. }
  317. constexpr iterator erase(
  318. const_iterator from,
  319. const_iterator till) noexcept {
  320. return impl().erase(from._impl, till._impl);
  321. }
  322. constexpr int erase(const Type &value) noexcept {
  323. return removeAll(value);
  324. }
  325. template <typename OtherType>
  326. constexpr iterator findFirst(const OtherType &value) noexcept {
  327. if (empty()
  328. || compare()(value, front())
  329. || compare()(back(), value)) {
  330. return end();
  331. }
  332. auto where = getLowerBound(value);
  333. return compare()(value, *where) ? impl().end() : where;
  334. }
  335. constexpr iterator findFirst(const Type &value) noexcept {
  336. return findFirst<Type>(value);
  337. }
  338. template <typename OtherType>
  339. constexpr const_iterator findFirst(
  340. const OtherType &value) const noexcept {
  341. if (empty()
  342. || compare()(value, front())
  343. || compare()(back(), value)) {
  344. return end();
  345. }
  346. auto where = getLowerBound(value);
  347. return compare()(value, *where) ? impl().end() : where;
  348. }
  349. constexpr const_iterator findFirst(
  350. const Type &value) const noexcept {
  351. return findFirst<Type>(value);
  352. }
  353. template <typename OtherType>
  354. constexpr bool contains(const OtherType &value) const noexcept {
  355. return findFirst(value) != end();
  356. }
  357. constexpr bool contains(const Type &value) const noexcept {
  358. return contains<Type>(value);
  359. }
  360. template <typename OtherType>
  361. constexpr int count(const OtherType &value) const noexcept {
  362. if (empty()
  363. || compare()(value, front())
  364. || compare()(back(), value)) {
  365. return 0;
  366. }
  367. auto range = getEqualRange(value);
  368. return (range.second - range.first);
  369. }
  370. constexpr int count(const Type &value) const noexcept {
  371. return count<Type>(value);
  372. }
  373. template <typename OtherType>
  374. constexpr iterator lower_bound(const OtherType &value) noexcept {
  375. return getLowerBound(value);
  376. }
  377. template <typename OtherType>
  378. constexpr const_iterator lower_bound(
  379. const OtherType &value) const noexcept {
  380. return getLowerBound(value);
  381. }
  382. template <typename OtherType>
  383. constexpr iterator upper_bound(const OtherType &value) noexcept {
  384. return getUpperBound(value);
  385. }
  386. template <typename OtherType>
  387. constexpr const_iterator upper_bound(
  388. const OtherType &value) const noexcept {
  389. return getUpperBound(value);
  390. }
  391. template <typename OtherType>
  392. constexpr std::pair<iterator, iterator> equal_range(
  393. const OtherType &value) noexcept {
  394. return getEqualRange(value);
  395. }
  396. template <typename OtherType>
  397. constexpr std::pair<const_iterator, const_iterator> equal_range(
  398. const OtherType &value) const noexcept {
  399. return getEqualRange(value);
  400. }
  401. template <typename Action>
  402. constexpr auto modify(iterator which, Action action) noexcept {
  403. auto result = action(which.wrapped());
  404. for (auto i = which + 1, e = end(); i != e; ++i) {
  405. if (compare()(*i, *which)) {
  406. std::swap(i.wrapped(), which.wrapped());
  407. } else {
  408. break;
  409. }
  410. }
  411. for (auto i = which, b = begin(); i != b;) {
  412. --i;
  413. if (compare()(*which, *i)) {
  414. std::swap(i.wrapped(), which.wrapped());
  415. } else {
  416. break;
  417. }
  418. }
  419. return result;
  420. }
  421. template <
  422. typename Iterator,
  423. typename = typename std::iterator_traits<Iterator>::iterator_category>
  424. constexpr void merge(Iterator first, Iterator last) noexcept {
  425. impl().insert(impl().end(), first, last);
  426. std::sort(std::begin(impl()), std::end(impl()), compare());
  427. }
  428. constexpr void merge(
  429. const flat_multi_set<Type, Compare> &other) noexcept {
  430. merge(other.begin(), other.end());
  431. }
  432. constexpr void merge(std::initializer_list<Type> list) noexcept {
  433. merge(list.begin(), list.end());
  434. }
  435. friend inline constexpr bool operator<(
  436. const flat_multi_set &a,
  437. const flat_multi_set &b) noexcept {
  438. return a.impl() < b.impl();
  439. }
  440. friend inline constexpr bool operator>(
  441. const flat_multi_set &a,
  442. const flat_multi_set &b) noexcept {
  443. return a.impl() > b.impl();
  444. }
  445. friend inline constexpr bool operator<=(
  446. const flat_multi_set &a,
  447. const flat_multi_set &b) noexcept {
  448. return a.impl() <= b.impl();
  449. }
  450. friend inline constexpr bool operator>=(
  451. const flat_multi_set &a,
  452. const flat_multi_set &b) noexcept {
  453. return a.impl() >= b.impl();
  454. }
  455. friend inline constexpr bool operator==(
  456. const flat_multi_set &a,
  457. const flat_multi_set &b) noexcept {
  458. return a.impl() == b.impl();
  459. }
  460. friend inline constexpr bool operator!=(
  461. const flat_multi_set &a,
  462. const flat_multi_set &b) noexcept {
  463. return a.impl() != b.impl();
  464. }
  465. private:
  466. friend class flat_set<Type, Compare>;
  467. struct transparent_compare : Compare {
  468. constexpr const Compare &initial() const noexcept {
  469. return *this;
  470. }
  471. template <
  472. typename OtherType1,
  473. typename OtherType2,
  474. typename = std::enable_if_t<
  475. !std::is_same_v<std::decay_t<OtherType1>, const_wrap> &&
  476. !std::is_same_v<std::decay_t<OtherType2>, const_wrap>>>
  477. constexpr auto operator()(
  478. OtherType1 &&a,
  479. OtherType2 &&b) const noexcept {
  480. return initial()(
  481. std::forward<OtherType1>(a),
  482. std::forward<OtherType2>(b));
  483. }
  484. template <
  485. typename OtherType1,
  486. typename OtherType2>
  487. constexpr auto operator()(
  488. OtherType1 &&a,
  489. OtherType2 &&b) const noexcept -> std::enable_if_t<
  490. std::is_same_v<std::decay_t<OtherType1>, const_wrap> &&
  491. std::is_same_v<std::decay_t<OtherType2>, const_wrap>, bool> {
  492. return initial()(
  493. static_cast<const Type&>(a),
  494. static_cast<const Type&>(b));
  495. }
  496. template <
  497. typename OtherType,
  498. typename = std::enable_if_t<
  499. !std::is_same_v<std::decay_t<OtherType>, const_wrap>>>
  500. constexpr auto operator()(
  501. const const_wrap &a,
  502. OtherType &&b) const noexcept {
  503. return initial()(
  504. static_cast<const Type&>(a),
  505. std::forward<OtherType>(b));
  506. }
  507. template <
  508. typename OtherType,
  509. typename = std::enable_if_t<
  510. !std::is_same_v<std::decay_t<OtherType>, const_wrap>>>
  511. constexpr auto operator()(
  512. OtherType &&a,
  513. const const_wrap &b) const noexcept {
  514. return initial()(
  515. std::forward<OtherType>(a),
  516. static_cast<const Type&>(b));
  517. }
  518. };
  519. struct Data : transparent_compare {
  520. template <typename ...Args>
  521. constexpr Data(Args &&...args) noexcept
  522. : elements(std::forward<Args>(args)...) {
  523. }
  524. impl_t elements;
  525. };
  526. Data _data;
  527. constexpr const transparent_compare &compare() const noexcept {
  528. return _data;
  529. }
  530. constexpr const impl_t &impl() const noexcept {
  531. return _data.elements;
  532. }
  533. constexpr impl_t &impl() noexcept {
  534. return _data.elements;
  535. }
  536. template <typename OtherType>
  537. constexpr typename impl_t::iterator getLowerBound(
  538. const OtherType &value) noexcept {
  539. return std::lower_bound(
  540. std::begin(impl()),
  541. std::end(impl()),
  542. value,
  543. compare());
  544. }
  545. template <typename OtherType>
  546. constexpr typename impl_t::const_iterator getLowerBound(
  547. const OtherType &value) const noexcept {
  548. return std::lower_bound(
  549. std::begin(impl()),
  550. std::end(impl()),
  551. value,
  552. compare());
  553. }
  554. template <typename OtherType>
  555. constexpr typename impl_t::iterator getUpperBound(
  556. const OtherType &value) noexcept {
  557. return std::upper_bound(
  558. std::begin(impl()),
  559. std::end(impl()),
  560. value,
  561. compare());
  562. }
  563. template <typename OtherType>
  564. constexpr typename impl_t::const_iterator getUpperBound(
  565. const OtherType &value) const noexcept {
  566. return std::upper_bound(
  567. std::begin(impl()),
  568. std::end(impl()),
  569. value,
  570. compare());
  571. }
  572. template <typename OtherType>
  573. constexpr std::pair<
  574. typename impl_t::iterator,
  575. typename impl_t::iterator
  576. > getEqualRange(const OtherType &value) noexcept {
  577. return std::equal_range(
  578. std::begin(impl()),
  579. std::end(impl()),
  580. value,
  581. compare());
  582. }
  583. template <typename OtherType>
  584. constexpr std::pair<
  585. typename impl_t::const_iterator,
  586. typename impl_t::const_iterator
  587. > getEqualRange(const OtherType &value) const noexcept {
  588. return std::equal_range(
  589. std::begin(impl()),
  590. std::end(impl()),
  591. value,
  592. compare());
  593. }
  594. };
  595. template <typename Type, typename Compare>
  596. class flat_set : private flat_multi_set<Type, Compare> {
  597. using parent = flat_multi_set<Type, Compare>;
  598. public:
  599. using iterator = typename parent::iterator;
  600. using const_iterator = typename parent::const_iterator;
  601. using reverse_iterator = typename parent::reverse_iterator;
  602. using const_reverse_iterator = typename parent::const_reverse_iterator;
  603. using value_type = typename parent::value_type;
  604. using size_type = typename parent::size_type;
  605. using difference_type = typename parent::difference_type;
  606. using pointer = typename parent::pointer;
  607. using reference = typename parent::reference;
  608. constexpr flat_set() = default;
  609. template <
  610. typename Iterator,
  611. typename = typename std::iterator_traits<Iterator>::iterator_category
  612. >
  613. constexpr flat_set(Iterator first, Iterator last) noexcept
  614. : parent(first, last) {
  615. finalize();
  616. }
  617. constexpr flat_set(std::initializer_list<Type> iter) noexcept
  618. : parent(iter.begin(), iter.end()) {
  619. finalize();
  620. }
  621. using parent::parent;
  622. using parent::size;
  623. using parent::empty;
  624. using parent::clear;
  625. using parent::reserve;
  626. using parent::shrink_to_fit;
  627. using parent::begin;
  628. using parent::end;
  629. using parent::cbegin;
  630. using parent::cend;
  631. using parent::rbegin;
  632. using parent::rend;
  633. using parent::crbegin;
  634. using parent::crend;
  635. using parent::front;
  636. using parent::back;
  637. using parent::contains;
  638. using parent::erase;
  639. using parent::lower_bound;
  640. using parent::upper_bound;
  641. using parent::equal_range;
  642. constexpr std::pair<iterator, bool> insert(const Type &value) noexcept {
  643. if (this->empty() || this->compare()(this->back(), value)) {
  644. this->impl().push_back(value);
  645. return std::make_pair(this->end() - 1, true);
  646. }
  647. auto where = this->getLowerBound(value);
  648. if (this->compare()(value, *where)) {
  649. return std::make_pair(this->impl().insert(where, value), true);
  650. }
  651. return std::make_pair(where, false);
  652. }
  653. constexpr std::pair<iterator, bool> insert(Type &&value) noexcept {
  654. if (this->empty() || this->compare()(this->back(), value)) {
  655. this->impl().push_back(std::move(value));
  656. return std::make_pair(this->end() - 1, true);
  657. }
  658. auto where = this->getLowerBound(value);
  659. if (this->compare()(value, *where)) {
  660. return std::make_pair(
  661. this->impl().insert(where, std::move(value)),
  662. true);
  663. }
  664. return std::make_pair(where, false);
  665. }
  666. template <typename... Args>
  667. constexpr std::pair<iterator, bool> emplace(Args&&... args) noexcept {
  668. return this->insert(Type(std::forward<Args>(args)...));
  669. }
  670. template <typename OtherType>
  671. constexpr bool remove(const OtherType &value) noexcept {
  672. return this->removeOne(value);
  673. }
  674. constexpr bool remove(const Type &value) noexcept {
  675. return remove<Type>(value);
  676. }
  677. template <typename OtherType>
  678. constexpr iterator find(const OtherType &value) noexcept {
  679. return this->findFirst(value);
  680. }
  681. constexpr iterator find(const Type &value) noexcept {
  682. return find<Type>(value);
  683. }
  684. template <typename OtherType>
  685. constexpr const_iterator find(const OtherType &value) const noexcept {
  686. return this->findFirst(value);
  687. }
  688. constexpr const_iterator find(const Type &value) const noexcept {
  689. return find<Type>(value);
  690. }
  691. template <typename Action>
  692. constexpr void modify(iterator which, Action action) noexcept {
  693. action(which.wrapped());
  694. for (auto i = iterator(which + 1), e = end(); i != e; ++i) {
  695. if (this->compare()(*i, *which)) {
  696. std::swap(i.wrapped(), which.wrapped());
  697. } else if (!this->compare()(*which, *i)) {
  698. erase(which);
  699. return;
  700. } else{
  701. break;
  702. }
  703. }
  704. for (auto i = which, b = begin(); i != b;) {
  705. --i;
  706. if (this->compare()(*which, *i)) {
  707. std::swap(i.wrapped(), which.wrapped());
  708. } else if (!this->compare()(*i, *which)) {
  709. erase(which);
  710. return;
  711. } else {
  712. break;
  713. }
  714. }
  715. }
  716. template <
  717. typename Iterator,
  718. typename = typename std::iterator_traits<Iterator>::iterator_category>
  719. constexpr void merge(Iterator first, Iterator last) noexcept {
  720. parent::merge(first, last);
  721. finalize();
  722. }
  723. constexpr void merge(
  724. const flat_multi_set<Type, Compare> &other) noexcept {
  725. merge(other.begin(), other.end());
  726. }
  727. constexpr void merge(std::initializer_list<Type> list) noexcept {
  728. merge(list.begin(), list.end());
  729. }
  730. friend inline constexpr bool operator<(
  731. const flat_set &a,
  732. const flat_set &b) noexcept {
  733. return static_cast<const parent&>(a) < static_cast<const parent&>(b);
  734. }
  735. friend inline constexpr bool operator>(
  736. const flat_set &a,
  737. const flat_set &b) noexcept {
  738. return static_cast<const parent&>(a) > static_cast<const parent&>(b);
  739. }
  740. friend inline constexpr bool operator<=(
  741. const flat_set &a,
  742. const flat_set &b) noexcept {
  743. return static_cast<const parent&>(a) <= static_cast<const parent&>(b);
  744. }
  745. friend inline constexpr bool operator>=(
  746. const flat_set &a,
  747. const flat_set &b) noexcept {
  748. return static_cast<const parent&>(a) >= static_cast<const parent&>(b);
  749. }
  750. friend inline constexpr bool operator==(
  751. const flat_set &a,
  752. const flat_set &b) noexcept {
  753. return static_cast<const parent&>(a) == static_cast<const parent&>(b);
  754. }
  755. friend inline constexpr bool operator!=(
  756. const flat_set &a,
  757. const flat_set &b) noexcept {
  758. return static_cast<const parent&>(a) != static_cast<const parent&>(b);
  759. }
  760. private:
  761. constexpr void finalize() noexcept {
  762. this->impl().erase(
  763. std::unique(
  764. std::begin(this->impl()),
  765. std::end(this->impl()),
  766. [&](auto &&a, auto &&b) {
  767. return !this->compare()(a, b);
  768. }
  769. ),
  770. std::end(this->impl()));
  771. }
  772. };
  773. } // namespace base