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#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>
52 class iconst_set
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_unique_sorted(begin(), end(), vcompare);
73 }
74
75 //*************************************************************************
77 //*************************************************************************
78 ETL_CONSTEXPR14 const_iterator begin() const ETL_NOEXCEPT
79 {
80 return element_list;
81 }
82
83 //*************************************************************************
85 //*************************************************************************
86 ETL_CONSTEXPR14 const_iterator cbegin() const ETL_NOEXCEPT
87 {
88 return element_list;
89 }
90
91 //*************************************************************************
93 //*************************************************************************
94 ETL_CONSTEXPR14 const_iterator end() const ETL_NOEXCEPT
95 {
96 return element_list_end;
97 }
98
99 //*************************************************************************
101 //*************************************************************************
102 ETL_CONSTEXPR14 const_iterator cend() const ETL_NOEXCEPT
103 {
104 return element_list_end;
105 }
106
107 //*************************************************************************
109 //*************************************************************************
110 ETL_CONSTEXPR14 const_pointer data() const ETL_NOEXCEPT
111 {
112 return element_list;
113 }
114
115 //*************************************************************************
120 //*************************************************************************
121 ETL_CONSTEXPR14 const_iterator find(const key_type& key) const ETL_NOEXCEPT
122 {
123 const_iterator itr = lower_bound(key);
124
125 if ((itr != end()) && (keys_are_equal(*itr, key)))
126 {
127 return itr;
128 }
129
130 return end();
131 }
132
133 //*************************************************************************
139 //*************************************************************************
140 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
141 ETL_CONSTEXPR14 const_iterator find(const K& key) const ETL_NOEXCEPT
142 {
143 const_iterator itr = lower_bound(key);
144
145 if ((itr != end()) && (keys_are_equal(*itr, key)))
146 {
147 return itr;
148 }
149
150 return end();
151 }
152
153 //*************************************************************************
157 //*************************************************************************
158 ETL_CONSTEXPR14 bool contains(const key_type& key) const ETL_NOEXCEPT
159 {
160 return find(key) != end();
161 }
162
163 //*************************************************************************
168 //*************************************************************************
169 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
170 ETL_CONSTEXPR14 bool contains(const K& key) const ETL_NOEXCEPT
171 {
172 return find(key) != end();
173 }
174
175 //*************************************************************************
179 //*************************************************************************
180 ETL_CONSTEXPR14 size_type count(const key_type& key) const ETL_NOEXCEPT
181 {
182 return contains(key) ? 1 : 0;
183 }
184
185 //*************************************************************************
190 //*************************************************************************
191 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
192 ETL_CONSTEXPR14 size_type count(const K& key) const ETL_NOEXCEPT
193 {
194 return contains(key) ? 1 : 0;
195 }
196
197 //*************************************************************************
204 //*************************************************************************
205 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const key_type& key) const ETL_NOEXCEPT
206 {
207 return etl::equal_range(begin(), end(), key, vcompare);
208 }
209
210 //*************************************************************************
218 //*************************************************************************
219 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
220 ETL_CONSTEXPR14 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const ETL_NOEXCEPT
221 {
222 return etl::equal_range(begin(), end(), key, vcompare);
223 }
224
225 //*************************************************************************
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 //*************************************************************************
245 //*************************************************************************
246 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
247 ETL_CONSTEXPR14 const_iterator lower_bound(const K& key) const ETL_NOEXCEPT
248 {
249 return etl::lower_bound(begin(), end(), key, vcompare);
250 }
251
252 //*************************************************************************
259 //*************************************************************************
260 ETL_CONSTEXPR14 const_iterator upper_bound(const key_type& key) const ETL_NOEXCEPT
261 {
262 return etl::upper_bound(begin(), end(), key, vcompare);
263 }
264
265 //*************************************************************************
272 //*************************************************************************
273 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
274 ETL_CONSTEXPR14 const_iterator upper_bound(const K& key) const ETL_NOEXCEPT
275 {
276 return etl::upper_bound(begin(), end(), key, vcompare);
277 }
278
279 //*************************************************************************
282 //*************************************************************************
283 ETL_CONSTEXPR14 size_type empty() const ETL_NOEXCEPT
284 {
285 return size() == 0U;
286 }
287
288 //*************************************************************************
291 //*************************************************************************
292 ETL_CONSTEXPR14 size_type full() const ETL_NOEXCEPT
293 {
294 return (max_elements != 0) && (size() == max_elements);
295 }
296
297 //*************************************************************************
300 //*************************************************************************
301 ETL_CONSTEXPR14 size_type size() const ETL_NOEXCEPT
302 {
303 return size_type(element_list_end - element_list);
304 }
305
306 //*************************************************************************
309 //*************************************************************************
310 ETL_CONSTEXPR14 size_type max_size() const ETL_NOEXCEPT
311 {
312 return max_elements;
313 }
314
315 //*************************************************************************
319 //*************************************************************************
320 ETL_CONSTEXPR14 size_type capacity() const ETL_NOEXCEPT
321 {
322 return max_elements;
323 }
324
325 //*************************************************************************
328 //*************************************************************************
329 ETL_CONSTEXPR14 key_compare key_comp() const ETL_NOEXCEPT
330 {
331 return key_compare();
332 }
333
334 //*************************************************************************
337 //*************************************************************************
338 ETL_CONSTEXPR14 value_compare value_comp() const ETL_NOEXCEPT
339 {
340 return value_compare();
341 }
342
343 protected:
344
345 //*************************************************************************
347 //*************************************************************************
348 template <typename... TElements>
349 ETL_CONSTEXPR14 explicit iconst_set(const value_type* element_list_, size_type size_, size_type max_elements_) ETL_NOEXCEPT
350 : element_list(element_list_)
351 , element_list_end{element_list_ + size_}
352 , max_elements(max_elements_)
353 {
354 }
355
356 private:
357
358 //*********************************************************************
360 //*********************************************************************
361 ETL_CONSTEXPR14 bool keys_are_equal(const_reference key1, const_reference key2) const ETL_NOEXCEPT
362 {
363 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
364 }
365
366 //*********************************************************************
369 //*********************************************************************
370 template <typename K1, typename K2, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
371 ETL_CONSTEXPR14 bool keys_are_equal(const K1& key1, const K2& key2) const ETL_NOEXCEPT
372 {
373 return !key_compare()(key1, key2) && !key_compare()(key2, key1);
374 }
375
376 key_compare kcompare;
377 value_compare vcompare;
378
379 const value_type* element_list;
380 const value_type* element_list_end;
381 size_type max_elements;
382 };
383
384 //*********************************************************************
386 //*********************************************************************
387 template <typename TKey, size_t Size, typename TKeyCompare = etl::less<TKey>>
388 class const_set : public iconst_set<TKey, TKeyCompare>
389 {
390 public:
391
392 using base_t = iconst_set<TKey, TKeyCompare>;
393
394 using key_type = typename base_t::key_type;
395 using value_type = typename base_t::value_type;
396 using key_compare = typename base_t::key_compare;
397 using const_reference = typename base_t::const_reference;
398 using const_pointer = typename base_t::const_pointer;
399 using const_iterator = typename base_t::const_iterator;
400 using size_type = typename base_t::size_type;
401
403 using const_key_reference = const key_type&;
404
405 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
406
407 //*************************************************************************
412 //*************************************************************************
413 template <typename... TElements>
414 ETL_CONSTEXPR14 explicit const_set(TElements&&... elements) ETL_NOEXCEPT
415 : iconst_set<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
416 , element_list{etl::forward<TElements>(elements)...}
417 {
418 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be key_type");
419 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
420 }
421
422 private:
423
424 value_type element_list[Size];
425 };
426
427 //*************************************************************************
429 //*************************************************************************
430 #if ETL_USING_CPP17
431 template <typename... TElements>
432 const_set(TElements...) -> const_set<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
433 #endif
434
435 //*********************************************************************
437 //*********************************************************************
438 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
439 class const_set_ext : public iconst_set<TKey, TKeyCompare>
440 {
441 public:
442
443 using base_t = iconst_set<TKey, TKeyCompare>;
444
445 using key_type = typename base_t::key_type;
446 using value_type = typename base_t::value_type;
447 using key_compare = typename base_t::key_compare;
448 using const_reference = typename base_t::const_reference;
449 using const_pointer = typename base_t::const_pointer;
450 using const_iterator = typename base_t::const_iterator;
451 using size_type = typename base_t::size_type;
452
453 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
454
455 //*************************************************************************
457 //*************************************************************************
458 ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
459 : iconst_set<TKey, TKeyCompare>(nullptr, 0, 0)
460 {
461 }
462
463 //*************************************************************************
465 //*************************************************************************
466 template <size_type Size>
467 ETL_CONSTEXPR14 explicit const_set_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
468 : iconst_set<TKey, TKeyCompare>(sp.data(), Size, Size)
469 {
470 }
471
472 //*************************************************************************
474 //*************************************************************************
475 template <size_type Size>
476 ETL_CONSTEXPR14 explicit const_set_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
477 : iconst_set<TKey, TKeyCompare>(begin_, Size, Size)
478 {
479 }
480 };
481
482 //*************************************************************************
484 //*************************************************************************
485 #if ETL_USING_CPP17
486 template <typename TElements, size_t Size>
487 const_set_ext(const etl::span<TElements, Size>&) -> const_set_ext<TElements>;
488
489 template <typename TElements, size_t Size>
490 const_set_ext(const TElements (&)[Size]) -> const_set_ext<TElements>;
491 #endif
492
493 //*************************************************************************
495 //*************************************************************************
496 template <typename TKey, typename TKeyCompare>
497 ETL_CONSTEXPR14 bool operator==(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
498 {
499 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
500 }
501
502 //*************************************************************************
504 //*************************************************************************
505 template <typename TKey, typename TKeyCompare>
506 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
507 {
508 return !(lhs == rhs);
509 }
510
511 //*************************************************************************
513 //*************************************************************************
514 template <typename TKey, typename TKeyCompare>
515 ETL_CONSTEXPR14 bool operator<(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
516 {
517 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), 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, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
525 {
526 return (rhs < lhs);
527 }
528
529 //*************************************************************************
531 //*************************************************************************
532 template <typename TKey, typename TKeyCompare>
533 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
534 {
535 return !(rhs < lhs);
536 }
537
538 //*************************************************************************
540 //*************************************************************************
541 template <typename TKey, typename TKeyCompare>
542 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
543 {
544 return !(lhs < rhs);
545 }
546} // namespace etl
547
548#endif
549#endif
ETL_NODISCARD ETL_CONSTEXPR14 bool is_unique_sorted(TIterator begin, TIterator end)
Definition algorithm.h:1727
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