Embedded Template Library 1.0
Loading...
Searching...
No Matches
const_multimap.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_CONST_MULTIMAP_INCLUDED
32#define ETL_CONST_MULTIMAP_INCLUDED
33
34#include "platform.h"
35
36#if ETL_NOT_USING_CPP11
37 #error NOT SUPPORTED FOR C++03 OR BELOW
38#endif
39
40#include "algorithm.h"
41#include "functional.h"
42#include "nth_type.h"
43#include "span.h"
44#include "type_traits.h"
45
47
50
51namespace etl
52{
53 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
55 {
56 public:
57
58 using key_type = TKey;
59 using value_type = ETL_OR_STD::pair<const TKey, TMapped>;
60 using mapped_type = TMapped;
61 using key_compare = TKeyCompare;
62 using const_reference = const value_type&;
63 using const_pointer = const value_type*;
64 using const_iterator = const value_type*;
65 using size_type = size_t;
66
67 //*********************************************************************
69 //*********************************************************************
71 {
72 public:
73
74 // Compare two value types.
75 ETL_CONSTEXPR14 bool operator()(const value_type& element1, const value_type& element2) const ETL_NOEXCEPT
76 {
77 return kcompare(element1.first, element2.first);
78 }
79
80 // Compare value type and key.
81 ETL_CONSTEXPR14 bool operator()(const value_type& element, const key_type& key) const ETL_NOEXCEPT
82 {
83 return kcompare(element.first, key);
84 }
85
86 // Compare value types and key.
87 // Enabled for transparent comparators.
88 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
89 ETL_CONSTEXPR14 bool operator()(const value_type& element, const K& key) const ETL_NOEXCEPT
90 {
91 return kcompare(element.first, key);
92 }
93
94 // Compare key and value type.
95 ETL_CONSTEXPR14 bool operator()(const key_type& key, const value_type& element) const ETL_NOEXCEPT
96 {
97 return kcompare(key, element.first);
98 }
99
100 // Compare key and value type.
101 // Enabled for transparent comparators.
102 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
103 ETL_CONSTEXPR14 bool operator()(const K& key, const value_type& element) const ETL_NOEXCEPT
104 {
105 return kcompare(key, element.first);
106 }
107
108 key_compare kcompare;
109 };
110
111 //*************************************************************************
115 //*************************************************************************
116 ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
117 {
118 return etl::is_sorted(begin(), end(), vcompare);
119 }
120
121 //*************************************************************************
124 //*************************************************************************
125 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
126 {
127 return element_list;
128 }
129
130 //*************************************************************************
133 //*************************************************************************
134 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
135 {
136 return element_list;
137 }
138
139 //*************************************************************************
142 //*************************************************************************
143 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
144 {
145 return element_list_end;
146 }
147
148 //*************************************************************************
151 //*************************************************************************
152 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
153 {
154 return element_list_end;
155 }
156
157 //*************************************************************************
160 //*************************************************************************
161 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
162 {
163 return element_list;
164 }
165
166 //*************************************************************************
171 //*************************************************************************
172 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
173 {
174 const_iterator itr = lower_bound(key);
175
176 if ((itr != end()) && (keys_are_equal(itr->first, key)))
177 {
178 return itr;
179 }
180
181 return end();
182 }
183
184 //*************************************************************************
190 //*************************************************************************
191 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
192 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
193 {
194 const_iterator itr = lower_bound(key);
195
196 if ((itr != end()) && (keys_are_equal(itr->first, key)))
197 {
198 return itr;
199 }
200
201 return end();
202 }
203
204 //*************************************************************************
208 //*************************************************************************
209 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
210 {
211 return find(key) != end();
212 }
213
214 //*************************************************************************
219 //*************************************************************************
220 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
221 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
222 {
223 return find(key) != end();
224 }
225
226 //*************************************************************************
230 //*************************************************************************
231 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
232 {
233 auto range = equal_range(key);
234
235 return size_type(etl::distance(range.first, range.second));
236 }
237
238 //*************************************************************************
243 //*************************************************************************
244 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
245 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
246 {
247 auto range = equal_range(key);
248
249 return size_type(etl::distance(range.first, range.second));
250 }
251
252 //*************************************************************************
259 //*************************************************************************
260 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
261 {
262 return etl::equal_range(begin(), end(), key, vcompare);
263 }
264
265 //*************************************************************************
273 //*************************************************************************
274 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
275 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
276 {
277 return etl::equal_range(begin(), end(), key, vcompare);
278 }
279
280 //*************************************************************************
287 //*************************************************************************
288 ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
289 {
290 return etl::lower_bound(begin(), end(), key, vcompare);
291 }
292
293 //*************************************************************************
300 //*************************************************************************
301 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
302 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
303 {
304 return etl::lower_bound(begin(), end(), key, vcompare);
305 }
306
307 //*************************************************************************
314 //*************************************************************************
315 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
316 {
317 return etl::upper_bound(begin(), end(), key, vcompare);
318 }
319
320 //*************************************************************************
327 //*************************************************************************
328 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
329 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
330 {
331 return etl::upper_bound(begin(), end(), key, vcompare);
332 }
333
334 //*************************************************************************
337 //*************************************************************************
338 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
339 {
340 return size() == 0U;
341 }
342
343 //*************************************************************************
346 //*************************************************************************
347 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
348 {
349 return (max_elements != 0) && (size() == max_elements);
350 }
351
352 //*************************************************************************
355 //*************************************************************************
356 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
357 {
358 return size_type(element_list_end - element_list);
359 }
360
361 //*************************************************************************
364 //*************************************************************************
365 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
366 {
367 return max_elements;
368 }
369
370 //*************************************************************************
374 //*************************************************************************
375 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
376 {
377 return max_elements;
378 }
379
380 //*************************************************************************
383 //*************************************************************************
384 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
385 {
386 return vcompare.kcompare;
387 }
388
389 //*************************************************************************
392 //*************************************************************************
393 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
394 {
395 return vcompare;
396 }
397
398 protected:
399
400 //*************************************************************************
402 //*************************************************************************
403 template <typename... TElements>
404 ETL_CONSTEXPR14 explicit iconst_multimap(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
405 : element_list(element_list_)
406 , element_list_end{element_list_ + size_}
407 , max_elements(max_elements_)
408 {
409 }
410
411 private:
412
413 //*********************************************************************
415 //*********************************************************************
416 ETL_CONSTEXPR14 bool keys_are_equal(const key_type& key1, const key_type& key2) const ETL_NOEXCEPT
417 {
418 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
419 }
420
421 //*********************************************************************
424 //*********************************************************************
425 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
426 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
427 {
428 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
429 }
430
431 value_compare vcompare;
432
433 const value_type* element_list;
434 const value_type* element_list_end;
435 size_type max_elements;
436 };
437
438 //*************************************************************************
440 //*************************************************************************
441 template <typename TKey, typename TMapped, size_t Size, typename TKeyCompare = etl::less<TKey>>
442 class const_multimap : public iconst_multimap<TKey, TMapped, TKeyCompare>
443 {
444 public:
445
447
448 using key_type = typename base_t::key_type;
449 using value_type = typename base_t::value_type;
450 using mapped_type = typename base_t::mapped_type;
451 using key_compare = typename base_t::key_compare;
452 using const_reference = typename base_t::const_reference;
453 using const_pointer = typename base_t::const_pointer;
454 using const_iterator = typename base_t::const_iterator;
455 using size_type = typename base_t::size_type;
456
457 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
458 static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
459
460 //*************************************************************************
466 //*************************************************************************
467 template <typename... TElements>
468 ETL_CONSTEXPR14 explicit const_multimap(TElements&&... elements) ETL_NOEXCEPT
469 : iconst_multimap<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
470 , element_list{etl::forward<TElements>(elements)...}
471 {
472 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
473 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
474 }
475
476 private:
477
478 value_type element_list[Size];
479 };
480
481 //*************************************************************************
483 //*************************************************************************
484#if ETL_USING_CPP17
485 template <typename... TPairs>
486 const_multimap(TPairs...)
487 -> const_multimap<typename etl::nth_type_t<0, TPairs...>::first_type, typename etl::nth_type_t<0, TPairs...>::second_type, sizeof...(TPairs)>;
488#endif
489
490 //*********************************************************************
492 //*********************************************************************
493 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
494 class const_multimap_ext : public iconst_multimap<TKey, TMapped, TKeyCompare>
495 {
496 public:
497
499
500 using key_type = typename base_t::key_type;
501 using value_type = typename base_t::value_type;
502 using mapped_type = typename base_t::mapped_type;
503 using key_compare = typename base_t::key_compare;
504 using const_reference = typename base_t::const_reference;
505 using const_pointer = typename base_t::const_pointer;
506 using const_iterator = typename base_t::const_iterator;
507 using size_type = typename base_t::size_type;
508
509 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
510 static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
511
512 //*************************************************************************
514 //*************************************************************************
515 ETL_CONSTEXPR14 const_multimap_ext() ETL_NOEXCEPT
516 : iconst_multimap<TKey, TMapped, TKeyCompare>(nullptr, 0, 0)
517 {
518 }
519
520 //*************************************************************************
522 //*************************************************************************
523 template <size_type Size>
524 ETL_CONSTEXPR14 explicit const_multimap_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
525 : iconst_multimap<TKey, TMapped, TKeyCompare>(sp.data(), Size, Size)
526 {
527 }
528
529 //*************************************************************************
531 //*************************************************************************
532 template <size_type Size>
533 ETL_CONSTEXPR14 explicit const_multimap_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
535 {
536 }
537 };
538
539 //*************************************************************************
541 //*************************************************************************
542#if ETL_USING_CPP17
543 template <typename TElements, size_t Size>
544 const_multimap_ext(const etl::span<TElements, Size>&) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
545
546 template <typename TElements, size_t Size>
547 const_multimap_ext(const TElements (&)[Size]) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
548#endif
549
550 //*************************************************************************
552 //*************************************************************************
553 template <typename TKey, typename TMapped, typename TKeyCompare>
556 {
557 return etl::equal(lhs.begin(), lhs.end(), rhs.begin());
558 }
559
560 //*************************************************************************
562 //*************************************************************************
563 template <typename TKey, typename TMapped, typename TKeyCompare>
566 {
567 return !(lhs == rhs);
568 }
569
570 //*************************************************************************
576 //*************************************************************************
577 template <typename TKey, typename TMapped, typename TKeyCompare>
580 {
581 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
582 }
583
584 //*************************************************************************
590 //*************************************************************************
591 template <typename TKey, typename TMapped, typename TKeyCompare>
594 {
595 return (rhs < lhs);
596 }
597
598 //*************************************************************************
605 //*************************************************************************
606 template <typename TKey, typename TMapped, typename TKeyCompare>
609 {
610 return !(rhs < lhs);
611 }
612
613 //*************************************************************************
619 //*************************************************************************
620 template <typename TKey, typename TMapped, typename TKeyCompare>
623 {
624 return !(lhs < rhs);
625 }
626} // namespace etl
627
628#endif
ETL_CONSTEXPR14 const_multimap_ext(const etl::span< const value_type, Size > &sp) ETL_NOEXCEPT
Construct a const_map from a variadic list of elements.
Definition const_multimap.h:524
ETL_CONSTEXPR14 const_multimap_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
Construct a const_map from an array.
Definition const_multimap.h:533
ETL_CONSTEXPR14 const_multimap_ext() ETL_NOEXCEPT
Default construct a const_map.
Definition const_multimap.h:515
Multimap type designed for constexpr.
Definition const_multimap.h:443
ETL_CONSTEXPR14 const_multimap(TElements &&... elements) ETL_NOEXCEPT
Construct a const_map from a variadic list of elements. Static asserts if the element type is not con...
Definition const_multimap.h:468
How to compare elements and keys.
Definition const_multimap.h:71
Definition const_multimap.h:55
ETL_CONSTEXPR14 const_iterator lower_bound(const K &key) const ETL_NOEXCEPT
Returns a const_iterator to the first element that is not less than the key. Returns a const_iterator...
Definition const_multimap.h:302
ETL_CONSTEXPR14 size_type count(const K &key) const ETL_NOEXCEPT
Counts the numbeer elements with key. Enabled if the comparator is transparent.
Definition const_multimap.h:245
ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
Definition const_multimap.h:393
ETL_CONSTEXPR14 const_iterator upper_bound(const K &key) const ETL_NOEXCEPT
Returns a const_iterator to the first element that is greater than the key. Returns a const_iterator ...
Definition const_multimap.h:329
ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
Gets the end of the map.
Definition const_multimap.h:143
ETL_CONSTEXPR14 const_iterator lower_bound(const key_type &key) const ETL_NOEXCEPT
Returns a const_iterator to the first element that is not less than the key. Returns a const_iterator...
Definition const_multimap.h:288
ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
Definition const_multimap.h:384
ETL_CONSTEXPR14 const_iterator find(const K &key) const ETL_NOEXCEPT
Gets a const_iterator to the mapped value at the key index. Enabled if the comparator is transparent.
Definition const_multimap.h:192
ETL_CONSTEXPR14 bool contains(const K &key) const ETL_NOEXCEPT
Checks if the map contains an element with key. Enabled if the comparator is transparent.
Definition const_multimap.h:221
ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
Definition const_multimap.h:338
ETL_CONSTEXPR14 ETL_OR_STD::pair< const_iterator, const_iterator > equal_range(const K &key) const ETL_NOEXCEPT
Returns a range containing all elements with the key. The range is defined by a pair of two iterators...
Definition const_multimap.h:275
ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
Definition const_multimap.h:356
ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
Definition const_multimap.h:365
ETL_CONSTEXPR14 iconst_multimap(const value_type *element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
Constructor.
Definition const_multimap.h:404
ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
Gets the beginning of the map.
Definition const_multimap.h:134
ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
Definition const_multimap.h:116
ETL_CONSTEXPR14 const_iterator find(const key_type &key) const ETL_NOEXCEPT
Gets a const_iterator to the mapped value at the key index.
Definition const_multimap.h:172
ETL_CONSTEXPR14 ETL_OR_STD::pair< const_iterator, const_iterator > equal_range(const key_type &key) const ETL_NOEXCEPT
Returns a range containing all elements with the key. The range is defined by a pair of two iterators...
Definition const_multimap.h:260
ETL_CONSTEXPR14 const_iterator upper_bound(const key_type &key) const ETL_NOEXCEPT
Returns a const_iterator to the first element that is greater than the key. Returns a const_iterator ...
Definition const_multimap.h:315
ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
Gets the beginning of the map.
Definition const_multimap.h:125
ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
Gets the end of the map.
Definition const_multimap.h:152
ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
Definition const_multimap.h:347
ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
Gets the beginning of the map.
Definition const_multimap.h:161
ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
Definition const_multimap.h:375
ETL_CONSTEXPR14 bool contains(const key_type &key) const ETL_NOEXCEPT
Checks if the map contains an element with key.
Definition const_multimap.h:209
ETL_CONSTEXPR14 size_type count(const key_type &key) const ETL_NOEXCEPT
Counts the numbeer elements with key.
Definition const_multimap.h:231
Span - Fixed Extent.
Definition span.h:208
ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1660
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1133
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1147
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1106
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1120