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())
107 const Allocator& alloc = Allocator())
115 std::allocator_traits<Allocator>::is_always_equal::value &&
116 std::is_nothrow_move_assignable<
std::less<>>::value) = default;
124 map_type::operator=(ilist);
136 T&
at(
const std::string& key) {
return map_type::at(key); }
146 const T&
at(
const std::string& key)
const {
return map_type::at(key); }
156 T&
at(
const char* key) {
return at(std::string_view{key}); }
166 const T&
at(
const char* key)
const {
return at(std::string_view{key}); }
176 T&
at(std::string_view key) {
177#ifdef __cpp_lib_associative_heterogeneous_insertion
178 return map_type::at(key);
181 if (it == this->end()) {
182 throw std::out_of_range{std::string{key}};
196 const T&
at(std::string_view key)
const {
197#ifdef __cpp_lib_associative_heterogeneous_insertion
198 return map_type::at(key);
201 if (it == this->end()) {
202 throw std::out_of_range{std::string{key}};
217 T&
operator[](
const std::string& key) {
return map_type::operator[](key); }
229 return map_type::operator[](std::move(key));
265 template <
typename M>
267 return map_type::insert_or_assign(std::move(key), std::forward<M>(obj));
281 template <
typename M>
297 template <
typename M>
299#ifdef __cpp_lib_associative_heterogeneous_insertion
300 return map_type::insert_or_assign(key, std::forward<M>(obj));
303 if (it != this->end() && it->first == key) {
304 it->second = std::forward<M>(obj);
307 return {this->insert(it, {std::string{key}, std::forward<M>(obj)}),
true};
323 template <
typename M>
325 return map_type::insert_or_assign(hint, std::move(key),
326 std::forward<M>(obj));
340 template <
typename M>
356 template <
typename M>
359#ifdef __cpp_lib_associative_heterogeneous_insertion
360 return map_type::insert_or_assign(hint, key, std::forward<M>(obj));
362 return map_type::insert_or_assign(hint, std::string{key},
363 std::forward<M>(obj));
379 template <
typename... Args>
380 std::pair<iterator, bool>
emplace(std::string&& key, Args&&... args) {
381 return map_type::emplace(std::move(key), std::forward<Args>(args)...);
396 template <
typename... Args>
397 std::pair<iterator, bool>
emplace(
const char* key, Args&&... args) {
398 return emplace(std::string_view{key}, std::forward<Args>(args)...);
413 template <
typename... Args>
414 std::pair<iterator, bool>
emplace(std::string_view key, Args&&... args) {
415 return try_emplace(key, std::forward<Args>(args)...);
429 template <
typename... Args>
430 std::pair<iterator, bool>
try_emplace(std::string&& key, Args&&... args) {
431 return map_type::try_emplace(std::move(key), std::forward<Args>(args)...);
445 template <
typename... Args>
446 std::pair<iterator, bool>
try_emplace(
const char* key, Args&&... args) {
447 return try_emplace(std::string_view{key}, std::forward<Args>(args)...);
461 template <
typename... Args>
462 std::pair<iterator, bool>
try_emplace(std::string_view key, Args&&... args) {
463#ifdef __cpp_lib_associative_heterogeneous_insertion
464 return map_type::try_emplace(key, std::forward<Args>(args)...);
467 if (it != this->end() && it->first == key) {
470 return {
try_emplace(it, key, std::forward<Args>(args)...),
true};
487 template <
typename... Args>
489 return map_type::try_emplace(hint, std::move(key),
490 std::forward<Args>(args)...);
505 template <
typename... Args>
508 std::forward<Args>(args)...);
523 template <
typename... Args>
526#ifdef __cpp_lib_associative_heterogeneous_insertion
527 return map_type::try_emplace(hint, key, std::forward<Args>(args)...);
529 return map_type::try_emplace(hint, std::string{key},
530 std::forward<Args>(args)...);
559 return map_type::erase(first, last);
571 return map_type::erase(first, last);
581#ifdef __cpp_lib_associative_heterogeneous_erasure
582 return map_type::erase(key);
585 if (it == this->end()) {
603 std::allocator_traits<Allocator>::is_always_equal::value &&
604 std::is_nothrow_swappable<std::less<>>::value) {
605 map_type::swap(other);
616 return map_type::extract(position);
629#ifdef __cpp_lib_associative_heterogeneous_insertion
630 return map_type::extract(key);
633 if (it == this->end()) {
665 return map_type::find(key);
674 bool contains(std::string_view key)
const {
return map_type::contains(key); }
693 return map_type::equal_range(key);
713 std::string_view key)
const {
714 return map_type::equal_range(key);
727 return map_type::lower_bound(key);
740 return map_type::lower_bound(key);
753 return map_type::upper_bound(key);
766 return map_type::upper_bound(key);
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.h:26
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.h:341
const T & at(const char *key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:166
StringMap & operator=(StringMap &&) noexcept(std::allocator_traits< Allocator >::is_always_equal::value &&std::is_nothrow_move_assignable< std::less<> >::value)=default
Move assignment operator.
T & at(std::string_view key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:176
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.h:217
size_type count(std::string_view key) const
Returns the number of elements with key that equals the specified argument.
Definition StringMap.h:646
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.h:628
typename std::map< std::string, T, std::less<> > map_type
Definition StringMap.h:28
StringMap(const StringMap &)=default
Copy constructor.
typename map_type::size_type size_type
Definition StringMap.h:32
StringMap(const Allocator &alloc)
Constructs an empty container.
Definition StringMap.h:55
typename map_type::key_type key_type
Definition StringMap.h:29
StringMap()=default
Constructs an empty container.
iterator upper_bound(std::string_view key)
Returns an interator pointing to the first element that is greater than key.
Definition StringMap.h:752
typename map_type::value_type value_type
Definition StringMap.h:31
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.h:524
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.h:615
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.h:282
const_iterator upper_bound(std::string_view key) const
Returns an interator pointing to the first element that is greater than key.
Definition StringMap.h:765
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.h:298
typename map_type::mapped_type mapped_type
Definition StringMap.h:30
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.h:462
T & at(const char *key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:156
typename map_type::key_compare key_compare
Definition StringMap.h:34
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.h:357
typename map_type::const_pointer const_pointer
Definition StringMap.h:39
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.h:570
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.h:241
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.h:446
StringMap(InputIt first, InputIt last, const Allocator &alloc=Allocator())
Constructs the container with the contents of the range [first, last).
Definition StringMap.h:68
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.h:692
bool contains(std::string_view key) const
Checks if there is an element with key equal to key in the container.
Definition StringMap.h:674
typename map_type::allocator_type allocator_type
Definition StringMap.h:35
StringMap & operator=(const StringMap &)=default
Copy assignment operator.
const T & at(const std::string &key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:146
const_iterator find(std::string_view key) const
Finds an element with key equal to key.
Definition StringMap.h:664
StringMap(StringMap &&other, const Allocator &alloc)
Move constructor.
Definition StringMap.h:94
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.h:414
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.h:739
StringMap(StringMap &&)=default
Move constructor.
T & at(const std::string &key)
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:136
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.h:266
typename map_type::reverse_iterator reverse_iterator
Definition StringMap.h:42
typename map_type::insert_return_type insert_return_type
Definition StringMap.h:45
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.h:228
iterator erase(const_iterator pos)
Removes the element at pos.
Definition StringMap.h:548
StringMap(const StringMap &other, const Allocator &alloc)
Copy constructor.
Definition StringMap.h:81
typename map_type::node_type node_type
Definition StringMap.h:44
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.h:488
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.h:324
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.h:252
iterator erase(iterator first, iterator last)
Removes the elements in the range [first, last), which must be a valid range in this.
Definition StringMap.h:558
typename map_type::const_iterator const_iterator
Definition StringMap.h:41
typename map_type::reference reference
Definition StringMap.h:36
iterator erase(iterator pos)
Removes the element at pos.
Definition StringMap.h:540
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.h:430
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.h:506
typename map_type::difference_type difference_type
Definition StringMap.h:33
typename map_type::pointer pointer
Definition StringMap.h:38
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.h:380
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.h:397
size_type erase(std::string_view key)
Removes the element (if one exists) with the key equal to key.
Definition StringMap.h:580
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.h:602
typename map_type::iterator iterator
Definition StringMap.h:40
iterator find(std::string_view key)
Finds an element with key equal to key.
Definition StringMap.h:655
typename map_type::const_reverse_iterator const_reverse_iterator
Definition StringMap.h:43
iterator lower_bound(std::string_view key)
Returns an iterator pointing to the first element that is not less than (i.e.
Definition StringMap.h:726
typename map_type::const_reference const_reference
Definition StringMap.h:37
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.h:712
const T & at(std::string_view key) const
Returns a reference to the mapped value of the element with the specified key.
Definition StringMap.h:196
StringMap(std::initializer_list< value_type > init, const Allocator &alloc=Allocator())
Initializer-list constructor.
Definition StringMap.h:106
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
Foonathan namespace.
Definition ntcore_cpp.h:26