Embedded Template Library 1.0
Loading...
Searching...
No Matches
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_)
90 {
91 }
92 };
93
94 //***************************************************************************
97 //***************************************************************************
99 {
100 public:
101
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
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
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 etl::fill_n(pdata, Number_Of_Elements - 1U, ALL_SET);
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 if (text == ETL_NULLPTR)
433 {
434 reset();
435 }
436 else
437 {
438 from_string(text);
439 }
440
441 return *this;
442 }
443
444 //*************************************************************************
446 //*************************************************************************
447 ibitset& set(const wchar_t* text)
448 {
449 if (text == ETL_NULLPTR)
450 {
451 reset();
452 }
453 else
454 {
455 from_string(text);
456 }
457
458 return *this;
459 }
460
461 //*************************************************************************
463 //*************************************************************************
464 ibitset& set(const char16_t* text)
465 {
466 if (text == ETL_NULLPTR)
467 {
468 reset();
469 }
470 else
471 {
472 from_string(text);
473 }
474
475 return *this;
476 }
477
478 //*************************************************************************
480 //*************************************************************************
481 ibitset& set(const char32_t* text)
482 {
483 if (text == ETL_NULLPTR)
484 {
485 reset();
486 }
487 else
488 {
489 from_string(text);
490 }
491
492 return *this;
493 }
494
495 //*************************************************************************
497 //*************************************************************************
498 template <typename T>
500 value() const
501 {
502 T v = T(0);
503
504 const bool OK = (sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
505
506 ETL_ASSERT_OR_RETURN_VALUE(OK, ETL_ERROR(etl::bitset_type_too_small), T(0));
507
508 if (OK)
509 {
511
512 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
513 {
514 v |= T(typename etl::make_unsigned<T>::type(pdata[i]) << shift);
515 shift += uint_least8_t(Bits_Per_Element);
516 }
517 }
518
519 return v;
520 }
521
522 //*************************************************************************
524 //*************************************************************************
525 unsigned long to_ulong() const
526 {
527 return value<unsigned long>();
528 }
529
530 //*************************************************************************
532 //*************************************************************************
533 unsigned long long to_ullong() const
534 {
536 }
537
538 //*************************************************************************
540 //*************************************************************************
542 {
543 etl::fill_n(pdata, Number_Of_Elements, ALL_CLEAR);
544
545 return *this;
546 }
547
548 //*************************************************************************
550 //*************************************************************************
551 ibitset& reset(size_t position)
552 {
553 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
554 size_t index;
555 element_type bit;
556
557 if (position < Active_Bits)
558 {
559 if (Number_Of_Elements == 0)
560 {
561 return *this;
562 }
563 else if (Number_Of_Elements == 1)
564 {
565 index = 0;
566 bit = element_type(1) << position;
567 }
568 else
569 {
570 index = position >> etl::log2<Bits_Per_Element>::value;
571 bit = element_type(1) << (position & (Bits_Per_Element - 1));
572 }
573
574 pdata[index] &= ~bit;
575 }
576
577 return *this;
578 }
579
580 //*************************************************************************
582 //*************************************************************************
584 {
585 etl::transform_n(pdata,
586 Number_Of_Elements,
587 pdata,
589
590 clear_unused_bits_in_msb();
591
592 return *this;
593 }
594
595 //*************************************************************************
597 //*************************************************************************
598 ibitset& flip(size_t position)
599 {
600 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
601 size_t index;
602 element_type bit;
603
604 if (Number_Of_Elements == 0)
605 {
606 return *this;
607 }
608 else if (Number_Of_Elements == 1)
609 {
610 index = 0;
611 bit = element_type(1) << position;
612 }
613 else
614 {
615 index = position >> log2<Bits_Per_Element>::value;
616 bit = element_type(1) << (position & (Bits_Per_Element - 1));
617 }
618
619 pdata[index] ^= bit;
620
621 return *this;
622 }
623
624 //*************************************************************************
625 // Are all the bits sets?
626 //*************************************************************************
627 bool all() const
628 {
629 if (Number_Of_Elements == 0UL)
630 {
631 return true;
632 }
633
634 // All but the last.
635 for (size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
636 {
637 if (pdata[i] != ALL_SET)
638 {
639 return false;
640 }
641 }
642
643 // The last.
644 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
645 {
646 return false;
647 }
648
649 return true;
650 }
651
652 //*************************************************************************
654 //*************************************************************************
655 bool any() const
656 {
657 return !none();
658 }
659
660 //*************************************************************************
662 //*************************************************************************
663 bool none() const
664 {
665 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
666 {
667 if (pdata[i] != 0)
668 {
669 return false;
670 }
671 }
672
673 return true;
674 }
675
676 //*************************************************************************
680 //*************************************************************************
681 size_t find_first(bool state) const
682 {
683 return find_next(state, 0);
684 }
685
686 //*************************************************************************
691 //*************************************************************************
692 size_t find_next(bool state, size_t position) const
693 {
694 // Where to start.
695 size_t index;
696 size_t bit;
697
698 if (Number_Of_Elements == 0)
699 {
700 return ibitset::npos;
701 }
702 else if (Number_Of_Elements == 1)
703 {
704 index = 0;
705 bit = position;
706 }
707 else
708 {
709 index = position >> log2<Bits_Per_Element>::value;
710 bit = position & (Bits_Per_Element - 1);
711 }
712
713 element_type mask = 1 << bit;
714
715 // For each element in the bitset...
716 while (index < Number_Of_Elements)
717 {
718 element_type value = pdata[index];
719
720 // Needs checking?
721 if ((state && (value != ALL_CLEAR)) ||
722 (!state && (value != ALL_SET)))
723 {
724 // For each bit in the element...
725 while ((bit < Bits_Per_Element) && (position < Active_Bits))
726 {
727 // Equal to the required state?
728 if (((value & mask) != 0) == state)
729 {
730 return position;
731 }
732
733 // Move on to the next bit.
734 mask <<= 1;
735 ++position;
736 ++bit;
737 }
738 }
739 else
740 {
741 position += (Bits_Per_Element - bit);
742 }
743
744 // Start at the beginning for all other elements.
745 bit = 0;
746 mask = 1;
747
748 ++index;
749 }
750
751 return ibitset::npos;
752 }
753
754 //*************************************************************************
756 //*************************************************************************
757 bool operator[] (size_t position) const
758 {
759 return test(position);
760 }
761
762 //*************************************************************************
764 //*************************************************************************
766 {
767 return bit_reference(*this, position);
768 }
769
770 //*************************************************************************
772 //*************************************************************************
774 {
775 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
776 {
777 pdata[i] &= other.pdata[i];
778 }
779
780 return *this;
781 }
782
783 //*************************************************************************
785 //*************************************************************************
787 {
788 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
789 {
790 pdata[i] |= other.pdata[i];
791 }
792
793 return *this;
794 }
795
796 //*************************************************************************
798 //*************************************************************************
800 {
801 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
802 {
803 pdata[i] ^= other.pdata[i];
804 }
805
806 return *this;
807 }
808
809 //*************************************************************************
811 //*************************************************************************
813 {
814 if (shift >= Active_Bits)
815 {
816 reset();
817 }
818 else if (Number_Of_Elements != 0UL)
819 {
820 // Just one element?
821 if (Number_Of_Elements == 1UL)
822 {
823 pdata[0] <<= shift;
824 }
825 else if (shift == Bits_Per_Element)
826 {
827 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
828 pdata[0] = 0;
829 }
830 else
831 {
832 // The place where the elements are split when shifting.
833 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
834
835 // Where we are shifting from.
836 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
837
838 // Where we are shifting to.
839 int dst_index = int(Number_Of_Elements - 1U);
840
841 // Shift control constants.
842 const size_t lsb_shift = Bits_Per_Element - split_position;
843 const size_t msb_shift = split_position;
844
845 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
847 const element_type lsb_shifted_mask = element_type(lsb_mask << lsb_shift);
848
849 // First lsb.
850 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
851 pdata[dst_index] = lsb;
852 --src_index;
853
854 // Now do the shifting.
855 while (src_index >= 0)
856 {
857 // Shift msb.
858 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
859 pdata[dst_index] = pdata[dst_index] | msb;
860 --dst_index;
861
862 // Shift lsb.
863 lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
864 pdata[dst_index] = lsb;
865 --src_index;
866 }
867
868 // Clear the remaining bits.
869 // First lsb.
870 pdata[dst_index] &= lsb_shifted_mask;
871 --dst_index;
872
873 // The other remaining elements.
874 for (int i = 0; i <= dst_index; ++i)
875 {
876 pdata[i] = 0;
877 }
878 }
879
880 // Truncate any bits shifted to the left.
881 clear_unused_bits_in_msb();
882 }
883
884 return *this;
885 }
886
887 //*************************************************************************
889 //*************************************************************************
891 {
892 if (shift >= Active_Bits)
893 {
894 reset();
895 }
896 else if (Number_Of_Elements != 0UL)
897 {
898 // Just one element?
899 if (Number_Of_Elements == 1UL)
900 {
901 pdata[0] >>= shift;
902 }
903 // Shift is the size of an element?
904 else if (shift == Bits_Per_Element)
905 {
906 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
907 pdata[Number_Of_Elements - 1U] = 0;
908 }
909 else
910 {
911 // The place where the elements are split when shifting.
912 const size_t split_position = shift % Bits_Per_Element;
913
914 // Where we are shifting from.
915 int src_index = int(shift / Bits_Per_Element);
916
917 // Where we are shifting to.
918 int dst_index = 0;
919
920 // Shift control constants.
921 const size_t lsb_shift = Bits_Per_Element - split_position;
922 const size_t msb_shift = split_position;
923
924 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
926 const element_type msb_shifted_mask = element_type(msb_mask >> msb_shift);
927
928 // Now do the shifting.
929 while (src_index < int(Number_Of_Elements - 1))
930 {
931 // Shift msb.
932 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
933 ++src_index;
934
935 // Shift lsb.
936 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
937
938 // Combine them.
939 pdata[dst_index] = lsb | msb;
940 ++dst_index;
941 }
942
943 // Final msb.
944 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
945 pdata[dst_index] = msb;
946
947 // Clear the remaining bits.
948 // First msb.
949 pdata[dst_index] &= msb_shifted_mask;
950 ++dst_index;
951
952 // The other remaining elements.
953 for (int i = dst_index; i < int(Number_Of_Elements); ++i)
954 {
955 pdata[i] = 0;
956 }
957 }
958 }
959
960 return *this;
961 }
962
963 //*************************************************************************
965 //*************************************************************************
967 {
968 if (this != &other)
969 {
970 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
971 }
972
973 return *this;
974 }
975
976 //*************************************************************************
978 //*************************************************************************
980 {
981 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
982 }
983
984#if ETL_USING_CPP11
985 //*************************************************************************
988 //*************************************************************************
989 span_type span()
990 {
991 return span_type(pdata, pdata + Number_Of_Elements);
992 }
993
994 //*************************************************************************
997 //*************************************************************************
998 const_span_type span() const
999 {
1000 return const_span_type(pdata, pdata + Number_Of_Elements);
1001 }
1002#endif
1003
1004 protected:
1005
1006 //*************************************************************************
1008 //*************************************************************************
1009 ibitset& initialise(unsigned long long value)
1010 {
1011 reset();
1012
1013 const size_t Shift = (integral_limits<unsigned long long>::bits <= (int)Bits_Per_Element) ? 0 : Bits_Per_Element;
1014
1015 // Can we do it in one hit?
1016 if (Shift == 0)
1017 {
1018 pdata[0] = element_type(value);
1019 }
1020 else
1021 {
1022 size_t i = 0UL;
1023
1024 while ((value != 0) && (i < Number_Of_Elements))
1025 {
1026 pdata[i++] = value & ALL_SET;
1027 value = value >> Shift;
1028 }
1029 }
1030
1031 clear_unused_bits_in_msb();
1032
1033 return *this;
1034 }
1035
1036 //*************************************************************************
1038 //*************************************************************************
1039 void invert()
1040 {
1041 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
1042 {
1043 pdata[i] = ~pdata[i];
1044 }
1045
1046 clear_unused_bits_in_msb();
1047 }
1048
1049 //*************************************************************************
1051 //*************************************************************************
1053 {
1054 return bit_reference(*this, position);
1055 }
1056
1057 //*************************************************************************
1059 //*************************************************************************
1060 ibitset(size_t nbits_, size_t size_, element_type* pdata_)
1061 : Active_Bits(nbits_)
1062 , Number_Of_Elements(size_)
1063 , pdata(pdata_)
1064 {
1065 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1066 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1067 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1068 }
1069
1070 //*************************************************************************
1072 //*************************************************************************
1073 static bool is_equal(const ibitset& lhs, const ibitset&rhs)
1074 {
1075 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1076 }
1077
1078 element_type Top_Mask;
1079
1080 private:
1081
1082 //*************************************************************************
1084 //*************************************************************************
1085 void clear_unused_bits_in_msb()
1086 {
1087 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1088 }
1089
1090 // Disable copy construction.
1091 ibitset(const ibitset&);
1092
1093 const size_t Active_Bits;
1094 const size_t Number_Of_Elements;
1095 element_type* pdata;
1096
1097 //*************************************************************************
1099 //*************************************************************************
1100#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1101 public:
1102 virtual ~ibitset()
1103 {
1104 }
1105#else
1106 protected:
1108 {
1109 }
1110#endif
1111 };
1112
1113 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1114
1115 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1116
1117 ETL_CONSTANT size_t ibitset::Bits_Per_Element;
1118
1119 //*************************************************************************
1125 //*************************************************************************
1126 template <size_t MaxN>
1127 class bitset : public etl::ibitset
1128 {
1129
1130 static ETL_CONSTANT size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1131
1132 public:
1133
1134 static ETL_CONSTANT size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1135 static ETL_CONSTANT size_t Allocated_Bits = ALLOCATED_BITS;
1136
1137 public:
1138
1139 //*************************************************************************
1141 //*************************************************************************
1143 : etl::ibitset(MaxN, Array_Size, data)
1144 {
1145 reset();
1146 }
1147
1148 //*************************************************************************
1150 //*************************************************************************
1152 : etl::ibitset(MaxN, Array_Size, data)
1153 {
1154 etl::copy_n(other.data, Array_Size, data);
1155 }
1156
1157 //*************************************************************************
1159 //*************************************************************************
1160 bitset(unsigned long long value)
1161 : etl::ibitset(MaxN, Array_Size, data)
1162 {
1164 }
1165
1166 //*************************************************************************
1168 //*************************************************************************
1169 bitset(const char* text)
1170 : etl::ibitset(MaxN, Array_Size, data)
1171 {
1172 set(text);
1173 }
1174
1175 //*************************************************************************
1177 //*************************************************************************
1178 bitset(const wchar_t* text)
1179 : etl::ibitset(MaxN, Array_Size, data)
1180 {
1181 set(text);
1182 }
1183
1184 //*************************************************************************
1186 //*************************************************************************
1187 bitset(const char16_t* text)
1188 : etl::ibitset(MaxN, Array_Size, data)
1189 {
1190 set(text);
1191 }
1192
1193 //*************************************************************************
1195 //*************************************************************************
1196 bitset(const char32_t* text)
1197 : etl::ibitset(MaxN, Array_Size, data)
1198 {
1199 set(text);
1200 }
1201
1202 //*************************************************************************
1204 //*************************************************************************
1206 {
1208 return *this;
1209 }
1210
1211 //*************************************************************************
1213 //*************************************************************************
1214 bitset<MaxN>& set(size_t position, bool value = true)
1215 {
1216 etl::ibitset::set(position, value);
1217 return *this;
1218 }
1219
1220 //*************************************************************************
1222 //*************************************************************************
1223 bitset<MaxN>& set(const char* text)
1224 {
1225 etl::ibitset::set(text);
1226
1227 return *this;
1228 }
1229
1230 //*************************************************************************
1232 //*************************************************************************
1233 bitset<MaxN>& set(const wchar_t* text)
1234 {
1235 etl::ibitset::set(text);
1236
1237 return *this;
1238 }
1239
1240 //*************************************************************************
1242 //*************************************************************************
1243 bitset<MaxN>& set(const char16_t* text)
1244 {
1245 etl::ibitset::set(text);
1246
1247 return *this;
1248 }
1249
1250 //*************************************************************************
1252 //*************************************************************************
1253 bitset<MaxN>& set(const char32_t* text)
1254 {
1255 etl::ibitset::set(text);
1256
1257 return *this;
1258 }
1259
1260 //*************************************************************************
1262 //*************************************************************************
1263 bitset<MaxN>& from_string(const char* text)
1264 {
1266
1267 return *this;
1268 }
1269
1270 //*************************************************************************
1272 //*************************************************************************
1273 bitset<MaxN>& from_string(const wchar_t* text)
1274 {
1276
1277 return *this;
1278 }
1279
1280 //*************************************************************************
1282 //*************************************************************************
1283 bitset<MaxN>& from_string(const char16_t* text)
1284 {
1286
1287 return *this;
1288 }
1289
1290 //*************************************************************************
1292 //*************************************************************************
1293 bitset<MaxN>& from_string(const char32_t* text)
1294 {
1296
1297 return *this;
1298 }
1299
1300 //*************************************************************************
1302 //*************************************************************************
1303 template <typename T>
1305 value() const
1306 {
1307 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
1308 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element), "Type too small");
1309
1310 return ibitset::value<T>();
1311 }
1312
1313 //*************************************************************************
1315 //*************************************************************************
1317 {
1319 return *this;
1320 }
1321
1322 //*************************************************************************
1324 //*************************************************************************
1325 bitset<MaxN>& reset(size_t position)
1326 {
1327 etl::ibitset::reset(position);
1328 return *this;
1329 }
1330
1331 //*************************************************************************
1333 //*************************************************************************
1335 {
1336 ibitset::flip();
1337 return *this;
1338 }
1339
1340 //*************************************************************************
1342 //*************************************************************************
1343 bitset<MaxN>& flip(size_t position)
1344 {
1345 etl::ibitset::flip(position);
1346 return *this;
1347 }
1348
1349 //*************************************************************************
1351 //*************************************************************************
1352#if ETL_USING_CPP11
1353 template <typename TString = etl::string<MaxN>>
1354#else
1355 template <typename TString>
1356#endif
1357 TString to_string(typename TString::value_type zero = typename TString::value_type('0'), typename TString::value_type one = typename TString::value_type('1')) const
1358 {
1359 TString result;
1360
1361 result.resize(MaxN, '\0');
1362
1363 ETL_ASSERT_OR_RETURN_VALUE((result.size() == MaxN), ETL_ERROR(etl::bitset_overflow), result);
1364
1365 for (size_t i = MaxN; i > 0; --i)
1366 {
1367 result[MaxN - i] = test(i - 1) ? one : zero;
1368 }
1369
1370 return result;
1371 }
1372
1373 //*************************************************************************
1375 //*************************************************************************
1377 {
1378 if (this != &other)
1379 {
1380 etl::copy_n(other.data, Array_Size, data);
1381 }
1382
1383 return *this;
1384 }
1385
1386 //*************************************************************************
1388 //*************************************************************************
1390 {
1392 return *this;
1393 }
1394
1395 //*************************************************************************
1397 //*************************************************************************
1399 {
1401 return *this;
1402 }
1403
1404 //*************************************************************************
1406 //*************************************************************************
1408 {
1410 return *this;
1411 }
1412
1413 //*************************************************************************
1415 //*************************************************************************
1417 {
1418 etl::bitset<MaxN> temp(*this);
1419
1420 temp.invert();
1421
1422 return temp;
1423 }
1424
1425 //*************************************************************************
1427 //*************************************************************************
1428 bitset<MaxN> operator<<(size_t shift) const
1429 {
1430 etl::bitset<MaxN> temp(*this);
1431
1432 temp <<= shift;
1433
1434 return temp;
1435 }
1436
1437 //*************************************************************************
1439 //*************************************************************************
1440 bitset<MaxN>& operator<<=(size_t shift)
1441 {
1443 return *this;
1444 }
1445
1446 //*************************************************************************
1448 //*************************************************************************
1450 {
1451 bitset<MaxN> temp(*this);
1452
1453 temp >>= shift;
1454
1455 return temp;
1456 }
1457
1458 //*************************************************************************
1460 //*************************************************************************
1462 {
1464 return *this;
1465 }
1466
1467 //*************************************************************************
1469 //*************************************************************************
1470 friend bool operator == (const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
1471 {
1473 }
1474
1475 private:
1476
1477 element_type data[Array_Size > 0U ? Array_Size : 1U];
1478 };
1479
1480 template <size_t MaxN>
1481 ETL_CONSTANT size_t bitset<MaxN>::ALLOCATED_BITS;
1482
1483 template <size_t MaxN>
1484 ETL_CONSTANT size_t bitset<MaxN>::Allocated_Bits;
1485
1486 //***************************************************************************
1489 //***************************************************************************
1490 template <size_t MaxN>
1492 {
1494 temp &= rhs;
1495 return temp;
1496 }
1497
1498 //***************************************************************************
1501 //***************************************************************************
1502 template<size_t MaxN>
1504 {
1506 temp |= rhs;
1507 return temp;
1508 }
1509
1510 //***************************************************************************
1513 //***************************************************************************
1514 template<size_t MaxN>
1516 {
1518 temp ^= rhs;
1519 return temp;
1520 }
1521
1522 //***************************************************************************
1525 //***************************************************************************
1526 template<size_t MaxN>
1528 {
1529 return !(lhs == rhs);
1530 }
1531}
1532
1533//*************************************************************************
1535//*************************************************************************
1536template <size_t MaxN>
1538{
1539 lhs.swap(rhs);
1540}
1541
1542#include "minmax_pop.h"
1543
1544#endif
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:2216
Definition binary.h:2249
Span - Fixed Extent.
Definition span.h:63
ETL_CONSTEXPR14 void transform_n(TInputIterator i_begin, TSize n, TOutputIterator o_begin, TUnaryFunction function)
Definition algorithm.h:2638
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:922
Definition binary.h:353
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:500
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:1214
ibitset & reset()
Resets the bitset.
Definition bitset_legacy.h:541
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition bitset_legacy.h:1389
bitset(const char16_t *text)
Construct from a string.
Definition bitset_legacy.h:1187
size_t find_first(bool state) const
Definition bitset_legacy.h:681
size_t find_next(bool state, size_t position) const
Definition bitset_legacy.h:692
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition bitset_legacy.h:1009
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:655
~ibitset()
Destructor.
Definition bitset_legacy.h:1107
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition bitset_legacy.h:1470
bitset< MaxN > & flip()
Flip all of the bits.
Definition bitset_legacy.h:1334
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition bitset_legacy.h:1052
ibitset & operator|=(const ibitset &other)
operator |=
Definition bitset_legacy.h:786
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition bitset_legacy.h:1151
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition bitset_legacy.h:1060
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition bitset_legacy.h:1253
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition bitset_legacy.h:1233
bitset(const char *text)
Construct from a string.
Definition bitset_legacy.h:1169
void swap(ibitset &other)
swap
Definition bitset_legacy.h:979
ibitset & operator=(const ibitset &other)
operator =
Definition bitset_legacy.h:966
bitset< MaxN > & reset()
Reset all of the bits.
Definition bitset_legacy.h:1316
bitset(const wchar_t *text)
Construct from a string.
Definition bitset_legacy.h:1178
ibitset & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:890
ibitset & operator^=(const ibitset &other)
operator ^=
Definition bitset_legacy.h:799
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:1305
bitset(unsigned long long value)
Construct from a value.
Definition bitset_legacy.h:1160
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:1325
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:1357
ibitset & set(const char32_t *text)
Set from a u32string.
Definition bitset_legacy.h:481
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:583
bitset(const char32_t *text)
Construct from a string.
Definition bitset_legacy.h:1196
ibitset & operator&=(const ibitset &other)
operator &=
Definition bitset_legacy.h:773
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:1273
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:1440
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition bitset_legacy.h:1428
unsigned long to_ulong() const
Put to a unsigned long.
Definition bitset_legacy.h:525
bool operator[](size_t position) const
Read [] operator.
Definition bitset_legacy.h:757
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition bitset_legacy.h:533
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition bitset_legacy.h:1398
void invert()
Invert.
Definition bitset_legacy.h:1039
bitset()
Default constructor.
Definition bitset_legacy.h:1142
bitset< MaxN > operator~() const
operator ~
Definition bitset_legacy.h:1416
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition bitset_legacy.h:1449
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:1283
ibitset & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:551
bitset< MaxN > & set(const char *text)
Set from a string.
Definition bitset_legacy.h:1223
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:1376
ibitset & set(const char16_t *text)
Set from a u16string.
Definition bitset_legacy.h:464
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition bitset_legacy.h:447
ibitset & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:812
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition bitset_legacy.h:1407
ETL_CONSTEXPR14 bool test() const
Definition bitset_new.h:2360
bool none() const
Are none of the bits set?
Definition bitset_legacy.h:663
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:1293
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:1243
ibitset & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:598
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:1343
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:1205
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:1461
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition bitset_legacy.h:1073
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:1263
Bitset forward declaration.
Definition bitset_legacy.h:1128
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
Definition exception.h:47
Definition integral_limits.h:516
Definition log.h:99
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 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:654
ETL_CONSTEXPR14 size_t strlen(const T *t)
Alternative strlen for all character types.
Definition char_traits.h:285
etl::byte operator^(etl::byte lhs, etl::byte rhs)
Exclusive Or.
Definition byte.h:281
pair holds two objects of arbitrary type
Definition utility.h:164