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
408 //*************************************************************************
413 //*************************************************************************
414 // The base class is passed the address of 'element_list' before that member has been
415 // initialised. The base class only stores the address and never reads through it during
416 // construction, so the compiler's 'may be used uninitialized' warning is a false positive.
417 template <typename... TElements>
418 ETL_CONSTEXPR14 explicit const_set(TElements&&... elements) ETL_NOEXCEPT
419 : iconst_set<TKey, TKeyCompare>(element_list, sizeof...(elements), Size)
420 , element_list{etl::forward<TElements>(elements)...}
421 {
422 static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be key_type");
423 static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
424 }
425 #include "private/diagnostic_pop.h"
426
427 private:
428
429 value_type element_list[Size];
430 };
431
432 //*************************************************************************
434 //*************************************************************************
435 #if ETL_USING_CPP17
436 template <typename... TElements>
437 const_set(TElements...) -> const_set<etl::nth_type_t<0, TElements...>, sizeof...(TElements)>;
438 #endif
439
440 //*********************************************************************
442 //*********************************************************************
443 template <typename TKey, typename TKeyCompare = etl::less<TKey>>
444 class const_set_ext : public iconst_set<TKey, TKeyCompare>
445 {
446 public:
447
448 using base_t = iconst_set<TKey, TKeyCompare>;
449
450 using key_type = typename base_t::key_type;
451 using value_type = typename base_t::value_type;
452 using key_compare = typename base_t::key_compare;
453 using const_reference = typename base_t::const_reference;
454 using const_pointer = typename base_t::const_pointer;
455 using const_iterator = typename base_t::const_iterator;
456 using size_type = typename base_t::size_type;
457
458 static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
459
460 //*************************************************************************
462 //*************************************************************************
463 ETL_CONSTEXPR14 const_set_ext() ETL_NOEXCEPT
464 : iconst_set<TKey, TKeyCompare>(nullptr, 0, 0)
465 {
466 }
467
468 //*************************************************************************
470 //*************************************************************************
471 template <size_type Size>
472 ETL_CONSTEXPR14 explicit const_set_ext(const etl::span<const value_type, Size>& sp) ETL_NOEXCEPT
473 : iconst_set<TKey, TKeyCompare>(sp.data(), Size, Size)
474 {
475 }
476
477 //*************************************************************************
479 //*************************************************************************
480 template <size_type Size>
481 ETL_CONSTEXPR14 explicit const_set_ext(const value_type (&begin_)[Size]) ETL_NOEXCEPT
482 : iconst_set<TKey, TKeyCompare>(begin_, Size, Size)
483 {
484 }
485 };
486
487 //*************************************************************************
489 //*************************************************************************
490 #if ETL_USING_CPP17
491 template <typename TElements, size_t Size>
492 const_set_ext(const etl::span<TElements, Size>&) -> const_set_ext<TElements>;
493
494 template <typename TElements, size_t Size>
495 const_set_ext(const TElements (&)[Size]) -> const_set_ext<TElements>;
496 #endif
497
498 //*************************************************************************
500 //*************************************************************************
501 template <typename TKey, typename TKeyCompare>
502 ETL_CONSTEXPR14 bool operator==(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
503 {
504 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
505 }
506
507 //*************************************************************************
509 //*************************************************************************
510 template <typename TKey, typename TKeyCompare>
511 ETL_CONSTEXPR14 bool operator!=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
512 {
513 return !(lhs == rhs);
514 }
515
516 //*************************************************************************
518 //*************************************************************************
519 template <typename TKey, typename TKeyCompare>
520 ETL_CONSTEXPR14 bool operator<(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
521 {
522 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), lhs.value_comp());
523 }
524
525 //*************************************************************************
527 //*************************************************************************
528 template <typename TKey, typename TKeyCompare>
529 ETL_CONSTEXPR14 bool operator>(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
530 {
531 return (rhs < lhs);
532 }
533
534 //*************************************************************************
536 //*************************************************************************
537 template <typename TKey, typename TKeyCompare>
538 ETL_CONSTEXPR14 bool operator<=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
539 {
540 return !(rhs < lhs);
541 }
542
543 //*************************************************************************
545 //*************************************************************************
546 template <typename TKey, typename TKeyCompare>
547 ETL_CONSTEXPR14 bool operator>=(const etl::iconst_set<TKey, TKeyCompare>& lhs, const etl::iconst_set<TKey, TKeyCompare>& rhs) ETL_NOEXCEPT
548 {
549 return !(lhs < rhs);
550 }
551} // namespace etl
552
553#endif
554#endif
Make this a clone of the supplied priority queue.
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