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#include "algorithm.h"
37#include "functional.h"
38#include "nth_type.h"
39#include "span.h"
40#include "type_traits.h"
41
43
44#if ETL_USING_CPP11
45
48
49namespace etl
50{
51 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
52 class iconst_multimap
53 {
54 public:
55
56 using key_type = TKey;
57 using value_type = ETL_OR_STD::pair<const TKey, TMapped>;
58 using mapped_type = TMapped;
59 using key_compare = TKeyCompare;
60 using const_reference = const value_type&;
61 using const_pointer = const value_type*;
62 using const_iterator = const value_type*;
63 using size_type = size_t;
64
65 //*********************************************************************
67 //*********************************************************************
68 class value_compare
69 {
70 public:
71
72 // Compare two value types.
73 ETL_CONSTEXPR14 bool operator()(const value_type& element1, const value_type& element2) const ETL_NOEXCEPT
74 {
75 return kcompare(element1.first, element2.first);
76 }
77
78 // Compare value type and key.
79 ETL_CONSTEXPR14 bool operator()(const value_type& element, const key_type& key) const ETL_NOEXCEPT
80 {
81 return kcompare(element.first, key);
82 }
83
84 // Compare value types and key.
85 // Enabled for transparent comparators.
86 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
87 ETL_CONSTEXPR14 bool operator()(const value_type& element, const K& key) const ETL_NOEXCEPT
88 {
89 return kcompare(element.first, key);
90 }
91
92 // Compare key and value type.
93 ETL_CONSTEXPR14 bool operator()(const key_type& key, const value_type& element) const ETL_NOEXCEPT
94 {
95 return kcompare(key, element.first);
96 }
97
98 // Compare key and value type.
99 // Enabled for transparent comparators.
100 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
101 ETL_CONSTEXPR14 bool operator()(const K& key, const value_type& element) const ETL_NOEXCEPT
102 {
103 return kcompare(key, element.first);
104 }
105
106 key_compare kcompare;
107 };
108
109 //*************************************************************************
113 //*************************************************************************
114 ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
115 {
116 return etl::is_sorted(begin(), end(), vcompare);
117 }
118
119 //*************************************************************************
122 //*************************************************************************
123 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
124 {
125 return element_list;
126 }
127
128 //*************************************************************************
131 //*************************************************************************
132 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
133 {
134 return element_list;
135 }
136
137 //*************************************************************************
140 //*************************************************************************
141 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
142 {
143 return element_list_end;
144 }
145
146 //*************************************************************************
149 //*************************************************************************
150 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
151 {
152 return element_list_end;
153 }
154
155 //*************************************************************************
158 //*************************************************************************
159 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
160 {
161 return element_list;
162 }
163
164 //*************************************************************************
169 //*************************************************************************
170 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
171 {
172 const_iterator itr = lower_bound(key);
173
174 if ((itr != end()) && (keys_are_equal(itr->first, key)))
175 {
176 return itr;
177 }
178
179 return end();
180 }
181
182 //*************************************************************************
188 //*************************************************************************
189 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
190 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
191 {
192 const_iterator itr = lower_bound(key);
193
194 if ((itr != end()) && (keys_are_equal(itr->first, key)))
195 {
196 return itr;
197 }
198
199 return end();
200 }
201
202 //*************************************************************************
206 //*************************************************************************
207 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
208 {
209 return find(key) != end();
210 }
211
212 //*************************************************************************
217 //*************************************************************************
218 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
219 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
220 {
221 return find(key) != end();
222 }
223
224 //*************************************************************************
228 //*************************************************************************
229 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
230 {
231 auto range = equal_range(key);
232
233 return size_type(etl::distance(range.first, range.second));
234 }
235
236 //*************************************************************************
241 //*************************************************************************
242 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
243 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
244 {
245 auto range = equal_range(key);
246
247 return size_type(etl::distance(range.first, range.second));
248 }
249
250 //*************************************************************************
257 //*************************************************************************
258 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
259 {
260 return etl::equal_range(begin(), end(), key, vcompare);
261 }
262
263 //*************************************************************************
271 //*************************************************************************
272 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
273 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
274 {
275 return etl::equal_range(begin(), end(), key, vcompare);
276 }
277
278 //*************************************************************************
285 //*************************************************************************
286 ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
287 {
288 return etl::lower_bound(begin(), end(), key, vcompare);
289 }
290
291 //*************************************************************************
298 //*************************************************************************
299 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
300 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
301 {
302 return etl::lower_bound(begin(), end(), key, vcompare);
303 }
304
305 //*************************************************************************
312 //*************************************************************************
313 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
314 {
315 return etl::upper_bound(begin(), end(), key, vcompare);
316 }
317
318 //*************************************************************************
325 //*************************************************************************
326 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
327 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
328 {
329 return etl::upper_bound(begin(), end(), key, vcompare);
330 }
331
332 //*************************************************************************
335 //*************************************************************************
336 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
337 {
338 return size() == 0U;
339 }
340
341 //*************************************************************************
344 //*************************************************************************
345 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
346 {
347 return (max_elements != 0) && (size() == max_elements);
348 }
349
350 //*************************************************************************
353 //*************************************************************************
354 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
355 {
356 return size_type(element_list_end - element_list);
357 }
358
359 //*************************************************************************
362 //*************************************************************************
363 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
364 {
365 return max_elements;
366 }
367
368 //*************************************************************************
372 //*************************************************************************
373 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
374 {
375 return max_elements;
376 }
377
378 //*************************************************************************
381 //*************************************************************************
382 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
383 {
384 return vcompare.kcompare;
385 }
386
387 //*************************************************************************
390 //*************************************************************************
391 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
392 {
393 return vcompare;
394 }
395
396 protected:
397
398 //*************************************************************************
400 //*************************************************************************
401 template <typename... TElements>
402 ETL_CONSTEXPR14 explicit iconst_multimap(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
403 : element_list(element_list_)
404 , element_list_end{element_list_ + size_}
405 , max_elements(max_elements_)
406 {
407 }
408
409 private:
410
411 //*********************************************************************
413 //*********************************************************************
414 ETL_CONSTEXPR14 bool keys_are_equal(const key_type& key1, const key_type& key2) const ETL_NOEXCEPT
415 {
416 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
417 }
418
419 //*********************************************************************
422 //*********************************************************************
423 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
424 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
425 {
426 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
427 }
428
429 value_compare vcompare;
430
431 const value_type* element_list;
432 const value_type* element_list_end;
433 size_type max_elements;
434 };
435
436 //*************************************************************************
438 //*************************************************************************
439 template <typename TKey, typename TMapped, size_t Size, typename TKeyCompare = etl::less<TKey>>
440 class const_multimap : public iconst_multimap<TKey, TMapped, TKeyCompare>
441 {
442 public:
443
444 using base_t = iconst_multimap<TKey, TMapped, TKeyCompare>;
445
446 using key_type = typename base_t::key_type;
447 using value_type = typename base_t::value_type;
448 using mapped_type = typename base_t::mapped_type;
449 using key_compare = typename base_t::key_compare;
450 using const_reference = typename base_t::const_reference;
451 using const_pointer = typename base_t::const_pointer;
452 using const_iterator = typename base_t::const_iterator;
453 using size_type = typename base_t::size_type;
454
455 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
456 static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
457
459 //*************************************************************************
465 //*************************************************************************
466 // The base class is passed the address of 'element_list' before that member has been
467 // initialised. The base class only stores the address and never reads through it during
468 // construction, so the compiler's 'may be used uninitialized' warning is a false positive.
469 template <typename... TElements>
470 ETL_CONSTEXPR14 explicit const_multimap(TElements&&... elements) ETL_NOEXCEPT
471 : iconst_multimap<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
472 , element_list{etl::forward<TElements>(elements)...}
473 {
474 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
475 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
476 }
477 #include "private/diagnostic_pop.h"
478
479 private:
480
481 value_type element_list[Size];
482 };
483
484 //*************************************************************************
486 //*************************************************************************
487 #if ETL_USING_CPP17
488 template <typename... TPairs>
489 const_multimap(TPairs...)
490 -> const_multimap<typename etl::nth_type_t<0, TPairs...>::first_type, typename etl::nth_type_t<0, TPairs...>::second_type, sizeof...(TPairs)>;
491 #endif
492
493 //*********************************************************************
495 //*********************************************************************
496 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
497 class const_multimap_ext : public iconst_multimap<TKey, TMapped, TKeyCompare>
498 {
499 public:
500
501 using base_t = iconst_multimap<TKey, TMapped, TKeyCompare>;
502
503 using key_type = typename base_t::key_type;
504 using value_type = typename base_t::value_type;
505 using mapped_type = typename base_t::mapped_type;
506 using key_compare = typename base_t::key_compare;
507 using const_reference = typename base_t::const_reference;
508 using const_pointer = typename base_t::const_pointer;
509 using const_iterator = typename base_t::const_iterator;
510 using size_type = typename base_t::size_type;
511
512 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
513 static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
514
515 //*************************************************************************
517 //*************************************************************************
518 ETL_CONSTEXPR14 const_multimap_ext() ETL_NOEXCEPT
519 : iconst_multimap<TKey, TMapped, TKeyCompare>(nullptr, 0, 0)
520 {
521 }
522
523 //*************************************************************************
525 //*************************************************************************
526 template <size_type Size>
527 ETL_CONSTEXPR14 explicit const_multimap_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
528 : iconst_multimap<TKey, TMapped, TKeyCompare>(sp.data(), Size, Size)
529 {
530 }
531
532 //*************************************************************************
534 //*************************************************************************
535 template <size_type Size>
536 ETL_CONSTEXPR14 explicit const_multimap_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
537 : iconst_multimap<TKey, TMapped, TKeyCompare>(begin_, Size, Size)
538 {
539 }
540 };
541
542 //*************************************************************************
544 //*************************************************************************
545 #if ETL_USING_CPP17
546 template <typename TElements, size_t Size>
547 const_multimap_ext(const etl::span<TElements, Size>&) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
548
549 template <typename TElements, size_t Size>
550 const_multimap_ext(const TElements (&)[Size]) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
551 #endif
552
553 //*************************************************************************
555 //*************************************************************************
556 template <typename TKey, typename TMapped, typename TKeyCompare>
557 ETL_CONSTEXPR14 bool operator==(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
558 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
559 {
560 return etl::equal(lhs.begin(), lhs.end(), rhs.begin());
561 }
562
563 //*************************************************************************
565 //*************************************************************************
566 template <typename TKey, typename TMapped, typename TKeyCompare>
567 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
568 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
569 {
570 return !(lhs == rhs);
571 }
572
573 //*************************************************************************
579 //*************************************************************************
580 template <typename TKey, typename TMapped, typename TKeyCompare>
581 ETL_CONSTEXPR14 bool operator<(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
582 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
583 {
584 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
585 }
586
587 //*************************************************************************
593 //*************************************************************************
594 template <typename TKey, typename TMapped, typename TKeyCompare>
595 ETL_CONSTEXPR14 bool operator>(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
596 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
597 {
598 return (rhs < lhs);
599 }
600
601 //*************************************************************************
608 //*************************************************************************
609 template <typename TKey, typename TMapped, typename TKeyCompare>
610 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
611 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
612 {
613 return !(rhs < lhs);
614 }
615
616 //*************************************************************************
622 //*************************************************************************
623 template <typename TKey, typename TMapped, typename TKeyCompare>
624 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
625 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
626 {
627 return !(lhs < rhs);
628 }
629} // namespace etl
630
631#endif
632#endif
Make this a clone of the supplied priority queue.
ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1669
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1130
TContainer::const_iterator cbegin(const TContainer &container)
Definition iterator.h:1156
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1144
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1186
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1103
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1117