Embedded Template Library  1.0
bitset.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_BITSET_INCLUDED
32 #define ETL_BITSET_INCLUDED
33 
34 #include <string.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 
38 #include "platform.h"
39 
40 #include "algorithm.h"
41 #include "iterator.h"
42 
43 #include "integral_limits.h"
44 #include "algorithm.h"
45 #include "nullptr.h"
46 #include "log.h"
47 #include "exception.h"
48 #include "integral_limits.h"
49 #include "binary.h"
50 #include "char_traits.h"
51 #include "static_assert.h"
52 #include "error_handler.h"
53 #include "span.h"
54 
55 #include "private/minmax_push.h"
56 
57 #undef ETL_FILE
58 #define ETL_FILE "52"
59 
60 #if defined(ETL_COMPILER_KEIL)
61 #pragma diag_suppress 1300
62 #endif
63 
64 //*****************************************************************************
68 //*****************************************************************************
69 
70 namespace etl
71 {
72  //***************************************************************************
75  //***************************************************************************
77  {
78  public:
79 
80  bitset_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
81  : exception(reason_, file_name_, line_number_)
82  {
83  }
84  };
85 
86  //***************************************************************************
89  //***************************************************************************
91  {
92  public:
93 
94  bitset_nullptr(string_type file_name_, numeric_type line_number_)
95  : bitset_exception(ETL_ERROR_TEXT("bitset:null pointer", ETL_FILE"A"), file_name_, line_number_)
96  {
97  }
98  };
99 
100  //***************************************************************************
103  //***************************************************************************
105  {
106  public:
107 
108  bitset_type_too_small(string_type file_name_, numeric_type line_number_)
109  : bitset_exception(ETL_ERROR_TEXT("bitset:type_too_small", ETL_FILE"B"), file_name_, line_number_)
110  {
111  }
112  };
113 
114  //*************************************************************************
117  //*************************************************************************
118  class ibitset
119  {
120  protected:
121 
122  // The type used for each element in the array.
123 #if !defined(ETL_BITSET_ELEMENT_TYPE)
124  typedef uint_least8_t element_t;
125 #else
126  typedef ETL_BITSET_ELEMENT_TYPE element_t;
127 #endif
128 
129  public:
130 
131  static const element_t ALL_SET = etl::integral_limits<element_t>::max;
132  static const element_t ALL_CLEAR = 0;
133 
134  static const size_t BITS_PER_ELEMENT = etl::integral_limits<element_t>::bits;
135 
138 
139  enum
140  {
142  };
143 
144  //*************************************************************************
146  //*************************************************************************
148  {
149  public:
150 
151  friend class ibitset;
152 
153  //*******************************
155  //*******************************
156  operator bool() const
157  {
158  return p_bitset->test(position);
159  }
160 
161  //*******************************
163  //*******************************
165  {
166  p_bitset->set(position, b);
167  return *this;
168  }
169 
170  //*******************************
172  //*******************************
174  {
175  p_bitset->set(position, bool(r));
176  return *this;
177  }
178 
179  //*******************************
181  //*******************************
183  {
184  p_bitset->flip(position);
185  return *this;
186  }
187 
188  //*******************************
190  //*******************************
191  bool operator~() const
192  {
193  return !p_bitset->test(position);
194  }
195 
196  private:
197 
198  //*******************************
200  //*******************************
201  bit_reference()
202  : p_bitset(ETL_NULLPTR),
203  position(0)
204  {
205  }
206 
207  //*******************************
209  //*******************************
210  bit_reference(ibitset& r_bitset, size_t position_)
211  : p_bitset(&r_bitset),
212  position(position_)
213  {
214  }
215 
216  ibitset* p_bitset;
217  size_t position;
218  };
219 
220  //*************************************************************************
222  //*************************************************************************
223  size_t size() const
224  {
225  return NBITS;
226  }
227 
228  //*************************************************************************
230  //*************************************************************************
231  size_t count() const
232  {
233  size_t n = 0;
234 
235  for (size_t i = 0; i < SIZE; ++i)
236  {
237  n += etl::count_bits(pdata[i]);
238  }
239 
240  return n;
241  }
242 
243  //*************************************************************************
246  //*************************************************************************
247  bool test(size_t position) const
248  {
249  size_t index;
250  element_t mask;
251 
252  if (SIZE == 1)
253  {
254  index = 0;
255  mask = element_t(1) << position;
256  }
257  else
258  {
259  index = position >> etl::log2<BITS_PER_ELEMENT>::value;
260  mask = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
261  }
262 
263  return (pdata[index] & mask) != 0;
264  }
265 
266  //*************************************************************************
268  //*************************************************************************
270  {
271  for (size_t i = 0; i < SIZE; ++i)
272  {
273  pdata[i] = ALL_SET;
274  }
275 
276  pdata[SIZE - 1] &= TOP_MASK;
277 
278  return *this;
279  }
280 
281  //*************************************************************************
283  //*************************************************************************
284  ibitset& set(size_t position, bool value = true)
285  {
286  size_t index;
287  element_t bit;
288 
289  if (SIZE == 1)
290  {
291  index = 0;
292  bit = element_t(1) << position;
293  }
294  else
295  {
296  index = position >> etl::log2<BITS_PER_ELEMENT>::value;
297  bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
298  }
299 
300  if (value)
301  {
302  pdata[index] |= bit;
303  }
304  else
305  {
306  pdata[index] &= ~bit;
307  }
308 
309  return *this;
310  }
311 
312  //*************************************************************************
314  //*************************************************************************
315  ibitset& set(const char* text)
316  {
317  reset();
318 
319  size_t i = etl::min(NBITS, etl::strlen(text));
320 
321  while (i > 0)
322  {
323  set(--i, *text++ == '1');
324  }
325 
326  return *this;
327  }
328 
329  //*************************************************************************
331  //*************************************************************************
332  ibitset& from_string(const char* text)
333  {
334  reset();
335 
336  size_t i = etl::min(NBITS, etl::strlen(text));
337 
338  while (i > 0)
339  {
340  set(--i, *text++ == L'1');
341  }
342 
343  return *this;
344  }
345 
346  //*************************************************************************
348  //*************************************************************************
349  ibitset& from_string(const wchar_t* text)
350  {
351  reset();
352 
353  size_t i = etl::min(NBITS, etl::strlen(text));
354 
355  while (i > 0)
356  {
357  set(--i, *text++ == L'1');
358  }
359 
360  return *this;
361  }
362 
363  //*************************************************************************
365  //*************************************************************************
366  ibitset& from_string(const char16_t* text)
367  {
368  reset();
369 
370  size_t i = etl::min(NBITS, etl::strlen(text));
371 
372  while (i > 0)
373  {
374  set(--i, *text++ == u'1');
375  }
376 
377  return *this;
378  }
379 
380  //*************************************************************************
382  //*************************************************************************
383  ibitset& from_string(const char32_t* text)
384  {
385  reset();
386 
387  size_t i = etl::min(NBITS, etl::strlen(text));
388 
389  while (i > 0)
390  {
391  set(--i, *text++ == U'1');
392  }
393 
394  return *this;
395  }
396 
397  //*************************************************************************
399  //*************************************************************************
400  template <typename T>
402  value() const
403  {
404  T v = T(0);
405 
406  const bool OK = (sizeof(T) * CHAR_BIT) >= (SIZE * BITS_PER_ELEMENT);
407 
408  ETL_ASSERT(OK, ETL_ERROR(etl::bitset_type_too_small));
409 
410  if (OK)
411  {
412  T shift = T(0);
413 
414  for (size_t i = 0; i < SIZE; ++i)
415  {
416  v |= T(pdata[i]) << shift;
417  shift += T(BITS_PER_ELEMENT);
418  }
419  }
420 
421  return v;
422  }
423 
424  //*************************************************************************
426  //*************************************************************************
428  {
429  for (size_t i = 0; i < SIZE; ++i)
430  {
431  pdata[i] = ALL_CLEAR;
432  }
433 
434  return *this;
435  }
436 
437  //*************************************************************************
439  //*************************************************************************
440  ibitset& reset(size_t position)
441  {
442  size_t index;
443  element_t bit;
444 
445  if (SIZE == 1)
446  {
447  index = 0;
448  bit = element_t(1) << position;
449  }
450  else
451  {
452  index = position >> etl::log2<BITS_PER_ELEMENT>::value;
453  bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
454  }
455 
456  pdata[index] &= ~bit;
457 
458  return *this;
459  }
460 
461  //*************************************************************************
463  //*************************************************************************
465  {
466  for (size_t i = 0; i < SIZE; ++i)
467  {
468  pdata[i] = ~pdata[i];
469  }
470 
471  pdata[SIZE - 1] &= TOP_MASK;
472 
473  return *this;
474  }
475 
476  //*************************************************************************
478  //*************************************************************************
479  ibitset& flip(size_t position)
480  {
481  if (position < NBITS)
482  {
483  size_t index;
484  element_t bit;
485 
486  if (SIZE == 1)
487  {
488  index = 0;
489  bit = element_t(1) << position;
490  }
491  else
492  {
493  index = position >> log2<BITS_PER_ELEMENT>::value;
494  bit = element_t(1) << (position & (BITS_PER_ELEMENT - 1));
495  }
496 
497  pdata[index] ^= bit;
498  }
499 
500  return *this;
501  }
502 
503  //*************************************************************************
504  // Are all the bits sets?
505  //*************************************************************************
506  bool all() const
507  {
508  // All but the last.
509  for (size_t i = 0; i < (SIZE - 1); ++i)
510  {
511  if (pdata[i] != ALL_SET)
512  {
513  return false;
514  }
515  }
516 
517  // The last.
518  if (pdata[SIZE - 1] != (ALL_SET & TOP_MASK))
519  {
520  return false;
521  }
522 
523  return true;
524  }
525 
526  //*************************************************************************
528  //*************************************************************************
529  bool any() const
530  {
531  return !none();
532  }
533 
534  //*************************************************************************
536  //*************************************************************************
537  bool none() const
538  {
539  for (size_t i = 0; i < SIZE; ++i)
540  {
541  if (pdata[i] != 0)
542  {
543  return false;
544  }
545  }
546 
547  return true;
548  }
549 
550  //*************************************************************************
554  //*************************************************************************
555  size_t find_first(bool state) const
556  {
557  return find_next(state, 0);
558  }
559 
560  //*************************************************************************
565  //*************************************************************************
566  size_t find_next(bool state, size_t position) const
567  {
568  // Where to start.
569  size_t index;
570  size_t bit;
571 
572  if (SIZE == 1)
573  {
574  index = 0;
575  bit = position;
576  }
577  else
578  {
579  index = position >> log2<BITS_PER_ELEMENT>::value;
580  bit = position & (BITS_PER_ELEMENT - 1);
581  }
582 
583  element_t mask = 1 << bit;
584 
585  // For each element in the bitset...
586  while (index < SIZE)
587  {
588  element_t value = pdata[index];
589 
590  // Needs checking?
591  if ((state && (value != ALL_CLEAR)) ||
592  (!state && (value != ALL_SET)))
593  {
594  // For each bit in the element...
595  while ((bit < BITS_PER_ELEMENT) && (position < NBITS))
596  {
597  // Equal to the required state?
598  if (((value & mask) != 0) == state)
599  {
600  return position;
601  }
602 
603  // Move on to the next bit.
604  mask <<= 1;
605  ++position;
606  ++bit;
607  }
608  }
609  else
610  {
611  position += BITS_PER_ELEMENT;
612  }
613 
614  // Start at the beginning for all other elements.
615  bit = 0;
616  mask = 1;
617 
618  ++index;
619  }
620 
621  return ibitset::npos;
622  }
623 
624  //*************************************************************************
626  //*************************************************************************
627  bool operator[] (size_t position) const
628  {
629  return test(position);
630  }
631 
632  //*************************************************************************
634  //*************************************************************************
635  bit_reference operator [] (size_t position)
636  {
637  return bit_reference(*this, position);
638  }
639 
640  //*************************************************************************
642  //*************************************************************************
643  ibitset& operator &=(const ibitset& other)
644  {
645  for (size_t i = 0; i < SIZE; ++i)
646  {
647  pdata[i] &= other.pdata[i];
648  }
649 
650  return *this;
651  }
652 
653  //*************************************************************************
655  //*************************************************************************
656  ibitset& operator |=(const ibitset& other)
657  {
658  for (size_t i = 0; i < SIZE; ++i)
659  {
660  pdata[i] |= other.pdata[i];
661  }
662 
663  return *this;
664  }
665 
666  //*************************************************************************
668  //*************************************************************************
669  ibitset& operator ^=(const ibitset& other)
670  {
671  for (size_t i = 0; i < SIZE; ++i)
672  {
673  pdata[i] ^= other.pdata[i];
674  }
675 
676  return *this;
677  }
678 
679  //*************************************************************************
681  //*************************************************************************
682  ibitset& operator<<=(size_t shift)
683  {
684  if (SIZE == 1)
685  {
686  pdata[0] <<= shift;
687  }
688  else
689  {
690  size_t source = NBITS - shift - 1;
691  size_t destination = NBITS - 1;
692 
693  for (size_t i = 0; i < (NBITS - shift); ++i)
694  {
695  set(destination--, test(source--));
696  }
697 
698  for (size_t i = 0; i < shift; ++i)
699  {
700  reset(destination--);
701  }
702  }
703 
704  return *this;
705  }
706 
707  //*************************************************************************
709  //*************************************************************************
710  ibitset& operator>>=(size_t shift)
711  {
712  if (SIZE == 1)
713  {
714  pdata[0] >>= shift;
715  }
716  else
717  {
718  size_t source = shift;
719  size_t destination = 0;
720 
721  for (size_t i = 0; i < (NBITS - shift); ++i)
722  {
723  set(destination++, test(source++));
724  }
725 
726  for (size_t i = 0; i < shift; ++i)
727  {
728  reset(destination++);
729  }
730  }
731 
732  return *this;
733  }
734 
735  //*************************************************************************
737  //*************************************************************************
738  ibitset& operator =(const ibitset& other)
739  {
740  if (this != &other)
741  {
742  etl::copy_n(other.pdata, SIZE, pdata);
743  }
744 
745  return *this;
746  }
747 
748  //*************************************************************************
750  //*************************************************************************
751  void swap(ibitset& other)
752  {
753  etl::swap_ranges(pdata, pdata + SIZE, other.pdata);
754  }
755 
756  //*************************************************************************
759  //*************************************************************************
761  {
762  return span_type(pdata, pdata + SIZE);
763  }
764 
765  //*************************************************************************
768  //*************************************************************************
770  {
771  return const_span_type(pdata, pdata + SIZE);
772  }
773 
774  protected:
775 
776  //*************************************************************************
778  //*************************************************************************
779  ibitset& initialise(unsigned long long value)
780  {
781  reset();
782 
783  const size_t SHIFT = (integral_limits<unsigned long long>::bits <= (int)BITS_PER_ELEMENT) ? 0 : BITS_PER_ELEMENT;
784 
785  // Can we do it in one hit?
786  if (SHIFT == 0)
787  {
788  pdata[0] = element_t(value);
789  }
790  else
791  {
792  size_t i = 0;
793 
794  while ((value != 0) && (i < SIZE))
795  {
796  pdata[i++] = value & ALL_SET;
797  value = value >> SHIFT;
798  }
799  }
800 
801  pdata[SIZE - 1] &= TOP_MASK;
802 
803  return *this;
804  }
805 
806  //*************************************************************************
808  //*************************************************************************
809  void invert()
810  {
811  for (size_t i = 0; i < SIZE; ++i)
812  {
813  pdata[i] = ~pdata[i];
814  }
815  }
816 
817  //*************************************************************************
819  //*************************************************************************
821  {
822  return bit_reference(*this, position);
823  }
824 
825  //*************************************************************************
827  //*************************************************************************
828  ibitset(size_t nbits_, size_t size_, element_t* pdata_)
829  : NBITS(nbits_),
830  SIZE(size_),
831  pdata(pdata_)
832  {
833  size_t allocated_bits = SIZE * BITS_PER_ELEMENT;
834  size_t top_mask_shift = ((BITS_PER_ELEMENT - (allocated_bits - NBITS)) % BITS_PER_ELEMENT);
835  TOP_MASK = element_t(top_mask_shift == 0 ? ALL_SET : ~(ALL_SET << top_mask_shift));
836  }
837 
838  //*************************************************************************
840  //*************************************************************************
841  static bool is_equal(const ibitset& lhs, const ibitset&rhs)
842  {
843  return etl::equal(lhs.pdata, lhs.pdata + lhs.SIZE, rhs.pdata);
844  }
845 
846  element_t TOP_MASK;
847 
848  private:
849 
850  // Disable copy construction.
851  ibitset(const ibitset&);
852 
853  const size_t NBITS;
854  const size_t SIZE;
855  element_t* pdata;
856 
857  //*************************************************************************
859  //*************************************************************************
860 #if defined(ETL_POLYMORPHIC_BITSET) || defined(ETL_POLYMORPHIC_CONTAINERS)
861  public:
862  virtual ~ibitset()
863  {
864  }
865 #else
866  protected:
868  {
869  }
870 #endif
871  };
872 
873  //*************************************************************************
879  //*************************************************************************
880  template <const size_t MAXN>
881  class bitset : public etl::ibitset
882  {
883 
884  static const size_t ARRAY_SIZE = (MAXN % BITS_PER_ELEMENT == 0) ? MAXN / BITS_PER_ELEMENT : MAXN / BITS_PER_ELEMENT + 1;
885 
886  public:
887 
888  static const size_t ALLOCATED_BITS = ARRAY_SIZE * BITS_PER_ELEMENT;
889 
890  public:
891 
892  //*************************************************************************
894  //*************************************************************************
896  : etl::ibitset(MAXN, ARRAY_SIZE, data)
897  {
898  reset();
899  }
900 
901  //*************************************************************************
903  //*************************************************************************
904  bitset(const bitset<MAXN>& other)
905  : etl::ibitset(MAXN, ARRAY_SIZE, data)
906  {
907  etl::copy_n(other.data, ARRAY_SIZE, data);
908  }
909 
910  //*************************************************************************
912  //*************************************************************************
913  bitset(unsigned long long value)
914  : etl::ibitset(MAXN, ARRAY_SIZE, data)
915  {
916  initialise(value);
917  }
918 
919  //*************************************************************************
921  //*************************************************************************
922  bitset(const char* text)
923  : etl::ibitset(MAXN, ARRAY_SIZE, data)
924  {
925  set(text);
926  }
927 
928  //*************************************************************************
930  //*************************************************************************
932  {
934  return *this;
935  }
936 
937  //*************************************************************************
939  //*************************************************************************
940  bitset<MAXN>& set(size_t position, bool value = true)
941  {
942  etl::ibitset::set(position, value);
943  return *this;
944  }
945 
946  //*************************************************************************
948  //*************************************************************************
949  bitset<MAXN>& set(const char* text)
950  {
951  ETL_ASSERT(text != 0, ETL_ERROR(bitset_nullptr));
952  etl::ibitset::set(text);
953 
954  return *this;
955  }
956 
957  //*************************************************************************
959  //*************************************************************************
960  bitset<MAXN>& from_string(const char* text)
961  {
962  ibitset::from_string(text);
963 
964  return *this;
965  }
966 
967  //*************************************************************************
969  //*************************************************************************
970  bitset<MAXN>& from_string(const wchar_t* text)
971  {
972  ibitset::from_string(text);
973 
974  return *this;
975  }
976 
977  //*************************************************************************
979  //*************************************************************************
980  bitset<MAXN>& from_string(const char16_t* text)
981  {
982  ibitset::from_string(text);
983 
984  return *this;
985  }
986 
987  //*************************************************************************
989  //*************************************************************************
990  bitset<MAXN>& from_string(const char32_t* text)
991  {
992  ibitset::from_string(text);
993 
994  return *this;
995  }
996 
997  //*************************************************************************
999  //*************************************************************************
1000  template <typename T>
1002  value() const
1003  {
1004  ETL_STATIC_ASSERT((sizeof(T) * CHAR_BIT) >= (ARRAY_SIZE * BITS_PER_ELEMENT), "Type too small");
1005 
1006  return ibitset::value<T>();
1007  }
1008 
1009  //*************************************************************************
1011  //*************************************************************************
1013  {
1014  ibitset::reset();
1015  return *this;
1016  }
1017 
1018  //*************************************************************************
1020  //*************************************************************************
1021  bitset<MAXN>& reset(size_t position)
1022  {
1023  etl::ibitset::reset(position);
1024  return *this;
1025  }
1026 
1027  //*************************************************************************
1029  //*************************************************************************
1031  {
1032  ibitset::flip();
1033  return *this;
1034  }
1035 
1036  //*************************************************************************
1038  //*************************************************************************
1039  bitset<MAXN>& flip(size_t position)
1040  {
1041  etl::ibitset::flip(position);
1042  return *this;
1043  }
1044 
1045  //*************************************************************************
1047  //*************************************************************************
1049  {
1050  if (this != &other)
1051  {
1052  etl::copy_n(other.data, ARRAY_SIZE, data);
1053  }
1054 
1055  return *this;
1056  }
1057 
1058  //*************************************************************************
1060  //*************************************************************************
1062  {
1064  return *this;
1065  }
1066 
1067  //*************************************************************************
1069  //*************************************************************************
1071  {
1073  return *this;
1074  }
1075 
1076  //*************************************************************************
1078  //*************************************************************************
1080  {
1081  ibitset::operator ^=(other);
1082  return *this;
1083  }
1084 
1085  //*************************************************************************
1087  //*************************************************************************
1089  {
1090  etl::bitset<MAXN> temp(*this);
1091 
1092  temp.invert();
1093 
1094  return temp;
1095  }
1096 
1097  //*************************************************************************
1099  //*************************************************************************
1100  bitset<MAXN> operator<<(size_t shift) const
1101  {
1102  etl::bitset<MAXN> temp(*this);
1103 
1104  temp <<= shift;
1105 
1106  return temp;
1107  }
1108 
1109  //*************************************************************************
1111  //*************************************************************************
1112  bitset<MAXN>& operator<<=(size_t shift)
1113  {
1115  return *this;
1116  }
1117 
1118  //*************************************************************************
1120  //*************************************************************************
1121  bitset<MAXN> operator>>(size_t shift) const
1122  {
1123  bitset<MAXN> temp(*this);
1124 
1125  temp >>= shift;
1126 
1127  return temp;
1128  }
1129 
1130  //*************************************************************************
1132  //*************************************************************************
1133  bitset<MAXN>& operator>>=(size_t shift)
1134  {
1136  return *this;
1137  }
1138 
1139  //*************************************************************************
1141  //*************************************************************************
1142  friend bool operator == (const bitset<MAXN>& lhs, const bitset<MAXN>& rhs)
1143  {
1144  return etl::ibitset::is_equal(lhs, rhs);
1145  }
1146 
1147  private:
1148 
1149  element_t data[ARRAY_SIZE];
1150  };
1151 
1152  //***************************************************************************
1155  //***************************************************************************
1156  template <const size_t MAXN>
1158  {
1159  bitset<MAXN> temp(lhs);
1160  temp &= rhs;
1161  return temp;
1162  }
1163 
1164  //***************************************************************************
1167  //***************************************************************************
1168  template<const size_t MAXN>
1170  {
1171  bitset<MAXN> temp(lhs);
1172  temp |= rhs;
1173  return temp;
1174  }
1175 
1176  //***************************************************************************
1179  //***************************************************************************
1180  template<const size_t MAXN>
1182  {
1183  bitset<MAXN> temp(lhs);
1184  temp ^= rhs;
1185  return temp;
1186  }
1187 
1188  //***************************************************************************
1191  //***************************************************************************
1192  template<const size_t MAXN>
1193  bool operator != (const bitset<MAXN>& lhs, const bitset<MAXN>& rhs)
1194  {
1195  return !(lhs == rhs);
1196  }
1197 }
1198 
1199 //*************************************************************************
1201 //*************************************************************************
1202 template <const size_t MAXN>
1204 {
1205  lhs.swap(rhs);
1206 }
1207 
1208 #include "private/minmax_pop.h"
1209 
1210 #undef ETL_FILE
1211 
1212 #endif
void swap(etl::bitset< MAXN > &lhs, etl::bitset< MAXN > &rhs)
swap
Definition: bitset.h:1203
The reference type returned.
Definition: bitset.h:148
bit_reference & operator=(bool b)
Assignment operator.
Definition: bitset.h:164
bool operator~() const
Return the logical inverse of the bit.
Definition: bitset.h:191
bit_reference & flip()
Flip the bit.
Definition: bitset.h:182
Array view.
Definition: span.h:59
ETL_CONSTEXPR14 uint_least8_t count_bits(uint16_t value)
Definition: binary.h:736
Definition: binary.h:361
bitset< MAXN > & operator=(const bitset< MAXN > &other)
operator =
Definition: bitset.h:1048
size_t find_first(bool state) const
Definition: bitset.h:555
size_t find_next(bool state, size_t position) const
Definition: bitset.h:566
bitset< MAXN > & flip()
Flip all of the bits.
Definition: bitset.h:1030
bool any() const
Are any of the bits set?
Definition: bitset.h:529
~ibitset()
Destructor.
Definition: bitset.h:867
ibitset & set(const char *text)
Set from a string.
Definition: bitset.h:315
ibitset & flip(size_t position)
Flip the bit at the position.
Definition: bitset.h:479
bitset< MAXN > & operator<<=(size_t shift)
operator <<=
Definition: bitset.h:1112
bit_reference get_bit_reference(size_t position)
Gets a reference to the specified bit.
Definition: bitset.h:820
ibitset & set()
Set the bit at the position.
Definition: bitset.h:269
bitset(const char *text)
Construct from a string.
Definition: bitset.h:922
ibitset & operator^=(const ibitset &other)
operator ^=
Definition: bitset.h:669
ibitset & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset.h:383
bitset< MAXN > & reset()
Reset all of the bits.
Definition: bitset.h:1012
ibitset & reset(size_t position)
Reset the bit at the position.
Definition: bitset.h:440
ibitset & operator|=(const ibitset &other)
operator |=
Definition: bitset.h:656
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset.h:402
bitset()
Default constructor.
Definition: bitset.h:895
bitset< MAXN > & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset.h:940
bitset< MAXN > operator<<(size_t shift) const
operator <<
Definition: bitset.h:1100
bitset< MAXN > & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset.h:970
ibitset & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset.h:366
void swap(ibitset &other)
swap
Definition: bitset.h:751
ibitset(size_t nbits_, size_t size_, element_t *pdata_)
Constructor.
Definition: bitset.h:828
bitset< MAXN > & set(const char *text)
Set from a string.
Definition: bitset.h:949
etl::enable_if< etl::is_integral< T >::value, T >::type value() const
Put to a value.
Definition: bitset.h:1002
bitset< MAXN > & operator|=(const bitset< MAXN > &other)
operator |=
Definition: bitset.h:1070
bitset< MAXN > & from_string(const char32_t *text)
Set from a u32 string.
Definition: bitset.h:990
bitset< MAXN > & set()
Set all of the bits.
Definition: bitset.h:931
size_t count() const
Count the number of bits set.
Definition: bitset.h:231
ibitset & operator>>=(size_t shift)
operator >>=
Definition: bitset.h:710
bitset< MAXN > & reset(size_t position)
Reset the bit at the position.
Definition: bitset.h:1021
bool operator[](size_t position) const
Read [] operator.
Definition: bitset.h:627
ibitset & reset()
Resets the bitset.
Definition: bitset.h:427
bitset< MAXN > & from_string(const char16_t *text)
Set from a u16 string.
Definition: bitset.h:980
void invert()
Invert.
Definition: bitset.h:809
const_span_type span() const
Definition: bitset.h:769
ibitset & operator&=(const ibitset &other)
operator &=
Definition: bitset.h:643
ibitset & from_string(const wchar_t *text)
Set from a wide string.
Definition: bitset.h:349
ibitset & initialise(unsigned long long value)
Initialise from an unsigned long long.
Definition: bitset.h:779
bitset< MAXN > operator~() const
operator ~
Definition: bitset.h:1088
bitset< MAXN > & flip(size_t position)
Flip the bit at the position.
Definition: bitset.h:1039
bitset(const bitset< MAXN > &other)
Copy constructor.
Definition: bitset.h:904
bitset< MAXN > & operator&=(const bitset< MAXN > &other)
operator &=
Definition: bitset.h:1061
ibitset & operator=(const ibitset &other)
operator =
Definition: bitset.h:738
ibitset & from_string(const char *text)
Set from a string.
Definition: bitset.h:332
friend bool operator==(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
operator ==
Definition: bitset.h:1142
bitset(unsigned long long value)
Construct from a value.
Definition: bitset.h:913
bitset< MAXN > operator>>(size_t shift) const
operator >>
Definition: bitset.h:1121
bool none() const
Are none of the bits set?
Definition: bitset.h:537
bitset< MAXN > & operator>>=(size_t shift)
operator >>=
Definition: bitset.h:1133
ibitset & flip()
Flip all of the bits.
Definition: bitset.h:464
ibitset & operator<<=(size_t shift)
operator <<=
Definition: bitset.h:682
bool test(size_t position) const
Definition: bitset.h:247
bitset< MAXN > & operator^=(const bitset< MAXN > &other)
operator ^=
Definition: bitset.h:1079
size_t size() const
The size of the bitset.
Definition: bitset.h:223
bitset< MAXN > & from_string(const char *text)
Set from a string.
Definition: bitset.h:960
ibitset & set(size_t position, bool value=true)
Set the bit at the position.
Definition: bitset.h:284
span_type span()
Definition: bitset.h:760
static bool is_equal(const ibitset &lhs, const ibitset &rhs)
Compare bitsets.
Definition: bitset.h:841
Definition: bitset.h:882
Definition: bitset.h:77
Definition: bitset.h:91
Definition: bitset.h:105
Definition: bitset.h:119
bitset< MAXN > operator^(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1181
bitset< MAXN > operator|(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1169
bitset< MAXN > operator&(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1157
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: integral_limits.h:54
Definition: log.h:99
enable_if
Definition: type_traits_generator.h:1228
Definition: absolute.h:37
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
size_t strlen(const T *t)
Alternative strlen for all character types.
Definition: char_traits.h:247