3#ifndef RPNXDATASTRUCTURES_MAP_HPP
4#define RPNXDATASTRUCTURES_MAP_HPP
7#include <initializer_list>
29 template <
typename Key,
typename T,
typename Compare = std::less< Key >,
typename Allocator = std::allocator< std::pair< Key const, T > > >
36 using key_type =
typename underlying_type::key_type;
42 using size_type =
typename underlying_type::size_type;
52 using reference =
typename underlying_type::reference;
56 using pointer =
typename underlying_type::pointer;
60 using iterator =
typename underlying_type::iterator;
68 using node_type =
typename underlying_type::node_type;
86 explicit map(Compare
const& comp, Allocator
const& alloc = Allocator()) : m_map(comp, alloc)
94 explicit map(Allocator
const& alloc) : m_map(alloc)
106 template <
typename InputIt >
107 map(InputIt first, InputIt last, Compare
const& comp = Compare(), Allocator
const& alloc = Allocator()) : m_map(first, last, comp, alloc)
118 template <
typename InputIt >
119 map(InputIt first, InputIt last, Allocator
const& alloc) : m_map(first, last, alloc)
129 map(std::initializer_list< value_type > init, Compare
const& comp = Compare(), Allocator
const& alloc = Allocator()) : m_map(init, comp, alloc)
138 map(std::initializer_list< value_type > init, Allocator
const& alloc) : m_map(init, alloc)
145 map(map
const&) =
default;
157 map(map const& other, Allocator const& alloc) : m_map(other.m_map, alloc)
166 map(map&& other, Allocator
const& alloc) : m_map(std::move(other.m_map), alloc)
199 return m_map.get_allocator();
208 T&
at(Key
const& key)
210 return m_map.at(key);
214 T
const&
at(Key
const& key)
const
216 return m_map.at(key);
236 return m_map[std::move(key)];
242 return m_map.begin();
248 return m_map.begin();
254 return m_map.cbegin();
278 return m_map.rbegin();
284 return m_map.rbegin();
290 return m_map.crbegin();
308 return m_map.crend();
314 return m_map.empty();
326 return m_map.max_size();
342 return m_map.insert(value);
352 return m_map.insert(std::move(value));
361 template <
typename P >
362 requires std::is_constructible_v< value_type, P&& >
363 std::pair< iterator, bool >
insert(P&& value)
365 return m_map.insert(std::forward< P >(value));
376 return m_map.insert(hint, value);
387 return m_map.insert(hint, std::move(value));
397 template <
typename P >
398 requires std::is_constructible_v< value_type, P&& >
401 return m_map.insert(hint, std::forward< P >(value));
410 template <
typename InputIt >
413 m_map.insert(first, last);
417 void insert(std::initializer_list< value_type > init)
429 return m_map.insert(std::move(node));
440 return m_map.insert(hint, std::move(node));
449 template <
typename... Args >
450 std::pair< iterator, bool >
emplace(Args&&... args)
452 return m_map.emplace(std::forward< Args >(args)...);
462 template <
typename... Args >
465 return m_map.emplace_hint(hint, std::forward< Args >(args)...);
475 template <
typename... Args >
476 std::pair< iterator, bool >
try_emplace(Key
const& key, Args&&... args)
478 return m_map.try_emplace(key, std::forward< Args >(args)...);
488 template <
typename... Args >
489 std::pair< iterator, bool >
try_emplace(Key&& key, Args&&... args)
491 return m_map.try_emplace(std::move(key), std::forward< Args >(args)...);
502 template <
typename... Args >
505 return m_map.try_emplace(hint, key, std::forward< Args >(args)...);
516 template <
typename... Args >
519 return m_map.try_emplace(hint, std::move(key), std::forward< Args >(args)...);
529 template <
typename M >
532 return m_map.insert_or_assign(key, std::forward< M >(obj));
542 template <
typename M >
545 return m_map.insert_or_assign(std::move(key), std::forward< M >(obj));
556 template <
typename M >
559 return m_map.insert_or_assign(hint, key, std::forward< M >(obj));
570 template <
typename M >
573 return m_map.insert_or_assign(hint, std::move(key), std::forward< M >(obj));
579 return m_map.erase(pos);
585 return m_map.erase(first, last);
591 return m_map.erase(key);
595 void swap(map& other)
noexcept(
noexcept(m_map.swap(other.m_map)))
597 m_map.swap(other.m_map);
603 return m_map.extract(pos);
609 return m_map.extract(key);
613 template <
typename C2 >
614 void merge(map< Key, T, C2, Allocator >& source)
616 m_map.merge(source.m_map);
620 template <
typename C2 >
621 void merge(map< Key, T, C2, Allocator >&& source)
623 m_map.merge(source.m_map);
627 template <
typename C2 >
628 void merge(std::map< Key, T, C2, Allocator >& source)
634 template <
typename C2 >
635 void merge(std::map< Key, T, C2, Allocator >&& source)
643 return m_map.count(key);
649 return m_map.find(key);
655 return m_map.find(key);
661 return m_map.contains(key);
667 return m_map.equal_range(key);
671 std::pair< const_iterator, const_iterator >
equal_range(Key
const& key)
const
673 return m_map.equal_range(key);
679 return m_map.lower_bound(key);
685 return m_map.lower_bound(key);
691 return m_map.upper_bound(key);
697 return m_map.upper_bound(key);
703 return m_map.key_comp();
709 return m_map.value_comp();
712 template <
typename K,
typename U,
typename C,
typename A >
716 template <
typename K,
typename U,
typename C,
typename A >
717 friend bool operator==(map< K, U, C, A >
const& lhs, map< K, U, C, A >
const& rhs);
720 template <
typename K,
typename U,
typename C,
typename A >
721 friend bool operator<(map< K, U, C, A >
const& lhs, map< K, U, C, A >
const& rhs);
724 template <
typename K,
typename U,
typename C,
typename A >
725 friend auto operator<=>(map< K, U, C, A >
const& lhs, map< K, U, C, A >
const& rhs) ->
decltype(std::declval< typename map< K, U, C, A >::underlying_type
const& >() <=> std::declval< typename map< K, U, C, A >::underlying_type
const& >());
733 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
745 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
748 return lhs.m_map == rhs.m_map;
757 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
760 return !(lhs == rhs);
769 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
777 return lhs.m_map < rhs.m_map;
786 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
798 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
810 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
822 template <
typename Key,
typename T,
typename Compare,
typename Allocator >
823 auto operator<=>(
map< Key, T, Compare, Allocator > const& lhs,
map< Key, T, Compare, Allocator > const& rhs) ->
decltype(std::declval< typename map< Key, T, Compare, Allocator >::underlying_type
const& >() <=> std::declval< typename map< Key, T, Compare, Allocator >::underlying_type
const& >())
825 using ordering_type =
decltype(lhs.m_map <=> rhs.m_map);
827 if (lhs.size() < rhs.size())
829 return ordering_type::less;
832 if (rhs.size() < lhs.size())
834 return ordering_type::greater;
837 return lhs.m_map <=> rhs.m_map;
Ordered key-value container with size-first ordering.
Definition map.hpp:31
map(InputIt first, InputIt last, Compare const &comp=Compare(), Allocator const &alloc=Allocator())
Constructs a map from an iterator range.
Definition map.hpp:107
const_iterator upper_bound(Key const &key) const
Finds the first element ordered after a key.
Definition map.hpp:695
std::pair< iterator, bool > emplace(Args &&... args)
Constructs a key-value pair in place if its key is absent.
Definition map.hpp:450
typename underlying_type::allocator_type allocator_type
Allocator type used to manage nodes.
Definition map.hpp:50
typename underlying_type::mapped_type mapped_type
Value associated with each key.
Definition map.hpp:38
typename underlying_type::const_reference const_reference
Immutable stored-value reference type.
Definition map.hpp:54
T const & at(Key const &key) const
Returns the mapped value for an existing key.
Definition map.hpp:214
T & at(Key const &key)
Returns the mapped value for an existing key.
Definition map.hpp:208
typename underlying_type::size_type size_type
Unsigned type used for element counts.
Definition map.hpp:42
iterator insert(const_iterator hint, value_type const &value)
Inserts a copied key-value pair using an ordering hint.
Definition map.hpp:374
typename underlying_type::const_iterator const_iterator
Immutable bidirectional iterator type.
Definition map.hpp:62
iterator end() noexcept
Returns an iterator one past the final key-value pair.
Definition map.hpp:258
iterator erase(const_iterator pos)
Erases one element.
Definition map.hpp:577
iterator lower_bound(Key const &key)
Finds the first element not ordered before a key.
Definition map.hpp:677
map & operator=(map &&) noexcept(std::is_nothrow_move_assignable_v< underlying_type >)=default
Move-assigns another map.
typename underlying_type::value_compare value_compare
Function object used to order stored key-value pairs by key.
Definition map.hpp:48
iterator insert(const_iterator hint, P &&value)
Inserts a pair-like value using an ordering hint.
Definition map.hpp:399
bool empty() const noexcept
Returns whether the map has no elements.
Definition map.hpp:312
insert_return_type insert(node_type &&node)
Inserts an extracted node if its key is absent.
Definition map.hpp:427
std::pair< const_iterator, const_iterator > equal_range(Key const &key) const
Finds the range matching a key.
Definition map.hpp:671
void swap(map &other) noexcept(noexcept(m_map.swap(other.m_map)))
Exchanges contents with another map.
Definition map.hpp:595
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the final key-value pair.
Definition map.hpp:276
iterator erase(const_iterator first, const_iterator last)
Erases a range.
Definition map.hpp:583
std::pair< iterator, bool > insert_or_assign(Key &&key, M &&obj)
Inserts a moved key or assigns its existing mapped value.
Definition map.hpp:543
iterator insert_or_assign(const_iterator hint, Key const &key, M &&obj)
Inserts a copied key or assigns its value using an ordering hint.
Definition map.hpp:557
const_iterator cend() const noexcept
Returns an immutable iterator one past the final key-value pair.
Definition map.hpp:270
reverse_iterator rend() noexcept
Returns a reverse iterator preceding the first key-value pair.
Definition map.hpp:294
size_type max_size() const noexcept
Returns the maximum number of elements supported by the implementation.
Definition map.hpp:324
std::map< Key, T, Compare, Allocator > underlying_type
Underlying ordered associative container type.
Definition map.hpp:34
T & operator[](Key const &key)
Returns the mapped value, inserting a default value when absent.
Definition map.hpp:224
map(map &&) noexcept(std::is_nothrow_move_constructible_v< underlying_type >)=default
Move-constructs a map.
key_compare key_comp() const
Returns the key-ordering predicate.
Definition map.hpp:701
iterator try_emplace(const_iterator hint, Key const &key, Args &&... args)
Constructs a mapped value for an absent copied key using an ordering hint.
Definition map.hpp:503
typename underlying_type::insert_return_type insert_return_type
Result returned when inserting a node handle without a hint.
Definition map.hpp:70
std::pair< iterator, bool > insert(value_type &&value)
Inserts a key-value pair by moving it if its key is absent.
Definition map.hpp:350
iterator try_emplace(const_iterator hint, Key &&key, Args &&... args)
Constructs a mapped value for an absent moved key using an ordering hint.
Definition map.hpp:517
std::pair< iterator, bool > insert(P &&value)
Inserts a value constructible as a key-value pair if its key is absent.
Definition map.hpp:363
value_compare value_comp() const
Returns the stored-value ordering predicate.
Definition map.hpp:707
size_type size() const noexcept
Returns the number of stored key-value pairs.
Definition map.hpp:318
iterator emplace_hint(const_iterator hint, Args &&... args)
Constructs a key-value pair in place using an ordering hint.
Definition map.hpp:463
const_reverse_iterator rend() const noexcept
Returns an immutable reverse iterator preceding the first key-value pair.
Definition map.hpp:300
void insert(std::initializer_list< value_type > init)
Inserts key-value pairs from an initializer list.
Definition map.hpp:417
typename underlying_type::const_pointer const_pointer
Immutable stored-value pointer type.
Definition map.hpp:58
std::pair< iterator, bool > insert(value_type const &value)
Inserts a key-value pair if its key is absent.
Definition map.hpp:340
std::pair< iterator, iterator > equal_range(Key const &key)
Finds the range matching a key.
Definition map.hpp:665
map(map &&other, Allocator const &alloc)
Move-constructs a map using the specified allocator.
Definition map.hpp:166
std::pair< iterator, bool > insert_or_assign(Key const &key, M &&obj)
Inserts a copied key or assigns its existing mapped value.
Definition map.hpp:530
size_type erase(Key const &key)
Erases the element with a key.
Definition map.hpp:589
map(std::initializer_list< value_type > init, Compare const &comp=Compare(), Allocator const &alloc=Allocator())
Constructs a map from an initializer list.
Definition map.hpp:129
typename underlying_type::iterator iterator
Mutable bidirectional iterator type.
Definition map.hpp:60
const_iterator lower_bound(Key const &key) const
Finds the first element not ordered before a key.
Definition map.hpp:683
const_reverse_iterator rbegin() const noexcept
Returns an immutable reverse iterator to the final key-value pair.
Definition map.hpp:282
const_reverse_iterator crend() const noexcept
Returns an immutable reverse iterator preceding the first key-value pair.
Definition map.hpp:306
friend auto operator<=>(map< K, U, C, A > const &lhs, map< K, U, C, A > const &rhs) -> decltype(std::declval< typename map< K, U, C, A >::underlying_type const & >()<=> std::declval< typename map< K, U, C, A >::underlying_type const & >())
Grants three-way comparison access to the underlying map.
typename underlying_type::reference reference
Mutable stored-value reference type.
Definition map.hpp:52
map(map const &)=default
Copy-constructs a map.
const_iterator find(Key const &key) const
Finds an element by key.
Definition map.hpp:653
T & operator[](Key &&key)
Returns the mapped value, moving the key into a new element when absent.
Definition map.hpp:234
iterator insert(const_iterator hint, node_type &&node)
Inserts an extracted node using an ordering hint.
Definition map.hpp:438
std::pair< iterator, bool > try_emplace(Key const &key, Args &&... args)
Constructs a mapped value only when a copied key is absent.
Definition map.hpp:476
typename underlying_type::const_reverse_iterator const_reverse_iterator
Immutable reverse-iterator type.
Definition map.hpp:66
void merge(map< Key, T, C2, Allocator > &source)
Transfers non-duplicate nodes from another RPNX map.
Definition map.hpp:614
size_type count(Key const &key) const
Counts elements matching a key.
Definition map.hpp:641
iterator insert_or_assign(const_iterator hint, Key &&key, M &&obj)
Inserts a moved key or assigns its value using an ordering hint.
Definition map.hpp:571
typename underlying_type::node_type node_type
Owning handle for an extracted node.
Definition map.hpp:68
iterator find(Key const &key)
Finds an element by key.
Definition map.hpp:647
void merge(std::map< Key, T, C2, Allocator > &&source)
Transfers non-duplicate nodes from a standard map.
Definition map.hpp:635
friend bool operator<(map< K, U, C, A > const &lhs, map< K, U, C, A > const &rhs)
Grants less-than comparison access to the underlying map.
node_type extract(const_iterator pos)
Removes an element without destroying it.
Definition map.hpp:601
allocator_type get_allocator() const noexcept
Returns the allocator used by this map.
Definition map.hpp:197
std::pair< iterator, bool > try_emplace(Key &&key, Args &&... args)
Constructs a mapped value only when a moved key is absent.
Definition map.hpp:489
typename underlying_type::key_compare key_compare
Function object used to order keys.
Definition map.hpp:46
map(Allocator const &alloc)
Constructs an empty map with an allocator.
Definition map.hpp:94
void insert(InputIt first, InputIt last)
Inserts every key-value pair in an iterator range.
Definition map.hpp:411
typename underlying_type::pointer pointer
Mutable stored-value pointer type.
Definition map.hpp:56
typename underlying_type::difference_type difference_type
Signed type used for iterator distances.
Definition map.hpp:44
node_type extract(Key const &key)
Removes an element by key without destroying it.
Definition map.hpp:607
map(std::initializer_list< value_type > init, Allocator const &alloc)
Constructs a map from an initializer list and allocator.
Definition map.hpp:138
const_reverse_iterator crbegin() const noexcept
Returns an immutable reverse iterator to the final key-value pair.
Definition map.hpp:288
bool contains(Key const &key) const
Tests whether a key is present.
Definition map.hpp:659
friend bool operator==(map< K, U, C, A > const &lhs, map< K, U, C, A > const &rhs)
Grants equality comparison access to the underlying map.
typename underlying_type::reverse_iterator reverse_iterator
Mutable reverse-iterator type.
Definition map.hpp:64
typename underlying_type::value_type value_type
Stored key-value pair type.
Definition map.hpp:40
map(InputIt first, InputIt last, Allocator const &alloc)
Constructs a map from an iterator range and allocator.
Definition map.hpp:119
map(Compare const &comp, Allocator const &alloc=Allocator())
Constructs an empty map with a comparator and allocator.
Definition map.hpp:86
map & operator=(map const &)=default
Copy-assigns another map.
const_iterator begin() const noexcept
Returns an immutable iterator to the first key-value pair.
Definition map.hpp:246
const_iterator cbegin() const noexcept
Returns an immutable iterator to the first key-value pair.
Definition map.hpp:252
iterator insert(const_iterator hint, value_type &&value)
Inserts a moved key-value pair using an ordering hint.
Definition map.hpp:385
typename underlying_type::key_type key_type
Key type used to order and identify elements.
Definition map.hpp:36
map()=default
Constructs an empty map.
void clear() noexcept
Removes all key-value pairs.
Definition map.hpp:330
void merge(map< Key, T, C2, Allocator > &&source)
Transfers non-duplicate nodes from another RPNX map.
Definition map.hpp:621
void merge(std::map< Key, T, C2, Allocator > &source)
Transfers non-duplicate nodes from a standard map.
Definition map.hpp:628
const_iterator end() const noexcept
Returns an immutable iterator one past the final key-value pair.
Definition map.hpp:264
iterator begin() noexcept
Returns an iterator to the first key-value pair.
Definition map.hpp:240
iterator upper_bound(Key const &key)
Finds the first element ordered after a key.
Definition map.hpp:689
Containers, iterator adapters, callable wrappers, and value utilities.
Definition annex.hpp:14
void swap(annex< T, Alloc > &lhs, annex< T, Alloc > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Swaps two annex objects.
Definition annex.hpp:577
bool operator<=(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Orders two annex objects.
Definition annex.hpp:651
bool operator>=(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Orders two annex objects.
Definition annex.hpp:663
bool operator<(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Orders two annex objects.
Definition annex.hpp:627
bool operator!=(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Compares two annex objects for inequality.
Definition annex.hpp:615
bool operator>(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Orders two annex objects.
Definition annex.hpp:639
auto operator<=>(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs) -> decltype(*lhs<=> *rhs)
Three-way compares two annex objects when T supports <=>.
Definition annex.hpp:676
bool operator==(annex< T, Alloc > const &lhs, annex< T, Alloc > const &rhs)
Compares two annex objects for equality.
Definition annex.hpp:598