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 size_t size_type;
152
153 typedef typename etl::make_unsigned<ETL_BITSET_ELEMENT_TYPE>::type element_type;
154 typedef element_type element_t; // Backward compatibility
155
156 static ETL_CONSTANT element_type ALL_SET = etl::integral_limits<element_type>::max;
157 static ETL_CONSTANT element_type ALL_CLEAR = 0;
158
159 static ETL_CONSTANT size_t Bits_Per_Element = etl::integral_limits<element_type>::bits;
160
161#if ETL_USING_CPP11
162 typedef etl::span<element_type> span_type;
163 typedef etl::span<const element_type> const_span_type;
164#endif
165
166 enum
167 {
169 };
170
171 //*************************************************************************
173 //*************************************************************************
175 {
176 public:
177
178 friend class ibitset;
179
180 //*******************************
182 //*******************************
183 operator bool() const
184 {
185 return p_bitset->test(position);
186 }
187
188 //*******************************
190 //*******************************
192 : p_bitset(other.p_bitset)
193 , position(other.position)
194 {
195 }
196
197 //*******************************
199 //*******************************
201 {
202 p_bitset->set(position, b);
203 return *this;
204 }
205
206 //*******************************
208 //*******************************
210 {
211 p_bitset->set(position, bool(r));
212 return *this;
213 }
214
215 //*******************************
217 //*******************************
219 {
220 p_bitset->flip(position);
221 return *this;
222 }
223
224 //*******************************
226 //*******************************
227 bool operator~() const
228 {
229 return !p_bitset->test(position);
230 }
231
232 private:
233
234 //*******************************
236 //*******************************
238 : p_bitset(ETL_NULLPTR)
239 , position(0)
240 {
241 }
242
243 //*******************************
245 //*******************************
246 bit_reference(ibitset& r_bitset, size_t position_)
247 : p_bitset(&r_bitset)
248 , position(position_)
249 {
250 }
251
252 ibitset* p_bitset;
253 size_t position;
254 };
255
256 //*************************************************************************
258 //*************************************************************************
259 size_t size() const
260 {
261 return Active_Bits;
262 }
263
264 //*************************************************************************
266 //*************************************************************************
267 size_t count() const
268 {
269 size_t n = 0UL;
270
271 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
272 {
273 n += etl::count_bits(pdata[i]);
274 }
275
276 return n;
277 }
278
279 //*************************************************************************
282 //*************************************************************************
283 bool test(size_t position) const
284 {
285 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), false);
286 size_t index;
287 element_type mask;
288
289 if (position >= Active_Bits)
290 {
291 return false;
292 }
293 if (Number_Of_Elements == 0)
294 {
295 return false;
296 }
297 else if (Number_Of_Elements == 1)
298 {
299 index = 0;
300 mask = element_type(1) << position;
301 }
302 else
303 {
304 index = position >> etl::log2<Bits_Per_Element>::value;
305 mask = element_type(1) << (position & (Bits_Per_Element - 1));
306 }
307
308 return (pdata[index] & mask) != 0;
309 }
310
311 //*************************************************************************
313 //*************************************************************************
315 {
316 etl::fill_n(pdata, Number_Of_Elements - 1U, ALL_SET);
317 pdata[Number_Of_Elements - 1U] = Top_Mask;
318
319 return *this;
320 }
321
322 //*************************************************************************
324 //*************************************************************************
325 ibitset& set(size_t position, bool value = true)
326 {
327 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
328 size_t index;
329 element_type bit;
330
331 if (position < Active_Bits)
332 {
333 if (Number_Of_Elements == 0)
334 {
335 return *this;
336 }
337 else if (Number_Of_Elements == 1)
338 {
339 index = 0;
340 bit = element_type(1) << position;
341 }
342 else
343 {
344 index = position >> etl::log2<Bits_Per_Element>::value;
345 bit = element_type(1) << (position & (Bits_Per_Element - 1));
346 }
347
348 if (value)
349 {
350 pdata[index] |= bit;
351 }
352 else
353 {
354 pdata[index] &= ~bit;
355 }
356 }
357
358 return *this;
359 }
360
361 //*************************************************************************
363 //*************************************************************************
364 ibitset& from_string(const char* text)
365 {
366 reset();
367
368 size_t i = etl::min(Active_Bits, etl::strlen(text));
369
370 while (i > 0)
371 {
372 set(--i, *text++ == ETL_STRL('1'));
373 }
374
375 return *this;
376 }
377
378 //*************************************************************************
380 //*************************************************************************
381 ibitset& from_string(const wchar_t* text)
382 {
383 reset();
384
385 size_t i = etl::min(Active_Bits, etl::strlen(text));
386
387 while (i > 0)
388 {
389 set(--i, *text++ == ETL_STRL('1'));
390 }
391
392 return *this;
393 }
394
395 //*************************************************************************
397 //*************************************************************************
398 ibitset& from_string(const char16_t* text)
399 {
400 reset();
401
402 size_t i = etl::min(Active_Bits, etl::strlen(text));
403
404 while (i > 0)
405 {
406 set(--i, *text++ == ETL_STRu('1'));
407 }
408
409 return *this;
410 }
411
412 //*************************************************************************
414 //*************************************************************************
415 ibitset& from_string(const char32_t* text)
416 {
417 reset();
418
419 size_t i = etl::min(Active_Bits, etl::strlen(text));
420
421 while (i > 0)
422 {
423 set(--i, *text++ == ETL_STRU('1'));
424 }
425
426 return *this;
427 }
428
429 //*************************************************************************
431 //*************************************************************************
432 ibitset& set(const char* text)
433 {
434 if (text == ETL_NULLPTR)
435 {
436 reset();
437 }
438 else
439 {
440 from_string(text);
441 }
442
443 return *this;
444 }
445
446 //*************************************************************************
448 //*************************************************************************
449 ibitset& set(const wchar_t* text)
450 {
451 if (text == ETL_NULLPTR)
452 {
453 reset();
454 }
455 else
456 {
457 from_string(text);
458 }
459
460 return *this;
461 }
462
463 //*************************************************************************
465 //*************************************************************************
466 ibitset& set(const char16_t* text)
467 {
468 if (text == ETL_NULLPTR)
469 {
470 reset();
471 }
472 else
473 {
474 from_string(text);
475 }
476
477 return *this;
478 }
479
480 //*************************************************************************
482 //*************************************************************************
483 ibitset& set(const char32_t* text)
484 {
485 if (text == ETL_NULLPTR)
486 {
487 reset();
488 }
489 else
490 {
491 from_string(text);
492 }
493
494 return *this;
495 }
496
497 //*************************************************************************
499 //*************************************************************************
500 template <typename T>
502 value() const
503 {
504 T v = T(0);
505
506 const bool OK = (sizeof(T) * CHAR_BIT) >= (Number_Of_Elements * Bits_Per_Element);
507
508 ETL_ASSERT_OR_RETURN_VALUE(OK, ETL_ERROR(etl::bitset_type_too_small), T(0));
509
510 if (OK)
511 {
513
514 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
515 {
516 v |= T(typename etl::make_unsigned<T>::type(pdata[i]) << shift);
517 shift += uint_least8_t(Bits_Per_Element);
518 }
519 }
520
521 return v;
522 }
523
524 //*************************************************************************
526 //*************************************************************************
527 unsigned long to_ulong() const
528 {
529 return value<unsigned long>();
530 }
531
532 //*************************************************************************
534 //*************************************************************************
535 unsigned long long to_ullong() const
536 {
538 }
539
540 //*************************************************************************
542 //*************************************************************************
544 {
545 etl::fill_n(pdata, Number_Of_Elements, ALL_CLEAR);
546
547 return *this;
548 }
549
550 //*************************************************************************
552 //*************************************************************************
553 ibitset& reset(size_t position)
554 {
555 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
556 size_t index;
557 element_type bit;
558
559 if (position < Active_Bits)
560 {
561 if (Number_Of_Elements == 0)
562 {
563 return *this;
564 }
565 else if (Number_Of_Elements == 1)
566 {
567 index = 0;
568 bit = element_type(1) << position;
569 }
570 else
571 {
572 index = position >> etl::log2<Bits_Per_Element>::value;
573 bit = element_type(1) << (position & (Bits_Per_Element - 1));
574 }
575
576 pdata[index] &= ~bit;
577 }
578
579 return *this;
580 }
581
582 //*************************************************************************
584 //*************************************************************************
586 {
587 etl::transform_n(pdata,
588 Number_Of_Elements,
589 pdata,
591
592 clear_unused_bits_in_msb();
593
594 return *this;
595 }
596
597 //*************************************************************************
599 //*************************************************************************
600 ibitset& flip(size_t position)
601 {
602 ETL_ASSERT_OR_RETURN_VALUE(position < Active_Bits, ETL_ERROR(bitset_overflow), *this);
603 size_t index;
604 element_type bit;
605
606 if (Number_Of_Elements == 0)
607 {
608 return *this;
609 }
610 else if (Number_Of_Elements == 1)
611 {
612 index = 0;
613 bit = element_type(1) << position;
614 }
615 else
616 {
617 index = position >> log2<Bits_Per_Element>::value;
618 bit = element_type(1) << (position & (Bits_Per_Element - 1));
619 }
620
621 pdata[index] ^= bit;
622
623 return *this;
624 }
625
626 //*************************************************************************
627 // Are all the bits sets?
628 //*************************************************************************
629 bool all() const
630 {
631 if (Number_Of_Elements == 0UL)
632 {
633 return true;
634 }
635
636 // All but the last.
637 for (size_t i = 0UL; i < (Number_Of_Elements - 1U); ++i)
638 {
639 if (pdata[i] != ALL_SET)
640 {
641 return false;
642 }
643 }
644
645 // The last.
646 if (pdata[Number_Of_Elements - 1U] != (ALL_SET & Top_Mask))
647 {
648 return false;
649 }
650
651 return true;
652 }
653
654 //*************************************************************************
656 //*************************************************************************
657 bool any() const
658 {
659 return !none();
660 }
661
662 //*************************************************************************
664 //*************************************************************************
665 bool none() const
666 {
667 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
668 {
669 if (pdata[i] != 0)
670 {
671 return false;
672 }
673 }
674
675 return true;
676 }
677
678 //*************************************************************************
682 //*************************************************************************
683 size_t find_first(bool state) const
684 {
685 return find_next(state, 0);
686 }
687
688 //*************************************************************************
693 //*************************************************************************
694 size_t find_next(bool state, size_t position) const
695 {
696 // Where to start.
697 size_t index;
698 size_t bit;
699
700 if (Number_Of_Elements == 0)
701 {
702 return ibitset::npos;
703 }
704 else if (Number_Of_Elements == 1)
705 {
706 index = 0;
707 bit = position;
708 }
709 else
710 {
711 index = position >> log2<Bits_Per_Element>::value;
712 bit = position & (Bits_Per_Element - 1);
713 }
714
715 element_type mask = 1 << bit;
716
717 // For each element in the bitset...
718 while (index < Number_Of_Elements)
719 {
720 element_type value = pdata[index];
721
722 // Needs checking?
723 if ((state && (value != ALL_CLEAR)) ||
724 (!state && (value != ALL_SET)))
725 {
726 // For each bit in the element...
727 while ((bit < Bits_Per_Element) && (position < Active_Bits))
728 {
729 // Equal to the required state?
730 if (((value & mask) != 0) == state)
731 {
732 return position;
733 }
734
735 // Move on to the next bit.
736 mask <<= 1;
737 ++position;
738 ++bit;
739 }
740 }
741 else
742 {
743 position += (Bits_Per_Element - bit);
744 }
745
746 // Start at the beginning for all other elements.
747 bit = 0;
748 mask = 1;
749
750 ++index;
751 }
752
753 return ibitset::npos;
754 }
755
756 //*************************************************************************
758 //*************************************************************************
759 bool operator[] (size_t position) const
760 {
761 return test(position);
762 }
763
764 //*************************************************************************
766 //*************************************************************************
768 {
769 return bit_reference(*this, position);
770 }
771
772 //*************************************************************************
774 //*************************************************************************
776 {
777 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
778 {
779 pdata[i] &= other.pdata[i];
780 }
781
782 return *this;
783 }
784
785 //*************************************************************************
787 //*************************************************************************
789 {
790 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
791 {
792 pdata[i] |= other.pdata[i];
793 }
794
795 return *this;
796 }
797
798 //*************************************************************************
800 //*************************************************************************
802 {
803 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
804 {
805 pdata[i] ^= other.pdata[i];
806 }
807
808 return *this;
809 }
810
811 //*************************************************************************
813 //*************************************************************************
815 {
816 if (shift >= Active_Bits)
817 {
818 reset();
819 }
820 else if (Number_Of_Elements != 0UL)
821 {
822 // Just one element?
823 if (Number_Of_Elements == 1UL)
824 {
825 pdata[0] <<= shift;
826 }
827 else if (shift == Bits_Per_Element)
828 {
829 etl::copy_backward(pdata, pdata + Number_Of_Elements - 1U, pdata + Number_Of_Elements);
830 pdata[0] = 0;
831 }
832 else
833 {
834 // The place where the elements are split when shifting.
835 const size_t split_position = Bits_Per_Element - (shift % Bits_Per_Element);
836
837 // Where we are shifting from.
838 int src_index = int(Number_Of_Elements - (shift / Bits_Per_Element) - 1U);
839
840 // Where we are shifting to.
841 int dst_index = int(Number_Of_Elements - 1U);
842
843 // Shift control constants.
844 const size_t lsb_shift = Bits_Per_Element - split_position;
845 const size_t msb_shift = split_position;
846
847 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
849 const element_type lsb_shifted_mask = element_type(lsb_mask << lsb_shift);
850
851 // First lsb.
852 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
853 pdata[dst_index] = lsb;
854 --src_index;
855
856 // Now do the shifting.
857 while (src_index >= 0)
858 {
859 // Shift msb.
860 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
861 pdata[dst_index] = pdata[dst_index] | msb;
862 --dst_index;
863
864 // Shift lsb.
865 lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
866 pdata[dst_index] = lsb;
867 --src_index;
868 }
869
870 // Clear the remaining bits.
871 // First lsb.
872 pdata[dst_index] &= lsb_shifted_mask;
873 --dst_index;
874
875 // The other remaining elements.
876 for (int i = 0; i <= dst_index; ++i)
877 {
878 pdata[i] = 0;
879 }
880 }
881
882 // Truncate any bits shifted to the left.
883 clear_unused_bits_in_msb();
884 }
885
886 return *this;
887 }
888
889 //*************************************************************************
891 //*************************************************************************
893 {
894 if (shift >= Active_Bits)
895 {
896 reset();
897 }
898 else if (Number_Of_Elements != 0UL)
899 {
900 // Just one element?
901 if (Number_Of_Elements == 1UL)
902 {
903 pdata[0] >>= shift;
904 }
905 // Shift is the size of an element?
906 else if (shift == Bits_Per_Element)
907 {
908 etl::copy(pdata + 1, pdata + Number_Of_Elements, pdata);
909 pdata[Number_Of_Elements - 1U] = 0;
910 }
911 else
912 {
913 // The place where the elements are split when shifting.
914 const size_t split_position = shift % Bits_Per_Element;
915
916 // Where we are shifting from.
917 int src_index = int(shift / Bits_Per_Element);
918
919 // Where we are shifting to.
920 int dst_index = 0;
921
922 // Shift control constants.
923 const size_t lsb_shift = Bits_Per_Element - split_position;
924 const size_t msb_shift = split_position;
925
926 const element_type lsb_mask = element_type(etl::integral_limits<element_type>::max >> (Bits_Per_Element - split_position));
928 const element_type msb_shifted_mask = element_type(msb_mask >> msb_shift);
929
930 // Now do the shifting.
931 while (src_index < int(Number_Of_Elements - 1))
932 {
933 // Shift msb.
934 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
935 ++src_index;
936
937 // Shift lsb.
938 element_type lsb = element_type((pdata[src_index] & lsb_mask) << lsb_shift);
939
940 // Combine them.
941 pdata[dst_index] = lsb | msb;
942 ++dst_index;
943 }
944
945 // Final msb.
946 element_type msb = element_type((pdata[src_index] & msb_mask) >> msb_shift);
947 pdata[dst_index] = msb;
948
949 // Clear the remaining bits.
950 // First msb.
951 pdata[dst_index] &= msb_shifted_mask;
952 ++dst_index;
953
954 // The other remaining elements.
955 for (int i = dst_index; i < int(Number_Of_Elements); ++i)
956 {
957 pdata[i] = 0;
958 }
959 }
960 }
961
962 return *this;
963 }
964
965 //*************************************************************************
967 //*************************************************************************
969 {
970 if (this != &other)
971 {
972 etl::copy_n(other.pdata, Number_Of_Elements, pdata);
973 }
974
975 return *this;
976 }
977
978 //*************************************************************************
980 //*************************************************************************
982 {
983 etl::swap_ranges(pdata, pdata + Number_Of_Elements, other.pdata);
984 }
985
986#if ETL_USING_CPP11
987 //*************************************************************************
990 //*************************************************************************
991 span_type span()
992 {
993 return span_type(pdata, pdata + Number_Of_Elements);
994 }
995
996 //*************************************************************************
999 //*************************************************************************
1000 const_span_type span() const
1001 {
1002 return const_span_type(pdata, pdata + Number_Of_Elements);
1003 }
1004#endif
1005
1006 protected:
1007
1008 //*************************************************************************
1010 //*************************************************************************
1011 ibitset& initialise(unsigned long long value)
1012 {
1013 reset();
1014
1015 const size_t Shift = (integral_limits<unsigned long long>::bits <= (int)Bits_Per_Element) ? 0 : Bits_Per_Element;
1016
1017 // Can we do it in one hit?
1018 if (Shift == 0)
1019 {
1020 pdata[0] = element_type(value);
1021 }
1022 else
1023 {
1024 size_t i = 0UL;
1025
1026 while ((value != 0) && (i < Number_Of_Elements))
1027 {
1028 pdata[i++] = value & ALL_SET;
1029 value = value >> Shift;
1030 }
1031 }
1032
1033 clear_unused_bits_in_msb();
1034
1035 return *this;
1036 }
1037
1038 //*************************************************************************
1040 //*************************************************************************
1041 void invert()
1042 {
1043 for (size_t i = 0UL; i < Number_Of_Elements; ++i)
1044 {
1045 pdata[i] = ~pdata[i];
1046 }
1047
1048 clear_unused_bits_in_msb();
1049 }
1050
1051 //*************************************************************************
1053 //*************************************************************************
1055 {
1056 return bit_reference(*this, position);
1057 }
1058
1059 //*************************************************************************
1061 //*************************************************************************
1062 ibitset(size_t nbits_, size_t size_, element_type* pdata_)
1063 : Active_Bits(nbits_)
1064 , Number_Of_Elements(size_)
1065 , pdata(pdata_)
1066 {
1067 const size_t allocated_bits = Number_Of_Elements * Bits_Per_Element;
1068 const size_t top_mask_shift = ((Bits_Per_Element - (allocated_bits - Active_Bits)) % Bits_Per_Element);
1069 Top_Mask = element_type(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
1070 }
1071
1072 //*************************************************************************
1074 //*************************************************************************
1075 static bool is_equal(const ibitset& lhs, const ibitset&rhs)
1076 {
1077 return etl::equal(lhs.pdata, lhs.pdata + lhs.Number_Of_Elements, rhs.pdata);
1078 }
1079
1080 element_type Top_Mask;
1081
1082 private:
1083
1084 //*************************************************************************
1086 //*************************************************************************
1087 void clear_unused_bits_in_msb()
1088 {
1089 pdata[Number_Of_Elements - 1U] &= Top_Mask;
1090 }
1091
1092 // Disable copy construction.
1093 ibitset(const ibitset&);
1094
1095 const size_t Active_Bits;
1096 const size_t Number_Of_Elements;
1097 element_type* pdata;
1098
1099 //*************************************************************************
1101 //*************************************************************************
1102#if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1103 public:
1104 virtual ~ibitset()
1105 {
1106 }
1107#else
1108 protected:
1110 {
1111 }
1112#endif
1113 };
1114
1115 ETL_CONSTANT ibitset::element_type ibitset::ALL_SET;
1116
1117 ETL_CONSTANT ibitset::element_type ibitset::ALL_CLEAR;
1118
1119 ETL_CONSTANT size_t ibitset::Bits_Per_Element;
1120
1121 //*************************************************************************
1127 //*************************************************************************
1128 template <size_t MaxN>
1129 class bitset : public etl::ibitset
1130 {
1131
1132 static ETL_CONSTANT size_t Array_Size = (MaxN % Bits_Per_Element == 0) ? MaxN / Bits_Per_Element : MaxN / Bits_Per_Element + 1;
1133
1134 public:
1135
1136 static ETL_CONSTANT size_t ALLOCATED_BITS = Array_Size * Bits_Per_Element;
1137 static ETL_CONSTANT size_t Allocated_Bits = ALLOCATED_BITS;
1138
1139 public:
1140
1141 //*************************************************************************
1143 //*************************************************************************
1145 : etl::ibitset(MaxN, Array_Size, data)
1146 {
1147 reset();
1148 }
1149
1150 //*************************************************************************
1152 //*************************************************************************
1154 : etl::ibitset(MaxN, Array_Size, data)
1155 {
1156 etl::copy_n(other.data, Array_Size, data);
1157 }
1158
1159 //*************************************************************************
1161 //*************************************************************************
1162 bitset(unsigned long long value)
1163 : etl::ibitset(MaxN, Array_Size, data)
1164 {
1166 }
1167
1168 //*************************************************************************
1170 //*************************************************************************
1171 bitset(const char* text)
1172 : etl::ibitset(MaxN, Array_Size, data)
1173 {
1174 set(text);
1175 }
1176
1177 //*************************************************************************
1179 //*************************************************************************
1180 bitset(const wchar_t* text)
1181 : etl::ibitset(MaxN, Array_Size, data)
1182 {
1183 set(text);
1184 }
1185
1186 //*************************************************************************
1188 //*************************************************************************
1189 bitset(const char16_t* text)
1190 : etl::ibitset(MaxN, Array_Size, data)
1191 {
1192 set(text);
1193 }
1194
1195 //*************************************************************************
1197 //*************************************************************************
1198 bitset(const char32_t* text)
1199 : etl::ibitset(MaxN, Array_Size, data)
1200 {
1201 set(text);
1202 }
1203
1204 //*************************************************************************
1206 //*************************************************************************
1208 {
1210 return *this;
1211 }
1212
1213 //*************************************************************************
1215 //*************************************************************************
1216 bitset<MaxN>& set(size_t position, bool value = true)
1217 {
1218 etl::ibitset::set(position, value);
1219 return *this;
1220 }
1221
1222 //*************************************************************************
1224 //*************************************************************************
1225 bitset<MaxN>& set(const char* text)
1226 {
1227 etl::ibitset::set(text);
1228
1229 return *this;
1230 }
1231
1232 //*************************************************************************
1234 //*************************************************************************
1235 bitset<MaxN>& set(const wchar_t* text)
1236 {
1237 etl::ibitset::set(text);
1238
1239 return *this;
1240 }
1241
1242 //*************************************************************************
1244 //*************************************************************************
1245 bitset<MaxN>& set(const char16_t* text)
1246 {
1247 etl::ibitset::set(text);
1248
1249 return *this;
1250 }
1251
1252 //*************************************************************************
1254 //*************************************************************************
1255 bitset<MaxN>& set(const char32_t* text)
1256 {
1257 etl::ibitset::set(text);
1258
1259 return *this;
1260 }
1261
1262 //*************************************************************************
1264 //*************************************************************************
1265 bitset<MaxN>& from_string(const char* text)
1266 {
1268
1269 return *this;
1270 }
1271
1272 //*************************************************************************
1274 //*************************************************************************
1275 bitset<MaxN>& from_string(const wchar_t* text)
1276 {
1278
1279 return *this;
1280 }
1281
1282 //*************************************************************************
1284 //*************************************************************************
1285 bitset<MaxN>& from_string(const char16_t* text)
1286 {
1288
1289 return *this;
1290 }
1291
1292 //*************************************************************************
1294 //*************************************************************************
1295 bitset<MaxN>& from_string(const char32_t* text)
1296 {
1298
1299 return *this;
1300 }
1301
1302 //*************************************************************************
1304 //*************************************************************************
1305 template <typename T>
1307 value() const
1308 {
1309 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Only integral types are supported");
1310 ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (Array_Size * Bits_Per_Element), "Type too small");
1311
1312 return ibitset::value<T>();
1313 }
1314
1315 //*************************************************************************
1317 //*************************************************************************
1319 {
1321 return *this;
1322 }
1323
1324 //*************************************************************************
1326 //*************************************************************************
1327 bitset<MaxN>& reset(size_t position)
1328 {
1329 etl::ibitset::reset(position);
1330 return *this;
1331 }
1332
1333 //*************************************************************************
1335 //*************************************************************************
1337 {
1338 ibitset::flip();
1339 return *this;
1340 }
1341
1342 //*************************************************************************
1344 //*************************************************************************
1345 bitset<MaxN>& flip(size_t position)
1346 {
1347 etl::ibitset::flip(position);
1348 return *this;
1349 }
1350
1351 //*************************************************************************
1353 //*************************************************************************
1354#if ETL_USING_CPP11
1355 template <typename TString = etl::string<MaxN>>
1356#else
1357 template <typename TString>
1358#endif
1359 TString to_string(typename TString::value_type zero = typename TString::value_type('0'), typename TString::value_type one = typename TString::value_type('1')) const
1360 {
1361 TString result;
1362
1363 result.resize(MaxN, '\0');
1364
1365 ETL_ASSERT_OR_RETURN_VALUE((result.size() == MaxN), ETL_ERROR(etl::bitset_overflow), result);
1366
1367 for (size_t i = MaxN; i > 0; --i)
1368 {
1369 result[MaxN - i] = test(i - 1) ? one : zero;
1370 }
1371
1372 return result;
1373 }
1374
1375 //*************************************************************************
1377 //*************************************************************************
1379 {
1380 if (this != &other)
1381 {
1382 etl::copy_n(other.data, Array_Size, data);
1383 }
1384
1385 return *this;
1386 }
1387
1388 //*************************************************************************
1390 //*************************************************************************
1392 {
1394 return *this;
1395 }
1396
1397 //*************************************************************************
1399 //*************************************************************************
1401 {
1403 return *this;
1404 }
1405
1406 //*************************************************************************
1408 //*************************************************************************
1410 {
1412 return *this;
1413 }
1414
1415 //*************************************************************************
1417 //*************************************************************************
1419 {
1420 etl::bitset<MaxN> temp(*this);
1421
1422 temp.invert();
1423
1424 return temp;
1425 }
1426
1427 //*************************************************************************
1429 //*************************************************************************
1430 bitset<MaxN> operator<<(size_t shift) const
1431 {
1432 etl::bitset<MaxN> temp(*this);
1433
1434 temp <<= shift;
1435
1436 return temp;
1437 }
1438
1439 //*************************************************************************
1441 //*************************************************************************
1442 bitset<MaxN>& operator<<=(size_t shift)
1443 {
1445 return *this;
1446 }
1447
1448 //*************************************************************************
1450 //*************************************************************************
1452 {
1453 bitset<MaxN> temp(*this);
1454
1455 temp >>= shift;
1456
1457 return temp;
1458 }
1459
1460 //*************************************************************************
1462 //*************************************************************************
1464 {
1466 return *this;
1467 }
1468
1469 //*************************************************************************
1471 //*************************************************************************
1472 friend bool operator == (const bitset<MaxN>& lhs, const bitset<MaxN>& rhs)
1473 {
1475 }
1476
1477 private:
1478
1479 element_type data[Array_Size > 0U ? Array_Size : 1U];
1480 };
1481
1482 template <size_t MaxN>
1483 ETL_CONSTANT size_t bitset<MaxN>::ALLOCATED_BITS;
1484
1485 template <size_t MaxN>
1486 ETL_CONSTANT size_t bitset<MaxN>::Allocated_Bits;
1487
1488 //***************************************************************************
1491 //***************************************************************************
1492 template <size_t MaxN>
1494 {
1496 temp &= rhs;
1497 return temp;
1498 }
1499
1500 //***************************************************************************
1503 //***************************************************************************
1504 template<size_t MaxN>
1506 {
1508 temp |= rhs;
1509 return temp;
1510 }
1511
1512 //***************************************************************************
1515 //***************************************************************************
1516 template<size_t MaxN>
1518 {
1520 temp ^= rhs;
1521 return temp;
1522 }
1523
1524 //***************************************************************************
1527 //***************************************************************************
1528 template<size_t MaxN>
1530 {
1531 return !(lhs == rhs);
1532 }
1533}
1534
1535//*************************************************************************
1537//*************************************************************************
1538template <size_t MaxN>
1540{
1541 lhs.swap(rhs);
1542}
1543
1544#include "minmax_pop.h"
1545
1546#endif
The reference type returned.
Definition bitset_legacy.h:175
bool operator~() const
Return the logical inverse of the bit.
Definition bitset_legacy.h:227
bit_reference(const bit_reference &other)
Copy constructor.
Definition bitset_legacy.h:191
bit_reference & operator=(bool b)
Assignment operator.
Definition bitset_legacy.h:200
bit_reference & flip()
Flip the bit.
Definition bitset_legacy.h:218
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:2650
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:502
ibitset & set()
Set all bits.
Definition bitset_legacy.h:314
bitset< MaxN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:1216
ibitset & reset()
Resets the bitset.
Definition bitset_legacy.h:543
bitset< MaxN > & operator&=(const bitset< MaxN > &other)
operator &=
Definition bitset_legacy.h:1391
bitset(const char16_t *text)
Construct from a string.
Definition bitset_legacy.h:1189
size_t find_first(bool state) const
Definition bitset_legacy.h:683
size_t find_next(bool state, size_t position) const
Definition bitset_legacy.h:694
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition bitset_legacy.h:1011
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:398
bool any() const
Are any of the bits set?
Definition bitset_legacy.h:657
~ibitset()
Destructor.
Definition bitset_legacy.h:1109
friend bool operator==(const bitset< MaxN > &lhs, const bitset< MaxN > &rhs)
operator ==
Definition bitset_legacy.h:1472
bitset< MaxN > & flip()
Flip all of the bits.
Definition bitset_legacy.h:1336
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition bitset_legacy.h:1054
ibitset & operator|=(const ibitset &other)
operator |=
Definition bitset_legacy.h:788
bitset(const bitset< MaxN > &other)
Copy constructor.
Definition bitset_legacy.h:1153
ibitset(size_t nbits_, size_t size_, element_type *pdata_)
Constructor.
Definition bitset_legacy.h:1062
bitset< MaxN > & set(const char32_t *text)
Set from a string.
Definition bitset_legacy.h:1255
bitset< MaxN > & set(const wchar_t *text)
Set from a string.
Definition bitset_legacy.h:1235
bitset(const char *text)
Construct from a string.
Definition bitset_legacy.h:1171
void swap(ibitset &other)
swap
Definition bitset_legacy.h:981
ibitset & operator=(const ibitset &other)
operator =
Definition bitset_legacy.h:968
bitset< MaxN > & reset()
Reset all of the bits.
Definition bitset_legacy.h:1318
bitset(const wchar_t *text)
Construct from a string.
Definition bitset_legacy.h:1180
ibitset & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:892
ibitset & operator^=(const ibitset &other)
operator ^=
Definition bitset_legacy.h:801
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition bitset_legacy.h:1307
bitset(unsigned long long value)
Construct from a value.
Definition bitset_legacy.h:1162
bitset< MaxN > & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:1327
size_t count() const
Count the number of bits set.
Definition bitset_legacy.h:267
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:381
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:1359
ibitset & set(const char32_t *text)
Set from a u32string.
Definition bitset_legacy.h:483
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:415
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition bitset_legacy.h:325
ibitset & flip()
Flip all of the bits.
Definition bitset_legacy.h:585
bitset(const char32_t *text)
Construct from a string.
Definition bitset_legacy.h:1198
ibitset & operator&=(const ibitset &other)
operator &=
Definition bitset_legacy.h:775
bitset< MaxN > & from_string(const wchar_t *text)
Set from a wide string.
Definition bitset_legacy.h:1275
bitset< MaxN > & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:1442
bitset< MaxN > operator<<(size_t shift) const
operator <<
Definition bitset_legacy.h:1430
unsigned long to_ulong() const
Put to a unsigned long.
Definition bitset_legacy.h:527
bool operator[](size_t position) const
Read [] operator.
Definition bitset_legacy.h:759
unsigned long long to_ullong() const
Put to a unsigned long long.
Definition bitset_legacy.h:535
bitset< MaxN > & operator|=(const bitset< MaxN > &other)
operator |=
Definition bitset_legacy.h:1400
void invert()
Invert.
Definition bitset_legacy.h:1041
bitset()
Default constructor.
Definition bitset_legacy.h:1144
bitset< MaxN > operator~() const
operator ~
Definition bitset_legacy.h:1418
bitset< MaxN > operator>>(size_t shift) const
operator >>
Definition bitset_legacy.h:1451
bitset< MaxN > & from_string(const char16_t *text)
Set from a u16 string.
Definition bitset_legacy.h:1285
ibitset & reset(size_t position)
Reset the bit at the position.
Definition bitset_legacy.h:553
bitset< MaxN > & set(const char *text)
Set from a string.
Definition bitset_legacy.h:1225
ibitset & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:364
bitset< MaxN > & operator=(const bitset< MaxN > &other)
operator =
Definition bitset_legacy.h:1378
ibitset & set(const char16_t *text)
Set from a u16string.
Definition bitset_legacy.h:466
ibitset & set(const wchar_t *text)
Set from a wstring.
Definition bitset_legacy.h:449
ibitset & operator<<=(size_t shift)
operator <<=
Definition bitset_legacy.h:814
bitset< MaxN > & operator^=(const bitset< MaxN > &other)
operator ^=
Definition bitset_legacy.h:1409
ETL_CONSTEXPR14 bool test() const
Definition bitset_new.h:2363
bool none() const
Are none of the bits set?
Definition bitset_legacy.h:665
bitset< MaxN > & from_string(const char32_t *text)
Set from a u32 string.
Definition bitset_legacy.h:1295
bool test(size_t position) const
Definition bitset_legacy.h:283
size_t size() const
The number of bits in the bitset.
Definition bitset_legacy.h:259
bitset< MaxN > & set(const char16_t *text)
Set from a string.
Definition bitset_legacy.h:1245
ibitset & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:600
bitset< MaxN > & flip(size_t position)
Flip the bit at the position.
Definition bitset_legacy.h:1345
ibitset & set(const char *text)
Set from a string.
Definition bitset_legacy.h:432
bitset< MaxN > & set()
Set all of the bits.
Definition bitset_legacy.h:1207
bitset< MaxN > & operator>>=(size_t shift)
operator >>=
Definition bitset_legacy.h:1463
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition bitset_legacy.h:1075
bitset< MaxN > & from_string(const char *text)
Set from a string.
Definition bitset_legacy.h:1265
Bitset forward declaration.
Definition bitset_legacy.h:1130
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:996
make_unsigned
Definition type_traits_generator.h:1176
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