DeSiGNAR  0.5a
Data Structures General Library
bitset.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGBITSET_H
26 # define DSGBITSET_H
27 
28 # include <array.H>
29 
30 namespace Designar
31 {
32 
33  class Byte
34  {
35  unsigned int b1 : 1;
36  unsigned int b2 : 1;
37  unsigned int b3 : 1;
38  unsigned int b4 : 1;
39  unsigned int b5 : 1;
40  unsigned int b6 : 1;
41  unsigned int b7 : 1;
42  unsigned int b8 : 1;
43 
44  public:
45  Byte();
46 
47  Byte(bool, bool, bool, bool, bool, bool, bool, bool);
48 
49  Byte(int);
50 
51  bool get_bit(unsigned char) const;
52 
53  void set_bit(unsigned char, bool);
54 
55  void flip();
56 
57  void set(int);
58 
59  int to_num() const;
60 
61  operator int() const;
62 
63  std::string to_string() const;
64 
65  operator std::string() const;
66 
67  Byte & operator = (const Byte &);
68 
69  Byte & operator = (int);
70 
72 
73  void operator <<= (nat_t);
74 
76 
77  void operator >>= (nat_t);
78 
80 
81  void operator &= (nat_t);
82 
84 
85  void operator |= (nat_t);
86 
87  Byte operator ~ ();
88 
89  bool operator == (int) const;
90 
91  bool operator != (int) const;
92 
93  bool operator < (int) const;
94 
95  bool operator <= (int) const;
96 
97  bool operator > (int) const;
98 
99  bool operator >= (int) const;
100  };
101 
102  class DynBitSet
103  {
104  public:
105  using ItemType = bool;
106 
107  class RWProxy
108  {
109  DynBitSet & dbs;
110  nat_t i;
111 
112  public:
113  RWProxy(DynBitSet &, nat_t);
114 
115  operator bool() const;
116 
117  bool operator = (bool);
118  };
119 
120  private:
121  static constexpr unsigned char BIT_SIZE = 8;
122 
123  nat_t num_bits;
124  DynArray<Byte> bit_array;
125 
126  static nat_t which_byte(nat_t num_bit)
127  {
128  return num_bit / 8;
129  }
130 
131  static nat_t which_bit_in_byte(nat_t num_bit)
132  {
133  return num_bit % 8;
134  }
135 
136  static nat_t how_many_bytes(nat_t num_bits)
137  {
138  return which_byte(num_bits - 1) + 1;
139  }
140 
141  void init(nat_t, bool);
142 
143  public:
144  DynBitSet();
145 
146  DynBitSet(nat_t, bool val = false);
147 
148  DynBitSet(const DynBitSet &);
149 
150  DynBitSet(DynBitSet &&);
151 
152  DynBitSet(const std::initializer_list<bool> &);
153 
154  ~DynBitSet() = default;
155 
156  DynBitSet & operator = (const DynBitSet &);
157 
159 
160  void swap(DynBitSet &);
161 
162  bool is_empty() const;
163 
164  void clear();
165 
166  nat_t size() const;
167 
168  void append(bool);
169 
170  bool remove_last();
171 
172  void set_bit(nat_t, bool);
173 
174  bool get_bit(nat_t) const;
175 
176  std::string to_string() const;
177 
178  void write(std::ostream &) const;
179 
180  void read(std::istream &);
181 
182  const RWProxy operator [] (nat_t) const;
183 
184  RWProxy operator [] (nat_t);
185 
186  class Iterator : public ArrayIterator<Iterator, DynBitSet, bool, true>
187  {
189  using Base::Base;
190 
191  public:
192  RWProxy get_current();
193 
194  RWProxy get_current() const;
195  };
196 
197  Iterator begin();
198 
199  Iterator begin() const;
200 
201  Iterator end();
202 
203  Iterator end() const;
204  };
205 
206 } // end namespace Designar
207 
208 # endif // DSGBITSET
Definition: bitset.H:186
void operator>>=(nat_t)
std::string to_string() const
bool operator!=(int) const
bool ItemType
Definition: bitset.H:105
void operator|=(nat_t)
bool operator<(int) const
bool operator>(int) const
Byte operator|(nat_t)
void operator&=(nat_t)
void set_bit(unsigned char, bool)
bool operator==(int) const
bool operator<=(int) const
Byte operator<<(nat_t)
Definition: array.H:375
Byte & operator=(const Byte &)
void operator<<=(nat_t)
Byte operator&(nat_t)
Definition: bitset.H:33
Byte operator>>(nat_t)
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
int to_num() const
Definition: bitset.H:107
bool operator>=(int) const
bool get_bit(unsigned char) const
Definition: array.H:35
Definition: bitset.H:102