Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fast_bitset.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 15.06.2013 21:35:16 EDT
3 // File: fast_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 __FAST_BITSET_HEADER
12 #define __FAST_BITSET_HEADER
13 
14 //CAN ONLY BE USED UP TO N==64
15 
16 #include "ard_assert.hpp"
17 #include "../util/byte_operation.hpp"
18 
19 namespace ustd {
20  namespace detail {
21 
22  template<bool, bool, bool, bool>
23  struct size_trait_impl {
24  };
25  template<>
26  struct size_trait_impl<false, false, false, false> {
27  typedef uint8_t type;
28  };
29  template<>
30  struct size_trait_impl<true, false, false, false> {
31  typedef uint16_t type;
32  };
33  template<>
34  struct size_trait_impl<true, true, false, false> {
35  typedef uint32_t type;
36  };
37  template<>
38  struct size_trait_impl<true, true, true, false> {
39  typedef uint64_t type;
40  };
41  }//end namespace detail
42  template<uint16_t N>
43  struct size_trait {
44  typedef typename detail::size_trait_impl<(N > 8), (N > 16), (N > 32), (N > 64)>::type type;
45  };
46 
47  template<uint8_t N>
48  class fast_bitset {
49  public:
50  typedef typename size_trait<N>::type T;
51  typedef uint8_t size_type;
52  //------------------- ctors -------------------
53  fast_bitset(): data_() {
54  }
55  fast_bitset(fast_bitset const & arg): data_(arg.data_) {
56  }
58  data_ = rhs.data_;
59  return (*this);
60  }
61  //------------------- ops -------------------
62  void set() { data_ = T(-1); }
63  void set(size_type const & pos, uint8_t const & val = true) { util::write_bit(data_, pos, val); }
64  void flip() { data_ ^= T(-1); }
65  void flip(size_type const & pos) { util::flip_bit(data_, pos); }
66  void reset() { data_ = T(); }
67  void reset(size_type const & pos) { util::clear_bit(data_, pos);}
68  //------------------- getter -------------------
69  bool operator[](size_type const & pos) const {
70  return util::read_bit(data_, pos);
71  }
72  operator T&() {
73  return data_;
74  }
75  //------------------- info -------------------
76  bool any() const {
77  return data_ == T();
78  }
79  bool none() const {
80  return !any();
81  }
82  //------------------- print & serialize-------------------
83  template<typename S>
84  void print(S & os) const {
85  for(size_type i = 0; i < N; ++i) {
86  os << util::read_bit(data_, N - i - 1);
87  }
88  }
89  template<typename Archive>
90  void serialize(Archive & ar) {
91  size_type size_ = N;
92  ar & size_;
93  ASSERT(size_ == N)
94  ar & data_;
95  }
96  private:
97  T data_;
98  };
99 }//end namespace ustd
100 
101 #endif //__FAST_BITSET_HEADER
uint8_t size_type
Definition: fast_bitset.hpp:51
void flip(size_type const &pos)
Definition: fast_bitset.hpp:65
void reset()
Definition: fast_bitset.hpp:66
bool none() const
Definition: fast_bitset.hpp:79
void set()
Definition: fast_bitset.hpp:62
void reset(size_type const &pos)
Definition: fast_bitset.hpp:67
void clear_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:44
void serialize(Archive &ar)
Definition: fast_bitset.hpp:90
fast_bitset()
Definition: fast_bitset.hpp:53
fast_bitset & operator=(fast_bitset const &rhs)
Definition: fast_bitset.hpp:57
void print(S &os) const
Definition: fast_bitset.hpp:84
void write_bit(T &unit, uint8_t const &pos, bool state=true)
Definition: byte_operation.hpp:48
Definition: fast_bitset.hpp:48
void set(size_type const &pos, uint8_t const &val=true)
Definition: fast_bitset.hpp:63
void flip_bit(T &unit, uint8_t const &pos)
Definition: byte_operation.hpp:52
bool operator[](size_type const &pos) const
Definition: fast_bitset.hpp:69
void flip()
Definition: fast_bitset.hpp:64
detail::size_trait_impl<(N > 8),(N > 16),(N > 32),(N > 64)>::type type
Definition: fast_bitset.hpp:44
size_trait< N >::type T
Definition: fast_bitset.hpp:50
bool any() const
Definition: fast_bitset.hpp:76
fast_bitset(fast_bitset const &arg)
Definition: fast_bitset.hpp:55
bool read_bit(T const &unit, uint8_t const &pos)
Definition: byte_operation.hpp:36
#define ASSERT(exp)
Definition: ard_assert.hpp:33
Definition: fast_bitset.hpp:43