Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bitset.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 15.06.2013 19:00:12 EDT
3 // File: bitset.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __BITSET_HEADER
12 #define __BITSET_HEADER
13 
14 #include "ard_assert.hpp"
15 #include "../util/byte_operation.hpp"
16 
17 namespace ustd {
18  template<uint16_t Nbit> //number of bits
19  class bitset {
20  typedef uint16_t size_type;
21 
22  class reference {
23  friend class bitset;
24  reference() {}
25  reference(uint8_t & unit, uint8_t const & subpos): unit_(unit), subpos_(subpos) {
26  }
27  public:
28  operator bool() const {
29  return util::read_bit(unit_, subpos_);
30  }
31  reference& operator=(bool state) {
32  util::write_bit(unit_, subpos_, state);
33  return (*this);
34  }
35  reference& operator=(const reference& state) {
36  util::write_bit(unit_, subpos_, state);
37  return (*this);
38  }
39  reference& flip() {
40  util::flip_bit(unit_, subpos_);
41  return (*this);
42  }
43  bool operator~() const {
44  flip();
45  return (*this);
46  }
47  private:
48  uint8_t & unit_;
49  uint8_t const subpos_;
50  };
51 
52  public:
53  //------------------- ctors -------------------
54  bitset() {
55  reset();
56  }
57  bitset(bitset const & arg) {
58  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
59  array_[i] = arg.array_[i];
60  }
61  }
62  bitset & operator=(bitset const & rhs) {
63  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
64  array_[i] = rhs.array_[i];
65  }
66  return (*this);
67  }
68  //------------------- ops -------------------
69  bitset & set() {
70  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
71  array_[i] = 0xFF;
72  }
73  return (*this);
74  }
75  bitset & set(size_type const & pos, bool const & val = true) {
76  (*this)[pos] = val;
77  return (*this);
78  }
79  bitset & flip() {
80  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
81  array_[i] ^= 0xFF;
82  }
83  return (*this);
84  }
85  bitset & flip(size_type const & pos) {
86  (*this)[pos].flip();
87  return (*this);
88  }
90  flip();
91  }
92  bitset & reset() {
93  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
94  array_[i] = 0;
95  }
96  return (*this);
97  }
98  bitset & reset(size_type const & pos) {
99  set(pos, 0);
100  return (*this);
101  }
102  //------------------- getter -------------------
103  reference operator[](size_type const & pos) {
104  ASSERT(pos >= 0)
105  ASSERT(pos < Nbit)
106  return reference(array_[pos/8], pos % 8); //&7 === %8
107  }
108  bool operator[](size_type const & pos) const {
109  ASSERT(pos >= 0)
110  ASSERT(pos < Nbit)
111  return util::read_bit(array_[pos/8], pos % 8);
112  }
113  //------------------- info -------------------
114  size_type size() const {
115  return Nbit;
116  }
117  bool any() const {
118  for(size_type i = 0; i < (Nbit + 7) / 8; ++i) {
119  if(array_[i] != 0)
120  return true;
121  }
122  return false;
123  }
124  bool none() const {
125  return !any();
126  }
127  size_type count() const {
128  return 0;
129  }
130  //------------------- print & serialize-------------------
131  template<typename S>
132  void print(S & os) const {
133  for(size_type i = 0; i < Nbit; ++i) {
134  os << (*this)[Nbit - i - 1];
135  }
136  }
137  template<typename Archive>
138  void serialize(Archive & ar) {
139  ar & array_;
140  }
141  private:
142  uint8_t array_[(Nbit + 7) / 8];
143  };
144 }//end namespace ustd
145 #endif //__BITSET_HEADER
reference operator[](size_type const &pos)
Definition: bitset.hpp:103
bitset & set(size_type const &pos, bool const &val=true)
Definition: bitset.hpp:75
bitset & reset(size_type const &pos)
Definition: bitset.hpp:98
bitset & flip()
Definition: bitset.hpp:79
bool any() const
Definition: bitset.hpp:117
size_type size() const
Definition: bitset.hpp:114
bitset & operator=(bitset const &rhs)
Definition: bitset.hpp:62
void write_bit(T &unit, uint8_t const &pos, bool state=true)
Definition: byte_operation.hpp:48
bitset & flip(size_type const &pos)
Definition: bitset.hpp:85
void serialize(Archive &ar)
Definition: bitset.hpp:138
void flip_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:52
bitset(bitset const &arg)
Definition: bitset.hpp:57
bitset & set()
Definition: bitset.hpp:69
Definition: bitset.hpp:19
bitset & operator~()
Definition: bitset.hpp:89
void print(S &os) const
Definition: bitset.hpp:132
bool none() const
Definition: bitset.hpp:124
bitset & reset()
Definition: bitset.hpp:92
size_type count() const
Definition: bitset.hpp:127
bool operator[](size_type const &pos) const
Definition: bitset.hpp:108
bool read_bit(T const &unit, uint8_t const &pos)
Definition: byte_operation.hpp:36
#define ASSERT(exp)
Definition: ard_assert.hpp:33
bitset()
Definition: bitset.hpp:54