| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- // This file is part of Desktop App Toolkit,
- // a set of libraries for developing nice desktop applications.
- //
- // For license and copyright information please follow this link:
- // https://github.com/desktop-app/legal/blob/master/LEGAL
- //
- #pragma once
- #include <vector>
- #include <algorithm>
- #include "base/optional.h"
- namespace base {
- using std::begin;
- using std::end;
- template <
- typename Key,
- typename Type,
- typename Compare = std::less<>>
- class flat_map;
- template <
- typename Key,
- typename Type,
- typename Compare = std::less<>>
- class flat_multi_map;
- template <
- typename Me,
- typename Key,
- typename Type,
- typename iterator_impl,
- typename pointer_impl,
- typename reference_impl>
- class flat_multi_map_iterator_base_impl;
- template <typename Key, typename Value>
- struct flat_multi_map_pair_type {
- using first_type = const Key;
- using second_type = Value;
- constexpr flat_multi_map_pair_type() noexcept
- : first()
- , second() {
- }
- template <typename OtherKey, typename OtherValue>
- constexpr flat_multi_map_pair_type(
- OtherKey &&key,
- OtherValue &&value) noexcept
- : first(std::forward<OtherKey>(key))
- , second(std::forward<OtherValue>(value)) {
- }
- constexpr flat_multi_map_pair_type(
- const flat_multi_map_pair_type &pair) noexcept
- : first(pair.first)
- , second(pair.second) {
- }
- constexpr flat_multi_map_pair_type(
- flat_multi_map_pair_type &&pair) noexcept
- : first(std::move(const_cast<Key&>(pair.first)))
- , second(std::move(pair.second)) {
- }
- flat_multi_map_pair_type &operator=(
- const flat_multi_map_pair_type&) = delete;
- constexpr flat_multi_map_pair_type &operator=(
- flat_multi_map_pair_type &&other) noexcept {
- const_cast<Key&>(first) = std::move(const_cast<Key&>(other.first));
- second = std::move(other.second);
- return *this;
- }
- friend inline constexpr bool operator<(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- if (a.first < b.first) {
- return true;
- } else if (a.first > b.first) {
- return false;
- }
- return (a.second < b.second);
- }
- friend inline constexpr bool operator>(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- return b < a;
- }
- friend inline constexpr bool operator<=(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- return !(b < a);
- }
- friend inline constexpr bool operator>=(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- return !(a < b);
- }
- friend inline constexpr bool operator==(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- return (a.first == b.first) && (a.second == b.second);
- }
- friend inline constexpr bool operator!=(
- const flat_multi_map_pair_type &a,
- const flat_multi_map_pair_type &b) noexcept {
- return !(a == b);
- }
- constexpr void swap(flat_multi_map_pair_type &other) noexcept {
- using std::swap;
- if (this != &other) {
- std::swap(
- const_cast<Key&>(first),
- const_cast<Key&>(other.first));
- std::swap(second, other.second);
- }
- }
- const Key first;
- Value second;
- };
- template <
- typename Me,
- typename Key,
- typename Type,
- typename iterator_impl,
- typename pointer_impl,
- typename reference_impl>
- class flat_multi_map_iterator_base_impl {
- public:
- using iterator_category = typename iterator_impl::iterator_category;
- using pair_type = flat_multi_map_pair_type<Key, Type>;
- using value_type = pair_type;
- using difference_type = typename iterator_impl::difference_type;
- using pointer = pointer_impl;
- using reference = reference_impl;
- constexpr flat_multi_map_iterator_base_impl(
- iterator_impl impl = iterator_impl()) noexcept
- : _impl(impl) {
- }
- constexpr reference operator*() const noexcept {
- return *_impl;
- }
- constexpr pointer operator->() const noexcept {
- return std::addressof(**this);
- }
- constexpr Me &operator++() noexcept {
- ++_impl;
- return static_cast<Me&>(*this);
- }
- constexpr Me operator++(int) noexcept {
- return _impl++;
- }
- constexpr Me &operator--() noexcept {
- --_impl;
- return static_cast<Me&>(*this);
- }
- constexpr Me operator--(int) noexcept {
- return _impl--;
- }
- constexpr Me &operator+=(difference_type offset) noexcept {
- _impl += offset;
- return static_cast<Me&>(*this);
- }
- constexpr Me operator+(difference_type offset) const noexcept {
- return _impl + offset;
- }
- constexpr Me &operator-=(difference_type offset) noexcept {
- _impl -= offset;
- return static_cast<Me&>(*this);
- }
- constexpr Me operator-(difference_type offset) const noexcept {
- return _impl - offset;
- }
- template <
- typename other_me,
- typename other_iterator_impl,
- typename other_pointer_impl,
- typename other_reference_impl>
- constexpr difference_type operator-(
- const flat_multi_map_iterator_base_impl<
- other_me,
- Key,
- Type,
- other_iterator_impl,
- other_pointer_impl,
- other_reference_impl> &right) const noexcept {
- return _impl - right._impl;
- }
- constexpr reference operator[](difference_type offset) const noexcept {
- return _impl[offset];
- }
- template <
- typename other_me,
- typename other_iterator_impl,
- typename other_pointer_impl,
- typename other_reference_impl>
- constexpr bool operator==(
- const flat_multi_map_iterator_base_impl<
- other_me,
- Key,
- Type,
- other_iterator_impl,
- other_pointer_impl,
- other_reference_impl> &right) const noexcept {
- return _impl == right._impl;
- }
- template <
- typename other_me,
- typename other_iterator_impl,
- typename other_pointer_impl,
- typename other_reference_impl>
- constexpr bool operator!=(
- const flat_multi_map_iterator_base_impl<
- other_me,
- Key,
- Type,
- other_iterator_impl,
- other_pointer_impl,
- other_reference_impl> &right) const noexcept {
- return _impl != right._impl;
- }
- template <
- typename other_me,
- typename other_iterator_impl,
- typename other_pointer_impl,
- typename other_reference_impl>
- constexpr bool operator<(
- const flat_multi_map_iterator_base_impl<
- other_me,
- Key,
- Type,
- other_iterator_impl,
- other_pointer_impl,
- other_reference_impl> &right) const noexcept {
- return _impl < right._impl;
- }
- private:
- iterator_impl _impl;
- template <
- typename OtherKey,
- typename OtherType,
- typename OtherCompare>
- friend class flat_multi_map;
- template <
- typename OtherMe,
- typename OtherKey,
- typename OtherType,
- typename other_iterator_impl,
- typename other_pointer_impl,
- typename other_reference_impl>
- friend class flat_multi_map_iterator_base_impl;
- };
- template <typename Key, typename Type, typename Compare>
- class flat_multi_map {
- public:
- class iterator;
- class const_iterator;
- class reverse_iterator;
- class const_reverse_iterator;
- private:
- using pair_type = flat_multi_map_pair_type<Key, Type>;
- using impl_t = std::vector<pair_type>;
- using iterator_base = flat_multi_map_iterator_base_impl<
- iterator,
- Key,
- Type,
- typename impl_t::iterator,
- pair_type*,
- pair_type&>;
- using const_iterator_base = flat_multi_map_iterator_base_impl<
- const_iterator,
- Key,
- Type,
- typename impl_t::const_iterator,
- const pair_type*,
- const pair_type&>;
- using reverse_iterator_base = flat_multi_map_iterator_base_impl<
- reverse_iterator,
- Key,
- Type,
- typename impl_t::reverse_iterator,
- pair_type*,
- pair_type&>;
- using const_reverse_iterator_base = flat_multi_map_iterator_base_impl<
- const_reverse_iterator,
- Key,
- Type,
- typename impl_t::const_reverse_iterator,
- const pair_type*,
- const pair_type&>;
- public:
- using value_type = pair_type;
- using size_type = typename impl_t::size_type;
- using difference_type = typename impl_t::difference_type;
- using pointer = pair_type*;
- using const_pointer = const pair_type*;
- using reference = pair_type&;
- using const_reference = const pair_type&;
- class iterator : public iterator_base {
- public:
- using iterator_base::iterator_base;
- constexpr iterator() = default;
- constexpr iterator(const iterator_base &other) noexcept
- : iterator_base(other) {
- }
- friend class const_iterator;
- };
- class const_iterator : public const_iterator_base {
- public:
- using const_iterator_base::const_iterator_base;
- constexpr const_iterator() = default;
- constexpr const_iterator(const_iterator_base other) noexcept
- : const_iterator_base(other) {
- }
- constexpr const_iterator(const iterator &other) noexcept
- : const_iterator_base(other._impl) {
- }
- };
- class reverse_iterator : public reverse_iterator_base {
- public:
- using reverse_iterator_base::reverse_iterator_base;
- constexpr reverse_iterator() = default;
- constexpr reverse_iterator(reverse_iterator_base other) noexcept
- : reverse_iterator_base(other) {
- }
- friend class const_reverse_iterator;
- };
- class const_reverse_iterator : public const_reverse_iterator_base {
- public:
- using const_reverse_iterator_base::const_reverse_iterator_base;
- constexpr const_reverse_iterator() = default;
- constexpr const_reverse_iterator(
- const_reverse_iterator_base other) noexcept
- : const_reverse_iterator_base(other) {
- }
- constexpr const_reverse_iterator(
- const reverse_iterator &other) noexcept
- : const_reverse_iterator_base(other._impl) {
- }
- };
- constexpr flat_multi_map() = default;
- constexpr flat_multi_map(const flat_multi_map &other) = default;
- constexpr flat_multi_map(flat_multi_map &&other) = default;
- constexpr flat_multi_map &operator=(
- const flat_multi_map &other) noexcept {
- auto copy = other;
- return (*this = std::move(copy));
- }
- constexpr flat_multi_map &operator=(flat_multi_map &&other) = default;
- friend inline constexpr bool operator<(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() < b.impl();
- }
- friend inline constexpr bool operator>(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() > b.impl();
- }
- friend inline constexpr bool operator<=(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() <= b.impl();
- }
- friend inline constexpr bool operator>=(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() >= b.impl();
- }
- friend inline constexpr bool operator==(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() == b.impl();
- }
- friend inline constexpr bool operator!=(
- const flat_multi_map &a,
- const flat_multi_map &b) noexcept {
- return a.impl() != b.impl();
- }
- template <
- typename Iterator,
- typename = typename std::iterator_traits<Iterator>::iterator_category>
- constexpr flat_multi_map(Iterator first, Iterator last) noexcept
- : _data(first, last) {
- std::sort(std::begin(impl()), std::end(impl()), compare());
- }
- constexpr flat_multi_map(std::initializer_list<pair_type> iter) noexcept
- : flat_multi_map(iter.begin(), iter.end()) {
- }
- constexpr size_type size() const noexcept {
- return impl().size();
- }
- constexpr bool empty() const noexcept {
- return impl().empty();
- }
- constexpr void clear() noexcept {
- impl().clear();
- }
- constexpr void reserve(size_type size) noexcept {
- impl().reserve(size);
- }
- constexpr void shrink_to_fit() noexcept {
- impl().shrink_to_fit();
- }
- constexpr iterator begin() noexcept {
- return impl().begin();
- }
- constexpr iterator end() noexcept {
- return impl().end();
- }
- constexpr const_iterator begin() const noexcept {
- return impl().begin();
- }
- constexpr const_iterator end() const noexcept {
- return impl().end();
- }
- constexpr const_iterator cbegin() const noexcept {
- return impl().cbegin();
- }
- constexpr const_iterator cend() const noexcept {
- return impl().cend();
- }
- constexpr reverse_iterator rbegin() noexcept {
- return impl().rbegin();
- }
- constexpr reverse_iterator rend() noexcept {
- return impl().rend();
- }
- constexpr const_reverse_iterator rbegin() const noexcept {
- return impl().rbegin();
- }
- constexpr const_reverse_iterator rend() const noexcept {
- return impl().rend();
- }
- constexpr const_reverse_iterator crbegin() const noexcept {
- return impl().crbegin();
- }
- constexpr const_reverse_iterator crend() const noexcept {
- return impl().crend();
- }
- constexpr reference front() noexcept {
- return *begin();
- }
- constexpr const_reference front() const noexcept {
- return *begin();
- }
- constexpr reference back() noexcept {
- return *(end() - 1);
- }
- constexpr const_reference back() const noexcept {
- return *(end() - 1);
- }
- constexpr iterator insert(const value_type &value) noexcept {
- if (empty() || !compare()(value.first, back().first)) {
- impl().push_back(value);
- return (end() - 1);
- }
- auto where = getUpperBound(value.first);
- return impl().insert(where, value);
- }
- constexpr iterator insert(value_type &&value) noexcept {
- if (empty() || !compare()(value.first, back().first)) {
- impl().push_back(std::move(value));
- return (end() - 1);
- }
- auto where = getUpperBound(value.first);
- return impl().insert(where, std::move(value));
- }
- template <typename... Args>
- constexpr iterator emplace(Args&&... args) noexcept {
- return insert(value_type(std::forward<Args>(args)...));
- }
- template <typename OtherKey>
- constexpr bool removeOne(const OtherKey &key) noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return false;
- }
- auto where = getLowerBound(key);
- if (compare()(key, where->first)) {
- return false;
- }
- impl().erase(where);
- return true;
- }
- constexpr bool removeOne(const Key &key) noexcept {
- return removeOne<Key>(key);
- }
- template <typename OtherKey>
- constexpr int removeAll(const OtherKey &key) noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return 0;
- }
- auto range = getEqualRange(key);
- if (range.first == range.second) {
- return 0;
- }
- const auto result = (range.second - range.first);
- impl().erase(range.first, range.second);
- return result;
- }
- constexpr int removeAll(const Key &key) noexcept {
- return removeAll<Key>(key);
- }
- template <typename OtherKey>
- constexpr bool remove(const OtherKey &key, const Type &value) noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return false;
- }
- const auto e = impl().end();
- for (auto where = getLowerBound(key);
- (where != e) && !compare()(key, where->first);
- ++where) {
- if (where->second == value) {
- impl().erase(where);
- return true;
- }
- }
- return false;
- }
- constexpr bool remove(const Key &key, const Type &value) noexcept {
- return remove<Key>(key, value);
- }
- constexpr iterator erase(const_iterator where) noexcept {
- return impl().erase(where._impl);
- }
- constexpr iterator erase(
- const_iterator from,
- const_iterator till) noexcept {
- return impl().erase(from._impl, till._impl);
- }
- constexpr int erase(const Key &key) {
- return removeAll(key);
- }
- template <typename OtherKey>
- constexpr iterator findFirst(const OtherKey &key) noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return end();
- }
- auto where = getLowerBound(key);
- return compare()(key, where->first) ? impl().end() : where;
- }
- constexpr iterator findFirst(const Key &key) noexcept {
- return findFirst<Key>(key);
- }
- template <typename OtherKey>
- constexpr const_iterator findFirst(const OtherKey &key) const noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return end();
- }
- auto where = getLowerBound(key);
- return compare()(key, where->first) ? impl().end() : where;
- }
- constexpr const_iterator findFirst(const Key &key) const noexcept {
- return findFirst<Key>(key);
- }
- template <typename OtherKey>
- constexpr bool contains(const OtherKey &key) const noexcept {
- return findFirst(key) != end();
- }
- constexpr bool contains(const Key &key) const noexcept {
- return contains<Key>(key);
- }
- template <typename OtherKey>
- constexpr int count(const OtherKey &key) const noexcept {
- if (empty()
- || compare()(key, front().first)
- || compare()(back().first, key)) {
- return 0;
- }
- auto range = getEqualRange(key);
- return (range.second - range.first);
- }
- constexpr int count(const Key &key) const noexcept {
- return count<Key>(key);
- }
- template <typename OtherKey>
- constexpr iterator lower_bound(const OtherKey &key) noexcept {
- return getLowerBound(key);
- }
- template <typename OtherKey>
- constexpr const_iterator lower_bound(
- const OtherKey &key) const noexcept {
- return getLowerBound(key);
- }
- template <typename OtherKey>
- constexpr iterator upper_bound(const OtherKey &key) noexcept {
- return getUpperBound(key);
- }
- template <typename OtherKey>
- constexpr const_iterator upper_bound(
- const OtherKey &key) const noexcept {
- return getUpperBound(key);
- }
- template <typename OtherKey>
- constexpr std::pair<iterator, iterator> equal_range(
- const OtherKey &key) noexcept {
- return getEqualRange(key);
- }
- template <typename OtherKey>
- constexpr std::pair<const_iterator, const_iterator> equal_range(
- const OtherKey &key) const noexcept {
- return getEqualRange(key);
- }
- private:
- friend class flat_map<Key, Type, Compare>;
- struct transparent_compare : Compare {
- constexpr const Compare &initial() const noexcept {
- return *this;
- }
- template <
- typename OtherType1,
- typename OtherType2,
- typename = std::enable_if_t<
- !std::is_same_v<std::decay_t<OtherType1>, pair_type> &&
- !std::is_same_v<std::decay_t<OtherType2>, pair_type>>>
- constexpr auto operator()(
- OtherType1 &&a,
- OtherType2 &&b) const noexcept {
- return initial()(
- std::forward<OtherType1>(a),
- std::forward<OtherType2>(b));
- }
- template <
- typename OtherType1,
- typename OtherType2>
- constexpr auto operator()(
- OtherType1 &&a,
- OtherType2 &&b) const noexcept -> std::enable_if_t<
- std::is_same_v<std::decay_t<OtherType1>, pair_type> &&
- std::is_same_v<std::decay_t<OtherType2>, pair_type>, bool> {
- return initial()(a.first, b.first);
- }
- template <
- typename OtherType,
- typename = std::enable_if_t<
- !std::is_same_v<std::decay_t<OtherType>, pair_type>>>
- constexpr auto operator()(
- const pair_type &a,
- OtherType &&b) const noexcept {
- return operator()(a.first, std::forward<OtherType>(b));
- }
- template <
- typename OtherType,
- typename = std::enable_if_t<
- !std::is_same_v<std::decay_t<OtherType>, pair_type>>>
- constexpr auto operator()(
- OtherType &&a,
- const pair_type &b) const noexcept {
- return operator()(std::forward<OtherType>(a), b.first);
- }
- };
- struct Data : transparent_compare {
- template <typename ...Args>
- constexpr Data(Args &&...args) noexcept
- : elements(std::forward<Args>(args)...) {
- }
- impl_t elements;
- };
- Data _data;
- constexpr const transparent_compare &compare() const noexcept {
- return _data;
- }
- constexpr const impl_t &impl() const noexcept {
- return _data.elements;
- }
- constexpr impl_t &impl() noexcept {
- return _data.elements;
- }
- template <typename OtherKey>
- constexpr typename impl_t::iterator getLowerBound(
- const OtherKey &key) noexcept {
- return std::lower_bound(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- template <typename OtherKey>
- constexpr typename impl_t::const_iterator getLowerBound(
- const OtherKey &key) const noexcept {
- return std::lower_bound(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- template <typename OtherKey>
- constexpr typename impl_t::iterator getUpperBound(
- const OtherKey &key) noexcept {
- return std::upper_bound(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- template <typename OtherKey>
- constexpr typename impl_t::const_iterator getUpperBound(
- const OtherKey &key) const noexcept {
- return std::upper_bound(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- template <typename OtherKey>
- constexpr std::pair<
- typename impl_t::iterator,
- typename impl_t::iterator
- > getEqualRange(const OtherKey &key) noexcept {
- return std::equal_range(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- template <typename OtherKey>
- constexpr std::pair<
- typename impl_t::const_iterator,
- typename impl_t::const_iterator
- > getEqualRange(const OtherKey &key) const noexcept {
- return std::equal_range(
- std::begin(impl()),
- std::end(impl()),
- key,
- compare());
- }
- };
- template <typename Key, typename Type, typename Compare>
- class flat_map : private flat_multi_map<Key, Type, Compare> {
- using parent = flat_multi_map<Key, Type, Compare>;
- using pair_type = typename parent::pair_type;
- public:
- using value_type = typename parent::value_type;
- using size_type = typename parent::size_type;
- using difference_type = typename parent::difference_type;
- using pointer = typename parent::pointer;
- using const_pointer = typename parent::const_pointer;
- using reference = typename parent::reference;
- using const_reference = typename parent::const_reference;
- using iterator = typename parent::iterator;
- using const_iterator = typename parent::const_iterator;
- using reverse_iterator = typename parent::reverse_iterator;
- using const_reverse_iterator = typename parent::const_reverse_iterator;
- constexpr flat_map() = default;
- template <
- typename Iterator,
- typename = typename std::iterator_traits<Iterator>::iterator_category
- >
- constexpr flat_map(Iterator first, Iterator last) noexcept
- : parent(first, last) {
- finalize();
- }
- constexpr flat_map(std::initializer_list<pair_type> iter) noexcept
- : parent(iter.begin(), iter.end()) {
- finalize();
- }
- using parent::parent;
- using parent::size;
- using parent::empty;
- using parent::clear;
- using parent::reserve;
- using parent::shrink_to_fit;
- using parent::begin;
- using parent::end;
- using parent::cbegin;
- using parent::cend;
- using parent::rbegin;
- using parent::rend;
- using parent::crbegin;
- using parent::crend;
- using parent::front;
- using parent::back;
- using parent::erase;
- using parent::contains;
- using parent::lower_bound;
- using parent::upper_bound;
- using parent::equal_range;
- std::pair<iterator, bool> insert(const value_type &value) {
- if (this->empty()
- || this->compare()(this->back().first, value.first)) {
- this->impl().push_back(value);
- return { this->end() - 1, true };
- }
- auto where = this->getLowerBound(value.first);
- if (this->compare()(value.first, where->first)) {
- return { this->impl().insert(where, value), true };
- }
- return { where, false };
- }
- constexpr std::pair<iterator, bool> insert(value_type &&value) {
- if (this->empty() || this->compare()(this->back().first, value.first)) {
- this->impl().push_back(std::move(value));
- return { this->end() - 1, true };
- }
- auto where = this->getLowerBound(value.first);
- if (this->compare()(value.first, where->first)) {
- return { this->impl().insert(where, std::move(value)), true };
- }
- return { where, false };
- }
- constexpr std::pair<iterator, bool> insert_or_assign(
- const Key &key,
- const Type &value) noexcept {
- if (this->empty() || this->compare()(this->back().first, key)) {
- this->impl().emplace_back(key, value);
- return { this->end() - 1, true };
- }
- auto where = this->getLowerBound(key);
- if (this->compare()(key, where->first)) {
- return {
- this->impl().insert(where, value_type(key, value)),
- true
- };
- }
- where->second = value;
- return { where, false };
- }
- constexpr std::pair<iterator, bool> insert_or_assign(
- const Key &key,
- Type &&value) noexcept {
- if (this->empty() || this->compare()(this->back().first, key)) {
- this->impl().emplace_back(key, std::move(value));
- return { this->end() - 1, true };
- }
- auto where = this->getLowerBound(key);
- if (this->compare()(key, where->first)) {
- return {
- this->impl().insert(
- where,
- value_type(key, std::move(value))),
- true
- };
- }
- where->second = std::move(value);
- return { where, false };
- }
- template <typename OtherKey, typename... Args>
- constexpr std::pair<iterator, bool> emplace(
- OtherKey &&key,
- Args&&... args) noexcept {
- return this->insert(value_type(
- std::forward<OtherKey>(key),
- Type(std::forward<Args>(args)...)));
- }
- template <typename... Args>
- constexpr std::pair<iterator, bool> emplace_or_assign(
- const Key &key,
- Args&&... args) noexcept {
- return this->insert_or_assign(
- key,
- Type(std::forward<Args>(args)...));
- }
- template <typename... Args>
- constexpr std::pair<iterator, bool> try_emplace(
- const Key &key,
- Args&&... args) noexcept {
- if (this->empty() || this->compare()(this->back().first, key)) {
- this->impl().push_back(value_type(
- key,
- Type(std::forward<Args>(args)...)));
- return { this->end() - 1, true };
- }
- auto where = this->getLowerBound(key);
- if (this->compare()(key, where->first)) {
- return {
- this->impl().insert(
- where,
- value_type(
- key,
- Type(std::forward<Args>(args)...))),
- true
- };
- }
- return { where, false };
- }
- template <typename OtherKey>
- constexpr bool remove(const OtherKey &key) noexcept {
- return this->removeOne(key);
- }
- constexpr bool remove(const Key &key) noexcept {
- return remove<Key>(key);
- }
- template <typename OtherKey>
- constexpr iterator find(const OtherKey &key) noexcept {
- return this->template findFirst<OtherKey>(key);
- }
- constexpr iterator find(const Key &key) noexcept {
- return find<Key>(key);
- }
- template <typename OtherKey>
- constexpr const_iterator find(const OtherKey &key) const noexcept {
- return this->template findFirst<OtherKey>(key);
- }
- constexpr const_iterator find(const Key &key) const noexcept {
- return find<Key>(key);
- }
- constexpr Type &operator[](const Key &key) noexcept {
- if (this->empty() || this->compare()(this->back().first, key)) {
- this->impl().push_back({ key, Type() });
- return this->back().second;
- }
- auto where = this->getLowerBound(key);
- if (this->compare()(key, where->first)) {
- return this->impl().insert(where, { key, Type() })->second;
- }
- return where->second;
- }
- template <typename OtherKey>
- constexpr std::optional<Type> take(const OtherKey &key) noexcept {
- auto it = find(key);
- if (it == this->end()) {
- return std::nullopt;
- }
- auto result = std::move(it->second);
- this->erase(it);
- return result;
- }
- constexpr std::optional<Type> take(const Key &key) noexcept {
- return take<Key>(key);
- }
- friend inline constexpr bool operator<(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) < static_cast<const parent&>(b);
- }
- friend inline constexpr bool operator>(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) > static_cast<const parent&>(b);
- }
- friend inline constexpr bool operator<=(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) <= static_cast<const parent&>(b);
- }
- friend inline constexpr bool operator>=(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) >= static_cast<const parent&>(b);
- }
- friend inline constexpr bool operator==(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) == static_cast<const parent&>(b);
- }
- friend inline constexpr bool operator!=(
- const flat_map &a,
- const flat_map &b) noexcept {
- return static_cast<const parent&>(a) != static_cast<const parent&>(b);
- }
- private:
- constexpr void finalize() noexcept {
- this->impl().erase(
- std::unique(
- std::begin(this->impl()),
- std::end(this->impl()),
- [&](auto &&a, auto &&b) {
- return !this->compare()(a, b);
- }
- ),
- std::end(this->impl()));
- }
- };
- } // namespace base
- // Structured bindings support.
- namespace std {
- template <typename Key, typename Value>
- class tuple_size<base::flat_multi_map_pair_type<Key, Value>>
- : public integral_constant<size_t, 2> {
- };
- template <typename Key, typename Value>
- class tuple_element<0, base::flat_multi_map_pair_type<Key, Value>> {
- public:
- using type = const Key;
- };
- template <typename Key, typename Value>
- class tuple_element<1, base::flat_multi_map_pair_type<Key, Value>> {
- public:
- using type = Value;
- };
- } // namespace std
- // Structured bindings support.
- namespace base {
- namespace details {
- template <std::size_t N, typename Key, typename Value>
- using flat_multi_map_pair_element = std::tuple_element_t<
- N,
- flat_multi_map_pair_type<Key, Value>>;
- } // namespace details
- template <std::size_t N, typename Key, typename Value>
- constexpr auto get(base::flat_multi_map_pair_type<Key, Value> &value) noexcept
- -> details::flat_multi_map_pair_element<N, Key, Value> & {
- if constexpr (N == 0) {
- return value.first;
- } else {
- return value.second;
- }
- }
- template <std::size_t N, typename Key, typename Value>
- constexpr auto get(
- const base::flat_multi_map_pair_type<Key, Value> &value) noexcept
- -> const details::flat_multi_map_pair_element<N, Key, Value> & {
- if constexpr (N == 0) {
- return value.first;
- } else {
- return value.second;
- }
- }
- } // namespace base
|