123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- #ifndef BASE_CONTAINERS_FLAT_MAP_H_
- #define BASE_CONTAINERS_FLAT_MAP_H_
- #include <functional>
- #include <tuple>
- #include <utility>
- #include "base/check.h"
- #include "base/containers/flat_tree.h"
- #include "base/template_util.h"
- namespace base {
- namespace internal {
- template <class Key, class Mapped>
- struct GetKeyFromValuePairFirst {
- const Key& operator()(const std::pair<Key, Mapped>& p) const {
- return p.first;
- }
- };
- }
- template <class Key, class Mapped, class Compare = std::less<>>
- class flat_map : public ::base::internal::flat_tree<
- Key,
- std::pair<Key, Mapped>,
- ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>,
- Compare> {
- private:
- using tree = typename ::base::internal::flat_tree<
- Key,
- std::pair<Key, Mapped>,
- ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>,
- Compare>;
- using underlying_type = typename tree::underlying_type;
- public:
- using key_type = typename tree::key_type;
- using mapped_type = Mapped;
- using value_type = typename tree::value_type;
- using iterator = typename tree::iterator;
- using const_iterator = typename tree::const_iterator;
-
-
-
-
-
-
-
- flat_map() = default;
- explicit flat_map(const Compare& comp);
- template <class InputIterator>
- flat_map(InputIterator first,
- InputIterator last,
- const Compare& comp = Compare());
- flat_map(const flat_map&) = default;
- flat_map(flat_map&&) noexcept = default;
- flat_map(const underlying_type& items, const Compare& comp = Compare());
- flat_map(underlying_type&& items, const Compare& comp = Compare());
- flat_map(std::initializer_list<value_type> ilist,
- const Compare& comp = Compare());
- ~flat_map() = default;
- flat_map& operator=(const flat_map&) = default;
- flat_map& operator=(flat_map&&) noexcept = default;
-
- flat_map& operator=(std::initializer_list<value_type> ilist);
-
- template <class K>
- mapped_type& at(const K& key);
- template <class K>
- const mapped_type& at(const K& key) const;
-
-
-
-
-
-
-
- mapped_type& operator[](const key_type& key);
- mapped_type& operator[](key_type&& key);
- template <class K, class M>
- std::pair<iterator, bool> insert_or_assign(K&& key, M&& obj);
- template <class K, class M>
- iterator insert_or_assign(const_iterator hint, K&& key, M&& obj);
- template <class K, class... Args>
- std::enable_if_t<std::is_constructible<key_type, K&&>::value,
- std::pair<iterator, bool>>
- try_emplace(K&& key, Args&&... args);
- template <class K, class... Args>
- std::enable_if_t<std::is_constructible<key_type, K&&>::value, iterator>
- try_emplace(const_iterator hint, K&& key, Args&&... args);
-
-
-
-
- void swap(flat_map& other) noexcept;
- friend void swap(flat_map& lhs, flat_map& rhs) noexcept { lhs.swap(rhs); }
- };
- template <class Key, class Mapped, class Compare>
- flat_map<Key, Mapped, Compare>::flat_map(const Compare& comp) : tree(comp) {}
- template <class Key, class Mapped, class Compare>
- template <class InputIterator>
- flat_map<Key, Mapped, Compare>::flat_map(InputIterator first,
- InputIterator last,
- const Compare& comp)
- : tree(first, last, comp) {}
- template <class Key, class Mapped, class Compare>
- flat_map<Key, Mapped, Compare>::flat_map(const underlying_type& items,
- const Compare& comp)
- : tree(items, comp) {}
- template <class Key, class Mapped, class Compare>
- flat_map<Key, Mapped, Compare>::flat_map(underlying_type&& items,
- const Compare& comp)
- : tree(std::move(items), comp) {}
- template <class Key, class Mapped, class Compare>
- flat_map<Key, Mapped, Compare>::flat_map(
- std::initializer_list<value_type> ilist,
- const Compare& comp)
- : flat_map(std::begin(ilist), std::end(ilist), comp) {}
- template <class Key, class Mapped, class Compare>
- auto flat_map<Key, Mapped, Compare>::operator=(
- std::initializer_list<value_type> ilist) -> flat_map& {
-
-
-
-
-
-
- tree::operator=(ilist);
- return *this;
- }
- template <class Key, class Mapped, class Compare>
- template <class K>
- auto flat_map<Key, Mapped, Compare>::at(const K& key) -> mapped_type& {
- iterator found = tree::find(key);
- CHECK(found != tree::end());
- return found->second;
- }
- template <class Key, class Mapped, class Compare>
- template <class K>
- auto flat_map<Key, Mapped, Compare>::at(const K& key) const
- -> const mapped_type& {
- const_iterator found = tree::find(key);
- CHECK(found != tree::cend());
- return found->second;
- }
- template <class Key, class Mapped, class Compare>
- auto flat_map<Key, Mapped, Compare>::operator[](const key_type& key)
- -> mapped_type& {
- iterator found = tree::lower_bound(key);
- if (found == tree::end() || tree::key_comp()(key, found->first))
- found = tree::unsafe_emplace(found, key, mapped_type());
- return found->second;
- }
- template <class Key, class Mapped, class Compare>
- auto flat_map<Key, Mapped, Compare>::operator[](key_type&& key)
- -> mapped_type& {
- iterator found = tree::lower_bound(key);
- if (found == tree::end() || tree::key_comp()(key, found->first))
- found = tree::unsafe_emplace(found, std::move(key), mapped_type());
- return found->second;
- }
- template <class Key, class Mapped, class Compare>
- template <class K, class M>
- auto flat_map<Key, Mapped, Compare>::insert_or_assign(K&& key, M&& obj)
- -> std::pair<iterator, bool> {
- auto result =
- tree::emplace_key_args(key, std::forward<K>(key), std::forward<M>(obj));
- if (!result.second)
- result.first->second = std::forward<M>(obj);
- return result;
- }
- template <class Key, class Mapped, class Compare>
- template <class K, class M>
- auto flat_map<Key, Mapped, Compare>::insert_or_assign(const_iterator hint,
- K&& key,
- M&& obj) -> iterator {
- auto result = tree::emplace_hint_key_args(hint, key, std::forward<K>(key),
- std::forward<M>(obj));
- if (!result.second)
- result.first->second = std::forward<M>(obj);
- return result.first;
- }
- template <class Key, class Mapped, class Compare>
- template <class K, class... Args>
- auto flat_map<Key, Mapped, Compare>::try_emplace(K&& key, Args&&... args)
- -> std::enable_if_t<std::is_constructible<key_type, K&&>::value,
- std::pair<iterator, bool>> {
- return tree::emplace_key_args(
- key, std::piecewise_construct,
- std::forward_as_tuple(std::forward<K>(key)),
- std::forward_as_tuple(std::forward<Args>(args)...));
- }
- template <class Key, class Mapped, class Compare>
- template <class K, class... Args>
- auto flat_map<Key, Mapped, Compare>::try_emplace(const_iterator hint,
- K&& key,
- Args&&... args)
- -> std::enable_if_t<std::is_constructible<key_type, K&&>::value, iterator> {
- return tree::emplace_hint_key_args(
- hint, key, std::piecewise_construct,
- std::forward_as_tuple(std::forward<K>(key)),
- std::forward_as_tuple(std::forward<Args>(args)...))
- .first;
- }
- template <class Key, class Mapped, class Compare>
- void flat_map<Key, Mapped, Compare>::swap(flat_map& other) noexcept {
- tree::swap(other);
- }
- }
- #endif
|