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