Embedded Template Library 1.0
Loading...
Searching...
No Matches
const_set.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_SET_INCLUDED
32#define ETL_CONST_SET_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>
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_unique_sorted(begin(), end(), vcompare);
75 }
76
77 //*************************************************************************
79 //*************************************************************************
80 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
81 {
82 return element_list;
83 }
84
85 //*************************************************************************
87 //*************************************************************************
88 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
89 {
90 return element_list;
91 }
92
93 //*************************************************************************
95 //*************************************************************************
96 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
97 {
98 return element_list_end;
99 }
100
101 //*************************************************************************
103 //*************************************************************************
104 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
105 {
106 return element_list_end;
107 }
108
109 //*************************************************************************
111 //*************************************************************************
112 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
113 {
114 return element_list;
115 }
116
117 //*************************************************************************
122 //*************************************************************************
123 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
124 {
125 const_iterator itr = lower_bound(key);
126
127 if ((itr != end()) && (keys_are_equal(*itr, key)))
128 {
129 return itr;
130 }
131
132 return end();
133 }
134
135 //*************************************************************************
141 //*************************************************************************
142 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
143 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
144 {
145 const_iterator itr = lower_bound(key);
146
147 if ((itr != end()) && (keys_are_equal(*itr, key)))
148 {
149 return itr;
150 }
151
152 return end();
153 }
154
155 //*************************************************************************
159 //*************************************************************************
160 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
161 {
162 return find(key) != end();
163 }
164
165 //*************************************************************************
170 //*************************************************************************
171 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
172 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
173 {
174 return find(key) != end();
175 }
176
177 //*************************************************************************
181 //*************************************************************************
182 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
183 {
184 return contains(key) ? 1 : 0;
185 }
186
187 //*************************************************************************
192 //*************************************************************************
193 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
194 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
195 {
196 return contains(key) ? 1 : 0;
197 }
198
199 //*************************************************************************
206 //*************************************************************************
207 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
208 {
209 return etl::equal_range(begin(), end(), key, vcompare);
210 }
211
212 //*************************************************************************
220 //*************************************************************************
221 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
222 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
223 {
224 return etl::equal_range(begin(), end(), key, vcompare);
225 }
226
227 //*************************************************************************
232 //*************************************************************************
233 ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
234 {
235 return etl::lower_bound(begin(), end(), key, vcompare);
236 }
237
238 //*************************************************************************
244 //*************************************************************************
245 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
246 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
247 {
248 return etl::lower_bound(begin(), end(), key, vcompare);
249 }
250
251 //*************************************************************************
256 //*************************************************************************
257 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
258 {
259 return etl::upper_bound(begin(), end(), key, vcompare);
260 }
261
262 //*************************************************************************
268 //*************************************************************************
269 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
270 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
271 {
272 return etl::upper_bound(begin(), end(), key, vcompare);
273 }
274
275 //*************************************************************************
278 //*************************************************************************
279 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
280 {
281 return size() == 0U;
282 }
283
284 //*************************************************************************
287 //*************************************************************************
288 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
289 {
290 return (max_elements != 0) && (size() == max_elements);
291 }
292
293 //*************************************************************************
296 //*************************************************************************
297 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
298 {
299 return size_type(element_list_end - element_list);
300 }
301
302 //*************************************************************************
305 //*************************************************************************
306 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
307 {
308 return max_elements;
309 }
310
311 //*************************************************************************
315 //*************************************************************************
316 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
317 {
318 return max_elements;
319 }
320
321 //*************************************************************************
324 //*************************************************************************
325 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
326 {
327 return key_compare();
328 }
329
330 //*************************************************************************
333 //*************************************************************************
334 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
335 {
336 return value_compare();
337 }
338
339 protected:
340
341 //*************************************************************************
343 //*************************************************************************
344 template <typename... TElements>
345 ETL_CONSTEXPR14 explicit iconst_set(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
346 : element_list(element_list_)
347 , element_list_end{element_list_ + size_}
348 , max_elements(max_elements_)
349 {
350 }
351
352 private:
353
354 //*********************************************************************
356 //*********************************************************************
357 ETL_CONSTEXPR14 bool keys_are_equal(const_reference key1, const_reference key2) const ETL_NOEXCEPT
358 {
359 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
360 }
361
362 //*********************************************************************
365 //*********************************************************************
366 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
367 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
368 {
369 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
370 }
371
372 key_compare kcompare;
373 value_compare vcompare;
374
375 const value_type* element_list;
376 const value_type* element_list_end;
377 size_type max_elements;
378 };
379
380 //*********************************************************************
382 //*********************************************************************
383 template <typename TKey, size_t Size, typename TKeyCompare = etl::less<TKey>>
384 class const_set : public iconst_set<TKey, TKeyCompare>
385 {
386 public:
387
388 using base_t = iconst_set<TKey, TKeyCompare>;
389
390 using key_type = typename base_t::key_type;
391 using value_type = typename base_t::value_type;
392 using key_compare = typename base_t::key_compare;
393 using const_reference = typename base_t::const_reference;
394 using const_pointer = typename base_t::const_pointer;
395 using const_iterator = typename base_t::const_iterator;
396 using size_type = typename base_t::size_type;
397
399 using const_key_reference = const key_type&;
400
401 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
402
403 //*************************************************************************
407 //*************************************************************************
408 template <typename... TElements>
409 ETL_CONSTEXPR14 explicit const_set(TElements&&... elements) ETL_NOEXCEPT
410 : iconst_set<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
411 , element_list{etl::forward<TElements>(elements)...}
412 {
413 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be key_type");
414 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
415 }
416
417 private:
418
419 value_type element_list[Size];
420 };
421
422 //*************************************************************************
424 //*************************************************************************
425#if ETL_USING_CPP17
426 template <typename... TElements>
427 const_set(TElements...) -> const_set<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
428#endif
429
430 //*********************************************************************
432 //*********************************************************************
433 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
434 class const_set_ext : public iconst_set<TKey, TKeyCompare>
435 {
436 public:
437
438 using base_t = iconst_set<TKey, TKeyCompare>;
439
440 using key_type = typename base_t::key_type;
441 using value_type = typename base_t::value_type;
442 using key_compare = typename base_t::key_compare;
443 using const_reference = typename base_t::const_reference;
444 using const_pointer = typename base_t::const_pointer;
445 using const_iterator = typename base_t::const_iterator;
446 using size_type = typename base_t::size_type;
447
448 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
449
450 //*************************************************************************
452 //*************************************************************************
453 ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
454 : iconst_set<TKey, TKeyCompare>(nullptr, 0, 0)
455 {
456 }
457
458 //*************************************************************************
460 //*************************************************************************
461 template <size_type Size>
462 ETL_CONSTEXPR14 explicit const_set_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
463 : iconst_set<TKey, TKeyCompare>(sp.data(), Size, Size)
464 {
465 }
466
467 //*************************************************************************
469 //*************************************************************************
470 template <size_type Size>
471 ETL_CONSTEXPR14 explicit const_set_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
472 : iconst_set<TKey, TKeyCompare>(begin_, Size, Size)
473 {
474 }
475 };
476
477 //*************************************************************************
479 //*************************************************************************
480#if ETL_USING_CPP17
481 template <typename TElements, size_t Size>
482 const_set_ext(const etl::span<TElements, Size>&) -> const_set_ext<TElements>;
483
484 template <typename TElements, size_t Size>
485 const_set_ext(const TElements(&)[Size]) -> const_set_ext<TElements>;
486#endif
487
488 //*************************************************************************
490 //*************************************************************************
491 template <typename TKey, typename TKeyCompare>
492 ETL_CONSTEXPR14 bool operator ==(const etl::iconst_set<TKey, TKeyCompare>& lhs,
493 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
494 {
495 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
496 }
497
498 //*************************************************************************
500 //*************************************************************************
501 template <typename TKey, typename TKeyCompare>
502 ETL_CONSTEXPR14 bool operator !=(const etl::iconst_set<TKey, TKeyCompare>& lhs,
503 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
504 {
505 return !(lhs == rhs);
506 }
507
508 //*************************************************************************
510 //*************************************************************************
511 template <typename TKey, typename TKeyCompare>
512 ETL_CONSTEXPR14 bool operator <(const etl::iconst_set<TKey, TKeyCompare>& lhs,
513 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
514 {
515 return etl::lexicographical_compare(lhs.begin(), lhs.end(),
516 rhs.begin(), rhs.end(),
517 lhs.value_comp());
518 }
519
520 //*************************************************************************
522 //*************************************************************************
523 template <typename TKey, typename TKeyCompare>
524 ETL_CONSTEXPR14 bool operator >(const etl::iconst_set<TKey, TKeyCompare>& lhs,
525 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
526 {
527 return (rhs < lhs);
528 }
529
530 //*************************************************************************
532 //*************************************************************************
533 template <typename TKey, typename TKeyCompare>
534 ETL_CONSTEXPR14 bool operator <=(const etl::iconst_set<TKey, TKeyCompare>& lhs,
535 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
536 {
537 return !(rhs < lhs);
538 }
539
540 //*************************************************************************
542 //*************************************************************************
543 template <typename TKey, typename TKeyCompare>
544 ETL_CONSTEXPR14 bool operator >=(const etl::iconst_set<TKey, TKeyCompare>& lhs,
545 const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
546 {
547 return !(lhs < rhs);
548 }
549}
550
551#endif
ETL_CONSTEXPR14 const_set_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
Construct a const_set from an array.
Definition const_set.h:471
ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
Default construct a const_set.
Definition const_set.h:453
ETL_CONSTEXPR14 const_set_ext(const etl::span< const value_type, Size > &sp) ETL_NOEXCEPT
Construct a const_set from a variadic list of elements.
Definition const_set.h:462
Map type designed for constexpr.
Definition const_set.h:385
const key_type & const_key_reference
Defines the parameter types.
Definition const_set.h:399
ETL_CONSTEXPR14 const_set(TElements &&... elements) ETL_NOEXCEPT
Construct a const_set from a variadic list of elements. Static asserts if the elements are not of typ...
Definition const_set.h:409
Definition const_set.h:55
ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
Definition const_set.h:325
ETL_CONSTEXPR14 bool contains(const key_type &key) const ETL_NOEXCEPT
Checks if the set contains an element with key.
Definition const_set.h:160
ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
Definition const_set.h:297
ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
Definition const_set.h:316
ETL_CONSTEXPR14 bool is_valid() const ETL_NOEXCEPT
Definition const_set.h:72
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_set.h:257
ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
Definition const_set.h:279
ETL_CONSTEXPR14 bool contains(const K &key) const ETL_NOEXCEPT
Checks if the set contains an element with key. Enabled if the comparator is transparent.
Definition const_set.h:172
ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
Returns a const_iterator to the beginning of the set.
Definition const_set.h:80
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_set.h:207
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_set.h:233
ETL_CONSTEXPR14 const_iterator find(const K &key) const ETL_NOEXCEPT
Gets a const_iterator to the key. Enabled if the comparator is transparent.
Definition const_set.h:143
ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
Returns a const_iterator to the beginning of the set.
Definition const_set.h:88
ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
Definition const_set.h:306
ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
Definition const_set.h:288
ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
Returns a const_iterator to the end of the set.
Definition const_set.h:104
ETL_CONSTEXPR14 const_iterator find(const key_type &key) const ETL_NOEXCEPT
Gets a const_iterator to the key.
Definition const_set.h:123
ETL_CONSTEXPR14 iconst_set(const value_type *element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
Constructor.
Definition const_set.h:345
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_set.h:194
ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
Returns a const_iterator to the end of the set.
Definition const_set.h:96
ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
Returns a const_pointer to the beginning of the set.
Definition const_set.h:112
ETL_CONSTEXPR14 size_type count(const key_type &key) const ETL_NOEXCEPT
Counts the numbeer elements with key.
Definition const_set.h:182
ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
Definition const_set.h:334
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_set.h:270
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_set.h:246
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_set.h:222
Span - Fixed Extent.
Definition span.h:138
ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1794
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