WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
StringMap.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <initializer_list>
9#include <map>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <type_traits>
14#include <utility>
15
16namespace wpi {
17
18/**
19 * StringMap is a sorted associative container that contains key-value pairs
20 * with unique string keys. Keys are sorted in the same order as std::string's
21 * are compared. Search, removal, and insertion operations have logarithmic
22 * complexity. The underlying implementation is std::map<std::string, T>.
23 */
24template <typename T,
25 typename Allocator = std::allocator<std::pair<const std::string, T>>>
26class StringMap : public std::map<std::string, T, std::less<>, Allocator> {
27 public:
28 using map_type = typename std::map<std::string, T, std::less<>>;
29 using key_type = typename map_type::key_type;
30 using mapped_type = typename map_type::mapped_type;
31 using value_type = typename map_type::value_type;
32 using size_type = typename map_type::size_type;
33 using difference_type = typename map_type::difference_type;
34 using key_compare = typename map_type::key_compare;
35 using allocator_type = typename map_type::allocator_type;
36 using reference = typename map_type::reference;
37 using const_reference = typename map_type::const_reference;
38 using pointer = typename map_type::pointer;
39 using const_pointer = typename map_type::const_pointer;
40 using iterator = typename map_type::iterator;
41 using const_iterator = typename map_type::const_iterator;
42 using reverse_iterator = typename map_type::reverse_iterator;
43 using const_reverse_iterator = typename map_type::const_reverse_iterator;
44 using node_type = typename map_type::node_type;
45 using insert_return_type = typename map_type::insert_return_type;
46
47 /** Constructs an empty container. */
48 StringMap() = default;
49
50 /**
51 * Constructs an empty container.
52 *
53 * @param alloc allocator to use for all memory allocations of this container
54 */
55 explicit StringMap(const Allocator& alloc) : map_type{alloc} {}
56
57 /**
58 * Constructs the container with the contents of the range [first, last). If
59 * multiple elements in the range have keys that compare equivalent, it is
60 * unspecified which element is inserted. If [first, last) is not a valid
61 * range, the behavior is undefined.
62 *
63 * @param first start of the range to copy the elements from
64 * @param last end of the range to copy the elements from
65 * @param alloc allocator to use for all memory allocations of this container
66 */
67 template <typename InputIt>
68 StringMap(InputIt first, InputIt last, const Allocator& alloc = Allocator())
69 : map_type{first, last, alloc} {}
70
71 /** Copy constructor. */
72 StringMap(const StringMap&) = default;
73
74 /**
75 * Copy constructor.
76 *
77 * @param other another container to be used as source to initialize the
78 * elements of the container with
79 * @param alloc allocator to use for all memory allocations of this container
80 */
81 StringMap(const StringMap& other, const Allocator& alloc)
82 : map_type{other, alloc} {}
83
84 /** Move constructor. */
85 StringMap(StringMap&&) = default;
86
87 /**
88 * Move constructor.
89 *
90 * @param other another container to be used as source to initialize the
91 * elements of the container with
92 * @param alloc allocator to use for all memory allocations of this container
93 */
94 StringMap(StringMap&& other, const Allocator& alloc)
95 : map_type{other, alloc} {}
96
97 /**
98 * Initializer-list constructor. Construct the container with the contents of
99 * the initializer list init. If multiple elements in the range have keys
100 * that compare equal, it is unspecified which element is inserted.
101 *
102 * @param init initializer list to initialize the elements of the container
103 * with
104 * @param alloc allocator to use for all memory allocations of this container
105 */
106 StringMap(std::initializer_list<value_type> init,
107 const Allocator& alloc = Allocator())
108 : map_type{init, alloc} {}
109
110 /** Copy assignment operator. */
111 StringMap& operator=(const StringMap&) = default;
112
113 /** Move assignment operator. */
115 std::allocator_traits<Allocator>::is_always_equal::value &&
116 std::is_nothrow_move_assignable<std::less<>>::value) = default;
117
118 /**
119 * Replaces the contents with those identified by initializer list ilist.
120 *
121 * @param ilist initializer list to use as data source
122 */
123 StringMap& operator=(std::initializer_list<value_type> ilist) {
124 map_type::operator=(ilist);
125 return *this;
126 }
127
128 /**
129 * Returns a reference to the mapped value of the element with the specified
130 * key. If no such element exists, an exception of type std::out_of_range is
131 * thrown.
132 *
133 * @param key the key of the element to find
134 * @return A reference to the mapped value of the requested element.
135 */
136 T& at(const std::string& key) { return map_type::at(key); }
137
138 /**
139 * Returns a reference to the mapped value of the element with the specified
140 * key. If no such element exists, an exception of type std::out_of_range is
141 * thrown.
142 *
143 * @param key the key of the element to find
144 * @return A reference to the mapped value of the requested element.
145 */
146 const T& at(const std::string& key) const { return map_type::at(key); }
147
148 /**
149 * Returns a reference to the mapped value of the element with the specified
150 * key. If no such element exists, an exception of type std::out_of_range is
151 * thrown.
152 *
153 * @param key the key of the element to find
154 * @return A reference to the mapped value of the requested element.
155 */
156 T& at(const char* key) { return at(std::string_view{key}); }
157
158 /**
159 * Returns a reference to the mapped value of the element with the specified
160 * key. If no such element exists, an exception of type std::out_of_range is
161 * thrown.
162 *
163 * @param key the key of the element to find
164 * @return A reference to the mapped value of the requested element.
165 */
166 const T& at(const char* key) const { return at(std::string_view{key}); }
167
168 /**
169 * Returns a reference to the mapped value of the element with the specified
170 * key. If no such element exists, an exception of type std::out_of_range is
171 * thrown.
172 *
173 * @param key the key of the element to find
174 * @return A reference to the mapped value of the requested element.
175 */
176 T& at(std::string_view key) {
177#ifdef __cpp_lib_associative_heterogeneous_insertion
178 return map_type::at(key);
179#else
180 auto it = find(key);
181 if (it == this->end()) {
182 throw std::out_of_range{std::string{key}};
183 }
184 return it->second;
185#endif
186 }
187
188 /**
189 * Returns a reference to the mapped value of the element with the specified
190 * key. If no such element exists, an exception of type std::out_of_range is
191 * thrown.
192 *
193 * @param key the key of the element to find
194 * @return A reference to the mapped value of the requested element.
195 */
196 const T& at(std::string_view key) const {
197#ifdef __cpp_lib_associative_heterogeneous_insertion
198 return map_type::at(key);
199#else
200 auto it = find(key);
201 if (it == this->end()) {
202 throw std::out_of_range{std::string{key}};
203 }
204 return it->second;
205#endif
206 }
207
208 /**
209 * Returns a reference to the value that is mapped to a key, performing an
210 * insertion if such a key does not exist.
211 *
212 * @param key the key of the element to find
213 * @return A reference to the mapped value of the new element if no element
214 * with key key existed. Otherwise, a reference to the mapped value
215 * of the existing element whose key is equal to key.
216 */
217 T& operator[](const std::string& key) { return map_type::operator[](key); }
218
219 /**
220 * Returns a reference to the value that is mapped to a key, performing an
221 * insertion if such a key does not exist.
222 *
223 * @param key the key of the element to find
224 * @return A reference to the mapped value of the new element if no element
225 * with key key existed. Otherwise, a reference to the mapped value
226 * of the existing element whose key is equal to key.
227 */
228 T& operator[](std::string&& key) {
229 return map_type::operator[](std::move(key));
230 }
231
232 /**
233 * Returns a reference to the value that is mapped to a key, performing an
234 * insertion if such a key does not exist.
235 *
236 * @param key the key of the element to find
237 * @return A reference to the mapped value of the new element if no element
238 * with key key existed. Otherwise, a reference to the mapped value
239 * of the existing element whose key is equal to key.
240 */
241 T& operator[](const char* key) { return operator[](std::string_view{key}); }
242
243 /**
244 * Returns a reference to the value that is mapped to a key, performing an
245 * insertion if such a key does not exist.
246 *
247 * @param key the key of the element to find
248 * @return A reference to the mapped value of the new element if no element
249 * with key key existed. Otherwise, a reference to the mapped value
250 * of the existing element whose key is equal to key.
251 */
252 T& operator[](std::string_view key) { return try_emplace(key).first->second; }
253
254 /**
255 * If a key equal to key already exists in the container, assigns obj to the
256 * mapped type corresponding to that key. If the key does not exist, inserts
257 * the value as if by calling insert().
258 *
259 * @param key the key used both to look up and to insert if not found
260 * @param obj the value to insert or assign
261 * @return The bool component is true if the insertion took place and false if
262 * the assignment took place. The iterator component is pointing at
263 * the element that was inserted or updated.
264 */
265 template <typename M>
266 std::pair<iterator, bool> insert_or_assign(std::string&& key, M&& obj) {
267 return map_type::insert_or_assign(std::move(key), std::forward<M>(obj));
268 }
269
270 /**
271 * If a key equal to key already exists in the container, assigns obj to the
272 * mapped type corresponding to that key. If the key does not exist, inserts
273 * the value as if by calling insert().
274 *
275 * @param key the key used both to look up and to insert if not found
276 * @param obj the value to insert or assign
277 * @return The bool component is true if the insertion took place and false if
278 * the assignment took place. The iterator component is pointing at
279 * the element that was inserted or updated.
280 */
281 template <typename M>
282 std::pair<iterator, bool> insert_or_assign(const char* key, M&& obj) {
283 return insert_or_assign(std::string_view{key}, std::forward<M>(obj));
284 }
285
286 /**
287 * If a key equal to key already exists in the container, assigns obj to the
288 * mapped type corresponding to that key. If the key does not exist, inserts
289 * the value as if by calling insert().
290 *
291 * @param key the key used both to look up and to insert if not found
292 * @param obj the value to insert or assign
293 * @return The bool component is true if the insertion took place and false if
294 * the assignment took place. The iterator component is pointing at
295 * the element that was inserted or updated.
296 */
297 template <typename M>
298 std::pair<iterator, bool> insert_or_assign(std::string_view key, M&& obj) {
299#ifdef __cpp_lib_associative_heterogeneous_insertion
300 return map_type::insert_or_assign(key, std::forward<M>(obj));
301#else
302 auto it = lower_bound(key);
303 if (it != this->end() && it->first == key) {
304 it->second = std::forward<M>(obj);
305 return {it, false};
306 } else {
307 return {this->insert(it, {std::string{key}, std::forward<M>(obj)}), true};
308 }
309#endif
310 }
311
312 /**
313 * If a key equal to key already exists in the container, assigns obj to the
314 * mapped type corresponding to that key. If the key does not exist, inserts
315 * the value as if by calling insert().
316 *
317 * @param hint iterator to the position before which the new element will be
318 * inserted
319 * @param key the key used both to look up and to insert if not found
320 * @param obj the value to insert or assign
321 * @return Iterator pointing at the element that was inserted or updated.
322 */
323 template <typename M>
324 iterator insert_or_assign(const_iterator hint, std::string&& key, M&& obj) {
325 return map_type::insert_or_assign(hint, std::move(key),
326 std::forward<M>(obj));
327 }
328
329 /**
330 * If a key equal to key already exists in the container, assigns obj to the
331 * mapped type corresponding to that key. If the key does not exist, inserts
332 * the value as if by calling insert().
333 *
334 * @param hint iterator to the position before which the new element will be
335 * inserted
336 * @param key the key used both to look up and to insert if not found
337 * @param obj the value to insert or assign
338 * @return Iterator pointing at the element that was inserted or updated.
339 */
340 template <typename M>
341 iterator insert_or_assign(const_iterator hint, const char* key, M&& obj) {
342 return insert_or_assign(hint, std::string_view{key}, std::forward<M>(obj));
343 }
344
345 /**
346 * If a key equal to key already exists in the container, assigns obj to the
347 * mapped type corresponding to that key. If the key does not exist, inserts
348 * the value as if by calling insert().
349 *
350 * @param hint iterator to the position before which the new element will be
351 * inserted
352 * @param key the key used both to look up and to insert if not found
353 * @param obj the value to insert or assign
354 * @return Iterator pointing at the element that was inserted or updated.
355 */
356 template <typename M>
357 iterator insert_or_assign(const_iterator hint, std::string_view key,
358 M&& obj) {
359#ifdef __cpp_lib_associative_heterogeneous_insertion
360 return map_type::insert_or_assign(hint, key, std::forward<M>(obj));
361#else
362 return map_type::insert_or_assign(hint, std::string{key},
363 std::forward<M>(obj));
364#endif
365 }
366
367 /**
368 * Inserts a new element into the container constructed in-place with the
369 * given args, if there is no element with the key in the container.
370 *
371 * No iterators or references are invalidated.
372 *
373 * @param key the key used both to look up and to insert if not found
374 * @param args arguments to forward to the constructor of the element
375 * @return A pair consisting of an interator to the inserted element (or to
376 * the element that prevented the insertion) and a bool value set to
377 * true if and only if the insertion took place.
378 */
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)...);
382 }
383
384 /**
385 * Inserts a new element into the container constructed in-place with the
386 * given args, if there is no element with the key in the container.
387 *
388 * No iterators or references are invalidated.
389 *
390 * @param key the key used both to look up and to insert if not found
391 * @param args arguments to forward to the constructor of the element
392 * @return A pair consisting of an interator to the inserted element (or to
393 * the element that prevented the insertion) and a bool value set to
394 * true if and only if the insertion took place.
395 */
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)...);
399 }
400
401 /**
402 * Inserts a new element into the container constructed in-place with the
403 * given args, if there is no element with the key in the container.
404 *
405 * No iterators or references are invalidated.
406 *
407 * @param key the key used both to look up and to insert if not found
408 * @param args arguments to forward to the constructor of the element
409 * @return A pair consisting of an interator to the inserted element (or to
410 * the element that prevented the insertion) and a bool value set to
411 * true if and only if the insertion took place.
412 */
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)...);
416 }
417
418 /**
419 * If a key equal to key already exists in the container, does nothing.
420 * Otherwise, inserts a new element into the container with key key and value
421 * constructed with args.
422 *
423 * @param key the key used both to look up and to insert if not found
424 * @param args arguments to forward to the constructor of the element
425 * @return A pair consisting of an interator to the inserted element (or to
426 * the element that prevented the insertion) and a bool value set to
427 * true if and only if the insertion took place.
428 */
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)...);
432 }
433
434 /**
435 * If a key equal to key already exists in the container, does nothing.
436 * Otherwise, inserts a new element into the container with key key and value
437 * constructed with args.
438 *
439 * @param key the key used both to look up and to insert if not found
440 * @param args arguments to forward to the constructor of the element
441 * @return A pair consisting of an interator to the inserted element (or to
442 * the element that prevented the insertion) and a bool value set to
443 * true if and only if the insertion took place.
444 */
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)...);
448 }
449
450 /**
451 * If a key equal to key already exists in the container, does nothing.
452 * Otherwise, inserts a new element into the container with key key and value
453 * constructed with args.
454 *
455 * @param key the key used both to look up and to insert if not found
456 * @param args arguments to forward to the constructor of the element
457 * @return A pair consisting of an interator to the inserted element (or to
458 * the element that prevented the insertion) and a bool value set to
459 * true if and only if the insertion took place.
460 */
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)...);
465#else
466 auto it = lower_bound(key);
467 if (it != this->end() && it->first == key) {
468 return {it, false};
469 } else {
470 return {try_emplace(it, key, std::forward<Args>(args)...), true};
471 }
472#endif
473 }
474
475 /**
476 * If a key equal to key already exists in the container, does nothing.
477 * Otherwise, inserts a new element into the container with key key and value
478 * constructed with args.
479 *
480 * @param hint iterator to the position before which the new element will be
481 * inserted
482 * @param key the key used both to look up and to insert if not found
483 * @param args arguments to forward to the constructor of the element
484 * @return An interator to the inserted element, or to the element that
485 * prevented the insertion.
486 */
487 template <typename... Args>
488 iterator try_emplace(const_iterator hint, std::string&& key, Args&&... args) {
489 return map_type::try_emplace(hint, std::move(key),
490 std::forward<Args>(args)...);
491 }
492
493 /**
494 * If a key equal to key already exists in the container, does nothing.
495 * Otherwise, inserts a new element into the container with key key and value
496 * constructed with args.
497 *
498 * @param hint iterator to the position before which the new element will be
499 * inserted
500 * @param key the key used both to look up and to insert if not found
501 * @param args arguments to forward to the constructor of the element
502 * @return An interator to the inserted element, or to the element that
503 * prevented the insertion.
504 */
505 template <typename... Args>
506 iterator try_emplace(const_iterator hint, const char* key, Args&&... args) {
507 return try_emplace(hint, std::string_view{key},
508 std::forward<Args>(args)...);
509 }
510
511 /**
512 * If a key equal to key already exists in the container, does nothing.
513 * Otherwise, inserts a new element into the container with key key and value
514 * constructed with args.
515 *
516 * @param hint iterator to the position before which the new element will be
517 * inserted
518 * @param key the key used both to look up and to insert if not found
519 * @param args arguments to forward to the constructor of the element
520 * @return An interator to the inserted element, or to the element that
521 * prevented the insertion.
522 */
523 template <typename... Args>
524 iterator try_emplace(const_iterator hint, std::string_view key,
525 Args&&... args) {
526#ifdef __cpp_lib_associative_heterogeneous_insertion
527 return map_type::try_emplace(hint, key, std::forward<Args>(args)...);
528#else
529 return map_type::try_emplace(hint, std::string{key},
530 std::forward<Args>(args)...);
531#endif
532 }
533
534 /**
535 * Removes the element at pos.
536 *
537 * @param pos iterator to the element to remove
538 * @return Iterator following the removed element.
539 */
540 iterator erase(iterator pos) { return map_type::erase(pos); }
541
542 /**
543 * Removes the element at pos.
544 *
545 * @param pos iterator to the element to remove
546 * @return Iterator following the removed element.
547 */
548 iterator erase(const_iterator pos) { return map_type::erase(pos); }
549
550 /**
551 * Removes the elements in the range [first, last), which must be a valid
552 * range in this.
553 *
554 * @param first Start of the range of elements to remove
555 * @param last End of the range of elements to remove
556 * @return Iterator following the last removed element.
557 */
559 return map_type::erase(first, last);
560 }
561
562 /**
563 * Removes the elements in the range [first, last), which must be a valid
564 * range in this.
565 *
566 * @param first Start of the range of elements to remove
567 * @param last End of the range of elements to remove
568 * @return Iterator following the last removed element.
569 */
571 return map_type::erase(first, last);
572 }
573
574 /**
575 * Removes the element (if one exists) with the key equal to key.
576 *
577 * @param key key value of the elements to remove
578 * @return Number of elements removed (0 or 1).
579 */
580 size_type erase(std::string_view key) {
581#ifdef __cpp_lib_associative_heterogeneous_erasure
582 return map_type::erase(key);
583#else
584 auto it = find(key);
585 if (it == this->end()) {
586 return 0;
587 }
588 this->erase(it);
589 return 1;
590#endif
591 }
592
593 /**
594 * Exchanges the contents of the container with those of other. Does not
595 * invoke any move, copy, or swap operations on individual elements.
596 *
597 * All iterators and references remain valid. The end() iterator is
598 * invalidated.
599 *
600 * @param other container to exchange the contents with
601 */
602 void swap(StringMap& other) noexcept(
603 std::allocator_traits<Allocator>::is_always_equal::value &&
604 std::is_nothrow_swappable<std::less<>>::value) {
605 map_type::swap(other);
606 }
607
608 /**
609 * Unlinks the node that contains the element pointed to by position and
610 * returns a node handle that owns it.
611 *
612 * @param position a valid iterator into this container
613 * @return A node handle that owns the extracted element
614 */
616 return map_type::extract(position);
617 }
618
619 /**
620 * If the container has an element with key equal to key, unlinks the node
621 * that contains that element from the container and returns a node handle
622 * that owns it. Otherwise, returns an empty node handle.
623 *
624 * @param key a key to identify the node to be extracted
625 * @return A node handle that owns the extracted element, or empty node handle
626 * in case the element is not found.
627 */
628 node_type extract(std::string_view key) {
629#ifdef __cpp_lib_associative_heterogeneous_insertion
630 return map_type::extract(key);
631#else
632 auto it = find(key);
633 if (it == this->end()) {
634 return {};
635 }
636 return extract(it);
637#endif
638 }
639
640 /**
641 * Returns the number of elements with key that equals the specified argument.
642 *
643 * @param key key value of the elements to count
644 * @return Number of elements with key that equals key (either 0 or 1).
645 */
646 size_type count(std::string_view key) const { return map_type::count(key); }
647
648 /**
649 * Finds an element with key equal to key.
650 *
651 * @param key key value of the element to search for
652 * @return An iterator to the requested element. If no such element is found,
653 * past-the-end (see end()) iterator is returned.
654 */
655 iterator find(std::string_view key) { return map_type::find(key); }
656
657 /**
658 * Finds an element with key equal to key.
659 *
660 * @param key key value of the element to search for
661 * @return An iterator to the requested element. If no such element is found,
662 * past-the-end (see end()) iterator is returned.
663 */
664 const_iterator find(std::string_view key) const {
665 return map_type::find(key);
666 }
667
668 /**
669 * Checks if there is an element with key equal to key in the container.
670 *
671 * @param key key value of the element to search for
672 * @return true if there is such an element, otherwise false
673 */
674 bool contains(std::string_view key) const { return map_type::contains(key); }
675
676 /**
677 * Returns a range containing all elements with the given key in the
678 * container. The range is defined by two iterators, one pointing to the
679 * first element that is not less than key and another pointing to the first
680 * element greater than key. Alternatively, the first iterator may be
681 * obtained with lower_bound(), and the second with upper_bound().
682 *
683 * @param key key value to compare the elements to
684 * @return std::pair containing a pair of iterators defining the wanted range:
685 * the first pointing to the first element that is not less than key
686 * and the second pointing to the first element greater than key. If
687 * there are no elements not less than key, past-the-end (see end())
688 * iterator is returned as the first element. Similarly if there are
689 * no elements greater than key, past-the-end iterator is returned as
690 * the second element.
691 */
692 std::pair<iterator, iterator> equal_range(std::string_view key) {
693 return map_type::equal_range(key);
694 }
695
696 /**
697 * Returns a range containing all elements with the given key in the
698 * container. The range is defined by two iterators, one pointing to the
699 * first element that is not less than key and another pointing to the first
700 * element greater than key. Alternatively, the first iterator may be
701 * obtained with lower_bound(), and the second with upper_bound().
702 *
703 * @param key key value to compare the elements to
704 * @return std::pair containing a pair of iterators defining the wanted range:
705 * the first pointing to the first element that is not less than key
706 * and the second pointing to the first element greater than key. If
707 * there are no elements not less than key, past-the-end (see end())
708 * iterator is returned as the first element. Similarly if there are
709 * no elements greater than key, past-the-end iterator is returned as
710 * the second element.
711 */
712 std::pair<const_iterator, const_iterator> equal_range(
713 std::string_view key) const {
714 return map_type::equal_range(key);
715 }
716
717 /**
718 * Returns an iterator pointing to the first element that is not less than
719 * (i.e. greater or equal to) key.
720 *
721 * @param key key value to compare the elements to
722 * @return Iterator pointing to the first element that is not less than key.
723 * If no such element is found, a past-the-end iterator (see end()) is
724 * returned.
725 */
726 iterator lower_bound(std::string_view key) {
727 return map_type::lower_bound(key);
728 }
729
730 /**
731 * Returns an iterator pointing to the first element that is not less than
732 * (i.e. greater or equal to) key.
733 *
734 * @param key key value to compare the elements to
735 * @return Iterator pointing to the first element that is not less than key.
736 * If no such element is found, a past-the-end iterator (see end()) is
737 * returned.
738 */
739 const_iterator lower_bound(std::string_view key) const {
740 return map_type::lower_bound(key);
741 }
742
743 /**
744 * Returns an interator pointing to the first element that is greater than
745 * key.
746 *
747 * @param key key value to compare the elements to
748 * @return Iterator pointing to the first element that is greater than key.
749 * If no such element is found, past-the-end (see end()) iterator is
750 * returned.
751 */
752 iterator upper_bound(std::string_view key) {
753 return map_type::upper_bound(key);
754 }
755
756 /**
757 * Returns an interator pointing to the first element that is greater than
758 * key.
759 *
760 * @param key key value to compare the elements to
761 * @return Iterator pointing to the first element that is greater than key.
762 * If no such element is found, past-the-end (see end()) iterator is
763 * returned.
764 */
765 const_iterator upper_bound(std::string_view key) const {
766 return map_type::upper_bound(key);
767 }
768};
769
770} // namespace wpi
771
772namespace std {
773template <typename T>
774inline void swap(wpi::StringMap<T>& lhs, wpi::StringMap<T>& rhs) {
775 lhs.swap(rhs);
776}
777} // namespace std
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