mn_fast_type.hpp
Go to the documentation of this file.
1 
19 #ifndef MINLIB_FAST_TYPE_H_
20 #define MINLIB_FAST_TYPE_H_
21 
22 #include "mn_config.hpp"
23 
24 #include "mn_algorithm.hpp"
25 #include "mn_def.hpp"
26 #include "mn_uint128.hpp"
27 
28 
29 #include <stdint.h>
30 
31 namespace mn {
35  class basic_fbit {
36  public:
40  union {
41  unsigned char bit : 1;
42  };
46  basic_fbit() : bit(0) { }
51  basic_fbit(bool b) { bit = b ? 1 : 0; }
55  basic_fbit(const basic_fbit& b) { bit = b.bit; }
56 
58  bit = other.bit;
59  }
60  bool operator == (const basic_fbit& other) {
61  return bit == other.bit;
62  }
63  bool operator != (const basic_fbit& other) {
64  return bit != other.bit;
65  }
66 
67  operator bool() { return bit == 1; }
71  void flip() { bit = (bit ==1) ? 0 : 1; }
72  };
73 
74  #include <stddef.h>
75 
80  template <size_t Bits, typename TBaseType>
81  class fast_type {
82  public:
84  using value_type = TBaseType;
85  using bit_type = fbit;
86 
90  union {
92  value_type Value;
94  bit_type bits[Bits];
95  };
103  explicit fast_type(value_type v) : Value(v) {}
104  fast_type(const fast_type& c) : Value(c.Value) { }
105 
110  unsigned int count() {
111  unsigned int i;
112  for(int j = 0; j < Bits; j++)
113  i += bits[i].bit;
114  return i;
115  }
120  unsigned int zeros() {
121  return Bits - count();
122  }
128  unsigned int set(size_t pos, bool p) {
129  bits[pos].bit = p ? 1 : 0;
130  }
135  size_t size() { return Bits; }
136 
141  value_type& operator [] (const size_t p) {
142  return bits[p];
143  }
149  Value = v; return *this;
150  }
151 
153  Value = other.Value; return *this;
154  }
159  bool operator == (self_type& other) {
160  return Value == other.Value;
161  }
166  bool operator != (self_type& other) {
167  return Value != other.Value;
168  }
169  bool operator <= (self_type& other) {
170  return Value <= other.Value;
171  }
172  bool operator >= (self_type& other) {
173  return Value >= other.Value;
174  }
175  bool operator < (self_type& other) {
176  return Value < other.Value;
177  }
178  bool operator > (self_type& other) {
179  return Value > other.Value;
180  }
181 
183  Value += other.Value; return *this;
184  }
186  Value -= other.Value; return *this;
187  }
189  Value *= other.Value; return *this;
190  }
192  Value &= other.Value; return *this;
193  }
195  Value |= other.Value; return *this;
196  }
198  Value ^= other.Value; return *this;
199  }
201  Value <<= other.Value; return *this;
202  }
204  Value >>= other.Value; return *this;
205  }
207  Value /= other.Value; return *this;
208  }
210  Value += v; return *this;
211  }
213  Value -= v; return *this;
214  }
216  Value *= v; return *this;
217  }
219  Value /= v; return *this;
220  }
222  Value <<= v; return *this;
223  }
225  Value >>= v; return *this;
226  }
228  return self_type(*this) -= rhs;
229  }
231  return self_type(*this) += rhs;
232  }
234  return self_type(*this) *= rhs;
235  }
237  return self_type(*this) /= rhs;
238  }
240  return self_type(*this) <<= rhs;
241  }
243  return self_type(*this) >>= rhs;
244  }
246  self_type result = self_type(*this);
247  result.Value |= rhs.Value;
248  return result;
249  }
251  self_type result = self_type(*this);
252  result.Value ^= rhs.Value;
253  return result;
254  }
256  self_type result = self_type(*this);
257  result.Value &= rhs.Value;
258  return result;
259  }
261  self_type result = self_type(*this);
262  result.Value = ~result.Value;
263  return result;
264  }
265 
267  Value++; return *this;
268  }
270  Value--; return *this;
271  }
273  Value = ~Value; return *this;
274  }
275  };
276 
277 
278 
279 
280 
284 
289 
295 
300 
306 
307 }
308 
309 #endif
Definition: mn_fast_type.hpp:35
void flip()
Definition: mn_fast_type.hpp:71
basic_fbit(const basic_fbit &b)
Definition: mn_fast_type.hpp:55
basic_fbit()
Definition: mn_fast_type.hpp:46
bool operator!=(const basic_fbit &other)
Definition: mn_fast_type.hpp:63
basic_fbit(bool b)
Definition: mn_fast_type.hpp:51
basic_fbit & operator=(const basic_fbit &other)
Definition: mn_fast_type.hpp:57
bool operator==(const basic_fbit &other)
Definition: mn_fast_type.hpp:60
Definition: mn_fast_type.hpp:81
self_type & operator+(const self_type &rhs)
Definition: mn_fast_type.hpp:230
self_type & operator=(value_type &v)
Definition: mn_fast_type.hpp:148
self_type & operator|=(self_type &other)
Definition: mn_fast_type.hpp:194
bool operator<=(self_type &other)
Definition: mn_fast_type.hpp:169
size_t size()
Definition: mn_fast_type.hpp:135
fbit bit_type
Definition: mn_fast_type.hpp:85
self_type & operator+=(self_type &other)
Definition: mn_fast_type.hpp:182
self_type & operator/(const self_type &rhs)
Definition: mn_fast_type.hpp:236
self_type & operator--()
Definition: mn_fast_type.hpp:269
fast_type(value_type v)
Definition: mn_fast_type.hpp:103
self_type & operator>>=(self_type &other)
Definition: mn_fast_type.hpp:203
self_type & operator*=(self_type &other)
Definition: mn_fast_type.hpp:188
self_type & operator<<=(self_type &other)
Definition: mn_fast_type.hpp:200
self_type & operator^=(self_type &other)
Definition: mn_fast_type.hpp:197
self_type & operator/=(self_type &other)
Definition: mn_fast_type.hpp:206
self_type & operator*(const self_type &rhs)
Definition: mn_fast_type.hpp:233
self_type & operator++()
Definition: mn_fast_type.hpp:266
self_type & operator&(const self_type &rhs)
Definition: mn_fast_type.hpp:255
self_type & operator-(const self_type &rhs)
Definition: mn_fast_type.hpp:227
value_type & operator[](const size_t p)
Definition: mn_fast_type.hpp:141
bool operator!=(self_type &other)
Definition: mn_fast_type.hpp:166
bool operator>(self_type &other)
Definition: mn_fast_type.hpp:178
TBaseType value_type
Definition: mn_fast_type.hpp:84
bool operator<(self_type &other)
Definition: mn_fast_type.hpp:175
self_type & operator-=(self_type &other)
Definition: mn_fast_type.hpp:185
self_type & operator|(const self_type &rhs)
Definition: mn_fast_type.hpp:245
self_type & operator>>(const self_type &rhs)
Definition: mn_fast_type.hpp:242
fast_type(const fast_type &c)
Definition: mn_fast_type.hpp:104
fast_type< Bits, TBaseType > self_type
Definition: mn_fast_type.hpp:83
unsigned int count()
Definition: mn_fast_type.hpp:110
self_type & operator^(const self_type &rhs)
Definition: mn_fast_type.hpp:250
self_type & operator&=(self_type &other)
Definition: mn_fast_type.hpp:191
bool operator>=(self_type &other)
Definition: mn_fast_type.hpp:172
self_type & operator~()
Definition: mn_fast_type.hpp:260
self_type & operator<<(const self_type &rhs)
Definition: mn_fast_type.hpp:239
unsigned int set(size_t pos, bool p)
Definition: mn_fast_type.hpp:128
bool operator==(self_type &other)
Definition: mn_fast_type.hpp:159
unsigned int zeros()
Definition: mn_fast_type.hpp:120
Basic algorithmens This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniT...
Definition: mn_allocator_typetraits.hpp:25
Definition: mn_uint128.hpp:30