Embedded Template Library 1.0
bitset_new.h
Go to the documentation of this file.
1
2/******************************************************************************
3The MIT License(MIT)
4
5Embedded Template Library.
6https://github.com/ETLCPP/etl
7https://www.etlcpp.com
8
9Copyright(c) 2022 John Wellbelove
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files(the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions :
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28******************************************************************************/
29
30#ifndef ETL_BITSET_NEW_INCLUDED
31#define ETL_BITSET_NEW_INCLUDED
32
33#include "../platform.h"
34#include "../algorithm.h"
35#include "../iterator.h"
36#include "../integral_limits.h"
37#include "../algorithm.h"
38#include "../nullptr.h"
39#include "../log.h"
40#include "../exception.h"
41#include "../integral_limits.h"
42#include "../binary.h"
43#include "../char_traits.h"
44#include "../static_assert.h"
45#include "../error_handler.h"
46#include "../span.h"
47#include "../string.h"
48
49#include <string.h>
50#include <stddef.h>
51#include <stdint.h>
52
53#include "minmax_push.h"
54
55#if defined(ETL_COMPILER_KEIL)
56#pragma diag_suppress 1300
57#endif
58
59#if ETL_USING_CPP11
60 #define ETL_STR(x) x
61 #define ETL_STRL(x) L##x
62 #define ETL_STRu(x) u##x
63 #define ETL_STRU(x) U##x
64#else
65 #define ETL_STR(x) x
66 #define ETL_STRL(x) x
67 #define ETL_STRu(x) x
68 #define ETL_STRU(x) x
69#endif
70
71//*****************************************************************************
75//*****************************************************************************
76
77namespace etl
78{
79 template <typename T = void>
81 {
82 static ETL_CONSTANT size_t npos = etl::integral_limits<size_t>::max;
83 };
84
85 template <typename T>
86 ETL_CONSTANT size_t bitset_constants<T>::npos;
87
88 //***************************************************************************
91 //***************************************************************************
92 class bitset_exception : public etl::exception
93 {
94 public:
95
96 bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
97 : exception(reason_, file_name_, line_number_)
98 {
99 }
100 };
101
102 //***************************************************************************
105 //***************************************************************************
107 {
108 public:
109
110 bitset_string_too_small(string_type file_name_, numeric_type line_number_)
111 : bitset_exception(ETL_ERROR_TEXT("bitset:type_too_small", ETL_BITSET_FILE_ID"A"), file_name_, line_number_)
112 {
113 }
114 };
115
116 //***************************************************************************
119 //***************************************************************************
120 class bitset_overflow : public bitset_exception
121 {
122 public:
123
124 bitset_overflow(string_type file_name_, numeric_type line_number_)
125 : bitset_exception(ETL_ERROR_TEXT("bitset:overflow", ETL_BITSET_FILE_ID"C"), file_name_, line_number_)
126 {
127 }
128 };
129
130 //*************************************************************************
133 //*************************************************************************
134 template <typename TElement>
136 {
137 public:
138
139 typedef typename etl::make_unsigned<TElement>::type element_type;
140
141 typedef element_type* pointer;
142 typedef const element_type* const_pointer;
143
144 static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
145 static ETL_CONSTANT element_type All_Set_Element = etl::integral_limits<element_type>::max;
146 static ETL_CONSTANT element_type All_Clear_Element = element_type(0);
147
148 //*************************************************************************
150 //*************************************************************************
151 ETL_CONSTEXPR14 size_t count(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
152 {
153 size_t n = 0UL;
154
155 for (size_t i = 0UL; i < number_of_elements; ++i)
156 {
157 n += etl::count_bits(pbuffer[i]);
158 }
159
160 return n;
161 }
162
163 //*************************************************************************
166 //*************************************************************************
167 ETL_CONSTEXPR14 bool test(const_pointer pbuffer, size_t number_of_elements, size_t position) const ETL_NOEXCEPT
168 {
169 size_t index = 0U;
170 element_type mask = element_type(0);
171
172 if (number_of_elements == 1U)
173 {
174 index = 0;
175 mask = element_type(1) << position;
176 }
177 else
178 {
179 index = position >> etl::log2<Bits_Per_Element>::value;
180 mask = element_type(1) << (position & (Bits_Per_Element - 1));
181 }
182
183 return (pbuffer[index] & mask) != 0;
184 }
185
186
187 //*************************************************************************
189 //*************************************************************************
190 ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t position, bool value = true) ETL_NOEXCEPT
191 {
192 size_t index = 0;
193 element_type bit = 0;
194
195 if (number_of_elements == 0)
196 {
197 return;
198 }
199 else if (number_of_elements == 1U)
200 {
201 index = 0;
202 bit = element_type(1) << position;
203 }
204 else
205 {
206 index = position >> etl::log2<Bits_Per_Element>::value;
207 bit = element_type(1) << (position & (Bits_Per_Element - 1));
208 }
209
210 if (value)
211 {
212 pbuffer[index] |= bit;
213 }
214 else
215 {
216 pbuffer[index] &= ~bit;
217 }
218 }
219
220 //*************************************************************************
222 //*************************************************************************
223 ETL_CONSTEXPR14 void from_string(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char* text) ETL_NOEXCEPT
224 {
225 if (text == ETL_NULLPTR)
226 {
227 etl::fill_n(pbuffer, number_of_elements, All_Clear_Element);
228 }
229 else
230 {
231 size_t string_length = etl::strlen(text);
232 size_t element_index = etl::min(number_of_elements - 1U, (string_length / Bits_Per_Element));
233
234 // Only reset elements we need to.
235 while (element_index != number_of_elements)
236 {
237 pbuffer[element_index++] = All_Clear_Element;
238 }
239
240 // Build from the string.
241 size_t i = etl::min(total_bits, string_length);
242
243 while (i > 0)
244 {
245 set(pbuffer, number_of_elements, --i, *text++ == ETL_STR('1'));
246 }
247 }
248 }
249
250 //*************************************************************************
252 //*************************************************************************
253 ETL_CONSTEXPR14 void from_string(pointer pbuffer, size_t number_of_elements, size_t total_bits, const wchar_t* text) ETL_NOEXCEPT
254 {
255 if (text == ETL_NULLPTR)
256 {
257 etl::fill_n(pbuffer, number_of_elements, All_Clear_Element);
258 }
259 else
260 {
261 size_t string_length = etl::strlen(text);
262 size_t element_index = etl::min(number_of_elements - 1U, (string_length / Bits_Per_Element));
263
264 // Only reset elements we need to.
265 while (element_index != number_of_elements)
266 {
267 pbuffer[element_index++] = All_Clear_Element;
268 }
269
270 // Build from the string.
271 size_t i = etl::min(total_bits, string_length);
272
273 while (i > 0)
274 {
275 set(pbuffer, number_of_elements, --i, *text++ == ETL_STRL('1'));
276 }
277 }
278 }
279
280 //*************************************************************************
282 //*************************************************************************
283 ETL_CONSTEXPR14 void from_string(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char16_t* text) ETL_NOEXCEPT
284 {
285 if (text == ETL_NULLPTR)
286 {
287 etl::fill_n(pbuffer, number_of_elements, All_Clear_Element);
288 }
289 else
290 {
291 size_t string_length = etl::strlen(text);
292 size_t element_index = etl::min(number_of_elements - 1U, (string_length / Bits_Per_Element));
293
294 // Only reset elements we need to.
295 while (element_index != number_of_elements)
296 {
297 pbuffer[element_index++] = All_Clear_Element;
298 }
299
300 // Build from the string.
301 size_t i = etl::min(total_bits, string_length);
302
303 while (i > 0)
304 {
305 set(pbuffer, number_of_elements, --i, *text++ == ETL_STRu('1'));
306 }
307 }
308 }
309
310 //*************************************************************************
312 //*************************************************************************
313 ETL_CONSTEXPR14 void from_string(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char32_t* text) ETL_NOEXCEPT
314 {
315 if (text == ETL_NULLPTR)
316 {
317 etl::fill_n(pbuffer, number_of_elements, All_Clear_Element);
318 }
319 else
320 {
321 size_t string_length = etl::strlen(text);
322 size_t element_index = etl::min(number_of_elements - 1U, (string_length / Bits_Per_Element));
323
324 // Only reset elements we need to.
325 while (element_index != number_of_elements)
326 {
327 pbuffer[element_index++] = All_Clear_Element;
328 }
329
330 // Build from the string.
331 size_t i = etl::min(total_bits, string_length);
332
333 while (i > 0)
334 {
335 set(pbuffer, number_of_elements, --i, *text++ == ETL_STRU('1'));
336 }
337 }
338 }
339
340 //*************************************************************************
342 //*************************************************************************
343 ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char* text) ETL_NOEXCEPT
344 {
345 from_string(pbuffer, number_of_elements, total_bits, text);
346 }
347
348 //*************************************************************************
350 //*************************************************************************
351 ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t total_bits, const wchar_t* text) ETL_NOEXCEPT
352 {
353 from_string(pbuffer, number_of_elements, total_bits, text);
354 }
355
356 //*************************************************************************
358 //*************************************************************************
359 ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char16_t* text) ETL_NOEXCEPT
360 {
361 from_string(pbuffer, number_of_elements, total_bits, text);
362 }
363
364 //*************************************************************************
366 //*************************************************************************
367 ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char32_t* text) ETL_NOEXCEPT
368 {
369 from_string(pbuffer, number_of_elements, total_bits, text);
370 }
371
372 //*************************************************************************
374 //*************************************************************************
375 template <typename T>
376 ETL_CONSTEXPR14
378 value(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
379 {
380 T v = T(0);
381
382 const bool OK = (sizeof(T) * CHAR_BIT) >= (number_of_elements * Bits_Per_Element);
383
384 if (OK)
385 {
386 uint_least8_t shift = 0U;
387
388 for (size_t i = 0UL; i < number_of_elements; ++i)
389 {
390 v |= T(typename etl::make_unsigned<T>::type(pbuffer[i]) << shift);
391 shift += uint_least8_t(Bits_Per_Element);
392 }
393 }
394
395 return v;
396 }
397
398 //*************************************************************************
400 //*************************************************************************
401 ETL_CONSTEXPR14 void reset(pointer pbuffer, size_t number_of_elements, size_t position) ETL_NOEXCEPT
402 {
403 size_t index = 0U;
404 element_type bit = element_type(0);
405
406 if (number_of_elements == 0)
407 {
408 return;
409 }
410 else if (number_of_elements == 1)
411 {
412 index = 0;
413 bit = element_type(1) << position;
414 }
415 else
416 {
417 index = position >> etl::log2<Bits_Per_Element>::value;
418 bit = element_type(1) << (position & (Bits_Per_Element - 1));
419 }
420
421 pbuffer[index] &= ~bit;
422 }
423
424 //*************************************************************************
426 //*************************************************************************
427 ETL_CONSTEXPR14 void flip(pointer pbuffer, size_t number_of_elements) ETL_NOEXCEPT
428 {
429 for (size_t i = 0UL; i < number_of_elements; ++i)
430 {
431 pbuffer[i] = ~pbuffer[i];
432 }
433 }
434
435 //*************************************************************************
437 //*************************************************************************
438 ETL_CONSTEXPR14 void flip(pointer pbuffer, size_t number_of_elements, size_t position) ETL_NOEXCEPT
439 {
440 size_t index = 0U;
441 element_type bit = element_type(0);
442
443 if (number_of_elements == 0)
444 {
445 return;
446 }
447 else if (number_of_elements == 1)
448 {
449 index = 0;
450 bit = element_type(1) << position;
451 }
452 else
453 {
454 index = position >> log2<Bits_Per_Element>::value;
455 bit = element_type(1) << (position & (Bits_Per_Element - 1));
456 }
457
458 pbuffer[index] ^= bit;
459 }
460
461 //*************************************************************************
462 // Are all the bits sets?
463 //*************************************************************************
464 ETL_CONSTEXPR14 bool all(const_pointer pbuffer, size_t number_of_elements, element_type top_mask) const ETL_NOEXCEPT
465 {
466 if (number_of_elements == 0UL)
467 {
468 return true;
469 }
470
471 // All but the last.
472 for (size_t i = 0UL; i < (number_of_elements - 1U); ++i)
473 {
474 if (pbuffer[i] != All_Set_Element)
475 {
476 return false;
477 }
478 }
479
480 // The last.
481 if (pbuffer[number_of_elements - 1U] != (All_Set_Element & top_mask))
482 {
483 return false;
484 }
485
486 return true;
487 }
488
489 //*************************************************************************
491 //*************************************************************************
492 ETL_CONSTEXPR14 bool none(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
493 {
494 for (size_t i = 0UL; i < number_of_elements; ++i)
495 {
496 if (pbuffer[i] != 0)
497 {
498 return false;
499 }
500 }
501
502 return true;
503 }
504
505 //*************************************************************************
509 //*************************************************************************
510 ETL_CONSTEXPR14 size_t find_first(const_pointer pbuffer, size_t number_of_elements, size_t total_bits, bool state) const ETL_NOEXCEPT
511 {
512 return find_next(pbuffer, number_of_elements, total_bits, state, 0);
513 }
514
515 //*************************************************************************
520 //*************************************************************************
521 ETL_CONSTEXPR14 size_t find_next(const_pointer pbuffer, size_t number_of_elements, size_t total_bits, bool state, size_t position) const ETL_NOEXCEPT
522 {
523 // Where to start.
524 size_t index = position >> log2<Bits_Per_Element>::value;
525 size_t bit = position & (Bits_Per_Element - 1);
526
527 element_type mask = 1 << bit;
528
529 // For each element in the bitset...
530 while (index < number_of_elements)
531 {
532 element_type value = pbuffer[index];
533
534 // Needs checking?
535 if ((state && (value != All_Clear_Element)) ||
536 (!state && (value != All_Set_Element)))
537 {
538 // For each bit in the element...
539 while ((bit < Bits_Per_Element) && (position < total_bits))
540 {
541 // Equal to the required state?
542 if (((value & mask) != 0) == state)
543 {
544 return position;
545 }
546
547 // Move on to the next bit.
548 mask <<= 1;
549 ++position;
550 ++bit;
551 }
552 }
553 else
554 {
555 position += (Bits_Per_Element - bit);
556 }
557
558 // Start at the beginning for all other elements.
559 bit = 0;
560 mask = 1;
561
562 ++index;
563 }
564
565 return npos;
566 }
567
568 //*************************************************************************
570 //*************************************************************************
571 template <typename TString>
572 ETL_CONSTEXPR14 TString to_string(const_pointer pbuffer,
573 size_t number_of_elements,
574 size_t active_bits,
575 typename TString::value_type zero,
576 typename TString::value_type one) const
577 {
578 TString result;
579
580 result.resize(active_bits, '\0');
581
582 // Check that the string type can contain the digits.
583 ETL_ASSERT_OR_RETURN_VALUE(result.size() == active_bits, ETL_ERROR(etl::bitset_string_too_small), result);
584
585 for (size_t i = active_bits; i > 0; --i)
586 {
587 result[active_bits - i] = test(pbuffer, number_of_elements, i - 1) ? one : zero;
588 }
589
590 return result;
591 }
592
593 //*************************************************************************
595 //*************************************************************************
596 ETL_CONSTEXPR14 void shift_left_equals(pointer pbuffer, size_t number_of_elements, size_t shift) ETL_NOEXCEPT
597 {
598 if (number_of_elements == 1U)
599 {
600 // Just one element.
601 pbuffer[0] <<= shift;
602 }
603 else if ((shift % Bits_Per_Element) == 0U)
604 {
605 // Shift by whole element.
606 const size_t element_shift = shift / Bits_Per_Element;
607 etl::copy_backward(pbuffer, pbuffer + number_of_elements - element_shift, pbuffer + number_of_elements);
608 etl::fill_n(pbuffer, element_shift, All_Clear_Element);
609 }
610 else
611 {
612 // The place where the elements are split when shifting.
613 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
614
615 // Where we are shifting from.
616 int src_index = int(number_of_elements - (shift / Bits_Per_Element) - 1U);
617
618 // Where we are shifting to.
619 int dst_index = int(number_of_elements - 1U);
620
621 // Shift control constants.
622 const size_t lsb_shift = Bits_Per_Element - split_position;
623 const size_t msb_shift = split_position;
624
625 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
627 const element_type lsb_shifted_mask = element_type(lsb_mask << lsb_shift);
628
629 // First lsb.
630 element_type lsb = element_type((pbuffer[src_index] & lsb_mask) << lsb_shift);
631 pbuffer[dst_index] = lsb;
632 --src_index;
633
634 // Now do the shifting.
635 while (src_index >= 0)
636 {
637 // Shift msb.
638 element_type msb = element_type((pbuffer[src_index] & msb_mask) >> msb_shift);
639 pbuffer[dst_index] = pbuffer[dst_index] | msb;
640 --dst_index;
641
642 // Shift lsb.
643 element_type lsb = element_type((pbuffer[src_index] & lsb_mask) << lsb_shift);
644 pbuffer[dst_index] = lsb;
645 --src_index;
646 }
647
648 // Clear the remaining bits.
649 // First lsb.
650 pbuffer[dst_index] &= lsb_shifted_mask;
651 --dst_index;
652
653 // The other remaining bytes on the right.
654 while (dst_index >= 0)
655 {
656 pbuffer[dst_index] = 0;
657 --dst_index;
658 }
659 }
660 }
661
662 //*************************************************************************
664 //*************************************************************************
665 ETL_CONSTEXPR14 void shift_right_equals(pointer pbuffer, size_t number_of_elements, size_t shift) ETL_NOEXCEPT
666 {
667 if (number_of_elements == 1U)
668 {
669 // Just one element.
670 pbuffer[0] >>= shift;
671 }
672 else if ((shift % Bits_Per_Element) == 0U)
673 {
674 // Shift by whole elements.
675 const size_t element_shift = (shift / Bits_Per_Element);
676 pointer pzeros_begin = etl::copy(pbuffer + element_shift, pbuffer + number_of_elements, pbuffer);
677 etl::fill_n(pzeros_begin, element_shift, All_Clear_Element);
678 }
679 else
680 {
681 // The place where the elements are split when shifting.
682 const size_t split_position = shift % Bits_Per_Element;
683
684 // Where we are shifting from.
685 int src_index = int(shift / Bits_Per_Element);
686
687 // Where we are shifting to.
688 int dst_index = 0;
689
690 // Shift control constants.
691 const size_t lsb_shift = Bits_Per_Element - split_position;
692 const size_t msb_shift = split_position;
693
694 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
696 const element_type msb_shifted_mask = element_type(msb_mask >> msb_shift);
697
698 // Now do the shifting.
699 while (src_index < int(number_of_elements - 1))
700 {
701 // Shift msb.
702 element_type msb = element_type((pbuffer[src_index] & msb_mask) >> msb_shift);
703 ++src_index;
704
705 // Shift lsb.
706 element_type lsb = element_type((pbuffer[src_index] & lsb_mask) << lsb_shift);
707
708 // Combine them.
709 pbuffer[dst_index] = lsb | msb;
710 ++dst_index;
711 }
712
713 // Final msb.
714 element_type msb = element_type((pbuffer[src_index] & msb_mask) >> msb_shift);
715 pbuffer[dst_index] = msb;
716
717 // Clear the remaining bits.
718 // First msb.
719 pbuffer[dst_index] &= msb_shifted_mask;
720 ++dst_index;
721
722 // The other remaining bytes.
723 while (dst_index < int(number_of_elements))
724 {
725 pbuffer[dst_index] = 0;
726 ++dst_index;
727 }
728 }
729 }
730
731 //*************************************************************************
733 //*************************************************************************
734 ETL_CONSTEXPR14 void and_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
735 {
736 for (size_t i = 0U; i < number_of_elements; ++i)
737 {
738 pbuffer[i] &= pbuffer2[i];
739 }
740 }
741
742 //*************************************************************************
744 //*************************************************************************
745 ETL_CONSTEXPR14 void or_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
746 {
747 for (size_t i = 0U; i < number_of_elements; ++i)
748 {
749 pbuffer[i] |= pbuffer2[i];
750 }
751 }
752
753 //*************************************************************************
755 //*************************************************************************
756 ETL_CONSTEXPR14 void xor_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
757 {
758 for (size_t i = 0U; i < number_of_elements; ++i)
759 {
760 pbuffer[i] ^= pbuffer2[i];
761 }
762 }
763
764 //*************************************************************************
766 //*************************************************************************
767 ETL_CONSTEXPR14 void initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value) ETL_NOEXCEPT
768 {
769 const size_t Shift = (etl::integral_limits<unsigned long long>::bits <= (int)Bits_Per_Element) ? 0 : Bits_Per_Element;
770
771 // Can we do it in one hit?
772 if (Shift == 0)
773 {
774 pbuffer[0] = element_type(value);
775 }
776 else
777 {
778 size_t i = 0UL;
779
780 // Set the non-zero elements.
781 while ((value != 0) && (i != number_of_elements))
782 {
783 pbuffer[i++] = value & All_Set_Element;
784 value = value >> Shift;
785 }
786
787 // Clear the remaining elements.
788 while (i != number_of_elements)
789 {
790 pbuffer[i++] = All_Clear_Element;
791 }
792 }
793 }
794
795 //*************************************************************************
797 //*************************************************************************
798 ETL_CONSTEXPR14 void swap(pointer pbuffer1, pointer pbuffer2, size_t number_of_elements)
799 {
800 const pointer pbuffer1_end = pbuffer1 + number_of_elements;
801
802 while (pbuffer1 != pbuffer1_end)
803 {
804 element_type temp = *pbuffer1;
805 *pbuffer1 = *pbuffer2;
806 *pbuffer2 = temp;
807 ++pbuffer1;
808 ++pbuffer2;
809 }
810 }
811 };
812
813 template <typename TElement>
814 ETL_CONSTANT size_t bitset_impl<TElement>::Bits_Per_Element;
815
816 template <typename TElement>
817 ETL_CONSTANT typename bitset_impl<TElement>::element_type bitset_impl<TElement>::All_Set_Element;
818
819 template <typename TElement>
820 ETL_CONSTANT typename bitset_impl<TElement>::element_type bitset_impl<TElement>::All_Clear_Element;
821
822 //***************************************************************************
823 template <size_t Active_Bits = 0U,
824 typename TElement = char,
825 bool IsSingleElement = etl::integral_limits<TElement>::bits == Active_Bits>
826 class bitset;
827
828 //***************************************************************************
831 //***************************************************************************
832 template <>
833 class bitset<0U, char, true> : public bitset_constants<>
834 {
835 };
836
837 //***************************************************************************
839 //***************************************************************************
840 template <>
841 class bitset<0U, char, false> : public bitset_constants<>
842 {
843 };
844
845 //***************************************************************************
848 //***************************************************************************
849 template <size_t Active_Bits, typename TElement>
850 class bitset<Active_Bits, TElement, true> : public bitset_constants<>
851 {
852 public:
853
854 // The element type is the unsigned variant of 'TElement'.
855 typedef typename etl::make_unsigned<TElement>::type element_type;
856
857 typedef element_type* pointer;
858 typedef const element_type* const_pointer;
859
860 static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
861 static ETL_CONSTANT size_t Number_Of_Elements = 1U;
862 static ETL_CONSTANT size_t Allocated_Bits = Bits_Per_Element;
863 static ETL_CONSTANT element_type All_Set_Element = etl::integral_limits<typename etl::make_unsigned<element_type>::type>::max;
864 static ETL_CONSTANT element_type All_Clear_Element = element_type(0);
865 static ETL_CONSTANT size_t Top_Mask_Shift = 0U;
866 static ETL_CONSTANT element_type Top_Mask = All_Set_Element;
867
870
871 //*************************************************************************
873 //*************************************************************************
874 class bit_reference
875 {
876 public:
877
878 friend class bitset;
879
880 //*******************************
882 //*******************************
883 ETL_CONSTEXPR14 bit_reference(const bit_reference& other) ETL_NOEXCEPT
884 : p_bitset(other.p_bitset)
885 , position(other.position)
886 {
887 }
888
889 //*******************************
891 //*******************************
892 ETL_CONSTEXPR14 operator bool() const ETL_NOEXCEPT
893 {
894 return p_bitset->test(position);
895 }
896
897 //*******************************
899 //*******************************
900 ETL_CONSTEXPR14 bit_reference& operator = (bool b) ETL_NOEXCEPT
901 {
902 p_bitset->set(position, b);
903 return *this;
904 }
905
906 //*******************************
908 //*******************************
909 ETL_CONSTEXPR14 bit_reference& operator = (const bit_reference& r) ETL_NOEXCEPT
910 {
911 p_bitset->set(position, bool(r));
912 return *this;
913 }
914
915 //*******************************
917 //*******************************
918 ETL_CONSTEXPR14 bit_reference& flip() ETL_NOEXCEPT
919 {
920 p_bitset->flip(position);
921 return *this;
922 }
923
924 //*******************************
926 //*******************************
927 ETL_CONSTEXPR14 bool operator~() const ETL_NOEXCEPT
928 {
929 return !p_bitset->test(position);
930 }
931
932 private:
933
934 //*******************************
936 //*******************************
937 ETL_CONSTEXPR14 bit_reference() ETL_NOEXCEPT
938 : p_bitset(ETL_NULLPTR)
939 , position(0)
940 {
941 }
942
943 //*******************************
945 //*******************************
946 ETL_CONSTEXPR14 bit_reference(bitset<Active_Bits, TElement, true>& r_bitset, size_t position_) ETL_NOEXCEPT
947 : p_bitset(&r_bitset)
948 , position(position_)
949 {
950 }
951
952 bitset<Active_Bits, TElement, true>* p_bitset;
953 size_t position;
954 };
955
956 //*************************************************************************
958 //*************************************************************************
959 ETL_CONSTEXPR14 bitset() ETL_NOEXCEPT
960 : buffer(All_Clear_Element)
961 {
962 }
963
964 //*************************************************************************
966 //*************************************************************************
967 ETL_CONSTEXPR14 bitset(const bitset<Active_Bits, TElement, true>& other) ETL_NOEXCEPT
968 : buffer(other.buffer)
969 {
970 }
971
972 //*************************************************************************
974 //*************************************************************************
975 ETL_CONSTEXPR14 bitset(unsigned long long value) ETL_NOEXCEPT
976 : buffer(element_type(value))
977 {
978 }
979
980 //*************************************************************************
982 //*************************************************************************
983 ETL_CONSTEXPR14 bitset(const char* text) ETL_NOEXCEPT
984 : buffer(All_Clear_Element)
985 {
986 set(text);
987 }
988
989 //*************************************************************************
991 //*************************************************************************
992 ETL_CONSTEXPR14 bitset(const wchar_t* text) ETL_NOEXCEPT
993 : buffer(All_Clear_Element)
994 {
995 set(text);
996 }
997
998 //*************************************************************************
1000 //*************************************************************************
1001 ETL_CONSTEXPR14 bitset(const char16_t* text) ETL_NOEXCEPT
1002 : buffer(All_Clear_Element)
1003 {
1004 set(text);
1005 }
1006
1007 //*************************************************************************
1009 //*************************************************************************
1010 ETL_CONSTEXPR14 bitset(const char32_t* text) ETL_NOEXCEPT
1011 : buffer(All_Clear_Element)
1012 {
1013 set(text);
1014 }
1015
1016 //*************************************************************************
1018 //*************************************************************************
1019 ETL_CONSTEXPR14 bitset& operator =(const bitset<Active_Bits, TElement, true>& other) ETL_NOEXCEPT
1020 {
1021 buffer = other.buffer;
1022
1023 return *this;
1024 }
1025
1026 //*************************************************************************
1028 //*************************************************************************
1029 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set() ETL_NOEXCEPT
1030 {
1031 buffer = All_Set_Element;
1032
1033 return *this;
1034 }
1035
1036 //*************************************************************************
1038 //*************************************************************************
1039 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(element_type value) ETL_NOEXCEPT
1040 {
1041 buffer = value;
1042
1043 return *this;
1044 }
1045
1046 //*************************************************************************
1048 //*************************************************************************
1049 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(size_t position, bool value = true)
1050 {
1051 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
1052
1053 const element_type mask = element_type(element_type(1) << position);
1054 if (value == true)
1055 {
1056 buffer |= mask;
1057 }
1058 else
1059 {
1060 buffer &= ~mask;
1061 }
1062
1063 return *this;
1064 }
1065
1066 //*************************************************************************
1068 //*************************************************************************
1069 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(const char* text) ETL_NOEXCEPT
1070 {
1071 from_string(text);
1072
1073 return *this;
1074 }
1075
1076 //*************************************************************************
1078 //*************************************************************************
1079 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(const wchar_t* text) ETL_NOEXCEPT
1080 {
1081 from_string(text);
1082
1083 return *this;
1084 }
1085
1086 //*************************************************************************
1088 //*************************************************************************
1089 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(const char16_t* text) ETL_NOEXCEPT
1090 {
1091 from_string(text);
1092
1093 return *this;
1094 }
1095
1096 //*************************************************************************
1098 //*************************************************************************
1099 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& set(const char32_t* text) ETL_NOEXCEPT
1100 {
1101 from_string(text);
1102
1103 return *this;
1104 }
1105
1106 //*************************************************************************
1108 //*************************************************************************
1109 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& from_string(const char* text) ETL_NOEXCEPT
1110 {
1111 if (text == ETL_NULLPTR)
1112 {
1113 reset();
1114 }
1115 else
1116 {
1117 size_t string_length = etl::strlen(text);
1118
1119 // Build from the string.
1120 string_length = etl::min(Active_Bits, string_length);
1121
1122 element_type mask = element_type(element_type(1) << (string_length - 1U));
1123
1124 for (size_t i = 0U; i < string_length; ++i)
1125 {
1126 if (text[i] == ETL_STR('1'))
1127 {
1128 buffer |= mask;
1129 }
1130 else
1131 {
1132 buffer &= ~mask;
1133 }
1134
1135 mask >>= 1U;
1136 }
1137 }
1138
1139 return *this;
1140 }
1141
1142 //*************************************************************************
1144 //*************************************************************************
1145 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& from_string(const wchar_t* text) ETL_NOEXCEPT
1146 {
1147 if (text == ETL_NULLPTR)
1148 {
1149 reset();
1150 }
1151 else
1152 {
1153 size_t string_length = etl::strlen(text);
1154
1155 // Build from the string.
1156 string_length = etl::min(Active_Bits, string_length);
1157
1158 element_type mask = element_type(element_type(1) << (string_length - 1U));
1159
1160 for (size_t i = 0U; i < string_length; ++i)
1161 {
1162 if (text[i] == ETL_STRL('1'))
1163 {
1164 buffer |= mask;
1165 }
1166 else
1167 {
1168 buffer &= ~mask;
1169 }
1170
1171 mask >>= 1U;
1172 }
1173 }
1174
1175 return *this;
1176 }
1177
1178 //*************************************************************************
1180 //*************************************************************************
1181 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& from_string(const char16_t* text) ETL_NOEXCEPT
1182 {
1183 if (text == ETL_NULLPTR)
1184 {
1185 reset();
1186 }
1187 else
1188 {
1189 size_t string_length = etl::strlen(text);
1190
1191 // Build from the string.
1192 string_length = etl::min(Active_Bits, string_length);
1193
1194 element_type mask = element_type(element_type(1) << (string_length - 1U));
1195
1196 for (size_t i = 0U; i < string_length; ++i)
1197 {
1198 if (text[i] == ETL_STRu('1'))
1199 {
1200 buffer |= mask;
1201 }
1202 else
1203 {
1204 buffer &= ~mask;
1205 }
1206
1207 mask >>= 1U;
1208 }
1209 }
1210
1211 return *this;
1212 }
1213
1214 //*************************************************************************
1216 //*************************************************************************
1217 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& from_string(const char32_t* text) ETL_NOEXCEPT
1218 {
1219 if (text == ETL_NULLPTR)
1220 {
1221 reset();
1222 }
1223 else
1224 {
1225 size_t string_length = etl::strlen(text);
1226
1227 // Build from the string.
1228 string_length = etl::min(Active_Bits, string_length);
1229
1230 element_type mask = element_type(element_type(1) << (string_length - 1U));
1231
1232 for (size_t i = 0U; i < string_length; ++i)
1233 {
1234 if (text[i] == ETL_STRU('1'))
1235 {
1236 buffer |= mask;
1237 }
1238 else
1239 {
1240 buffer &= ~mask;
1241 }
1242
1243 mask >>= 1U;
1244 }
1245 }
1246
1247 return *this;
1248 }
1249
1250 //*************************************************************************
1252 //*************************************************************************
1253 template <typename T>
1254 ETL_CONSTEXPR14
1256 value() const ETL_NOEXCEPT
1257 {
1258 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
1259 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element), "Integral type too small");
1260
1261 T v = T(0);
1262
1263 const bool OK = (sizeof(T) * CHAR_BIT) >= Bits_Per_Element;
1264
1265 if (OK)
1266 {
1267 v = T(typename etl::make_unsigned<T>::type(buffer));
1268 }
1269
1270 return v;
1271 }
1272
1273 //*************************************************************************
1275 //*************************************************************************
1276 ETL_CONSTEXPR14 unsigned long to_ulong() const ETL_NOEXCEPT
1277 {
1278 return value<unsigned long>();
1279 }
1280
1281 //*************************************************************************
1283 //*************************************************************************
1284 ETL_CONSTEXPR14 unsigned long long to_ullong() const ETL_NOEXCEPT
1285 {
1286 return value<unsigned long long>();
1287 }
1288
1289 //*************************************************************************
1291 //*************************************************************************
1292 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& reset() ETL_NOEXCEPT
1293 {
1294 buffer = All_Clear_Element;
1295
1296 return *this;
1297 }
1298
1299 //*************************************************************************
1301 //*************************************************************************
1302 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& reset(size_t position)
1303 {
1304 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
1305
1306 const element_type mask = element_type(element_type(1) << position);
1307 buffer &= ~mask;
1308
1309 return *this;
1310 }
1311
1312 //*************************************************************************
1315 //*************************************************************************
1316 ETL_CONSTEXPR14 bool test(size_t position) const
1317 {
1318 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
1319
1320 const element_type mask = element_type(element_type(1) << position);
1321 return (buffer & mask) != 0U;
1322
1323 return false;
1324 }
1325
1326 //*************************************************************************
1328 //*************************************************************************
1329 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
1330 {
1331 return Active_Bits;
1332 }
1333
1334 //*************************************************************************
1336 //*************************************************************************
1337 ETL_CONSTEXPR14 size_t count() const ETL_NOEXCEPT
1338 {
1339 return etl::count_bits(buffer);
1340 }
1341
1342 //*************************************************************************
1343 // Are all the bits sets?
1344 //*************************************************************************
1345 ETL_CONSTEXPR14 bool all() const ETL_NOEXCEPT
1346 {
1347 return buffer == All_Set_Element;
1348 }
1349
1350 //*************************************************************************
1351 // Are all the bits sets?
1352 //*************************************************************************
1353 ETL_CONSTEXPR14 bool all(element_type mask) const ETL_NOEXCEPT
1354 {
1355 return buffer == (All_Set_Element & mask);
1356 }
1357
1358 //*************************************************************************
1360 //*************************************************************************
1361 ETL_CONSTEXPR14 bool none() const ETL_NOEXCEPT
1362 {
1363 return buffer == All_Clear_Element;
1364 }
1365
1366 //*************************************************************************
1368 //*************************************************************************
1369 ETL_CONSTEXPR14 bool none(element_type mask) const ETL_NOEXCEPT
1370 {
1371 return (buffer & mask) == All_Clear_Element;
1372 }
1373
1374 //*************************************************************************
1376 //*************************************************************************
1377 ETL_CONSTEXPR14 bool any() const ETL_NOEXCEPT
1378 {
1379 return !none();
1380 }
1381
1382 //*************************************************************************
1384 //*************************************************************************
1385 ETL_CONSTEXPR14 bool any(element_type mask) const ETL_NOEXCEPT
1386 {
1387 return !none(mask);
1388 }
1389
1390 //*************************************************************************
1392 //*************************************************************************
1393 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& flip() ETL_NOEXCEPT
1394 {
1395 buffer = ~buffer;
1396
1397 return *this;
1398 }
1399
1400 //*************************************************************************
1402 //*************************************************************************
1403 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& flip_bits(element_type mask = etl::integral_limits<element_type>::max) ETL_NOEXCEPT
1404 {
1405 buffer ^= mask;
1406
1407 return *this;
1408 }
1409
1410 //*************************************************************************
1412 //*************************************************************************
1413 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& flip(size_t position)
1414 {
1415 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
1416
1417 const element_type mask = element_type(element_type(1) << position);
1418 buffer ^= mask;
1419
1420 return *this;
1421 }
1422
1423 //*************************************************************************
1425 //*************************************************************************
1426 ETL_CONSTEXPR14 bool operator[] (size_t position) const ETL_NOEXCEPT
1427 {
1428 if (position < Active_Bits)
1429 {
1430 const element_type mask = element_type(element_type(1) << position);
1431 return (buffer & mask) != 0U;
1432 }
1433
1434 return false;
1435 }
1436
1437 //*************************************************************************
1439 //*************************************************************************
1440 ETL_CONSTEXPR14 bit_reference operator [] (size_t position) ETL_NOEXCEPT
1441 {
1442 return bit_reference(*this, position);
1443 }
1444
1445 //*************************************************************************
1447 //*************************************************************************
1448#if ETL_USING_CPP11
1449 template <typename TString = etl::string<Active_Bits>>
1450#else
1451 template <typename TString>
1452#endif
1453 ETL_CONSTEXPR14 TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
1454 typename TString::value_type one = typename TString::value_type('1')) const
1455 {
1456 TString result;
1457
1458 result.resize(Active_Bits, '\0');
1459
1460 // Check that the string type can contain the digits.
1461 ETL_ASSERT_OR_RETURN_VALUE(result.size() == Active_Bits, ETL_ERROR(etl::bitset_string_too_small), result);
1462
1463 for (size_t i = Active_Bits; i > 0; --i)
1464 {
1465 result[Active_Bits - i] = test(i - 1) ? one : zero;
1466 }
1467
1468 return result;
1469 }
1470
1471 //*************************************************************************
1475 //*************************************************************************
1476 ETL_CONSTEXPR14 size_t find_first(bool state) const ETL_NOEXCEPT
1477 {
1478 return find_next(state, 0U);
1479 }
1480
1481 //*************************************************************************
1486 //*************************************************************************
1487 ETL_CONSTEXPR14 size_t find_next(bool state, size_t position) const ETL_NOEXCEPT
1488 {
1489 if (position < Active_Bits)
1490 {
1491 // Where to start.
1492 size_t bit = position;
1493
1494 element_type mask = 1U << position;
1495
1496 // Needs checking?
1497 if ((state && (buffer != All_Clear_Element)) || (!state && (buffer != All_Set_Element)))
1498 {
1499 // For each bit in the element...
1500 while (bit < Active_Bits)
1501 {
1502 // Equal to the required state?
1503 if (((buffer & mask) != 0) == state)
1504 {
1505 return bit;
1506 }
1507
1508 // Move on to the next bit.
1509 mask <<= 1;
1510 ++bit;
1511 }
1512 }
1513 }
1514
1515 return npos;
1516 }
1517
1518 //*************************************************************************
1520 //*************************************************************************
1522 {
1524
1525 temp &= other;
1526
1527 return temp;
1528 }
1529
1530 //*************************************************************************
1532 //*************************************************************************
1534 {
1535 buffer &= other.buffer;
1536
1537 return *this;
1538 }
1539
1540 //*************************************************************************
1542 //*************************************************************************
1544 {
1546
1547 temp |= other;
1548
1549 return temp;
1550 }
1551
1552 //*************************************************************************
1554 //*************************************************************************
1556 {
1557 buffer |= other.buffer;
1558
1559 return *this;
1560 }
1561
1562 //*************************************************************************
1564 //*************************************************************************
1566 {
1568
1569 temp ^= other;
1570
1571 return temp;
1572 }
1573
1574 //*************************************************************************
1576 //*************************************************************************
1578 {
1579 buffer ^= other.buffer;
1580
1581 return *this;
1582 }
1583
1584 //*************************************************************************
1586 //*************************************************************************
1587 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true> operator ~() const ETL_NOEXCEPT
1588 {
1590
1591 temp.flip();
1592
1593 return temp;
1594 }
1595
1596 //*************************************************************************
1598 //*************************************************************************
1599 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true> operator <<(size_t shift) const ETL_NOEXCEPT
1600 {
1602
1603 temp <<= shift;
1604
1605 return temp;
1606 }
1607
1608 //*************************************************************************
1610 //*************************************************************************
1611 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& operator <<=(size_t shift) ETL_NOEXCEPT
1612 {
1613 if (shift >= Active_Bits)
1614 {
1615 reset();
1616 }
1617 else
1618 {
1619 buffer <<= shift;
1620 }
1621
1622 return *this;
1623 }
1624
1625 //*************************************************************************
1627 //*************************************************************************
1628 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true> operator >>(size_t shift) const ETL_NOEXCEPT
1629 {
1631
1632 temp >>= shift;
1633
1634 return temp;
1635 }
1636
1637 //*************************************************************************
1639 //*************************************************************************
1640 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, true>& operator >>=(size_t shift) ETL_NOEXCEPT
1641 {
1642 if (shift >= Active_Bits)
1643 {
1644 reset();
1645 }
1646 else
1647 {
1648 buffer >>= shift;
1649 }
1650
1651 return *this;
1652 }
1653
1654 //*************************************************************************
1656 //*************************************************************************
1657 friend ETL_CONSTEXPR14 bool operator ==(const bitset<Active_Bits, TElement, true>& lhs, const bitset<Active_Bits, TElement, true>& rhs) ETL_NOEXCEPT
1658 {
1659 return (lhs.buffer == rhs.buffer);
1660 }
1661
1662 //*************************************************************************
1664 //*************************************************************************
1665 ETL_CONSTEXPR14 void swap(etl::bitset<Active_Bits, TElement, true>& other) ETL_NOEXCEPT
1666 {
1667 element_type temp = buffer;
1668 buffer = other.buffer;
1669 other.buffer = temp;
1670 }
1671
1672 //*************************************************************************
1675 //*************************************************************************
1676 ETL_CONSTEXPR14 span_type span() ETL_NOEXCEPT
1677 {
1678 return span_type(&buffer, 1);
1679 }
1680
1681 //*************************************************************************
1684 //*************************************************************************
1685 ETL_CONSTEXPR14 const_span_type span() const ETL_NOEXCEPT
1686 {
1687 return const_span_type(&buffer, 1);
1688 }
1689
1690 private:
1691
1692 element_type buffer;
1693 };
1694
1695 template <size_t Active_Bits, typename TElement>
1696 ETL_CONSTANT size_t bitset<Active_Bits, TElement, true>::Bits_Per_Element;
1697
1698 template <size_t Active_Bits, typename TElement>
1699 ETL_CONSTANT size_t bitset<Active_Bits, TElement, true>::Number_Of_Elements;
1700
1701 template <size_t Active_Bits, typename TElement>
1702 ETL_CONSTANT size_t bitset<Active_Bits, TElement, true>::Allocated_Bits;
1703
1704 template <size_t Active_Bits, typename TElement>
1705 ETL_CONSTANT typename bitset<Active_Bits, TElement, true>::element_type bitset<Active_Bits, TElement, true>::All_Set_Element;
1706
1707 template <size_t Active_Bits, typename TElement>
1708 ETL_CONSTANT typename bitset<Active_Bits, TElement, true>::element_type bitset<Active_Bits, TElement, true>::All_Clear_Element;
1709
1710 template <size_t Active_Bits, typename TElement>
1711 ETL_CONSTANT size_t bitset<Active_Bits, TElement, true>::Top_Mask_Shift;
1712
1713 template <size_t Active_Bits, typename TElement>
1714 ETL_CONSTANT typename bitset<Active_Bits, TElement, true>::element_type bitset<Active_Bits, TElement, true>::Top_Mask;
1715
1716 //*************************************************************************
1718 //*************************************************************************
1719 template <size_t Active_Bits, typename TElement>
1720 class bitset<Active_Bits, TElement, false> : public bitset_constants<>
1721 {
1722 private:
1723
1724 //*************************************************************************
1725 // If the 'TElement' is the default 'void', then use 'char', otherwise uses
1726 // the unsigned variant of 'TElement'.
1727 //*************************************************************************
1728 struct select_element_type
1729 {
1730 typedef typename etl::make_unsigned<typename etl::conditional<etl::is_same<void, TElement>::value, char, TElement>::type>::type type;
1731 };
1732
1733 public:
1734
1735 typedef typename select_element_type::type element_type;
1736 typedef element_type* pointer;
1737 typedef const element_type* const_pointer;
1738
1739 static ETL_CONSTANT size_t Bits_Per_Element = etl::bitset_impl<element_type>::Bits_Per_Element;
1740 static ETL_CONSTANT size_t Number_Of_Elements = (Active_Bits % Bits_Per_Element == 0) ? Active_Bits / Bits_Per_Element : Active_Bits / Bits_Per_Element + 1;
1741 static ETL_CONSTANT size_t Allocated_Bits = Number_Of_Elements * Bits_Per_Element;
1742 static ETL_CONSTANT size_t Top_Mask_Shift = ((Bits_Per_Element - (Allocated_Bits - Active_Bits)) % Bits_Per_Element);
1743 static ETL_CONSTANT element_type All_Set_Element = etl::bitset_impl<element_type>::All_Set_Element;
1744 static ETL_CONSTANT element_type All_Clear_Element = etl::bitset_impl<element_type>::All_Clear_Element;
1745 static ETL_CONSTANT element_type Top_Mask = element_type(Top_Mask_Shift == 0 ? All_Set_Element : ~(All_Set_Element << Top_Mask_Shift));
1746
1747 static ETL_CONSTANT size_t ALLOCATED_BITS = Allocated_Bits;
1748
1751
1752 //*************************************************************************
1754 //*************************************************************************
1755 class bit_reference
1756 {
1757 public:
1758
1759 friend class bitset;
1760
1761 //*******************************
1763 //*******************************
1764 ETL_CONSTEXPR14 bit_reference(const bit_reference& other) ETL_NOEXCEPT
1765 : p_bitset(other.p_bitset)
1766 , position(other.position)
1767 {
1768 }
1769
1770 //*******************************
1772 //*******************************
1773 ETL_CONSTEXPR14 operator bool() const ETL_NOEXCEPT
1774 {
1775 return p_bitset->test(position);
1776 }
1777
1778 //*******************************
1780 //*******************************
1781 ETL_CONSTEXPR14 bit_reference& operator = (bool b) ETL_NOEXCEPT
1782 {
1783 p_bitset->set(position, b);
1784 return *this;
1785 }
1786
1787 //*******************************
1789 //*******************************
1790 ETL_CONSTEXPR14 bit_reference& operator = (const bit_reference& r) ETL_NOEXCEPT
1791 {
1792 p_bitset->set(position, bool(r));
1793 return *this;
1794 }
1795
1796 //*******************************
1798 //*******************************
1799 ETL_CONSTEXPR14 bit_reference& flip() ETL_NOEXCEPT
1800 {
1801 p_bitset->flip(position);
1802 return *this;
1803 }
1804
1805 //*******************************
1807 //*******************************
1808 ETL_CONSTEXPR14 bool operator~() const ETL_NOEXCEPT
1809 {
1810 return !p_bitset->test(position);
1811 }
1812
1813 private:
1814
1815 //*******************************
1817 //*******************************
1818 ETL_CONSTEXPR14 bit_reference() ETL_NOEXCEPT
1819 : p_bitset(ETL_NULLPTR)
1820 , position(0)
1821 {
1822 }
1823
1824 //*******************************
1826 //*******************************
1827 ETL_CONSTEXPR14 bit_reference(bitset<Active_Bits, TElement>& r_bitset, size_t position_) ETL_NOEXCEPT
1828 : p_bitset(&r_bitset)
1829 , position(position_)
1830 {
1831 }
1832
1833 bitset<Active_Bits, TElement, false>* p_bitset;
1834 size_t position;
1835 };
1836
1837 //*************************************************************************
1839 //*************************************************************************
1840 ETL_CONSTEXPR14 bitset() ETL_NOEXCEPT
1841 : buffer()
1842 {
1843 reset();
1844 }
1845
1846 //*************************************************************************
1848 //*************************************************************************
1849 ETL_CONSTEXPR14 bitset(const bitset<Active_Bits, TElement, false>& other) ETL_NOEXCEPT
1850 : buffer()
1851 {
1852 etl::copy_n(other.buffer, Number_Of_Elements, buffer);
1853 }
1854
1855 //*************************************************************************
1857 //*************************************************************************
1858 ETL_CONSTEXPR14 bitset(unsigned long long value) ETL_NOEXCEPT
1859 : buffer()
1860 {
1861 ibitset.initialise(buffer, Number_Of_Elements, value);
1862 clear_unused_bits_in_msb();
1863 }
1864
1865 //*************************************************************************
1867 //*************************************************************************
1868 ETL_CONSTEXPR14 bitset(const char* text) ETL_NOEXCEPT
1869 : buffer()
1870 {
1871 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1872 clear_unused_bits_in_msb();
1873 }
1874
1875 //*************************************************************************
1877 //*************************************************************************
1878 ETL_CONSTEXPR14 bitset(const wchar_t* text) ETL_NOEXCEPT
1879 : buffer()
1880 {
1881 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1882 clear_unused_bits_in_msb();
1883 }
1884
1885 //*************************************************************************
1887 //*************************************************************************
1888 ETL_CONSTEXPR14 bitset(const char16_t* text) ETL_NOEXCEPT
1889 : buffer()
1890 {
1891 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1892 clear_unused_bits_in_msb();
1893 }
1894
1895 //*************************************************************************
1897 //*************************************************************************
1898 ETL_CONSTEXPR14 bitset(const char32_t* text) ETL_NOEXCEPT
1899 : buffer()
1900 {
1901 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1902 clear_unused_bits_in_msb();
1903 }
1904
1905 //*************************************************************************
1907 //*************************************************************************
1908 ETL_CONSTEXPR14 bitset& operator =(const bitset<Active_Bits, TElement, false>& other) ETL_NOEXCEPT
1909 {
1910 etl::copy_n(other.buffer, Number_Of_Elements, buffer);
1911
1912 return *this;
1913 }
1914
1915 //*************************************************************************
1917 //*************************************************************************
1918 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set() ETL_NOEXCEPT
1919 {
1920 etl::fill_n(buffer, Number_Of_Elements, All_Set_Element);
1921 clear_unused_bits_in_msb();
1922
1923 return *this;
1924 }
1925
1926 //*************************************************************************
1928 //*************************************************************************
1929 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set(size_t position, bool value = true)
1930 {
1931 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
1932
1933 ibitset.set(buffer, Number_Of_Elements, position, value);
1934 clear_unused_bits_in_msb();
1935
1936 return *this;
1937 }
1938
1939 //*************************************************************************
1941 //*************************************************************************
1942 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set(const char* text) ETL_NOEXCEPT
1943 {
1944 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1945
1946 return *this;
1947 }
1948
1949 //*************************************************************************
1951 //*************************************************************************
1952 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set(const wchar_t* text) ETL_NOEXCEPT
1953 {
1954 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1955
1956 return *this;
1957 }
1958
1959 //*************************************************************************
1961 //*************************************************************************
1962 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set(const char16_t* text) ETL_NOEXCEPT
1963 {
1964 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1965
1966 return *this;
1967 }
1968
1969 //*************************************************************************
1971 //*************************************************************************
1972 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& set(const char32_t* text) ETL_NOEXCEPT
1973 {
1974 ibitset.set(buffer, Number_Of_Elements, Active_Bits, text);
1975
1976 return *this;
1977 }
1978
1979 //*************************************************************************
1981 //*************************************************************************
1982 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& from_string(const char* text) ETL_NOEXCEPT
1983 {
1984 ibitset.from_string(buffer, Number_Of_Elements, Active_Bits, text);
1985
1986 return *this;
1987 }
1988
1989 //*************************************************************************
1991 //*************************************************************************
1992 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& from_string(const wchar_t* text) ETL_NOEXCEPT
1993 {
1994 ibitset.from_string(buffer, Number_Of_Elements, Active_Bits, text);
1995
1996 return *this;
1997 }
1998
1999 //*************************************************************************
2001 //*************************************************************************
2002 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& from_string(const char16_t* text) ETL_NOEXCEPT
2003 {
2004 ibitset.from_string(buffer, Number_Of_Elements, Active_Bits, text);
2005
2006 return *this;
2007 }
2008
2009 //*************************************************************************
2011 //*************************************************************************
2012 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& from_string(const char32_t* text) ETL_NOEXCEPT
2013 {
2014 ibitset.from_string(buffer, Number_Of_Elements, Active_Bits, text);
2015
2016 return *this;
2017 }
2018
2019 //*************************************************************************
2021 //*************************************************************************
2022 template <typename T>
2023 ETL_CONSTEXPR14
2025 value() const ETL_NOEXCEPT
2026 {
2027 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
2028 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element), "Type too small");
2029
2030 return ibitset.template value<T>(buffer, Number_Of_Elements);
2031 }
2032
2033 //*************************************************************************
2035 //*************************************************************************
2036 ETL_CONSTEXPR14 unsigned long to_ulong() const ETL_NOEXCEPT
2037 {
2038 return value<unsigned long>();
2039 }
2040
2041 //*************************************************************************
2043 //*************************************************************************
2044 ETL_CONSTEXPR14 unsigned long long to_ullong() const ETL_NOEXCEPT
2045 {
2046 return value<unsigned long long>();
2047 }
2048 //*************************************************************************
2050 //*************************************************************************
2051 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& reset() ETL_NOEXCEPT
2052 {
2053 etl::fill_n(buffer, Number_Of_Elements, All_Clear_Element);
2054
2055 return *this;
2056 }
2057
2058 //*************************************************************************
2060 //*************************************************************************
2061 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& reset(size_t position)
2062 {
2063 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
2064
2065 ibitset.reset(buffer, Number_Of_Elements, position);
2066
2067 return *this;
2068 }
2069
2070 //*************************************************************************
2073 //*************************************************************************
2074 ETL_CONSTEXPR14 bool test(size_t position) const
2075 {
2076 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
2077
2078 return ibitset.test(buffer, Number_Of_Elements, position);
2079 }
2080
2081 //*************************************************************************
2083 //*************************************************************************
2084 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
2085 {
2086 return Active_Bits;
2087 }
2088
2089 //*************************************************************************
2091 //*************************************************************************
2092 ETL_CONSTEXPR14 size_t count() const ETL_NOEXCEPT
2093 {
2094 return ibitset.count(buffer, Number_Of_Elements);
2095 }
2096
2097 //*************************************************************************
2098 // Are all the bits sets?
2099 //*************************************************************************
2100 ETL_CONSTEXPR14 bool all() const ETL_NOEXCEPT
2101 {
2102 return ibitset.all(buffer, Number_Of_Elements, Top_Mask);
2103 }
2104
2105 //*************************************************************************
2107 //*************************************************************************
2108 ETL_CONSTEXPR14 bool none() const ETL_NOEXCEPT
2109 {
2110 return ibitset.none(buffer, Number_Of_Elements);
2111 }
2112
2113 //*************************************************************************
2115 //*************************************************************************
2116 ETL_CONSTEXPR14 bool any() const ETL_NOEXCEPT
2117 {
2118 return !none();
2119 }
2120
2121 //*************************************************************************
2123 //*************************************************************************
2124 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& flip() ETL_NOEXCEPT
2125 {
2126 ibitset.flip(buffer, Number_Of_Elements);
2127 clear_unused_bits_in_msb();
2128
2129 return *this;
2130 }
2131
2132 //*************************************************************************
2134 //*************************************************************************
2135 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& flip(size_t position)
2136 {
2137 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
2138
2139 ibitset.flip(buffer, Number_Of_Elements, position);
2140
2141 return *this;
2142 }
2143
2144 //*************************************************************************
2146 //*************************************************************************
2147 ETL_CONSTEXPR14 bool operator[] (size_t position) const ETL_NOEXCEPT
2148 {
2149 return ibitset.test(buffer, Number_Of_Elements, position);
2150 }
2151
2152 //*************************************************************************
2154 //*************************************************************************
2155 ETL_CONSTEXPR14 bit_reference operator [] (size_t position) ETL_NOEXCEPT
2156 {
2157 return bit_reference(*this, position);
2158 }
2159
2160 //*************************************************************************
2162 //*************************************************************************
2163#if ETL_USING_CPP11
2164 template <typename TString = etl::string<Active_Bits>>
2165#else
2166 template <typename TString>
2167#endif
2168 ETL_CONSTEXPR14 TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
2169 typename TString::value_type one = typename TString::value_type('1')) const
2170 {
2171 return ibitset.template to_string<TString>(buffer, Number_Of_Elements, Active_Bits, zero, one);
2172 }
2173
2174 //*************************************************************************
2178 //*************************************************************************
2179 ETL_CONSTEXPR14 size_t find_first(bool state) const ETL_NOEXCEPT
2180 {
2181 return ibitset.find_next(buffer, Number_Of_Elements, Active_Bits, state, 0);
2182 }
2183
2184 //*************************************************************************
2189 //*************************************************************************
2190 ETL_CONSTEXPR14 size_t find_next(bool state, size_t position) const ETL_NOEXCEPT
2191 {
2192 return ibitset.find_next(buffer, Number_Of_Elements, Active_Bits, state, position);
2193 }
2194
2195 //*************************************************************************
2197 //*************************************************************************
2199 {
2201
2202 temp &= other;
2203
2204 return temp;
2205 }
2206
2207 //*************************************************************************
2209 //*************************************************************************
2211 {
2212 ibitset.and_equals(&buffer[0], &other.buffer[0], Number_Of_Elements);
2213
2214 return *this;
2215 }
2216
2217 //*************************************************************************
2219 //*************************************************************************
2221 {
2223
2224 temp |= other;
2225
2226 return temp;
2227 }
2228
2229 //*************************************************************************
2231 //*************************************************************************
2233 {
2234 ibitset.or_equals(&buffer[0], &other.buffer[0], Number_Of_Elements);
2235
2236 return *this;
2237 }
2238
2239 //*************************************************************************
2241 //*************************************************************************
2243 {
2245
2246 temp ^= other;
2247
2248 return temp;
2249 }
2250
2251 //*************************************************************************
2253 //*************************************************************************
2255 {
2256 ibitset.xor_equals(&buffer[0], &other.buffer[0], Number_Of_Elements);
2257
2258 return *this;
2259 }
2260
2261 //*************************************************************************
2263 //*************************************************************************
2264 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false> operator ~() const ETL_NOEXCEPT
2265 {
2267
2268 temp.flip();
2269
2270 return temp;
2271 }
2272
2273 //*************************************************************************
2275 //*************************************************************************
2276 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false> operator <<(size_t shift) const ETL_NOEXCEPT
2277 {
2279
2280 temp <<= shift;
2281
2282 return temp;
2283 }
2284
2285 //*************************************************************************
2287 //*************************************************************************
2288 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& operator <<=(size_t shift) ETL_NOEXCEPT
2289 {
2290 if (shift >= Active_Bits)
2291 {
2292 reset();
2293 }
2294 else
2295 {
2296 ibitset.shift_left_equals(buffer, Number_Of_Elements, shift);
2297 clear_unused_bits_in_msb();
2298 }
2299
2300 return *this;
2301 }
2302
2303 //*************************************************************************
2305 //*************************************************************************
2306 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false> operator >>(size_t shift) const ETL_NOEXCEPT
2307 {
2309
2310 temp >>= shift;
2311
2312 return temp;
2313 }
2314
2315 //*************************************************************************
2317 //*************************************************************************
2318 ETL_CONSTEXPR14 bitset<Active_Bits, TElement, false>& operator >>=(size_t shift) ETL_NOEXCEPT
2319 {
2320 if (shift >= Active_Bits)
2321 {
2322 reset();
2323 }
2324 else
2325 {
2326 ibitset.shift_right_equals(buffer, Number_Of_Elements, shift);
2327 }
2328
2329 return *this;
2330 }
2331
2332 //*************************************************************************
2334 //*************************************************************************
2335 friend ETL_CONSTEXPR14 bool operator ==(const bitset<Active_Bits, TElement, false>& lhs, const bitset<Active_Bits, TElement, false>& rhs) ETL_NOEXCEPT
2336 {
2337 return etl::equal(lhs.buffer,
2338 lhs.buffer + lhs.Number_Of_Elements,
2339 rhs.buffer);
2340 }
2341
2342 //*************************************************************************
2344 //*************************************************************************
2345 ETL_CONSTEXPR14 void swap(etl::bitset<Active_Bits, TElement, false>& other) ETL_NOEXCEPT
2346 {
2347 ibitset.swap(buffer, other.buffer, Number_Of_Elements);
2348 }
2349
2350 //*************************************************************************
2353 //*************************************************************************
2354 ETL_CONSTEXPR14 span_type span() ETL_NOEXCEPT
2355 {
2356 return span_type(buffer, Number_Of_Elements);
2357 }
2358
2359 //*************************************************************************
2362 //*************************************************************************
2363 ETL_CONSTEXPR14 const_span_type span() const ETL_NOEXCEPT
2364 {
2365 return const_span_type(buffer, Number_Of_Elements);
2366 }
2367
2368 private:
2369
2370 //*************************************************************************
2372 //*************************************************************************
2373 ETL_CONSTEXPR14 void clear_unused_bits_in_msb() ETL_NOEXCEPT
2374 {
2375 buffer[Number_Of_Elements - 1U] &= Top_Mask;
2376 }
2377
2379
2380 element_type buffer[Number_Of_Elements > 0U ? Number_Of_Elements : 1U];
2381 };
2382
2383 template <size_t Active_Bits, typename TElement>
2384 ETL_CONSTANT size_t bitset<Active_Bits, TElement, false>::Bits_Per_Element;
2385
2386 template <size_t Active_Bits, typename TElement>
2387 ETL_CONSTANT size_t bitset<Active_Bits, TElement, false>::Number_Of_Elements;
2388
2389 template <size_t Active_Bits, typename TElement>
2390 ETL_CONSTANT size_t bitset<Active_Bits, TElement, false>::Allocated_Bits;
2391
2392 template <size_t Active_Bits, typename TElement>
2393 ETL_CONSTANT typename bitset<Active_Bits, TElement, false>::element_type bitset<Active_Bits, TElement, false>::All_Set_Element;
2394
2395 template <size_t Active_Bits, typename TElement>
2396 ETL_CONSTANT typename bitset<Active_Bits, TElement, false>::element_type bitset<Active_Bits, TElement, false>::All_Clear_Element;
2397
2398 template <size_t Active_Bits, typename TElement>
2399 ETL_CONSTANT size_t bitset<Active_Bits, TElement, false>::Top_Mask_Shift;
2400
2401 template <size_t Active_Bits, typename TElement>
2402 ETL_CONSTANT typename bitset<Active_Bits, TElement, false>::element_type bitset<Active_Bits, TElement, false>::Top_Mask;
2403
2404 //***************************************************************************
2407 //***************************************************************************
2408 template <size_t Active_Bits, typename TElement, bool IsSingleElement>
2410 {
2411 bitset<Active_Bits> temp(lhs);
2412 temp &= rhs;
2413 return temp;
2414 }
2415
2416 //***************************************************************************
2419 //***************************************************************************
2420 template<size_t Active_Bits, typename TElement, bool IsSingleElement>
2422 {
2423 bitset<Active_Bits> temp(lhs);
2424 temp |= rhs;
2425 return temp;
2426 }
2427
2428 //***************************************************************************
2431 //***************************************************************************
2432 template<size_t Active_Bits, typename TElement, bool IsSingleElement>
2434 {
2435 bitset<Active_Bits> temp(lhs);
2436 temp ^= rhs;
2437 return temp;
2438 }
2439
2440 //***************************************************************************
2443 //***************************************************************************
2444 template<size_t Active_Bits, typename TElement, bool IsSingleElement>
2446 {
2447 return !(lhs == rhs);
2448 }
2449}
2450
2451//*************************************************************************
2453//*************************************************************************
2454template <size_t Active_Bits, typename TElement, bool IsSingleElement>
2456{
2457 lhs.swap(rhs);
2458}
2459
2460//***************************************************************************
2462//***************************************************************************
2463namespace etl
2464{
2465 //***************************************************************************
2466 template <size_t Active_Bits = 0U,
2467 typename TElement = char,
2468 bool IsSingleElement = etl::integral_limits<TElement>::bits == Active_Bits>
2470
2471 //***************************************************************************
2474 //***************************************************************************
2475 template <>
2476 class bitset_ext<0U, char, true> : public bitset_constants<>
2477 {
2478 };
2479
2480 //***************************************************************************
2482 //***************************************************************************
2483 template <>
2484 class bitset_ext<0U, char, false> : public bitset_constants<>
2485 {
2486 };
2487
2488 //***************************************************************************
2491 //***************************************************************************
2492 template <size_t Active_Bits, typename TElement>
2493 class bitset_ext<Active_Bits, TElement, true> : public bitset_constants<>
2494 {
2495 public:
2496
2497 // The element type is the unsigned variant of 'TElement'.
2498 typedef typename etl::make_unsigned<TElement>::type element_type;
2499
2500 typedef element_type* pointer;
2501 typedef const element_type* const_pointer;
2502
2503 static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
2504 static ETL_CONSTANT size_t Number_Of_Elements = 1U;
2505 static ETL_CONSTANT size_t Allocated_Bits = Bits_Per_Element;
2506 static ETL_CONSTANT element_type All_Set_Element = etl::integral_limits<typename etl::make_unsigned<element_type>::type>::max;
2507 static ETL_CONSTANT element_type All_Clear_Element = element_type(0);
2508 static ETL_CONSTANT size_t Top_Mask_Shift = 0U;
2509 static ETL_CONSTANT element_type Top_Mask = All_Set_Element;
2510
2513
2514 typedef element_type buffer_type;
2515
2516 //*************************************************************************
2518 //*************************************************************************
2519 class bit_reference
2520 {
2521 public:
2522
2523 friend class bitset_ext;
2524
2525 //*******************************
2527 //*******************************
2528 ETL_CONSTEXPR14 bit_reference(const bit_reference& other) ETL_NOEXCEPT
2529 : p_bitset(other.p_bitset)
2530 , position(other.position)
2531 {
2532 }
2533
2534 //*******************************
2536 //*******************************
2537 ETL_CONSTEXPR14 operator bool() const ETL_NOEXCEPT
2538 {
2539 return p_bitset->test(position);
2540 }
2541
2542 //*******************************
2544 //*******************************
2545 ETL_CONSTEXPR14 bit_reference& operator = (bool b) ETL_NOEXCEPT
2546 {
2547 p_bitset->set(position, b);
2548 return *this;
2549 }
2550
2551 //*******************************
2553 //*******************************
2554 ETL_CONSTEXPR14 bit_reference& operator = (const bit_reference& r) ETL_NOEXCEPT
2555 {
2556 p_bitset->set(position, bool(r));
2557 return *this;
2558 }
2559
2560 //*******************************
2562 //*******************************
2563 ETL_CONSTEXPR14 bit_reference& flip() ETL_NOEXCEPT
2564 {
2565 p_bitset->flip(position);
2566 return *this;
2567 }
2568
2569 //*******************************
2571 //*******************************
2572 ETL_CONSTEXPR14 bool operator~() const ETL_NOEXCEPT
2573 {
2574 return !p_bitset->test(position);
2575 }
2576
2577 private:
2578
2579 //*******************************
2581 //*******************************
2582 ETL_CONSTEXPR14 bit_reference() ETL_NOEXCEPT
2583 : p_bitset(ETL_NULLPTR)
2584 , position(0)
2585 {
2586 }
2587
2588 //*******************************
2590 //*******************************
2591 ETL_CONSTEXPR14 bit_reference(bitset_ext<Active_Bits, TElement, true>& r_bitset, size_t position_) ETL_NOEXCEPT
2592 : p_bitset(&r_bitset)
2593 , position(position_)
2594 {
2595 }
2596
2597 bitset_ext<Active_Bits, TElement, true>* p_bitset;
2598 size_t position;
2599 };
2600
2601 //*************************************************************************
2603 //*************************************************************************
2604 ETL_CONSTEXPR14 bitset_ext(element_type* pbuffer_) ETL_NOEXCEPT
2605 : pbuffer(pbuffer_)
2606 {
2607 *pbuffer = All_Clear_Element;
2608 }
2609
2610 //*************************************************************************
2612 //*************************************************************************
2613 ETL_CONSTEXPR14 bitset_ext(buffer_type& buffer) ETL_NOEXCEPT
2614 : pbuffer(&buffer)
2615 {
2616 *pbuffer = All_Clear_Element;
2617 }
2618
2619 //*************************************************************************
2621 //*************************************************************************
2622 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, true>& other, element_type* pbuffer_) ETL_NOEXCEPT
2623 : pbuffer(pbuffer_)
2624 {
2625 *pbuffer = *other.pbuffer;
2626 }
2627
2628 //*************************************************************************
2630 //*************************************************************************
2631 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, true>& other, buffer_type& buffer) ETL_NOEXCEPT
2632 : pbuffer(&buffer)
2633 {
2634 *pbuffer = *other.pbuffer;
2635 }
2636
2637 //*************************************************************************
2639 //*************************************************************************
2640 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, true>& other) ETL_NOEXCEPT ETL_DELETE;
2641
2642 //*************************************************************************
2644 //*************************************************************************
2645 ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_) ETL_NOEXCEPT
2646 : pbuffer(pbuffer_)
2647 {
2648 *pbuffer = element_type(value);
2649 }
2650
2651 //*************************************************************************
2653 //*************************************************************************
2654 ETL_CONSTEXPR14 bitset_ext(unsigned long long value, buffer_type& buffer) ETL_NOEXCEPT
2655 : pbuffer(&buffer)
2656 {
2657 *pbuffer = element_type(value);
2658 }
2659
2660 //*************************************************************************
2662 //*************************************************************************
2663 ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_) ETL_NOEXCEPT
2664 : pbuffer(pbuffer_)
2665 {
2666 *pbuffer = All_Clear_Element;
2667 set(text);
2668 }
2669
2670 //*************************************************************************
2672 //*************************************************************************
2673 ETL_CONSTEXPR14 bitset_ext(const char* text, buffer_type& buffer) ETL_NOEXCEPT
2674 : pbuffer(&buffer)
2675 {
2676 *pbuffer = All_Clear_Element;
2677 set(text);
2678 }
2679
2680 //*************************************************************************
2682 //*************************************************************************
2683 ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_) ETL_NOEXCEPT
2684 : pbuffer(pbuffer_)
2685 {
2686 *pbuffer = All_Clear_Element;
2687 set(text);
2688 }
2689
2690 //*************************************************************************
2692 //*************************************************************************
2693 ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, buffer_type& buffer) ETL_NOEXCEPT
2694 : pbuffer(&buffer)
2695 {
2696 *pbuffer = All_Clear_Element;
2697 set(text);
2698 }
2699
2700 //*************************************************************************
2702 //*************************************************************************
2703 ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_) ETL_NOEXCEPT
2704 : pbuffer(pbuffer_)
2705 {
2706 *pbuffer = All_Clear_Element;
2707 set(text);
2708 }
2709
2710 //*************************************************************************
2712 //*************************************************************************
2713 ETL_CONSTEXPR14 bitset_ext(const char16_t* text, buffer_type& buffer) ETL_NOEXCEPT
2714 : pbuffer(&buffer)
2715 {
2716 *pbuffer = All_Clear_Element;
2717 set(text);
2718 }
2719
2720 //*************************************************************************
2722 //*************************************************************************
2723 ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_) ETL_NOEXCEPT
2724 : pbuffer(pbuffer_)
2725 {
2726 *pbuffer = All_Clear_Element;
2727 set(text);
2728 }
2729
2730 //*************************************************************************
2732 //*************************************************************************
2733 ETL_CONSTEXPR14 bitset_ext(const char32_t* text, buffer_type& buffer) ETL_NOEXCEPT
2734 : pbuffer(&buffer)
2735 {
2736 *pbuffer = All_Clear_Element;
2737 set(text);
2738 }
2739
2740 //*************************************************************************
2742 //*************************************************************************
2743 ETL_CONSTEXPR14 bitset_ext& operator =(const bitset_ext<Active_Bits, TElement, true>& other) ETL_NOEXCEPT
2744 {
2745 *pbuffer = *other.pbuffer;
2746
2747 return *this;
2748 }
2749
2750 //*************************************************************************
2752 //*************************************************************************
2753 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set() ETL_NOEXCEPT
2754 {
2755 *pbuffer = All_Set_Element;
2756
2757 return *this;
2758 }
2759
2760 //*************************************************************************
2762 //*************************************************************************
2763 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set(size_t position, bool value = true)
2764 {
2765 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
2766
2767 const element_type mask = element_type(element_type(1) << position);
2768 if (value == true)
2769 {
2770 *pbuffer |= mask;
2771 }
2772 else
2773 {
2774 *pbuffer &= ~mask;
2775 }
2776
2777 return *this;
2778 }
2779
2780 //*************************************************************************
2782 //*************************************************************************
2783 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set(const char* text) ETL_NOEXCEPT
2784 {
2785 from_string(text);
2786
2787 return *this;
2788 }
2789
2790 //*************************************************************************
2792 //*************************************************************************
2793 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set(const wchar_t* text) ETL_NOEXCEPT
2794 {
2795 from_string(text);
2796
2797 return *this;
2798 }
2799
2800 //*************************************************************************
2802 //*************************************************************************
2803 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set(const char16_t* text) ETL_NOEXCEPT
2804 {
2805 from_string(text);
2806
2807 return *this;
2808 }
2809
2810 //*************************************************************************
2812 //*************************************************************************
2813 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& set(const char32_t* text) ETL_NOEXCEPT
2814 {
2815 from_string(text);
2816
2817 return *this;
2818 }
2819
2820 //*************************************************************************
2822 //*************************************************************************
2823 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& from_string(const char* text) ETL_NOEXCEPT
2824 {
2825 if (text == ETL_NULLPTR)
2826 {
2827 reset();
2828 }
2829 else
2830 {
2831 size_t string_length = etl::strlen(text);
2832
2833 // Build from the string.
2834 string_length = etl::min(Active_Bits, string_length);
2835
2836 element_type mask = element_type(element_type(1) << (string_length - 1U));
2837
2838 for (size_t i = 0U; i < string_length; ++i)
2839 {
2840 if (text[i] == ETL_STR('1'))
2841 {
2842 *pbuffer |= mask;
2843 }
2844 else
2845 {
2846 *pbuffer &= ~mask;
2847 }
2848
2849 mask >>= 1U;
2850 }
2851 }
2852
2853 return *this;
2854 }
2855
2856 //*************************************************************************
2858 //*************************************************************************
2859 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& from_string(const wchar_t* text) ETL_NOEXCEPT
2860 {
2861 if (text == ETL_NULLPTR)
2862 {
2863 reset();
2864 }
2865 else
2866 {
2867 size_t string_length = etl::strlen(text);
2868
2869 // Build from the string.
2870 string_length = etl::min(Active_Bits, string_length);
2871
2872 element_type mask = element_type(element_type(1) << (string_length - 1U));
2873
2874 for (size_t i = 0U; i < string_length; ++i)
2875 {
2876 if (text[i] == ETL_STRL('1'))
2877 {
2878 *pbuffer |= mask;
2879 }
2880 else
2881 {
2882 *pbuffer &= ~mask;
2883 }
2884
2885 mask >>= 1U;
2886 }
2887 }
2888
2889 return *this;
2890 }
2891
2892 //*************************************************************************
2894 //*************************************************************************
2895 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& from_string(const char16_t* text) ETL_NOEXCEPT
2896 {
2897 if (text == ETL_NULLPTR)
2898 {
2899 reset();
2900 }
2901 else
2902 {
2903 size_t string_length = etl::strlen(text);
2904
2905 // Build from the string.
2906 string_length = etl::min(Active_Bits, string_length);
2907
2908 element_type mask = element_type(element_type(1) << (string_length - 1U));
2909
2910 for (size_t i = 0U; i < string_length; ++i)
2911 {
2912 if (text[i] == ETL_STRu('1'))
2913 {
2914 *pbuffer |= mask;
2915 }
2916 else
2917 {
2918 *pbuffer &= ~mask;
2919 }
2920
2921 mask >>= 1U;
2922 }
2923 }
2924
2925 return *this;
2926 }
2927
2928 //*************************************************************************
2930 //*************************************************************************
2931 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& from_string(const char32_t* text) ETL_NOEXCEPT
2932 {
2933 if (text == ETL_NULLPTR)
2934 {
2935 reset();
2936 }
2937 else
2938 {
2939 size_t string_length = etl::strlen(text);
2940
2941 // Build from the string.
2942 string_length = etl::min(Active_Bits, string_length);
2943
2944 element_type mask = element_type(element_type(1) << (string_length - 1U));
2945
2946 for (size_t i = 0U; i < string_length; ++i)
2947 {
2948 if (text[i] == ETL_STRU('1'))
2949 {
2950 *pbuffer |= mask;
2951 }
2952 else
2953 {
2954 *pbuffer &= ~mask;
2955 }
2956
2957 mask >>= 1U;
2958 }
2959 }
2960
2961 return *this;
2962 }
2963
2964 //*************************************************************************
2966 //*************************************************************************
2967 template <typename T>
2968 ETL_CONSTEXPR14
2970 value() const ETL_NOEXCEPT
2971 {
2972 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
2973 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element), "Integral type too small");
2974
2975 T v = T(0);
2976
2977 const bool OK = (sizeof(T) * CHAR_BIT) >= Bits_Per_Element;
2978
2979 if (OK)
2980 {
2981 v = T(typename etl::make_unsigned<T>::type(*pbuffer));
2982 }
2983
2984 return v;
2985 }
2986
2987 //*************************************************************************
2989 //*************************************************************************
2990 ETL_CONSTEXPR14 unsigned long to_ulong() const ETL_NOEXCEPT
2991 {
2992 return value<unsigned long>();
2993 }
2994
2995 //*************************************************************************
2997 //*************************************************************************
2998 ETL_CONSTEXPR14 unsigned long long to_ullong() const ETL_NOEXCEPT
2999 {
3000 return value<unsigned long long>();
3001 }
3002
3003 //*************************************************************************
3005 //*************************************************************************
3006 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& reset() ETL_NOEXCEPT
3007 {
3008 *pbuffer = All_Clear_Element;
3009
3010 return *this;
3011 }
3012
3013 //*************************************************************************
3015 //*************************************************************************
3016 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& reset(size_t position)
3017 {
3018 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
3019
3020 const element_type mask = element_type(element_type(1) << position);
3021 *pbuffer &= ~mask;
3022
3023 return *this;
3024 }
3025
3026 //*************************************************************************
3029 //*************************************************************************
3030 ETL_CONSTEXPR14 bool test(size_t position) const
3031 {
3032 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
3033
3034 const element_type mask = element_type(element_type(1) << position);
3035 return (*pbuffer & mask) != 0U;
3036
3037 return false;
3038 }
3039
3040 //*************************************************************************
3042 //*************************************************************************
3043 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
3044 {
3045 return Active_Bits;
3046 }
3047
3048 //*************************************************************************
3050 //*************************************************************************
3051 ETL_CONSTEXPR14 size_t count() const ETL_NOEXCEPT
3052 {
3053 return etl::count_bits(*pbuffer);
3054 }
3055
3056 //*************************************************************************
3057 // Are all the bits sets?
3058 //*************************************************************************
3059 ETL_CONSTEXPR14 bool all() const ETL_NOEXCEPT
3060 {
3061 return *pbuffer == All_Set_Element;
3062 }
3063
3064 //*************************************************************************
3065 // Are all the bits sets?
3066 //*************************************************************************
3067 ETL_CONSTEXPR14 bool all(element_type mask) const ETL_NOEXCEPT
3068 {
3069 return *pbuffer == (All_Set_Element & mask);
3070 }
3071
3072 //*************************************************************************
3074 //*************************************************************************
3075 ETL_CONSTEXPR14 bool none() const ETL_NOEXCEPT
3076 {
3077 return *pbuffer == All_Clear_Element;
3078 }
3079
3080 //*************************************************************************
3082 //*************************************************************************
3083 ETL_CONSTEXPR14 bool none(element_type mask) const ETL_NOEXCEPT
3084 {
3085 return (*pbuffer & mask) == All_Clear_Element;
3086 }
3087
3088 //*************************************************************************
3090 //*************************************************************************
3091 ETL_CONSTEXPR14 bool any() const ETL_NOEXCEPT
3092 {
3093 return !none();
3094 }
3095
3096 //*************************************************************************
3098 //*************************************************************************
3099 ETL_CONSTEXPR14 bool any(element_type mask) const ETL_NOEXCEPT
3100 {
3101 return !none(mask);
3102 }
3103
3104 //*************************************************************************
3106 //*************************************************************************
3107 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& flip() ETL_NOEXCEPT
3108 {
3109 *pbuffer = ~*pbuffer;
3110
3111 return *this;
3112 }
3113
3114 //*************************************************************************
3116 //*************************************************************************
3117 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& flip_bits(element_type mask = etl::integral_limits<element_type>::max) ETL_NOEXCEPT
3118 {
3119 *pbuffer ^= mask;
3120
3121 return *this;
3122 }
3123
3124 //*************************************************************************
3126 //*************************************************************************
3127 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& flip(size_t position)
3128 {
3129 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
3130
3131 const element_type mask = element_type(element_type(1) << position);
3132 *pbuffer ^= mask;
3133
3134 return *this;
3135 }
3136
3137 //*************************************************************************
3139 //*************************************************************************
3140 ETL_CONSTEXPR14 bool operator[] (size_t position) const ETL_NOEXCEPT
3141 {
3142 if (position < Active_Bits)
3143 {
3144 const element_type mask = element_type(element_type(1) << position);
3145 return (*pbuffer & mask) != 0U;
3146 }
3147
3148 return false;
3149 }
3150
3151 //*************************************************************************
3153 //*************************************************************************
3154 ETL_CONSTEXPR14 bit_reference operator [] (size_t position) ETL_NOEXCEPT
3155 {
3156 return bit_reference(*this, position);
3157 }
3158
3159 //*************************************************************************
3161 //*************************************************************************
3162#if ETL_USING_CPP11
3163 template <typename TString = etl::string<Active_Bits>>
3164#else
3165 template <typename TString>
3166#endif
3167 ETL_CONSTEXPR14 TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
3168 typename TString::value_type one = typename TString::value_type('1')) const
3169 {
3170 TString result;
3171
3172 result.resize(Active_Bits, '\0');
3173
3174 // Check that the string type can contain the digits.
3175 ETL_ASSERT_OR_RETURN_VALUE(result.size() == Active_Bits, ETL_ERROR(etl::bitset_string_too_small), result);
3176
3177 for (size_t i = Active_Bits; i > 0; --i)
3178 {
3179 result[Active_Bits - i] = test(i - 1) ? one : zero;
3180 }
3181
3182 return result;
3183 }
3184
3185 //*************************************************************************
3189 //*************************************************************************
3190 ETL_CONSTEXPR14 size_t find_first(bool state) const ETL_NOEXCEPT
3191 {
3192 return find_next(state, 0U);
3193 }
3194
3195 //*************************************************************************
3200 //*************************************************************************
3201 ETL_CONSTEXPR14 size_t find_next(bool state, size_t position) const ETL_NOEXCEPT
3202 {
3203 if (position < Active_Bits)
3204 {
3205 // Where to start.
3206 size_t bit = position;
3207
3208 element_type mask = 1U << position;
3209
3210 // Needs checking?
3211 if ((state && (*pbuffer != All_Clear_Element)) || (!state && (*pbuffer != All_Set_Element)))
3212 {
3213 // For each bit in the element...
3214 while (bit < Active_Bits)
3215 {
3216 // Equal to the required state?
3217 if (((*pbuffer & mask) != 0) == state)
3218 {
3219 return bit;
3220 }
3221
3222 // Move on to the next bit.
3223 mask <<= 1;
3224 ++bit;
3225 }
3226 }
3227 }
3228
3229 return npos;
3230 }
3231
3232 //*************************************************************************
3234 //*************************************************************************
3236 {
3237 *pbuffer &= *other.pbuffer;
3238
3239 return *this;
3240 }
3241
3242 //*************************************************************************
3244 //*************************************************************************
3246 {
3247 *pbuffer |= *other.pbuffer;
3248
3249 return *this;
3250 }
3251
3252 //*************************************************************************
3254 //*************************************************************************
3256 {
3257 *pbuffer ^= *other.pbuffer;
3258
3259 return *this;
3260 }
3261
3262 //*************************************************************************
3264 //*************************************************************************
3265 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& operator <<=(size_t shift) ETL_NOEXCEPT
3266 {
3267 if (shift >= Active_Bits)
3268 {
3269 reset();
3270 }
3271 else
3272 {
3273 *pbuffer <<= shift;
3274 }
3275
3276 return *this;
3277 }
3278
3279 //*************************************************************************
3281 //*************************************************************************
3282 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, true>& operator >>=(size_t shift) ETL_NOEXCEPT
3283 {
3284 if (shift >= Active_Bits)
3285 {
3286 reset();
3287 }
3288 else
3289 {
3290 *pbuffer >>= shift;
3291 }
3292
3293 return *this;
3294 }
3295
3296 //*************************************************************************
3298 //*************************************************************************
3299 friend ETL_CONSTEXPR14 bool operator ==(const bitset_ext<Active_Bits, TElement, true>& lhs, const bitset_ext<Active_Bits, TElement, true>& rhs) ETL_NOEXCEPT
3300 {
3301 return (*lhs.pbuffer == *rhs.pbuffer);
3302 }
3303
3304 //*************************************************************************
3306 //*************************************************************************
3307 ETL_CONSTEXPR14 void swap(etl::bitset_ext<Active_Bits, TElement, true>& other) ETL_NOEXCEPT
3308 {
3309 element_type temp = *pbuffer;
3310 *pbuffer = *other.pbuffer;
3311 *other.pbuffer = temp;
3312 }
3313
3314 //*************************************************************************
3317 //*************************************************************************
3318 ETL_CONSTEXPR14 span_type span() ETL_NOEXCEPT
3319 {
3320 return span_type(pbuffer, 1);
3321 }
3322
3323 //*************************************************************************
3326 //*************************************************************************
3327 ETL_CONSTEXPR14 const_span_type span() const ETL_NOEXCEPT
3328 {
3329 return const_span_type(pbuffer, 1);
3330 }
3331
3332 private:
3333
3334 element_type* pbuffer;
3335 };
3336
3337 template <size_t Active_Bits, typename TElement>
3338 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, true>::Bits_Per_Element;
3339
3340 template <size_t Active_Bits, typename TElement>
3341 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, true>::Number_Of_Elements;
3342
3343 template <size_t Active_Bits, typename TElement>
3344 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, true>::Allocated_Bits;
3345
3346 template <size_t Active_Bits, typename TElement>
3347 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, true>::element_type bitset_ext<Active_Bits, TElement, true>::All_Set_Element;
3348
3349 template <size_t Active_Bits, typename TElement>
3350 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, true>::element_type bitset_ext<Active_Bits, TElement, true>::All_Clear_Element;
3351
3352 template <size_t Active_Bits, typename TElement>
3353 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, true>::Top_Mask_Shift;
3354
3355 template <size_t Active_Bits, typename TElement>
3356 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, true>::element_type bitset_ext<Active_Bits, TElement, true>::Top_Mask;
3357
3358 //*************************************************************************
3360 //*************************************************************************
3361 template <size_t Active_Bits, typename TElement>
3362 class bitset_ext<Active_Bits, TElement, false> : public bitset_constants<>
3363 {
3364 private:
3365
3366 //*************************************************************************
3367 // If the 'TElement' is the default 'void', then use 'char', otherwise uses
3368 // the unsigned variant of 'TElement'.
3369 //*************************************************************************
3370 struct select_element_type
3371 {
3372 typedef typename etl::make_unsigned<typename etl::conditional<etl::is_same<void, TElement>::value, char, TElement>::type>::type type;
3373 };
3374
3375 public:
3376
3377 typedef typename select_element_type::type element_type;
3378 typedef element_type* pointer;
3379 typedef const element_type* const_pointer;
3380
3381 static ETL_CONSTANT size_t Bits_Per_Element = etl::bitset_impl<element_type>::Bits_Per_Element;
3382 static ETL_CONSTANT size_t Number_Of_Elements = (Active_Bits % Bits_Per_Element == 0) ? Active_Bits / Bits_Per_Element : Active_Bits / Bits_Per_Element + 1;
3383 static ETL_CONSTANT size_t Allocated_Bits = Number_Of_Elements * Bits_Per_Element;
3384 static ETL_CONSTANT size_t Top_Mask_Shift = ((Bits_Per_Element - (Allocated_Bits - Active_Bits)) % Bits_Per_Element);
3385 static ETL_CONSTANT element_type All_Set_Element = etl::bitset_impl<element_type>::All_Set_Element;
3386 static ETL_CONSTANT element_type All_Clear_Element = etl::bitset_impl<element_type>::All_Clear_Element;
3387 static ETL_CONSTANT element_type Top_Mask = element_type(Top_Mask_Shift == 0 ? All_Set_Element : ~(All_Set_Element << Top_Mask_Shift));
3388
3389 static ETL_CONSTANT size_t ALLOCATED_BITS = Allocated_Bits;
3390
3393
3395
3396 //*************************************************************************
3398 //*************************************************************************
3399 class bit_reference
3400 {
3401 public:
3402
3403 friend class bitset_ext;
3404
3405 //*******************************
3407 //*******************************
3408 ETL_CONSTEXPR14 bit_reference(const bit_reference& other) ETL_NOEXCEPT
3409 : p_bitset(other.p_bitset)
3410 , position(other.position)
3411 {
3412 }
3413
3414 //*******************************
3416 //*******************************
3417 ETL_CONSTEXPR14 operator bool() const ETL_NOEXCEPT
3418 {
3419 return p_bitset->test(position);
3420 }
3421
3422 //*******************************
3424 //*******************************
3425 ETL_CONSTEXPR14 bit_reference& operator = (bool b) ETL_NOEXCEPT
3426 {
3427 p_bitset->set(position, b);
3428 return *this;
3429 }
3430
3431 //*******************************
3433 //*******************************
3434 ETL_CONSTEXPR14 bit_reference& operator = (const bit_reference& r) ETL_NOEXCEPT
3435 {
3436 p_bitset->set(position, bool(r));
3437 return *this;
3438 }
3439
3440 //*******************************
3442 //*******************************
3443 ETL_CONSTEXPR14 bit_reference& flip() ETL_NOEXCEPT
3444 {
3445 p_bitset->flip(position);
3446 return *this;
3447 }
3448
3449 //*******************************
3451 //*******************************
3452 ETL_CONSTEXPR14 bool operator~() const ETL_NOEXCEPT
3453 {
3454 return !p_bitset->test(position);
3455 }
3456
3457 private:
3458
3459 //*******************************
3461 //*******************************
3462 ETL_CONSTEXPR14 bit_reference() ETL_NOEXCEPT
3463 : p_bitset(ETL_NULLPTR)
3464 , position(0)
3465 {
3466 }
3467
3468 //*******************************
3470 //*******************************
3471 ETL_CONSTEXPR14 bit_reference(bitset_ext<Active_Bits, TElement>& r_bitset, size_t position_) ETL_NOEXCEPT
3472 : p_bitset(&r_bitset)
3473 , position(position_)
3474 {
3475 }
3476
3477 bitset_ext<Active_Bits, TElement, false>* p_bitset;
3478 size_t position;
3479 };
3480
3481 //*************************************************************************
3483 //*************************************************************************
3484 ETL_CONSTEXPR14 bitset_ext(element_type* pbuffer_) ETL_NOEXCEPT
3485 : pbuffer(pbuffer_)
3486 {
3487 reset();
3488 }
3489
3490 //*************************************************************************
3492 //*************************************************************************
3493 ETL_CONSTEXPR14 bitset_ext(buffer_type& buffer) ETL_NOEXCEPT
3494 : pbuffer(buffer.data())
3495 {
3496 reset();
3497 }
3498
3499 //*************************************************************************
3501 //*************************************************************************
3502 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, false>& other, element_type* pbuffer_) ETL_NOEXCEPT
3503 : pbuffer(pbuffer_)
3504 {
3505 etl::copy_n(other.pbuffer, Number_Of_Elements, pbuffer);
3506 }
3507
3508 //*************************************************************************
3510 //*************************************************************************
3511 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, false>& other, buffer_type& buffer) ETL_NOEXCEPT
3512 : pbuffer(buffer.data())
3513 {
3514 etl::copy_n(other.pbuffer, Number_Of_Elements, pbuffer);
3515 }
3516
3517 //*************************************************************************
3519 //*************************************************************************
3520 ETL_CONSTEXPR14 bitset_ext(const bitset_ext<Active_Bits, TElement, false>& other) ETL_NOEXCEPT ETL_DELETE;
3521
3522 //*************************************************************************
3524 //*************************************************************************
3525 ETL_CONSTEXPR14 bitset_ext(unsigned long long value, element_type* pbuffer_) ETL_NOEXCEPT
3526 : pbuffer(pbuffer_)
3527 {
3528 ibitset.initialise(pbuffer, Number_Of_Elements, value);
3529 clear_unused_bits_in_msb();
3530 }
3531
3532 //*************************************************************************
3534 //*************************************************************************
3535 ETL_CONSTEXPR14 bitset_ext(unsigned long long value, buffer_type& buffer) ETL_NOEXCEPT
3536 : pbuffer(buffer.data())
3537 {
3538 ibitset.initialise(pbuffer, Number_Of_Elements, value);
3539 clear_unused_bits_in_msb();
3540 }
3541
3542 //*************************************************************************
3544 //*************************************************************************
3545 ETL_CONSTEXPR14 bitset_ext(const char* text, element_type* pbuffer_) ETL_NOEXCEPT
3546 : pbuffer(pbuffer_)
3547 {
3548 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3549 clear_unused_bits_in_msb();
3550 }
3551
3552 //*************************************************************************
3554 //*************************************************************************
3555 ETL_CONSTEXPR14 bitset_ext(const char* text, buffer_type& buffer) ETL_NOEXCEPT
3556 : pbuffer(buffer.data())
3557 {
3558 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3559 clear_unused_bits_in_msb();
3560 }
3561
3562 //*************************************************************************
3564 //*************************************************************************
3565 ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, element_type* pbuffer_) ETL_NOEXCEPT
3566 : pbuffer(pbuffer_)
3567 {
3568 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3569 clear_unused_bits_in_msb();
3570 }
3571
3572 //*************************************************************************
3574 //*************************************************************************
3575 ETL_CONSTEXPR14 bitset_ext(const wchar_t* text, buffer_type& buffer) ETL_NOEXCEPT
3576 : pbuffer(buffer.data())
3577 {
3578 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3579 clear_unused_bits_in_msb();
3580 }
3581
3582 //*************************************************************************
3584 //*************************************************************************
3585 ETL_CONSTEXPR14 bitset_ext(const char16_t* text, element_type* pbuffer_) ETL_NOEXCEPT
3586 : pbuffer(pbuffer_)
3587 {
3588 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3589 clear_unused_bits_in_msb();
3590 }
3591
3592 //*************************************************************************
3594 //*************************************************************************
3595 ETL_CONSTEXPR14 bitset_ext(const char16_t* text, buffer_type& buffer) ETL_NOEXCEPT
3596 : pbuffer(buffer.data())
3597 {
3598 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3599 clear_unused_bits_in_msb();
3600 }
3601
3602 //*************************************************************************
3604 //*************************************************************************
3605 ETL_CONSTEXPR14 bitset_ext(const char32_t* text, element_type* pbuffer_) ETL_NOEXCEPT
3606 : pbuffer(pbuffer_)
3607 {
3608 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3609 clear_unused_bits_in_msb();
3610 }
3611
3612 //*************************************************************************
3614 //*************************************************************************
3615 ETL_CONSTEXPR14 bitset_ext(const char32_t* text, buffer_type& buffer) ETL_NOEXCEPT
3616 : pbuffer(buffer.data())
3617 {
3618 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3619 clear_unused_bits_in_msb();
3620 }
3621
3622 //*************************************************************************
3624 //*************************************************************************
3625 ETL_CONSTEXPR14 bitset_ext& operator =(const bitset_ext<Active_Bits, TElement, false>& other) ETL_NOEXCEPT
3626 {
3627 etl::copy_n(other.pbuffer, Number_Of_Elements, pbuffer);
3628
3629 return *this;
3630 }
3631
3632 //*************************************************************************
3634 //*************************************************************************
3635 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set() ETL_NOEXCEPT
3636 {
3637 etl::fill_n(pbuffer, Number_Of_Elements, All_Set_Element);
3638 clear_unused_bits_in_msb();
3639
3640 return *this;
3641 }
3642
3643 //*************************************************************************
3645 //*************************************************************************
3646 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set(size_t position, bool value = true)
3647 {
3648 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
3649
3650 ibitset.set(pbuffer, Number_Of_Elements, position, value);
3651 clear_unused_bits_in_msb();
3652
3653 return *this;
3654 }
3655
3656 //*************************************************************************
3658 //*************************************************************************
3659 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set(const char* text) ETL_NOEXCEPT
3660 {
3661 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3662
3663 return *this;
3664 }
3665
3666 //*************************************************************************
3668 //*************************************************************************
3669 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set(const wchar_t* text) ETL_NOEXCEPT
3670 {
3671 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3672
3673 return *this;
3674 }
3675
3676 //*************************************************************************
3678 //*************************************************************************
3679 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set(const char16_t* text) ETL_NOEXCEPT
3680 {
3681 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3682
3683 return *this;
3684 }
3685
3686 //*************************************************************************
3688 //*************************************************************************
3689 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& set(const char32_t* text) ETL_NOEXCEPT
3690 {
3691 ibitset.set(pbuffer, Number_Of_Elements, Active_Bits, text);
3692
3693 return *this;
3694 }
3695
3696 //*************************************************************************
3698 //*************************************************************************
3699 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& from_string(const char* text) ETL_NOEXCEPT
3700 {
3701 ibitset.from_string(pbuffer, Number_Of_Elements, Active_Bits, text);
3702
3703 return *this;
3704 }
3705
3706 //*************************************************************************
3708 //*************************************************************************
3709 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& from_string(const wchar_t* text) ETL_NOEXCEPT
3710 {
3711 ibitset.from_string(pbuffer, Number_Of_Elements, Active_Bits, text);
3712
3713 return *this;
3714 }
3715
3716 //*************************************************************************
3718 //*************************************************************************
3719 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& from_string(const char16_t* text) ETL_NOEXCEPT
3720 {
3721 ibitset.from_string(pbuffer, Number_Of_Elements, Active_Bits, text);
3722
3723 return *this;
3724 }
3725
3726 //*************************************************************************
3728 //*************************************************************************
3729 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& from_string(const char32_t* text) ETL_NOEXCEPT
3730 {
3731 ibitset.from_string(pbuffer, Number_Of_Elements, Active_Bits, text);
3732
3733 return *this;
3734 }
3735
3736 //*************************************************************************
3738 //*************************************************************************
3739 template <typename T>
3740 ETL_CONSTEXPR14
3742 value() const ETL_NOEXCEPT
3743 {
3744 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
3745 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element), "Type too small");
3746
3747 return ibitset.template value<T>(pbuffer, Number_Of_Elements);
3748 }
3749
3750 //*************************************************************************
3752 //*************************************************************************
3753 ETL_CONSTEXPR14 unsigned long to_ulong() const ETL_NOEXCEPT
3754 {
3755 return value<unsigned long>();
3756 }
3757
3758 //*************************************************************************
3760 //*************************************************************************
3761 ETL_CONSTEXPR14 unsigned long long to_ullong() const ETL_NOEXCEPT
3762 {
3763 return value<unsigned long long>();
3764 }
3765 //*************************************************************************
3767 //*************************************************************************
3768 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& reset() ETL_NOEXCEPT
3769 {
3770 etl::fill_n(pbuffer, Number_Of_Elements, All_Clear_Element);
3771
3772 return *this;
3773 }
3774
3775 //*************************************************************************
3777 //*************************************************************************
3778 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& reset(size_t position)
3779 {
3780 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
3781
3782 ibitset.reset(pbuffer, Number_Of_Elements, position);
3783
3784 return *this;
3785 }
3786
3787 //*************************************************************************
3790 //*************************************************************************
3791 ETL_CONSTEXPR14 bool test(size_t position) const
3792 {
3793 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
3794
3795 return ibitset.test(pbuffer, Number_Of_Elements, position);
3796 }
3797
3798 //*************************************************************************
3800 //*************************************************************************
3801 ETL_CONSTEXPR14 size_t size() const ETL_NOEXCEPT
3802 {
3803 return Active_Bits;
3804 }
3805
3806 //*************************************************************************
3808 //*************************************************************************
3809 ETL_CONSTEXPR14 size_t count() const ETL_NOEXCEPT
3810 {
3811 return ibitset.count(pbuffer, Number_Of_Elements);
3812 }
3813
3814 //*************************************************************************
3815 // Are all the bits sets?
3816 //*************************************************************************
3817 ETL_CONSTEXPR14 bool all() const ETL_NOEXCEPT
3818 {
3819 return ibitset.all(pbuffer, Number_Of_Elements, Top_Mask);
3820 }
3821
3822 //*************************************************************************
3824 //*************************************************************************
3825 ETL_CONSTEXPR14 bool none() const ETL_NOEXCEPT
3826 {
3827 return ibitset.none(pbuffer, Number_Of_Elements);
3828 }
3829
3830 //*************************************************************************
3832 //*************************************************************************
3833 ETL_CONSTEXPR14 bool any() const ETL_NOEXCEPT
3834 {
3835 return !none();
3836 }
3837
3838 //*************************************************************************
3840 //*************************************************************************
3841 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& flip() ETL_NOEXCEPT
3842 {
3843 ibitset.flip(pbuffer, Number_Of_Elements);
3844 clear_unused_bits_in_msb();
3845
3846 return *this;
3847 }
3848
3849 //*************************************************************************
3851 //*************************************************************************
3852 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& flip(size_t position)
3853 {
3854 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
3855
3856 ibitset.flip(pbuffer, Number_Of_Elements, position);
3857
3858 return *this;
3859 }
3860
3861 //*************************************************************************
3863 //*************************************************************************
3864 ETL_CONSTEXPR14 bool operator[] (size_t position) const ETL_NOEXCEPT
3865 {
3866 return ibitset.test(pbuffer, Number_Of_Elements, position);
3867 }
3868
3869 //*************************************************************************
3871 //*************************************************************************
3872 ETL_CONSTEXPR14 bit_reference operator [] (size_t position) ETL_NOEXCEPT
3873 {
3874 return bit_reference(*this, position);
3875 }
3876
3877 //*************************************************************************
3879 //*************************************************************************
3880#if ETL_USING_CPP11
3881 template <typename TString = etl::string<Active_Bits>>
3882#else
3883 template <typename TString>
3884#endif
3885 ETL_CONSTEXPR14 TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
3886 typename TString::value_type one = typename TString::value_type('1')) const
3887 {
3888 return ibitset.template to_string<TString>(pbuffer, Number_Of_Elements, Active_Bits, zero, one);
3889 }
3890
3891 //*************************************************************************
3895 //*************************************************************************
3896 ETL_CONSTEXPR14 size_t find_first(bool state) const ETL_NOEXCEPT
3897 {
3898 return ibitset.find_next(pbuffer, Number_Of_Elements, Active_Bits, state, 0);
3899 }
3900
3901 //*************************************************************************
3906 //*************************************************************************
3907 ETL_CONSTEXPR14 size_t find_next(bool state, size_t position) const ETL_NOEXCEPT
3908 {
3909 return ibitset.find_next(pbuffer, Number_Of_Elements, Active_Bits, state, position);
3910 }
3911
3912 //*************************************************************************
3914 //*************************************************************************
3916 {
3917 ibitset.and_equals(&pbuffer[0], &other.pbuffer[0], Number_Of_Elements);
3918
3919 return *this;
3920 }
3921
3922 //*************************************************************************
3924 //*************************************************************************
3926 {
3927 ibitset.or_equals(&pbuffer[0], &other.pbuffer[0], Number_Of_Elements);
3928
3929 return *this;
3930 }
3931
3932 //*************************************************************************
3934 //*************************************************************************
3936 {
3937 ibitset.xor_equals(&pbuffer[0], &other.pbuffer[0], Number_Of_Elements);
3938
3939 return *this;
3940 }
3941
3942 //*************************************************************************
3944 //*************************************************************************
3945 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& operator <<=(size_t shift) ETL_NOEXCEPT
3946 {
3947 if (shift >= Active_Bits)
3948 {
3949 reset();
3950 }
3951 else
3952 {
3953 ibitset.shift_left_equals(pbuffer, Number_Of_Elements, shift);
3954 clear_unused_bits_in_msb();
3955 }
3956
3957 return *this;
3958 }
3959
3960 //*************************************************************************
3962 //*************************************************************************
3963 ETL_CONSTEXPR14 bitset_ext<Active_Bits, TElement, false>& operator >>=(size_t shift) ETL_NOEXCEPT
3964 {
3965 if (shift >= Active_Bits)
3966 {
3967 reset();
3968 }
3969 else
3970 {
3971 ibitset.shift_right_equals(pbuffer, Number_Of_Elements, shift);
3972 }
3973
3974 return *this;
3975 }
3976
3977 //*************************************************************************
3979 //*************************************************************************
3980 friend ETL_CONSTEXPR14 bool operator ==(const bitset_ext<Active_Bits, TElement, false>& lhs, const bitset_ext<Active_Bits, TElement, false>& rhs) ETL_NOEXCEPT
3981 {
3982 return etl::equal(lhs.pbuffer,
3983 lhs.pbuffer + lhs.Number_Of_Elements,
3984 rhs.pbuffer);
3985 }
3986
3987 //*************************************************************************
3989 //*************************************************************************
3990 ETL_CONSTEXPR14 void swap(etl::bitset_ext<Active_Bits, TElement, false>& other) ETL_NOEXCEPT
3991 {
3992 ibitset.swap(pbuffer, other.pbuffer, Number_Of_Elements);
3993 }
3994
3995 //*************************************************************************
3998 //*************************************************************************
3999 ETL_CONSTEXPR14 span_type span() ETL_NOEXCEPT
4000 {
4001 return span_type(pbuffer, Number_Of_Elements);
4002 }
4003
4004 //*************************************************************************
4007 //*************************************************************************
4008 ETL_CONSTEXPR14 const_span_type span() const ETL_NOEXCEPT
4009 {
4010 return const_span_type(pbuffer, Number_Of_Elements);
4011 }
4012
4013 private:
4014
4015 //*************************************************************************
4017 //*************************************************************************
4018 ETL_CONSTEXPR14 void clear_unused_bits_in_msb() ETL_NOEXCEPT
4019 {
4020 pbuffer[Number_Of_Elements - 1U] &= Top_Mask;
4021 }
4022
4024 element_type* pbuffer;
4025 };
4026
4027 template <size_t Active_Bits, typename TElement>
4028 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, false>::Bits_Per_Element;
4029
4030 template <size_t Active_Bits, typename TElement>
4031 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, false>::Number_Of_Elements;
4032
4033 template <size_t Active_Bits, typename TElement>
4034 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, false>::Allocated_Bits;
4035
4036 template <size_t Active_Bits, typename TElement>
4037 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, false>::element_type bitset_ext<Active_Bits, TElement, false>::All_Set_Element;
4038
4039 template <size_t Active_Bits, typename TElement>
4040 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, false>::element_type bitset_ext<Active_Bits, TElement, false>::All_Clear_Element;
4041
4042 template <size_t Active_Bits, typename TElement>
4043 ETL_CONSTANT size_t bitset_ext<Active_Bits, TElement, false>::Top_Mask_Shift;
4044
4045 template <size_t Active_Bits, typename TElement>
4046 ETL_CONSTANT typename bitset_ext<Active_Bits, TElement, false>::element_type bitset_ext<Active_Bits, TElement, false>::Top_Mask;
4047
4048 //***************************************************************************
4051 //***************************************************************************
4052 template<size_t Active_Bits, typename TElement, bool IsSingleElement>
4054 {
4055 return !(lhs == rhs);
4056 }
4057}
4058
4059//*************************************************************************
4061//*************************************************************************
4062template <size_t Active_Bits, typename TElement, bool IsSingleElement>
4064{
4065 lhs.swap(rhs);
4066}
4067
4068
4069#include "minmax_pop.h"
4070
4071#endif
ETL_CONSTEXPR14 void swap(etl::bitset< Active_Bits, TElement, IsSingleElement > &lhs, etl::bitset< Active_Bits, TElement, IsSingleElement > &rhs) ETL_NOEXCEPT
swap
Definition: bitset_new.h:2455
The specialisation that uses an array of the default element type.
Definition: bitset_new.h:1721
ETL_CONSTEXPR14 bitset< Active_Bits, TElement, false > & flip() ETL_NOEXCEPT
Flip all of the bits.
Definition: bitset_new.h:2124
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type value() const ETL_NOEXCEPT
Get as an integral value.
Definition: bitset_new.h:2025
Definition: bitset_new.h:851
ETL_CONSTEXPR14 bitset< Active_Bits, TElement, true > & flip() ETL_NOEXCEPT
Flip all of the bits.
Definition: bitset_new.h:1393
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type value() const ETL_NOEXCEPT
Get as an integral value.
Definition: bitset_new.h:1256
The specialisation that uses an array of the default element type.
Definition: bitset_new.h:3363
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type value() const ETL_NOEXCEPT
Get as an integral value.
Definition: bitset_new.h:3742
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type value() const ETL_NOEXCEPT
Get as an integral value.
Definition: bitset_new.h:2970
Definition: bitset_new.h:2469
bool operator~() const
Return the logical inverse of the bit.
Definition: bitset_legacy.h:225
bit_reference & operator=(bool b)
Assignment operator.
Definition: bitset_legacy.h:198
bit_reference & flip()
Flip the bit.
Definition: bitset_legacy.h:216
Definition: binary.h:2211
Definition: binary.h:2233
A templated set implementation that uses a fixed size buffer.
Definition: set.h:2502
Span - Fixed Extent.
Definition: span.h:62
Definition: array.h:88
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value &&etl::is_unsigned< T >::value &&(etl::integral_limits< T >::bits==16U), uint_least8_t >::type count_bits(T value)
Definition: binary.h:956
Definition: binary.h:392
ETL_CONSTEXPR14 bool test(const_pointer pbuffer, size_t number_of_elements, size_t position) const ETL_NOEXCEPT
Definition: bitset_new.h:167
ibitset & set()
Set all bits.
Definition: bitset_legacy.h:312
ibitset & reset()
Resets the bitset.
Definition: bitset_legacy.h:513
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition: bitset_legacy.h:1367
size_t find_first(bool state) const
Definition: bitset_legacy.h:653
size_t find_next(bool state, size_t position) const
Definition: bitset_legacy.h:664
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition: bitset_legacy.h:983
ETL_CONSTEXPR14 void flip(pointer pbuffer, size_t number_of_elements) ETL_NOEXCEPT
Flip all of the bits.
Definition: bitset_new.h:427
bool any() const
Are any of the bits set?
Definition: bitset_legacy.h:627
ETL_CONSTEXPR14 TString to_string(const_pointer pbuffer, size_t number_of_elements, size_t active_bits, typename TString::value_type zero, typename TString::value_type one) const
Returns a string representing the bitset.
Definition: bitset_new.h:572
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition: bitset_legacy.h:1448
ETL_CONSTEXPR14 size_t find_next(const_pointer pbuffer, size_t number_of_elements, size_t total_bits, bool state, size_t position) const ETL_NOEXCEPT
Definition: bitset_new.h:521
ETL_CONSTEXPR14 size_t count(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
Count the number of bits set.
Definition: bitset_new.h:151
bitset< MaxN > & flip()
Flip all of the bits.
Definition: bitset_legacy.h:1312
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition: bitset_legacy.h:1034
ETL_CONSTEXPR14 void swap(pointer pbuffer1, pointer pbuffer2, size_t number_of_elements)
Swap bitset buffers.
Definition: bitset_new.h:798
ETL_CONSTEXPR14 void or_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
or_equals
Definition: bitset_new.h:745
void swap(ibitset &other)
swap
Definition: bitset_legacy.h:953
bitset< MaxN > & reset()
Reset all of the bits.
Definition: bitset_legacy.h:1294
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset_legacy.h:1283
ETL_CONSTEXPR14 void and_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
and_equals
Definition: bitset_new.h:734
size_t count() const
Count the number of bits set.
Definition: bitset_legacy.h:265
TString to_string(typename TString::value_type zero=typename TString::value_type('0'), typename TString::value_type one=typename TString::value_type('1')) const
Returns a string representing the bitset.
Definition: bitset_legacy.h:1335
ETL_CONSTEXPR14 void xor_equals(pointer pbuffer, const_pointer pbuffer2, size_t number_of_elements) ETL_NOEXCEPT
xor_equals
Definition: bitset_new.h:756
ETL_CONSTEXPR14 void set(pointer pbuffer, size_t number_of_elements, size_t position, bool value=true) ETL_NOEXCEPT
Set the bit at the position.
Definition: bitset_new.h:190
ETL_CONSTEXPR14 etl::enable_if< etl::is_integral< T >::value, T >::type value(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
Get as a value.
Definition: bitset_new.h:378
ibitset & flip()
Flip all of the bits.
Definition: bitset_legacy.h:555
ETL_CONSTEXPR14 void reset(pointer pbuffer, size_t number_of_elements, size_t position) ETL_NOEXCEPT
Reset the bit at the position.
Definition: bitset_new.h:401
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition: bitset_legacy.h:1418
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition: bitset_legacy.h:1406
unsigned long to_ulong() const
Put to a unsigned long.
Definition: bitset_legacy.h:497
bool operator[](size_t position) const
Read [] operator.
Definition: bitset_legacy.h:729
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition: bitset_legacy.h:505
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition: bitset_legacy.h:1376
bitset()
Default constructor.
Definition: bitset_legacy.h:1116
bitset< MaxN > operator~() const
operator ~
Definition: bitset_legacy.h:1394
ETL_CONSTEXPR14 void initialise(pointer pbuffer, size_t number_of_elements, unsigned long long value) ETL_NOEXCEPT
Initialise from an unsigned long long.
Definition: bitset_new.h:767
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition: bitset_legacy.h:1427
ETL_CONSTEXPR14 void shift_left_equals(pointer pbuffer, size_t number_of_elements, size_t shift) ETL_NOEXCEPT
shift_left_equals
Definition: bitset_new.h:596
ETL_CONSTEXPR14 bool none(const_pointer pbuffer, size_t number_of_elements) const ETL_NOEXCEPT
Are none of the bits set?
Definition: bitset_new.h:492
ibitset & from_string(const char *text)
Set from a string.
Definition: bitset_legacy.h:362
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition: bitset_legacy.h:1354
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition: bitset_legacy.h:1385
ETL_CONSTEXPR14 size_t find_first(const_pointer pbuffer, size_t number_of_elements, size_t total_bits, bool state) const ETL_NOEXCEPT
Definition: bitset_new.h:510
bool none() const
Are none of the bits set?
Definition: bitset_legacy.h:635
bool test(size_t position) const
Definition: bitset_legacy.h:281
size_t size() const
The number of bits in the bitset.
Definition: bitset_legacy.h:257
ETL_CONSTEXPR14 void from_string(pointer pbuffer, size_t number_of_elements, size_t total_bits, const char *text) ETL_NOEXCEPT
Set from a string.
Definition: bitset_new.h:223
ETL_CONSTEXPR14 void shift_right_equals(pointer pbuffer, size_t number_of_elements, size_t shift) ETL_NOEXCEPT
shift_right_equals
Definition: bitset_new.h:665
bitset< MaxN > & set()
Set all of the bits.
Definition: bitset_legacy.h:1179
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition: bitset_legacy.h:1439
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition: bitset_legacy.h:1241
Definition: bitset_legacy.h:1102
Definition: bitset_legacy.h:85
Definition: bitset_new.h:136
Definition: bitset_legacy.h:127
Definition: bitset_new.h:107
Definition: bitset_legacy.h:141
Definition: exception.h:47
Definition: integral_limits.h:468
Definition: log.h:99
enable_if
Definition: type_traits_generator.h:1191
is_integral
Definition: type_traits_generator.h:1001
make_unsigned
Definition: type_traits_generator.h:1181
bitset_ext
Definition: absolute.h:38
etl::byte operator~(etl::byte b)
Not.
Definition: byte.h:313
etl::byte & operator^=(etl::byte &lhs, etl::byte rhs)
Exclusive or equals.
Definition: byte.h:305
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator<<=(etl::byte &b, TInteger shift)
Shift left equals.
Definition: byte.h:243
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition: byte.h:265
etl::byte & operator|=(etl::byte &lhs, etl::byte rhs)
Or equals.
Definition: byte.h:289
etl::byte operator&(etl::byte lhs, etl::byte rhs)
And.
Definition: byte.h:273
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
etl::enable_if<!etl::is_same< T, etl::istring >::value &&!etl::is_same< T, etl::string_view >::value, constetl::istring & >::type to_string(const T value, etl::istring &str, bool append=false)
Definition: to_string.h:50
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition: array.h:621
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition: byte.h:281
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
etl::enable_if< etl::is_integral< TInteger >::value, etl::byte & >::type operator>>=(etl::byte &b, TInteger shift)
Shift right equals.
Definition: byte.h:255
etl::byte & operator&=(etl::byte &lhs, etl::byte rhs)
And equals.
Definition: byte.h:297
ETL_CONSTEXPR size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:267
Definition: bitset_new.h:81