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#if ETL_NOT_USING_CPP11
37 #error NOT SUPPORTED FOR C++03 OR BELOW
38#endif
39
40#include "algorithm.h"
41#include "type_traits.h"
42#include "functional.h"
43#include "nth_type.h"
44#include "span.h"
45
47
50
51namespace etl
52{
53 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
55 {
56 public:
57
58 using key_type = TKey;
59 using value_type = TKey;
60 using key_compare = TKeyCompare;
61 using value_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 //*************************************************************************
71 //*************************************************************************
72 ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
73 {
74 return etl::is_sorted(begin(), end(), vcompare);
75 }
76
77 //*************************************************************************
80 //*************************************************************************
81 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
82 {
83 return element_list;
84 }
85
86 //*************************************************************************
89 //*************************************************************************
90 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
91 {
92 return element_list;
93 }
94
95 //*************************************************************************
98 //*************************************************************************
99 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
100 {
101 return element_list_end;
102 }
103
104 //*************************************************************************
107 //*************************************************************************
108 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
109 {
110 return element_list_end;
111 }
112
113 //*************************************************************************
116 //*************************************************************************
117 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
118 {
119 return element_list;
120 }
121
122 //*************************************************************************
127 //*************************************************************************
128 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
129 {
130 const_iterator itr = lower_bound(key);
131
132 if ((itr != end()) && (keys_are_equal(*itr, key)))
133 {
134 return itr;
135 }
136
137 return end();
138 }
139
140 //*************************************************************************
146 //*************************************************************************
147 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
148 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
149 {
150 const_iterator itr = lower_bound(key);
151
152 if ((itr != end()) && (keys_are_equal(*itr, key)))
153 {
154 return itr;
155 }
156
157 return end();
158 }
159
160 //*************************************************************************
164 //*************************************************************************
165 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
166 {
167 return find(key) != end();
168 }
169
170 //*************************************************************************
175 //*************************************************************************
176 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
177 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
178 {
179 return find(key) != end();
180 }
181
182 //*************************************************************************
186 //*************************************************************************
187 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
188 {
189 auto range = equal_range(key);
190
191 return size_type(etl::distance(range.first, range.second));
192 }
193
194 //*************************************************************************
199 //*************************************************************************
200 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
201 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
202 {
203 auto range = equal_range(key);
204
205 return size_type(etl::distance(range.first, range.second));
206 }
207
208 //*************************************************************************
215 //*************************************************************************
216 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
217 {
218 return etl::equal_range(begin(), end(), key, vcompare);
219 }
220
221 //*************************************************************************
229 //*************************************************************************
230 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
231 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
232 {
233 return etl::equal_range(begin(), end(), key, vcompare);
234 }
235
236 //*************************************************************************
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 //*************************************************************************
253 //*************************************************************************
254 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
255 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
256 {
257 return etl::lower_bound(begin(), end(), key, vcompare);
258 }
259
260 //*************************************************************************
265 //*************************************************************************
266 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
267 {
268 return etl::upper_bound(begin(), end(), key, vcompare);
269 }
270
271 //*************************************************************************
277 //*************************************************************************
278 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
279 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
280 {
281 return etl::upper_bound(begin(), end(), key, vcompare);
282 }
283
284 //*************************************************************************
287 //*************************************************************************
288 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
289 {
290 return size() == 0U;
291 }
292
293 //*************************************************************************
296 //*************************************************************************
297 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
298 {
299 return (max_elements != 0) && (size() == max_elements);
300 }
301
302 //*************************************************************************
305 //*************************************************************************
306 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
307 {
308 return size_type(element_list_end - element_list);
309 }
310
311 //*************************************************************************
314 //*************************************************************************
315 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
316 {
317 return max_elements;
318 }
319
320 //*************************************************************************
324 //*************************************************************************
325 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
326 {
327 return max_elements;
328 }
329
330 //*************************************************************************
333 //*************************************************************************
334 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
335 {
336 return kcompare;
337 }
338
339 //*************************************************************************
342 //*************************************************************************
343 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
344 {
345 return vcompare;
346 }
347
348 protected:
349
350 //*************************************************************************
352 //*************************************************************************
353 template <typename... TElements>
354 ETL_CONSTEXPR14 explicit iconst_multiset(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
355 : element_list(element_list_)
356 , element_list_end{element_list_ + size_}
357 , max_elements(max_elements_)
358 {
359 }
360
361 private:
362
363 //*********************************************************************
365 //*********************************************************************
366 ETL_CONSTEXPR14 bool keys_are_equal(const key_type& key1, const key_type& key2) const ETL_NOEXCEPT
367 {
368 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
369 }
370
371 //*********************************************************************
374 //*********************************************************************
375 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
376 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
377 {
378 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
379 }
380
381 key_compare kcompare;
382 value_compare vcompare;
383
384 const value_type* element_list;
385 const value_type* element_list_end;
386 size_type max_elements;
387 };
388
389 //*************************************************************************
391 //*************************************************************************
392 template <typename TKey, size_t Size, typename TKeyCompare = etl::less<TKey>>
393 class const_multiset : public iconst_multiset<TKey, TKeyCompare>
394 {
395 public:
396
398
399 using key_type = typename base_t::key_type;
400 using value_type = typename base_t::value_type;
401 using key_compare = typename base_t::key_compare;
402 using const_reference = typename base_t::const_reference;
403 using const_pointer = typename base_t::const_pointer;
404 using const_iterator = typename base_t::const_iterator;
405 using size_type = typename base_t::size_type;
406
407 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
408
409 //*************************************************************************
414 //*************************************************************************
415 template <typename... TElements>
416 ETL_CONSTEXPR14 explicit const_multiset(TElements&&... elements) ETL_NOEXCEPT
417 : iconst_multiset<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
418 , element_list{etl::forward<TElements>(elements)...}
419 {
420 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
421 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
422 }
423
424 private:
425
426 value_type element_list[Size];
427 };
428
429 //*************************************************************************
431 //*************************************************************************
432#if ETL_USING_CPP17
433 template <typename... TElements>
434 const_multiset(TElements...) -> const_multiset<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
435#endif
436
437 //*********************************************************************
439 //*********************************************************************
440 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
441 class const_multiset_ext : public iconst_multiset<TKey, TKeyCompare>
442 {
443 public:
444
446
447 using key_type = typename base_t::key_type;
448 using value_type = typename base_t::value_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
457 //*************************************************************************
459 //*************************************************************************
460 ETL_CONSTEXPR14 const_multiset_ext() ETL_NOEXCEPT
461 : iconst_multiset<TKey, TKeyCompare>(nullptr, 0, 0)
462 {
463 }
464
465 //*************************************************************************
467 //*************************************************************************
468 template <size_type Size>
469 ETL_CONSTEXPR14 explicit const_multiset_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
470 : iconst_multiset<TKey, TKeyCompare>(sp.data(), Size, Size)
471 {
472 }
473
474 //*************************************************************************
476 //*************************************************************************
477 template <size_type Size>
478 ETL_CONSTEXPR14 explicit const_multiset_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
479 : iconst_multiset<TKey, TKeyCompare>(begin_, Size, Size)
480 {
481 }
482 };
483
484 //*************************************************************************
486 //*************************************************************************
487#if ETL_USING_CPP17
488 template <typename TElements, size_t Size>
489 const_multiset_ext(const etl::span<TElements, Size>&) -> const_multiset_ext<TElements>;
490
491 template <typename TElements, size_t Size>
492 const_multiset_ext(const TElements(&)[Size]) -> const_multiset_ext<TElements>;
493#endif
494
495 //*************************************************************************
497 //*************************************************************************
498 template <typename TKey, typename TKeyCompare>
500 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
501 {
502 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
503 }
504
505 //*************************************************************************
507 //*************************************************************************
508 template <typename TKey, typename TKeyCompare>
510 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
511 {
512 return !(lhs == rhs);
513 }
514
515 //*************************************************************************
521 //*************************************************************************
522 template <typename TKey, typename TKeyCompare>
524 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
525 {
526 return etl::lexicographical_compare(lhs.begin(), lhs.end(),
527 rhs.begin(), rhs.end(),
528 lhs.value_comp());
529 }
530
531 //*************************************************************************
537 //*************************************************************************
538 template <typename TKey, typename TKeyCompare>
540 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
541 {
542 return (rhs < lhs);
543 }
544
545 //*************************************************************************
551 //*************************************************************************
552 template <typename TKey, typename TKeyCompare>
554 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
555 {
556 return !(rhs < lhs);
557 }
558
559 //*************************************************************************
565 //*************************************************************************
566 template <typename TKey, typename TKeyCompare>
568 const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
569 {
570 return !(lhs < rhs);
571 }
572}
573
574#endif
ETL_CONSTEXPR14 const_multiset_ext(const etl::span< const value_type, Size > &sp) ETL_NOEXCEPT
Construct a const_multiset from a variadic list of elements.
Definition const_multiset.h:469
ETL_CONSTEXPR14 const_multiset_ext() ETL_NOEXCEPT
Default construct a const_multiset.
Definition const_multiset.h:460
ETL_CONSTEXPR14 const_multiset_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
Construct a const_multiset from an array.
Definition const_multiset.h:478
Multiset type designed for constexpr.
Definition const_multiset.h:394
ETL_CONSTEXPR14 const_multiset(TElements &&... elements) ETL_NOEXCEPT
Construct a const_set from a variadic list of elements. Static asserts if the element type is not con...
Definition const_multiset.h:416
Definition const_multiset.h:55
ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
Gets the beginning of the multiset.
Definition const_multiset.h:90
ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
Definition const_multiset.h:306
ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
Definition const_multiset.h:72
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_multiset.h:255
ETL_CONSTEXPR14 const_iterator find(const K &key) const ETL_NOEXCEPT
Gets a const_iterator to the value at the key index. Enabled if the comparator is transparent.
Definition const_multiset.h:148
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_multiset.h:279
ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
Definition const_multiset.h:297
ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
Definition const_multiset.h:343
ETL_CONSTEXPR14 bool contains(const K &key) const ETL_NOEXCEPT
Checks if the multiset contains an element with key. Enabled if the comparator is transparent.
Definition const_multiset.h:177
ETL_CONSTEXPR14 const_iterator find(const key_type &key) const ETL_NOEXCEPT
Gets a const_iterator to the value at the key index.
Definition const_multiset.h:128
ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
Gets the beginning of the multiset.
Definition const_multiset.h:81
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_multiset.h:231
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_multiset.h:216
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_multiset.h:242
ETL_CONSTEXPR14 size_type count(const key_type &key) const ETL_NOEXCEPT
Counts the numbeer elements with key.
Definition const_multiset.h:187
ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
Gets the end of the multiset.
Definition const_multiset.h:99
ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
Definition const_multiset.h:334
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_multiset.h:266
ETL_CONSTEXPR14 iconst_multiset(const value_type *element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
Constructor.
Definition const_multiset.h:354
ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
Definition const_multiset.h:288
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_multiset.h:201
ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
Definition const_multiset.h:315
ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
Gets the end of the multiset.
Definition const_multiset.h:108
ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
Gets the beginning of the multiset.
Definition const_multiset.h:117
ETL_CONSTEXPR14 bool contains(const key_type &key) const ETL_NOEXCEPT
Checks if the multiset contains an element with key.
Definition const_multiset.h:165
ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
Definition const_multiset.h:325
Span - Fixed Extent.
Definition span.h:138
ETL_NODISCARD ETL_CONSTEXPR14 bool is_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1709
bitset_ext
Definition absolute.h:39
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1190
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1202
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1151
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1139
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1163
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1178