8#include <initializer_list>
25 typename Allocator = std::allocator<std::pair<const std::string, T>>>
26class StringMap :
public std::map<std::string, T, std::less<>, Allocator> {
28 using map_type =
typename std::map<std::string, T, std::less<>>;
38 using pointer =
typename map_type::pointer;
67 template <
typename InputIt>
68 StringMap(InputIt first, InputIt last,
const Allocator& alloc = Allocator())
108 const Allocator& alloc = Allocator())
116 std::allocator_traits<Allocator>::is_always_equal::value &&
117 std::is_nothrow_move_assignable<
std::less<>>::value) = default;
125 map_type::operator=(ilist);
137 T&
at(
const std::string& key) {
return map_type::at(key); }
147 const T&
at(
const std::string& key)
const {
return map_type::at(key); }
157 T&
at(
const char* key) {
return at(std::string_view{key}); }
167 const T&
at(
const char* key)
const {
return at(std::string_view{key}); }
177 T&
at(std::string_view key) {
178#ifdef __cpp_lib_associative_heterogeneous_insertion
179 return map_type::at(key);
182 if (it == this->end()) {
183 throw std::out_of_range{std::string{key}};
197 const T&
at(std::string_view key)
const {
198#ifdef __cpp_lib_associative_heterogeneous_insertion
199 return map_type::at(key);
202 if (it == this->end()) {
203 throw std::out_of_range{std::string{key}};
218 T&
operator[](
const std::string& key) {
return map_type::operator[](key); }
230 return map_type::operator[](std::move(key));
266 template <
typename M>
268 return map_type::insert_or_assign(std::move(key), std::forward<M>(obj));
282 template <
typename M>
298 template <
typename M>
300#ifdef __cpp_lib_associative_heterogeneous_insertion
301 return map_type::insert_or_assign(key, std::forward<M>(obj));
304 if (it != this->end() && it->first == key) {
305 it->second = std::forward<M>(obj);
308 return {this->insert(it, {std::string{key}, std::forward<M>(obj)}),
true};
324 template <
typename M>
326 return map_type::insert_or_assign(hint, std::move(key),
327 std::forward<M>(obj));
341 template <
typename M>
357 template <
typename M>
360#ifdef __cpp_lib_associative_heterogeneous_insertion
361 return map_type::insert_or_assign(hint, key, std::forward<M>(obj));
363 return map_type::insert_or_assign(hint, std::string{key},
364 std::forward<M>(obj));
380 template <
typename... Args>
381 std::pair<iterator, bool>
emplace(std::string&& key, Args&&... args) {
382 return map_type::emplace(std::move(key), std::forward<Args>(args)...);
397 template <
typename... Args>
398 std::pair<iterator, bool>
emplace(
const char* key, Args&&... args) {
399 return emplace(std::string_view{key}, std::forward<Args>(args)...);
414 template <
typename... Args>
415 std::pair<iterator, bool>
emplace(std::string_view key, Args&&... args) {
416 return try_emplace(key, std::forward<Args>(args)...);
430 template <
typename... Args>
431 std::pair<iterator, bool>
try_emplace(std::string&& key, Args&&... args) {
432 return map_type::try_emplace(std::move(key), std::forward<Args>(args)...);
446 template <
typename... Args>
447 std::pair<iterator, bool>
try_emplace(
const char* key, Args&&... args) {
448 return try_emplace(std::string_view{key}, std::forward<Args>(args)...);
462 template <
typename... Args>
463 std::pair<iterator, bool>
try_emplace(std::string_view key, Args&&... args) {
464#ifdef __cpp_lib_associative_heterogeneous_insertion
465 return map_type::try_emplace(key, std::forward<Args>(args)...);
468 if (it != this->end() && it->first == key) {
471 return {
try_emplace(it, key, std::forward<Args>(args)...),
true};
488 template <
typename... Args>
490 return map_type::try_emplace(hint, std::move(key),
491 std::forward<Args>(args)...);
506 template <
typename... Args>
509 std::forward<Args>(args)...);
524 template <
typename... Args>
527#ifdef __cpp_lib_associative_heterogeneous_insertion
528 return map_type::try_emplace(hint, key, std::forward<Args>(args)...);
530 return map_type::try_emplace(hint, std::string{key},
531 std::forward<Args>(args)...);
560 return map_type::erase(first, last);
572 return map_type::erase(first, last);
582#ifdef __cpp_lib_associative_heterogeneous_erasure
583 return map_type::erase(key);
586 if (it == this->end()) {
604 std::allocator_traits<Allocator>::is_always_equal::value &&
605 std::is_nothrow_swappable<std::less<>>::value) {
606 map_type::swap(other);
617 return map_type::extract(position);
630#ifdef __cpp_lib_associative_heterogeneous_insertion
631 return map_type::extract(key);
634 if (it == this->end()) {
666 return map_type::find(key);
675 bool contains(std::string_view key)
const {
return map_type::contains(key); }
694 return map_type::equal_range(key);
714 std::string_view key)
const {
715 return map_type::equal_range(key);
728 return map_type::lower_bound(key);
741 return map_type::lower_bound(key);
754 return map_type::upper_bound(key);
767 return map_type::upper_bound(key);
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.hpp:26
std::pair< iterator, bool > insert_or_assign(std::string_view key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:299
StringMap(const Allocator &alloc)
Constructs an empty container.
Definition StringMap.hpp:55
typename map_type::node_type node_type
Definition StringMap.hpp:44
StringMap(const StringMap &)=default
Copy constructor.
typename map_type::allocator_type allocator_type
Definition StringMap.hpp:35
size_type erase(std::string_view key)
Removes the element (if one exists) with the key equal to key.
Definition StringMap.hpp:581
typename map_type::pointer pointer
Definition StringMap.hpp:38
std::pair< iterator, bool > insert_or_assign(std::string &&key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:267
typename map_type::insert_return_type insert_return_type
Definition StringMap.hpp:45
typename map_type::iterator iterator
Definition StringMap.hpp:40
std::pair< iterator, bool > emplace(const char *key, Args &&... args)
Inserts a new element into the container constructed in-place with the given args,...
Definition StringMap.hpp:398
typename map_type::key_type key_type
Definition StringMap.hpp:29
iterator erase(iterator pos)
Removes the element at pos.
Definition StringMap.hpp:541
StringMap()=default
Constructs an empty container.
StringMap(StringMap &&other, const Allocator &alloc)
Move constructor.
Definition StringMap.hpp:94
typename std::map< std::string, T, std::less<> > map_type
Definition StringMap.hpp:28
size_type count(std::string_view key) const
Returns the number of elements with key that equals the specified argument.
Definition StringMap.hpp:647
iterator insert_or_assign(const_iterator hint, const char *key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:342
bool contains(std::string_view key) const
Checks if there is an element with key equal to key in the container.
Definition StringMap.hpp:675
typename map_type::const_reference const_reference
Definition StringMap.hpp:37
StringMap & operator=(const StringMap &)=default
Copy assignment operator.
StringMap(StringMap &&)=default
Move constructor.
iterator erase(iterator first, iterator last)
Removes the elements in the range [first, last), which must be a valid range in this.
Definition StringMap.hpp:559
typename map_type::const_reverse_iterator const_reverse_iterator
Definition StringMap.hpp:43
iterator find(std::string_view key)
Finds an element with key equal to key.
Definition StringMap.hpp:656
StringMap(InputIt first, InputIt last, const Allocator &alloc=Allocator())
Constructs the container with the contents of the range [first, last).
Definition StringMap.hpp:68
const T & at(const char *key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:167
iterator try_emplace(const_iterator hint, std::string &&key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:489
T & at(std::string_view key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:177
iterator lower_bound(std::string_view key)
Returns an iterator pointing to the first element that is not less than (i.e.
Definition StringMap.hpp:727
typename map_type::difference_type difference_type
Definition StringMap.hpp:33
std::pair< iterator, bool > insert_or_assign(const char *key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:283
iterator try_emplace(const_iterator hint, std::string_view key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:525
typename map_type::reference reference
Definition StringMap.hpp:36
typename map_type::size_type size_type
Definition StringMap.hpp:32
void swap(StringMap &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value &&std::is_nothrow_swappable< std::less<> >::value)
Exchanges the contents of the container with those of other.
Definition StringMap.hpp:603
T & operator[](std::string &&key)
Returns a reference to the value that is mapped to a key, performing an insertion if such a key does ...
Definition StringMap.hpp:229
typename map_type::mapped_type mapped_type
Definition StringMap.hpp:30
node_type extract(const_iterator position)
Unlinks the node that contains the element pointed to by position and returns a node handle that owns...
Definition StringMap.hpp:616
std::pair< iterator, bool > emplace(std::string_view key, Args &&... args)
Inserts a new element into the container constructed in-place with the given args,...
Definition StringMap.hpp:415
iterator try_emplace(const_iterator hint, const char *key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:507
typename map_type::value_type value_type
Definition StringMap.hpp:31
typename map_type::reverse_iterator reverse_iterator
Definition StringMap.hpp:42
typename map_type::const_pointer const_pointer
Definition StringMap.hpp:39
StringMap & operator=(StringMap &&) noexcept(std::allocator_traits< Allocator >::is_always_equal::value &&std::is_nothrow_move_assignable< std::less<> >::value)=default
Move assignment operator.
std::pair< iterator, bool > try_emplace(std::string_view key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:463
iterator insert_or_assign(const_iterator hint, std::string_view key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:358
node_type extract(std::string_view key)
If the container has an element with key equal to key, unlinks the node that contains that element fr...
Definition StringMap.hpp:629
std::pair< iterator, iterator > equal_range(std::string_view key)
Returns a range containing all elements with the given key in the container.
Definition StringMap.hpp:693
T & operator[](const char *key)
Returns a reference to the value that is mapped to a key, performing an insertion if such a key does ...
Definition StringMap.hpp:242
const_iterator upper_bound(std::string_view key) const
Returns an interator pointing to the first element that is greater than key.
Definition StringMap.hpp:766
std::pair< const_iterator, const_iterator > equal_range(std::string_view key) const
Returns a range containing all elements with the given key in the container.
Definition StringMap.hpp:713
iterator erase(const_iterator pos)
Removes the element at pos.
Definition StringMap.hpp:549
const_iterator find(std::string_view key) const
Finds an element with key equal to key.
Definition StringMap.hpp:665
typename map_type::const_iterator const_iterator
Definition StringMap.hpp:41
std::pair< iterator, bool > try_emplace(const char *key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:447
iterator insert_or_assign(const_iterator hint, std::string &&key, M &&obj)
If a key equal to key already exists in the container, assigns obj to the mapped type corresponding t...
Definition StringMap.hpp:325
typename map_type::key_compare key_compare
Definition StringMap.hpp:34
const T & at(std::string_view key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:197
StringMap(const StringMap &other, const Allocator &alloc)
Copy constructor.
Definition StringMap.hpp:81
T & at(const char *key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:157
T & at(const std::string &key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:137
StringMap(std::initializer_list< value_type > init, const Allocator &alloc=Allocator())
Initializer-list constructor.
Definition StringMap.hpp:107
std::pair< iterator, bool > emplace(std::string &&key, Args &&... args)
Inserts a new element into the container constructed in-place with the given args,...
Definition StringMap.hpp:381
T & operator[](std::string_view key)
Returns a reference to the value that is mapped to a key, performing an insertion if such a key does ...
Definition StringMap.hpp:253
std::pair< iterator, bool > try_emplace(std::string &&key, Args &&... args)
If a key equal to key already exists in the container, does nothing.
Definition StringMap.hpp:431
iterator erase(const_iterator first, const_iterator last)
Removes the elements in the range [first, last), which must be a valid range in this.
Definition StringMap.hpp:571
const_iterator lower_bound(std::string_view key) const
Returns an iterator pointing to the first element that is not less than (i.e.
Definition StringMap.hpp:740
const T & at(const std::string &key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.hpp:147
iterator upper_bound(std::string_view key)
Returns an interator pointing to the first element that is greater than key.
Definition StringMap.hpp:753
T & operator[](const std::string &key)
Returns a reference to the value that is mapped to a key, performing an insertion if such a key does ...
Definition StringMap.hpp:218
Definition StringMap.hpp:773
void swap(wpi::util::StringMap< T > &lhs, wpi::util::StringMap< T > &rhs)
Definition StringMap.hpp:775
Definition raw_os_ostream.hpp:19