Embedded Template Library 1.0
bitset_legacy.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_BITSET_LEGACY_INCLUDED
32#define ETL_BITSET_LEGACY_INCLUDED
33
34#include "../platform.h"
35#include "../algorithm.h"
36#include "../iterator.h"
37#include "../integral_limits.h"
38#include "../algorithm.h"
39#include "../nullptr.h"
40#include "../log.h"
41#include "../exception.h"
42#include "../integral_limits.h"
43#include "../binary.h"
44#include "../char_traits.h"
45#include "../static_assert.h"
46#include "../error_handler.h"
47#include "../span.h"
48#include "../string.h"
49
50#include <string.h>
51#include <stddef.h>
52#include <stdint.h>
53
54#include "minmax_push.h"
55
56#if defined(ETL_COMPILER_KEIL)
57#pragma diag_suppress 1300
58#endif
59
60#if ETL_USING_CPP11
61 #define ETL_STR(x) x
62 #define ETL_STRL(x) L##x
63 #define ETL_STRu(x) u##x
64 #define ETL_STRU(x) U##x
65#else
66 #define ETL_STR(x) x
67 #define ETL_STRL(x) x
68 #define ETL_STRu(x) x
69 #define ETL_STRU(x) x
70#endif
71
72//*****************************************************************************
76//*****************************************************************************
77
78namespace etl
79{
80 //***************************************************************************
83 //***************************************************************************
85 {
86 public:
87
88 bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
89 : exception(reason_, file_name_, line_number_)
90 {
91 }
92 };
93
94 //***************************************************************************
97 //***************************************************************************
99 {
100 public:
101
102 bitset_nullptr(string_type file_name_, numeric_type line_number_)
103 : bitset_exception(ETL_ERROR_TEXT("bitset:null pointer", ETL_BITSET_FILE_ID"A"), file_name_, line_number_)
104 {
105 }
106 };
107
108 //***************************************************************************
111 //***************************************************************************
113 {
114 public:
115
116 bitset_type_too_small(string_type file_name_, numeric_type line_number_)
117 : bitset_exception(ETL_ERROR_TEXT("bitset:type_too_small", ETL_BITSET_FILE_ID"B"), file_name_, line_number_)
118 {
119 }
120 };
121
122 //***************************************************************************
125 //***************************************************************************
127 {
128 public:
129
130 bitset_overflow(string_type file_name_, numeric_type line_number_)
131 : bitset_exception(ETL_ERROR_TEXT("bitset:overflow", ETL_BITSET_FILE_ID"C"), file_name_, line_number_)
132 {
133 }
134 };
135
136 //*************************************************************************
139 //*************************************************************************
141 {
142 protected:
143
144 // The type used for each element in the array.
145#if !defined(ETL_BITSET_ELEMENT_TYPE)
146 #define ETL_BITSET_ELEMENT_TYPE uint_least8_t
147#endif
148
149 public:
150
151 typedef typename etl::make_unsigned<ETL_BITSET_ELEMENT_TYPE>::type element_type;
152 typedef element_type element_t; // Backward compatibility
153
154 static ETL_CONSTANT element_type ALL_SET = etl::integral_limits<element_type>::max;
155 static ETL_CONSTANT element_type ALL_CLEAR = 0;
156
157 static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
158
159#if ETL_USING_CPP11
160 typedef etl::span<element_type> span_type;
161 typedef etl::span<const element_type> const_span_type;
162#endif
163
164 enum
165 {
167 };
168
169 //*************************************************************************
171 //*************************************************************************
173 {
174 public:
175
176 friend class ibitset;
177
178 //*******************************
180 //*******************************
181 operator bool() const
182 {
183 return p_bitset->test(position);
184 }
185
186 //*******************************
188 //*******************************
190 : p_bitset(other.p_bitset)
191 , position(other.position)
192 {
193 }
194
195 //*******************************
197 //*******************************
199 {
200 p_bitset->set(position, b);
201 return *this;
202 }
203
204 //*******************************
206 //*******************************
208 {
209 p_bitset->set(position, bool(r));
210 return *this;
211 }
212
213 //*******************************
215 //*******************************
217 {
218 p_bitset->flip(position);
219 return *this;
220 }
221
222 //*******************************
224 //*******************************
225 bool operator~() const
226 {
227 return !p_bitset->test(position);
228 }
229
230 private:
231
232 //*******************************
234 //*******************************
236 : p_bitset(ETL_NULLPTR)
237 , position(0)
238 {
239 }
240
241 //*******************************
243 //*******************************
244 bit_reference(ibitset& r_bitset, size_t position_)
245 : p_bitset(&r_bitset)
246 , position(position_)
247 {
248 }
249
250 ibitset* p_bitset;
251 size_t position;
252 };
253
254 //*************************************************************************
256 //*************************************************************************
257 size_t size() const
258 {
259 return Active_Bits;
260 }
261
262 //*************************************************************************
264 //*************************************************************************
265 size_t count() const
266 {
267 size_t n = 0UL;
268
269 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
270 {
271 n += etl::count_bits(pdata[i]);
272 }
273
274 return n;
275 }
276
277 //*************************************************************************
280 //*************************************************************************
281 bool test(size_t position) const
282 {
283 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
284 size_t index;
285 element_type mask;
286
287 if (position >= Active_Bits)
288 {
289 return false;
290 }
291 if (Number_Of_Elements == 0)
292 {
293 return false;
294 }
295 else if (Number_Of_Elements == 1)
296 {
297 index = 0;
298 mask = element_type(1) << position;
299 }
300 else
301 {
302 index = position >> etl::log2<Bits_Per_Element>::value;
303 mask = element_type(1) << (position & (Bits_Per_Element - 1));
304 }
305
306 return (pdata[index] & mask) != 0;
307 }
308
309 //*************************************************************************
311 //*************************************************************************
313 {
314 ::memset(pdata, 0xFF, Number_Of_Elements);
315 pdata[Number_Of_Elements - 1U] = Top_Mask;
316
317 return *this;
318 }
319
320 //*************************************************************************
322 //*************************************************************************
323 ibitset& set(size_t position, bool value = true)
324 {
325 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
326 size_t index;
327 element_type bit;
328
329 if (position < Active_Bits)
330 {
331 if (Number_Of_Elements == 0)
332 {
333 return *this;
334 }
335 else if (Number_Of_Elements == 1)
336 {
337 index = 0;
338 bit = element_type(1) << position;
339 }
340 else
341 {
342 index = position >> etl::log2<Bits_Per_Element>::value;
343 bit = element_type(1) << (position & (Bits_Per_Element - 1));
344 }
345
346 if (value)
347 {
348 pdata[index] |= bit;
349 }
350 else
351 {
352 pdata[index] &= ~bit;
353 }
354 }
355
356 return *this;
357 }
358
359 //*************************************************************************
361 //*************************************************************************
362 ibitset& from_string(const char* text)
363 {
364 reset();
365
366 size_t i = etl::min(Active_Bits, etl::strlen(text));
367
368 while (i > 0)
369 {
370 set(--i, *text++ == ETL_STRL('1'));
371 }
372
373 return *this;
374 }
375
376 //*************************************************************************
378 //*************************************************************************
379 ibitset& from_string(const wchar_t* text)
380 {
381 reset();
382
383 size_t i = etl::min(Active_Bits, etl::strlen(text));
384
385 while (i > 0)
386 {
387 set(--i, *text++ == ETL_STRL('1'));
388 }
389
390 return *this;
391 }
392
393 //*************************************************************************
395 //*************************************************************************
396 ibitset& from_string(const char16_t* text)
397 {
398 reset();
399
400 size_t i = etl::min(Active_Bits, etl::strlen(text));
401
402 while (i > 0)
403 {
404 set(--i, *text++ == ETL_STRu('1'));
405 }
406
407 return *this;
408 }
409
410 //*************************************************************************
412 //*************************************************************************
413 ibitset& from_string(const char32_t* text)
414 {
415 reset();
416
417 size_t i = etl::min(Active_Bits, etl::strlen(text));
418
419 while (i > 0)
420 {
421 set(--i, *text++ == ETL_STRU('1'));
422 }
423
424 return *this;
425 }
426
427 //*************************************************************************
429 //*************************************************************************
430 ibitset& set(const char* text)
431 {
432 from_string(text);
433
434 return *this;
435 }
436
437 //*************************************************************************
439 //*************************************************************************
440 ibitset& set(const wchar_t* text)
441 {
442 from_string(text);
443
444 return *this;
445 }
446
447 //*************************************************************************
449 //*************************************************************************
450 ibitset& set(const char16_t* text)
451 {
452 from_string(text);
453
454 return *this;
455 }
456
457 //*************************************************************************
459 //*************************************************************************
460 ibitset& set(const char32_t* text)
461 {
462 from_string(text);
463
464 return *this;
465 }
466
467 //*************************************************************************
469 //*************************************************************************
470 template <typename T>
472 value() const
473 {
474 T v = T(0);
475
476 const bool OK = (sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
477
478 ETL_ASSERT_OR_RETURN_VALUE(OK, ETL_ERROR(etl::bitset_type_too_small), T(0));
479
480 if (OK)
481 {
482 uint_least8_t shift = 0U;
483
484 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
485 {
486 v |= T(typename etl::make_unsigned<T>::type(pdata[i]) << shift);
487 shift += uint_least8_t(Bits_Per_Element);
488 }
489 }
490
491 return v;
492 }
493
494 //*************************************************************************
496 //*************************************************************************
497 unsigned long to_ulong() const
498 {
499 return value<unsigned long>();
500 }
501
502 //*************************************************************************
504 //*************************************************************************
505 unsigned long long to_ullong() const
506 {
507 return value<unsigned long long>();
508 }
509
510 //*************************************************************************
512 //*************************************************************************
514 {
515 ::memset(pdata, 0x00, Number_Of_Elements);
516
517 return *this;
518 }
519
520 //*************************************************************************
522 //*************************************************************************
523 ibitset& reset(size_t position)
524 {
525 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
526 size_t index;
527 element_type bit;
528
529 if (position < Active_Bits)
530 {
531 if (Number_Of_Elements == 0)
532 {
533 return *this;
534 }
535 else if (Number_Of_Elements == 1)
536 {
537 index = 0;
538 bit = element_type(1) << position;
539 }
540 else
541 {
542 index = position >> etl::log2<Bits_Per_Element>::value;
543 bit = element_type(1) << (position & (Bits_Per_Element - 1));
544 }
545
546 pdata[index] &= ~bit;
547 }
548
549 return *this;
550 }
551
552 //*************************************************************************
554 //*************************************************************************
556 {
557 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
558 {
559 pdata[i] = ~pdata[i];
560 }
561
562 clear_unused_bits_in_msb();
563
564 return *this;
565 }
566
567 //*************************************************************************
569 //*************************************************************************
570 ibitset& flip(size_t position)
571 {
572 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
573 size_t index;
574 element_type bit;
575
576 if (Number_Of_Elements == 0)
577 {
578 return *this;
579 }
580 else if (Number_Of_Elements == 1)
581 {
582 index = 0;
583 bit = element_type(1) << position;
584 }
585 else
586 {
587 index = position >> log2<Bits_Per_Element>::value;
588 bit = element_type(1) << (position & (Bits_Per_Element - 1));
589 }
590
591 pdata[index] ^= bit;
592
593 return *this;
594 }
595
596 //*************************************************************************
597 // Are all the bits sets?
598 //*************************************************************************
599 bool all() const
600 {
601 if (Number_Of_Elements == 0UL)
602 {
603 return true;
604 }
605
606 // All but the last.
607 for (size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
608 {
609 if (pdata[i] != ALL_SET)
610 {
611 return false;
612 }
613 }
614
615 // The last.
616 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
617 {
618 return false;
619 }
620
621 return true;
622 }
623
624 //*************************************************************************
626 //*************************************************************************
627 bool any() const
628 {
629 return !none();
630 }
631
632 //*************************************************************************
634 //*************************************************************************
635 bool none() const
636 {
637 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
638 {
639 if (pdata[i] != 0)
640 {
641 return false;
642 }
643 }
644
645 return true;
646 }
647
648 //*************************************************************************
652 //*************************************************************************
653 size_t find_first(bool state) const
654 {
655 return find_next(state, 0);
656 }
657
658 //*************************************************************************
663 //*************************************************************************
664 size_t find_next(bool state, size_t position) const
665 {
666 // Where to start.
667 size_t index;
668 size_t bit;
669
670 if (Number_Of_Elements == 0)
671 {
672 return ibitset::npos;
673 }
674 else if (Number_Of_Elements == 1)
675 {
676 index = 0;
677 bit = position;
678 }
679 else
680 {
681 index = position >> log2<Bits_Per_Element>::value;
682 bit = position & (Bits_Per_Element - 1);
683 }
684
685 element_type mask = 1 << bit;
686
687 // For each element in the bitset...
688 while (index < Number_Of_Elements)
689 {
690 element_type value = pdata[index];
691
692 // Needs checking?
693 if ((state && (value != ALL_CLEAR)) ||
694 (!state && (value != ALL_SET)))
695 {
696 // For each bit in the element...
697 while ((bit < Bits_Per_Element) && (position < Active_Bits))
698 {
699 // Equal to the required state?
700 if (((value & mask) != 0) == state)
701 {
702 return position;
703 }
704
705 // Move on to the next bit.
706 mask <<= 1;
707 ++position;
708 ++bit;
709 }
710 }
711 else
712 {
713 position += (Bits_Per_Element - bit);
714 }
715
716 // Start at the beginning for all other elements.
717 bit = 0;
718 mask = 1;
719
720 ++index;
721 }
722
723 return ibitset::npos;
724 }
725
726 //*************************************************************************
728 //*************************************************************************
729 bool operator[] (size_t position) const
730 {
731 return test(position);
732 }
733
734 //*************************************************************************
736 //*************************************************************************
738 {
739 return bit_reference(*this, position);
740 }
741
742 //*************************************************************************
744 //*************************************************************************
746 {
747 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
748 {
749 pdata[i] &= other.pdata[i];
750 }
751
752 return *this;
753 }
754
755 //*************************************************************************
757 //*************************************************************************
759 {
760 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
761 {
762 pdata[i] |= other.pdata[i];
763 }
764
765 return *this;
766 }
767
768 //*************************************************************************
770 //*************************************************************************
772 {
773 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
774 {
775 pdata[i] ^= other.pdata[i];
776 }
777
778 return *this;
779 }
780
781 //*************************************************************************
783 //*************************************************************************
784 ibitset& operator<<=(size_t shift)
785 {
786 if (shift >= Active_Bits)
787 {
788 reset();
789 }
790 else if (Number_Of_Elements != 0UL)
791 {
792 // Just one element?
793 if (Number_Of_Elements == 1UL)
794 {
795 pdata[0] <<= shift;
796 }
797 else if (shift == Bits_Per_Element)
798 {
799 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
800 pdata[0] = 0;
801 }
802 else
803 {
804 // The place where the elements are split when shifting.
805 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
806
807 // Where we are shifting from.
808 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
809
810 // Where we are shifting to.
811 int dst_index = int(Number_Of_Elements - 1U);
812
813 // Shift control constants.
814 const size_t lsb_shift = Bits_Per_Element - split_position;
815 const size_t msb_shift = split_position;
816
817 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
819 const element_type lsb_shifted_mask = element_type(lsb_mask << lsb_shift);
820
821 // First lsb.
822 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
823 pdata[dst_index] = lsb;
824 --src_index;
825
826 // Now do the shifting.
827 while (src_index >= 0)
828 {
829 // Shift msb.
830 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
831 pdata[dst_index] = pdata[dst_index] | msb;
832 --dst_index;
833
834 // Shift lsb.
835 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
836 pdata[dst_index] = lsb;
837 --src_index;
838 }
839
840 // Clear the remaining bits.
841 // First lsb.
842 pdata[dst_index] &= lsb_shifted_mask;
843 --dst_index;
844
845 // The other remaining bytes on the right.
846 while (dst_index >= 0)
847 {
848 pdata[dst_index] = 0;
849 --dst_index;
850 }
851 }
852
853 // Truncate any bits shifted to the left.
854 clear_unused_bits_in_msb();
855 }
856
857 return *this;
858 }
859
860 //*************************************************************************
862 //*************************************************************************
863 ibitset& operator>>=(size_t shift)
864 {
865 if (shift >= Active_Bits)
866 {
867 reset();
868 }
869 else if (Number_Of_Elements != 0UL)
870 {
871 // Just one element?
872 if (Number_Of_Elements == 1UL)
873 {
874 pdata[0] >>= shift;
875 }
876 // Shift is the size of an element?
877 else if (shift == Bits_Per_Element)
878 {
879 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
880 pdata[Number_Of_Elements - 1U] = 0;
881 }
882 else
883 {
884 // The place where the elements are split when shifting.
885 const size_t split_position = shift % Bits_Per_Element;
886
887 // Where we are shifting from.
888 int src_index = int(shift / Bits_Per_Element);
889
890 // Where we are shifting to.
891 int dst_index = 0;
892
893 // Shift control constants.
894 const size_t lsb_shift = Bits_Per_Element - split_position;
895 const size_t msb_shift = split_position;
896
897 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
899 const element_type msb_shifted_mask = element_type(msb_mask >> msb_shift);
900
901 // Now do the shifting.
902 while (src_index < int(Number_Of_Elements - 1))
903 {
904 // Shift msb.
905 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
906 ++src_index;
907
908 // Shift lsb.
909 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
910
911 // Combine them.
912 pdata[dst_index] = lsb | msb;
913 ++dst_index;
914 }
915
916 // Final msb.
917 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
918 pdata[dst_index] = msb;
919
920 // Clear the remaining bits.
921 // First msb.
922 pdata[dst_index] &= msb_shifted_mask;
923 ++dst_index;
924
925 // The other remaining bytes.
926 while (dst_index < int(Number_Of_Elements))
927 {
928 pdata[dst_index] = 0;
929 ++dst_index;
930 }
931 }
932 }
933
934 return *this;
935 }
936
937 //*************************************************************************
939 //*************************************************************************
941 {
942 if (this != &other)
943 {
944 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
945 }
946
947 return *this;
948 }
949
950 //*************************************************************************
952 //*************************************************************************
953 void swap(ibitset& other)
954 {
955 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
956 }
957
958#if ETL_USING_CPP11
959 //*************************************************************************
962 //*************************************************************************
963 span_type span()
964 {
965 return span_type(pdata, pdata + Number_Of_Elements);
966 }
967
968 //*************************************************************************
971 //*************************************************************************
972 const_span_type span() const
973 {
974 return const_span_type(pdata, pdata + Number_Of_Elements);
975 }
976#endif
977
978 protected:
979
980 //*************************************************************************
982 //*************************************************************************
983 ibitset& initialise(unsigned long long value)
984 {
985 reset();
986
987 const size_t Shift = (integral_limits<unsigned long long>::bits <= (int)Bits_Per_Element) ? 0 : Bits_Per_Element;
988
989 // Can we do it in one hit?
990 if (Shift == 0)
991 {
992 pdata[0] = element_type(value);
993 }
994 else
995 {
996 size_t i = 0UL;
997
998 while ((value != 0) && (i < Number_Of_Elements))
999 {
1000 pdata[i++] = value & ALL_SET;
1001 value = value >> Shift;
1002 }
1003 }
1004
1005 clear_unused_bits_in_msb();
1006
1007 return *this;
1008 }
1009
1010 //*************************************************************************
1012 //*************************************************************************
1013 void invert()
1014 {
1015 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
1016 {
1017 pdata[i] = ~pdata[i];
1018 }
1019
1020 clear_unused_bits_in_msb();
1021 }
1022
1023 //*************************************************************************
1025 //*************************************************************************
1027 {
1028 return bit_reference(*this, position);
1029 }
1030
1031 //*************************************************************************
1033 //*************************************************************************
1034 ibitset(size_t nbits_, size_t size_, element_type* pdata_)
1035 : Active_Bits(nbits_)
1036 , Number_Of_Elements(size_)
1037 , pdata(pdata_)
1038 {
1039 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1040 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1041 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1042 }
1043
1044 //*************************************************************************
1046 //*************************************************************************
1047 static bool is_equal(const ibitset& lhs, const ibitset&rhs)
1048 {
1049 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1050 }
1051
1052 element_type Top_Mask;
1053
1054 private:
1055
1056 //*************************************************************************
1058 //*************************************************************************
1059 void clear_unused_bits_in_msb()
1060 {
1061 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1062 }
1063
1064 // Disable copy construction.
1065 ibitset(const ibitset&);
1066
1067 const size_t Active_Bits;
1068 const size_t Number_Of_Elements;
1069 element_type* pdata;
1070
1071 //*************************************************************************
1073 //*************************************************************************
1074#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1075 public:
1076 virtual ~ibitset()
1077 {
1078 }
1079#else
1080 protected:
1082 {
1083 }
1084#endif
1085 };
1086
1087 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1088
1089 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1090
1091 ETL_CONSTANT size_t ibitset::Bits_Per_Element;
1092
1093 //*************************************************************************
1099 //*************************************************************************
1100 template <size_t MaxN>
1101 class bitset : public etl::ibitset
1102 {
1103
1104 static ETL_CONSTANT size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1105
1106 public:
1107
1108 static ETL_CONSTANT size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1109 static ETL_CONSTANT size_t Allocated_Bits = ALLOCATED_BITS;
1110
1111 public:
1112
1113 //*************************************************************************
1115 //*************************************************************************
1117 : etl::ibitset(MaxN, Array_Size, data)
1118 {
1119 reset();
1120 }
1121
1122 //*************************************************************************
1124 //*************************************************************************
1125 bitset(const bitset<MaxN>& other)
1126 : etl::ibitset(MaxN, Array_Size, data)
1127 {
1128 etl::copy_n(other.data, Array_Size, data);
1129 }
1130
1131 //*************************************************************************
1133 //*************************************************************************
1134 bitset(unsigned long long value)
1135 : etl::ibitset(MaxN, Array_Size, data)
1136 {
1138 }
1139
1140 //*************************************************************************
1142 //*************************************************************************
1143 bitset(const char* text)
1144 : etl::ibitset(MaxN, Array_Size, data)
1145 {
1146 set(text);
1147 }
1148
1149 //*************************************************************************
1151 //*************************************************************************
1152 bitset(const wchar_t* text)
1153 : etl::ibitset(MaxN, Array_Size, data)
1154 {
1155 set(text);
1156 }
1157
1158 //*************************************************************************
1160 //*************************************************************************
1161 bitset(const char16_t* text)
1162 : etl::ibitset(MaxN, Array_Size, data)
1163 {
1164 set(text);
1165 }
1166
1167 //*************************************************************************
1169 //*************************************************************************
1170 bitset(const char32_t* text)
1171 : etl::ibitset(MaxN, Array_Size, data)
1172 {
1173 set(text);
1174 }
1175
1176 //*************************************************************************
1178 //*************************************************************************
1180 {
1182 return *this;
1183 }
1184
1185 //*************************************************************************
1187 //*************************************************************************
1188 bitset<MaxN>& set(size_t position, bool value = true)
1189 {
1190 etl::ibitset::set(position, value);
1191 return *this;
1192 }
1193
1194 //*************************************************************************
1196 //*************************************************************************
1197 bitset<MaxN>& set(const char* text)
1198 {
1199 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
1200 etl::ibitset::set(text);
1201
1202 return *this;
1203 }
1204
1205 //*************************************************************************
1207 //*************************************************************************
1208 bitset<MaxN>& set(const wchar_t* text)
1209 {
1210 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
1211 etl::ibitset::set(text);
1212
1213 return *this;
1214 }
1215
1216 //*************************************************************************
1218 //*************************************************************************
1219 bitset<MaxN>& set(const char16_t* text)
1220 {
1221 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
1222 etl::ibitset::set(text);
1223
1224 return *this;
1225 }
1226
1227 //*************************************************************************
1229 //*************************************************************************
1230 bitset<MaxN>& set(const char32_t* text)
1231 {
1232 ETL_ASSERT_OR_RETURN_VALUE(text != 0, ETL_ERROR(bitset_nullptr), *this);
1233 etl::ibitset::set(text);
1234
1235 return *this;
1236 }
1237
1238 //*************************************************************************
1240 //*************************************************************************
1241 bitset<MaxN>& from_string(const char* text)
1242 {
1244
1245 return *this;
1246 }
1247
1248 //*************************************************************************
1250 //*************************************************************************
1251 bitset<MaxN>& from_string(const wchar_t* text)
1252 {
1254
1255 return *this;
1256 }
1257
1258 //*************************************************************************
1260 //*************************************************************************
1261 bitset<MaxN>& from_string(const char16_t* text)
1262 {
1264
1265 return *this;
1266 }
1267
1268 //*************************************************************************
1270 //*************************************************************************
1271 bitset<MaxN>& from_string(const char32_t* text)
1272 {
1274
1275 return *this;
1276 }
1277
1278 //*************************************************************************
1280 //*************************************************************************
1281 template <typename T>
1283 value() const
1284 {
1285 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
1286 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element), "Type too small");
1287
1288 return ibitset::value<T>();
1289 }
1290
1291 //*************************************************************************
1293 //*************************************************************************
1295 {
1297 return *this;
1298 }
1299
1300 //*************************************************************************
1302 //*************************************************************************
1303 bitset<MaxN>& reset(size_t position)
1304 {
1305 etl::ibitset::reset(position);
1306 return *this;
1307 }
1308
1309 //*************************************************************************
1311 //*************************************************************************
1313 {
1314 ibitset::flip();
1315 return *this;
1316 }
1317
1318 //*************************************************************************
1320 //*************************************************************************
1321 bitset<MaxN>& flip(size_t position)
1322 {
1323 etl::ibitset::flip(position);
1324 return *this;
1325 }
1326
1327 //*************************************************************************
1329 //*************************************************************************
1330#if ETL_USING_CPP11
1331 template <typename TString = etl::string<MaxN>>
1332#else
1333 template <typename TString>
1334#endif
1335 TString to_string(typename TString::value_type zero = typename TString::value_type('0'), typename TString::value_type one = typename TString::value_type('1')) const
1336 {
1337 TString result;
1338
1339 result.resize(MaxN, '\0');
1340
1341 ETL_ASSERT_OR_RETURN_VALUE((result.size() == MaxN), ETL_ERROR(etl::bitset_overflow), result);
1342
1343 for (size_t i = MaxN; i > 0; --i)
1344 {
1345 result[MaxN - i] = test(i - 1) ? one : zero;
1346 }
1347
1348 return result;
1349 }
1350
1351 //*************************************************************************
1353 //*************************************************************************
1355 {
1356 if (this != &other)
1357 {
1358 etl::copy_n(other.data, Array_Size, data);
1359 }
1360
1361 return *this;
1362 }
1363
1364 //*************************************************************************
1366 //*************************************************************************
1368 {
1370 return *this;
1371 }
1372
1373 //*************************************************************************
1375 //*************************************************************************
1377 {
1379 return *this;
1380 }
1381
1382 //*************************************************************************
1384 //*************************************************************************
1386 {
1387 ibitset::operator ^=(other);
1388 return *this;
1389 }
1390
1391 //*************************************************************************
1393 //*************************************************************************
1395 {
1396 etl::bitset<MaxN> temp(*this);
1397
1398 temp.invert();
1399
1400 return temp;
1401 }
1402
1403 //*************************************************************************
1405 //*************************************************************************
1406 bitset<MaxN> operator<<(size_t shift) const
1407 {
1408 etl::bitset<MaxN> temp(*this);
1409
1410 temp <<= shift;
1411
1412 return temp;
1413 }
1414
1415 //*************************************************************************
1417 //*************************************************************************
1418 bitset<MaxN>& operator<<=(size_t shift)
1419 {
1421 return *this;
1422 }
1423
1424 //*************************************************************************
1426 //*************************************************************************
1427 bitset<MaxN> operator>>(size_t shift) const
1428 {
1429 bitset<MaxN> temp(*this);
1430
1431 temp >>= shift;
1432
1433 return temp;
1434 }
1435
1436 //*************************************************************************
1438 //*************************************************************************
1440 {
1442 return *this;
1443 }
1444
1445 //*************************************************************************
1447 //*************************************************************************
1448 friend bool operator == (const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
1449 {
1450 return etl::ibitset::is_equal(lhs, rhs);
1451 }
1452
1453 private:
1454
1455 element_type data[Array_Size > 0U ? Array_Size : 1U];
1456 };
1457
1458 template <size_t MaxN>
1459 ETL_CONSTANT size_t bitset<MaxN>::ALLOCATED_BITS;
1460
1461 template <size_t MaxN>
1462 ETL_CONSTANT size_t bitset<MaxN>::Allocated_Bits;
1463
1464 //***************************************************************************
1467 //***************************************************************************
1468 template <size_t MaxN>
1470 {
1471 bitset<MaxN> temp(lhs);
1472 temp &= rhs;
1473 return temp;
1474 }
1475
1476 //***************************************************************************
1479 //***************************************************************************
1480 template<size_t MaxN>
1482 {
1483 bitset<MaxN> temp(lhs);
1484 temp |= rhs;
1485 return temp;
1486 }
1487
1488 //***************************************************************************
1491 //***************************************************************************
1492 template<size_t MaxN>
1494 {
1495 bitset<MaxN> temp(lhs);
1496 temp ^= rhs;
1497 return temp;
1498 }
1499
1500 //***************************************************************************
1503 //***************************************************************************
1504 template<size_t MaxN>
1505 bool operator != (const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
1506 {
1507 return !(lhs == rhs);
1508 }
1509}
1510
1511//*************************************************************************
1513//*************************************************************************
1514template <size_t MaxN>
1516{
1517 lhs.swap(rhs);
1518}
1519
1520#include "minmax_pop.h"
1521
1522#endif
void swap(etl::bitset< MaxN > &lhs, etl::bitset< MaxN > &rhs)
swap
Definition: bitset_legacy.h:1515
The reference type returned.
Definition: bitset_legacy.h:173
bool operator~() const
Return the logical inverse of the bit.
Definition: bitset_legacy.h:225
bit_reference(const bit_reference &other)
Copy constructor.
Definition: bitset_legacy.h:189
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
Span - Fixed Extent.
Definition: span.h:62
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::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset_legacy.h:472
ibitset & set()
Set all bits.
Definition: bitset_legacy.h:312
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset_legacy.h:1188
ibitset & reset()
Resets the bitset.
Definition: bitset_legacy.h:513
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition: bitset_legacy.h:1367
bitset(const char16_t *text)
Construct from a string.
Definition: bitset_legacy.h:1161
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
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset_legacy.h:396
bool any() const
Are any of the bits set?
Definition: bitset_legacy.h:627
~ibitset()
Destructor.
Definition: bitset_legacy.h:1081
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition: bitset_legacy.h:1448
bitset< MaxN > & flip()
Flip all of the bits.
Definition: bitset_legacy.h:1312
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition: bitset_legacy.h:1026
ibitset & operator|=(const ibitset &other)
operator |=
Definition: bitset_legacy.h:758
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition: bitset_legacy.h:1125
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition: bitset_legacy.h:1034
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition: bitset_legacy.h:1230
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition: bitset_legacy.h:1208
bitset(const char *text)
Construct from a string.
Definition: bitset_legacy.h:1143
void swap(ibitset &other)
swap
Definition: bitset_legacy.h:953
ibitset & operator=(const ibitset &other)
operator =
Definition: bitset_legacy.h:940
bitset< MaxN > & reset()
Reset all of the bits.
Definition: bitset_legacy.h:1294
bitset(const wchar_t *text)
Construct from a string.
Definition: bitset_legacy.h:1152
ibitset & operator>>=(size_t shift)
operator >>=
Definition: bitset_legacy.h:863
ibitset & operator^=(const ibitset &other)
operator ^=
Definition: bitset_legacy.h:771
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset_legacy.h:1283
bitset(unsigned long long value)
Construct from a value.
Definition: bitset_legacy.h:1134
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition: bitset_legacy.h:1303
size_t count() const
Count the number of bits set.
Definition: bitset_legacy.h:265
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset_legacy.h:379
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
ibitset & set(const char32_t *text)
Set from a u32string.
Definition: bitset_legacy.h:460
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset_legacy.h:413
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset_legacy.h:323
ibitset & flip()
Flip all of the bits.
Definition: bitset_legacy.h:555
bitset(const char32_t *text)
Construct from a string.
Definition: bitset_legacy.h:1170
ibitset & operator&=(const ibitset &other)
operator &=
Definition: bitset_legacy.h:745
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset_legacy.h:1251
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
void invert()
Invert.
Definition: bitset_legacy.h:1013
bitset()
Default constructor.
Definition: bitset_legacy.h:1116
bitset< MaxN > operator~() const
operator ~
Definition: bitset_legacy.h:1394
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition: bitset_legacy.h:1427
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset_legacy.h:1261
ibitset & reset(size_t position)
Reset the bit at the position.
Definition: bitset_legacy.h:523
bitset< MaxN > & set(const char *text)
Set from a string.
Definition: bitset_legacy.h:1197
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
ibitset & set(const char16_t *text)
Set from a u16string.
Definition: bitset_legacy.h:450
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition: bitset_legacy.h:440
ibitset & operator<<=(size_t shift)
operator <<=
Definition: bitset_legacy.h:784
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition: bitset_legacy.h:1385
bool none() const
Are none of the bits set?
Definition: bitset_legacy.h:635
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset_legacy.h:1271
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
bitset< MaxN > & set(const char16_t *text)
Set from a string.
Definition: bitset_legacy.h:1219
ibitset & flip(size_t position)
Flip the bit at the position.
Definition: bitset_legacy.h:570
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition: bitset_legacy.h:1321
ibitset & set(const char *text)
Set from a string.
Definition: bitset_legacy.h:430
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
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition: bitset_legacy.h:1047
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_legacy.h:99
Definition: bitset_legacy.h:127
Definition: bitset_legacy.h:113
Definition: bitset_legacy.h:141
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition: exception.h:69
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
bitset_ext
Definition: absolute.h:38
etl::byte operator|(etl::byte lhs, etl::byte rhs)
Or.
Definition: byte.h:265
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::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition: byte.h:281
ETL_CONSTEXPR size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:267