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
413 //*************************************************************************
419 //*************************************************************************
420 template <typename... TElements>
421 ETL_CONSTEXPR14 explicit const_multiset(TElements&&... elements) ETL_NOEXCEPT
422 : iconst_multiset<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
423 , element_list{etl::forward<TElements>(elements)...}
424 {
425 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
426 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
427 }
428
429 private:
430
431 value_type element_list[Size];
432 };
433
434 //*************************************************************************
436 //*************************************************************************
437 #if ETL_USING_CPP17
438 template <typename... TElements>
439 const_multiset(TElements...) -> const_multiset<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
440 #endif
441
442 //*********************************************************************
444 //*********************************************************************
445 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
446 class const_multiset_ext : public iconst_multiset<TKey, TKeyCompare>
447 {
448 public:
449
450 using base_t = iconst_multiset<TKey, TKeyCompare>;
451
452 using key_type = typename base_t::key_type;
453 using value_type = typename base_t::value_type;
454 using key_compare = typename base_t::key_compare;
455 using const_reference = typename base_t::const_reference;
456 using const_pointer = typename base_t::const_pointer;
457 using const_iterator = typename base_t::const_iterator;
458 using size_type = typename base_t::size_type;
459
460 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
461
462 //*************************************************************************
464 //*************************************************************************
465 ETL_CONSTEXPR14 const_multiset_ext() ETL_NOEXCEPT
466 : iconst_multiset<TKey, TKeyCompare>(nullptr, 0, 0)
467 {
468 }
469
470 //*************************************************************************
472 //*************************************************************************
473 template <size_type Size>
474 ETL_CONSTEXPR14 explicit const_multiset_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
475 : iconst_multiset<TKey, TKeyCompare>(sp.data(), Size, Size)
476 {
477 }
478
479 //*************************************************************************
481 //*************************************************************************
482 template <size_type Size>
483 ETL_CONSTEXPR14 explicit const_multiset_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
484 : iconst_multiset<TKey, TKeyCompare>(begin_, Size, Size)
485 {
486 }
487 };
488
489 //*************************************************************************
491 //*************************************************************************
492 #if ETL_USING_CPP17
493 template <typename TElements, size_t Size>
494 const_multiset_ext(const etl::span<TElements, Size>&) -> const_multiset_ext<TElements>;
495
496 template <typename TElements, size_t Size>
497 const_multiset_ext(const TElements (&)[Size]) -> const_multiset_ext<TElements>;
498 #endif
499
500 //*************************************************************************
502 //*************************************************************************
503 template <typename TKey, typename TKeyCompare>
504 ETL_CONSTEXPR14 bool operator==(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
505 {
506 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
507 }
508
509 //*************************************************************************
511 //*************************************************************************
512 template <typename TKey, typename TKeyCompare>
513 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
514 {
515 return !(lhs == rhs);
516 }
517
518 //*************************************************************************
524 //*************************************************************************
525 template <typename TKey, typename TKeyCompare>
526 ETL_CONSTEXPR14 bool operator<(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
527 {
528 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
529 }
530
531 //*************************************************************************
537 //*************************************************************************
538 template <typename TKey, typename TKeyCompare>
539 ETL_CONSTEXPR14 bool operator>(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
540 {
541 return (rhs < lhs);
542 }
543
544 //*************************************************************************
551 //*************************************************************************
552 template <typename TKey, typename TKeyCompare>
553 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
554 {
555 return !(rhs < lhs);
556 }
557
558 //*************************************************************************
564 //*************************************************************************
565 template <typename TKey, typename TKeyCompare>
566 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_multiset<TKey, TKeyCompare>& lhs, const etl::iconst_multiset<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
567 {
568 return !(lhs < rhs);
569 }
570} // namespace etl
571
572#endif
573#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