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 "functional.h"
42#include "nth_type.h"
43#include "span.h"
44#include "type_traits.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 //*************************************************************************
234 //*************************************************************************
235 ETL_CONSTEXPR14 const_iterator lower_bound(const key_type& key) const ETL_NOEXCEPT
236 {
237 return etl::lower_bound(begin(), end(), key, vcompare);
238 }
239
240 //*************************************************************************
247 //*************************************************************************
248 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
249 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
250 {
251 return etl::lower_bound(begin(), end(), key, vcompare);
252 }
253
254 //*************************************************************************
261 //*************************************************************************
262 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
263 {
264 return etl::upper_bound(begin(), end(), key, vcompare);
265 }
266
267 //*************************************************************************
274 //*************************************************************************
275 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
276 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
277 {
278 return etl::upper_bound(begin(), end(), key, vcompare);
279 }
280
281 //*************************************************************************
284 //*************************************************************************
285 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
286 {
287 return size() == 0U;
288 }
289
290 //*************************************************************************
293 //*************************************************************************
294 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
295 {
296 return (max_elements != 0) && (size() == max_elements);
297 }
298
299 //*************************************************************************
302 //*************************************************************************
303 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
304 {
305 return size_type(element_list_end - element_list);
306 }
307
308 //*************************************************************************
311 //*************************************************************************
312 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
313 {
314 return max_elements;
315 }
316
317 //*************************************************************************
321 //*************************************************************************
322 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
323 {
324 return max_elements;
325 }
326
327 //*************************************************************************
330 //*************************************************************************
331 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
332 {
333 return key_compare();
334 }
335
336 //*************************************************************************
339 //*************************************************************************
340 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
341 {
342 return value_compare();
343 }
344
345 protected:
346
347 //*************************************************************************
349 //*************************************************************************
350 template <typename... TElements>
351 ETL_CONSTEXPR14 explicit iconst_set(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
352 : element_list(element_list_)
353 , element_list_end{element_list_ + size_}
354 , max_elements(max_elements_)
355 {
356 }
357
358 private:
359
360 //*********************************************************************
362 //*********************************************************************
363 ETL_CONSTEXPR14 bool keys_are_equal(const_reference key1, const_reference key2) const ETL_NOEXCEPT
364 {
365 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
366 }
367
368 //*********************************************************************
371 //*********************************************************************
372 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
373 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
374 {
375 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
376 }
377
378 key_compare kcompare;
379 value_compare vcompare;
380
381 const value_type* element_list;
382 const value_type* element_list_end;
383 size_type max_elements;
384 };
385
386 //*********************************************************************
388 //*********************************************************************
389 template <typename TKey, size_t Size, typename TKeyCompare = etl::less<TKey>>
390 class const_set : public iconst_set<TKey, TKeyCompare>
391 {
392 public:
393
394 using base_t = iconst_set<TKey, TKeyCompare>;
395
396 using key_type = typename base_t::key_type;
397 using value_type = typename base_t::value_type;
398 using key_compare = typename base_t::key_compare;
399 using const_reference = typename base_t::const_reference;
400 using const_pointer = typename base_t::const_pointer;
401 using const_iterator = typename base_t::const_iterator;
402 using size_type = typename base_t::size_type;
403
405 using const_key_reference = const key_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_set(TElements&&... elements) ETL_NOEXCEPT
417 : iconst_set<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 key_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_set(TElements...) -> const_set<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_set_ext : public iconst_set<TKey, TKeyCompare>
442 {
443 public:
444
445 using base_t = iconst_set<TKey, TKeyCompare>;
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_set_ext() ETL_NOEXCEPT
461 : iconst_set<TKey, TKeyCompare>(nullptr, 0, 0)
462 {
463 }
464
465 //*************************************************************************
467 //*************************************************************************
468 template <size_type Size>
469 ETL_CONSTEXPR14 explicit const_set_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
470 : iconst_set<TKey, TKeyCompare>(sp.data(), Size, Size)
471 {
472 }
473
474 //*************************************************************************
476 //*************************************************************************
477 template <size_type Size>
478 ETL_CONSTEXPR14 explicit const_set_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
479 : iconst_set<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_set_ext(const etl::span<TElements, Size>&) -> const_set_ext<TElements>;
490
491 template <typename TElements, size_t Size>
492 const_set_ext(const TElements (&)[Size]) -> const_set_ext<TElements>;
493#endif
494
495 //*************************************************************************
497 //*************************************************************************
498 template <typename TKey, typename TKeyCompare>
499 ETL_CONSTEXPR14 bool operator==(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
500 {
501 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
502 }
503
504 //*************************************************************************
506 //*************************************************************************
507 template <typename TKey, typename TKeyCompare>
508 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
509 {
510 return !(lhs == rhs);
511 }
512
513 //*************************************************************************
515 //*************************************************************************
516 template <typename TKey, typename TKeyCompare>
517 ETL_CONSTEXPR14 bool operator<(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
518 {
519 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
520 }
521
522 //*************************************************************************
524 //*************************************************************************
525 template <typename TKey, typename TKeyCompare>
526 ETL_CONSTEXPR14 bool operator>(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
527 {
528 return (rhs < lhs);
529 }
530
531 //*************************************************************************
533 //*************************************************************************
534 template <typename TKey, typename TKeyCompare>
535 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_set<TKey, TKeyCompare>& lhs, 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, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
545 {
546 return !(lhs < rhs);
547 }
548} // namespace etl
549
550#endif
ETL_CONSTEXPR14 const_set_ext(const value_type(&begin_)[Size]) ETL_NOEXCEPT
Construct a const_set from an array.
Definition const_set.h:478
ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
Default construct a const_set.
Definition const_set.h:460
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:469
Map type designed for constexpr.
Definition const_set.h:391
const key_type & const_key_reference
Defines the parameter types.
Definition const_set.h:405
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:416
Definition const_set.h:55
ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
Definition const_set.h:331
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:303
ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
Definition const_set.h:322
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:262
ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
Definition const_set.h:285
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:235
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:312
ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
Definition const_set.h:294
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:351
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:340
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:276
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:249
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:208
ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1718
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1133
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1147
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1106
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1120