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
458 //*************************************************************************
464 //*************************************************************************
465 template <typename... TElements>
466 ETL_CONSTEXPR14 explicit const_multimap(TElements&&... elements) ETL_NOEXCEPT
467 : iconst_multimap<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
468 , element_list{etl::forward<TElements>(elements)...}
469 {
470 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
471 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
472 }
473
474 private:
475
476 value_type element_list[Size];
477 };
478
479 //*************************************************************************
481 //*************************************************************************
482 #if ETL_USING_CPP17
483 template <typename... TPairs>
484 const_multimap(TPairs...)
485 -> const_multimap<typename etl::nth_type_t<0, TPairs...>::first_type, typename etl::nth_type_t<0, TPairs...>::second_type, sizeof...(TPairs)>;
486 #endif
487
488 //*********************************************************************
490 //*********************************************************************
491 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>>
492 class const_multimap_ext : public iconst_multimap<TKey, TMapped, TKeyCompare>
493 {
494 public:
495
496 using base_t = iconst_multimap<TKey, TMapped, TKeyCompare>;
497
498 using key_type = typename base_t::key_type;
499 using value_type = typename base_t::value_type;
500 using mapped_type = typename base_t::mapped_type;
501 using key_compare = typename base_t::key_compare;
502 using const_reference = typename base_t::const_reference;
503 using const_pointer = typename base_t::const_pointer;
504 using const_iterator = typename base_t::const_iterator;
505 using size_type = typename base_t::size_type;
506
507 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
508 static_assert((etl::is_default_constructible<mapped_type>::value), "mapped_type must be default constructible");
509
510 //*************************************************************************
512 //*************************************************************************
513 ETL_CONSTEXPR14 const_multimap_ext() ETL_NOEXCEPT
514 : iconst_multimap<TKey, TMapped, TKeyCompare>(nullptr, 0, 0)
515 {
516 }
517
518 //*************************************************************************
520 //*************************************************************************
521 template <size_type Size>
522 ETL_CONSTEXPR14 explicit const_multimap_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
523 : iconst_multimap<TKey, TMapped, TKeyCompare>(sp.data(), Size, Size)
524 {
525 }
526
527 //*************************************************************************
529 //*************************************************************************
530 template <size_type Size>
531 ETL_CONSTEXPR14 explicit const_multimap_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
532 : iconst_multimap<TKey, TMapped, TKeyCompare>(begin_, Size, Size)
533 {
534 }
535 };
536
537 //*************************************************************************
539 //*************************************************************************
540 #if ETL_USING_CPP17
541 template <typename TElements, size_t Size>
542 const_multimap_ext(const etl::span<TElements, Size>&) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
543
544 template <typename TElements, size_t Size>
545 const_multimap_ext(const TElements (&)[Size]) -> const_multimap_ext<typename TElements::first_type, typename TElements::second_type>;
546 #endif
547
548 //*************************************************************************
550 //*************************************************************************
551 template <typename TKey, typename TMapped, typename TKeyCompare>
552 ETL_CONSTEXPR14 bool operator==(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
553 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
554 {
555 return etl::equal(lhs.begin(), lhs.end(), rhs.begin());
556 }
557
558 //*************************************************************************
560 //*************************************************************************
561 template <typename TKey, typename TMapped, typename TKeyCompare>
562 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
563 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
564 {
565 return !(lhs == rhs);
566 }
567
568 //*************************************************************************
574 //*************************************************************************
575 template <typename TKey, typename TMapped, typename TKeyCompare>
576 ETL_CONSTEXPR14 bool operator<(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
577 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
578 {
579 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
580 }
581
582 //*************************************************************************
588 //*************************************************************************
589 template <typename TKey, typename TMapped, typename TKeyCompare>
590 ETL_CONSTEXPR14 bool operator>(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
591 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
592 {
593 return (rhs < lhs);
594 }
595
596 //*************************************************************************
603 //*************************************************************************
604 template <typename TKey, typename TMapped, typename TKeyCompare>
605 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
606 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
607 {
608 return !(rhs < lhs);
609 }
610
611 //*************************************************************************
617 //*************************************************************************
618 template <typename TKey, typename TMapped, typename TKeyCompare>
619 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& lhs,
620 const etl::iconst_multimap<TKey, TMapped, TKeyCompare>& rhs) ETL_NOEXCEPT
621 {
622 return !(lhs < rhs);
623 }
624} // namespace etl
625
626#endif
627#endif
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