Embedded Template Library 1.0
Loading...
Searching...
No Matches
const_multiset.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_MULTISET_INCLUDED
32#define ETL_CONST_MULTISET_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 TKeyCompare = etl::less<TKey>>
52 class iconst_multiset
53 {
54 public:
55
56 using key_type = TKey;
57 using value_type = TKey;
58 using key_compare = TKeyCompare;
59 using value_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 //*************************************************************************
69 //*************************************************************************
70 ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
71 {
72 return etl::is_sorted(begin(), end(), vcompare);
73 }
74
75 //*************************************************************************
78 //*************************************************************************
79 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
80 {
81 return element_list;
82 }
83
84 //*************************************************************************
87 //*************************************************************************
88 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
89 {
90 return element_list;
91 }
92
93 //*************************************************************************
96 //*************************************************************************
97 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
98 {
99 return element_list_end;
100 }
101
102 //*************************************************************************
105 //*************************************************************************
106 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
107 {
108 return element_list_end;
109 }
110
111 //*************************************************************************
114 //*************************************************************************
115 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
116 {
117 return element_list;
118 }
119
120 //*************************************************************************
125 //*************************************************************************
126 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
127 {
128 const_iterator itr = lower_bound(key);
129
130 if ((itr != end()) && (keys_are_equal(*itr, key)))
131 {
132 return itr;
133 }
134
135 return end();
136 }
137
138 //*************************************************************************
144 //*************************************************************************
145 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
146 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
147 {
148 const_iterator itr = lower_bound(key);
149
150 if ((itr != end()) && (keys_are_equal(*itr, key)))
151 {
152 return itr;
153 }
154
155 return end();
156 }
157
158 //*************************************************************************
162 //*************************************************************************
163 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
164 {
165 return find(key) != end();
166 }
167
168 //*************************************************************************
173 //*************************************************************************
174 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
175 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
176 {
177 return find(key) != end();
178 }
179
180 //*************************************************************************
184 //*************************************************************************
185 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
186 {
187 auto range = equal_range(key);
188
189 return size_type(etl::distance(range.first, range.second));
190 }
191
192 //*************************************************************************
197 //*************************************************************************
198 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
199 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
200 {
201 auto range = equal_range(key);
202
203 return size_type(etl::distance(range.first, range.second));
204 }
205
206 //*************************************************************************
213 //*************************************************************************
214 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
215 {
216 return etl::equal_range(begin(), end(), key, vcompare);
217 }
218
219 //*************************************************************************
227 //*************************************************************************
228 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
229 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
230 {
231 return etl::equal_range(begin(), end(), key, vcompare);
232 }
233
234 //*************************************************************************
241 //*************************************************************************
242 ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
243 {
244 return etl::lower_bound(begin(), end(), key, vcompare);
245 }
246
247 //*************************************************************************
254 //*************************************************************************
255 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
256 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
257 {
258 return etl::lower_bound(begin(), end(), key, vcompare);
259 }
260
261 //*************************************************************************
268 //*************************************************************************
269 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
270 {
271 return etl::upper_bound(begin(), end(), key, vcompare);
272 }
273
274 //*************************************************************************
281 //*************************************************************************
282 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
283 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
284 {
285 return etl::upper_bound(begin(), end(), key, vcompare);
286 }
287
288 //*************************************************************************
291 //*************************************************************************
292 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
293 {
294 return size() == 0U;
295 }
296
297 //*************************************************************************
300 //*************************************************************************
301 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
302 {
303 return (max_elements != 0) && (size() == max_elements);
304 }
305
306 //*************************************************************************
309 //*************************************************************************
310 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
311 {
312 return size_type(element_list_end - element_list);
313 }
314
315 //*************************************************************************
318 //*************************************************************************
319 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
320 {
321 return max_elements;
322 }
323
324 //*************************************************************************
328 //*************************************************************************
329 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
330 {
331 return max_elements;
332 }
333
334 //*************************************************************************
337 //*************************************************************************
338 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
339 {
340 return kcompare;
341 }
342
343 //*************************************************************************
346 //*************************************************************************
347 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
348 {
349 return vcompare;
350 }
351
352 protected:
353
354 //*************************************************************************
356 //*************************************************************************
357 template <typename... TElements>
358 ETL_CONSTEXPR14 explicit iconst_multiset(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
359 : element_list(element_list_)
360 , element_list_end{element_list_ + size_}
361 , max_elements(max_elements_)
362 {
363 }
364
365 private:
366
367 //*********************************************************************
369 //*********************************************************************
370 ETL_CONSTEXPR14 bool keys_are_equal(const key_type& key1, const key_type& key2) const ETL_NOEXCEPT
371 {
372 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
373 }
374
375 //*********************************************************************
378 //*********************************************************************
379 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
380 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
381 {
382 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
383 }
384
385 key_compare kcompare;
386 value_compare vcompare;
387
388 const value_type* element_list;
389 const value_type* element_list_end;
390 size_type max_elements;
391 };
392
393 //*************************************************************************
395 //*************************************************************************
396 template <typename TKey, size_t Size, typename TKeyCompare = etl::less<TKey>>
397 class const_multiset : public iconst_multiset<TKey, TKeyCompare>
398 {
399 public:
400
401 using base_t = iconst_multiset<TKey, TKeyCompare>;
402
403 using key_type = typename base_t::key_type;
404 using value_type = typename base_t::value_type;
405 using key_compare = typename base_t::key_compare;
406 using const_reference = typename base_t::const_reference;
407 using const_pointer = typename base_t::const_pointer;
408 using const_iterator = typename base_t::const_iterator;
409 using size_type = typename base_t::size_type;
410
411 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
412
414 //*************************************************************************
420 //*************************************************************************
421 // The base class is passed the address of 'element_list' before that member has been
422 // initialised. The base class only stores the address and never reads through it during
423 // construction, so the compiler's 'may be used uninitialized' warning is a false positive.
424 template <typename... TElements>
425 ETL_CONSTEXPR14 explicit const_multiset(TElements&&... elements) ETL_NOEXCEPT
426 : iconst_multiset<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
427 , element_list{etl::forward<TElements>(elements)...}
428 {
429 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
430 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
431 }
432 #include "private/diagnostic_pop.h"
433
434 private:
435
436 value_type element_list[Size];
437 };
438
439 //*************************************************************************
441 //*************************************************************************
442 #if ETL_USING_CPP17
443 template <typename... TElements>
444 const_multiset(TElements...) -> const_multiset<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
445 #endif
446
447 //*********************************************************************
449 //*********************************************************************
450 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
451 class const_multiset_ext : public iconst_multiset<TKey, TKeyCompare>
452 {
453 public:
454
455 using base_t = iconst_multiset<TKey, TKeyCompare>;
456
457 using key_type = typename base_t::key_type;
458 using value_type = typename base_t::value_type;
459 using key_compare = typename base_t::key_compare;
460 using const_reference = typename base_t::const_reference;
461 using const_pointer = typename base_t::const_pointer;
462 using const_iterator = typename base_t::const_iterator;
463 using size_type = typename base_t::size_type;
464
465 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
466
467 //*************************************************************************
469 //*************************************************************************
470 ETL_CONSTEXPR14 const_multiset_ext() ETL_NOEXCEPT
471 : iconst_multiset<TKey, TKeyCompare>(nullptr, 0, 0)
472 {
473 }
474
475 //*************************************************************************
477 //*************************************************************************
478 template <size_type Size>
479 ETL_CONSTEXPR14 explicit const_multiset_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
480 : iconst_multiset<TKey, TKeyCompare>(sp.data(), Size, Size)
481 {
482 }
483
484 //*************************************************************************
486 //*************************************************************************
487 template <size_type Size>
488 ETL_CONSTEXPR14 explicit const_multiset_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
489 : iconst_multiset<TKey, TKeyCompare>(begin_, Size, Size)
490 {
491 }
492 };
493
494 //*************************************************************************
496 //*************************************************************************
497 #if ETL_USING_CPP17
498 template <typename TElements, size_t Size>
499 const_multiset_ext(const etl::span<TElements, Size>&) -> const_multiset_ext<TElements>;
500
501 template <typename TElements, size_t Size>
502 const_multiset_ext(const TElements (&)[Size]) -> const_multiset_ext<TElements>;
503 #endif
504
505 //*************************************************************************
507 //*************************************************************************
508 template <typename TKey, typename TKeyCompare>
509 ETL_CONSTEXPR14 bool operator==(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
510 {
511 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
512 }
513
514 //*************************************************************************
516 //*************************************************************************
517 template <typename TKey, typename TKeyCompare>
518 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
519 {
520 return !(lhs == rhs);
521 }
522
523 //*************************************************************************
529 //*************************************************************************
530 template <typename TKey, typename TKeyCompare>
531 ETL_CONSTEXPR14 bool operator<(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
532 {
533 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
534 }
535
536 //*************************************************************************
542 //*************************************************************************
543 template <typename TKey, typename TKeyCompare>
544 ETL_CONSTEXPR14 bool operator>(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
545 {
546 return (rhs < lhs);
547 }
548
549 //*************************************************************************
556 //*************************************************************************
557 template <typename TKey, typename TKeyCompare>
558 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
559 {
560 return !(rhs < lhs);
561 }
562
563 //*************************************************************************
569 //*************************************************************************
570 template <typename TKey, typename TKeyCompare>
571 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
572 {
573 return !(lhs < rhs);
574 }
575} // namespace etl
576
577#endif
578#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