libtasks Documentation  1.6
bitset.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _BITSET_H_
9 #define _BITSET_H_
10 
11 #include <atomic>
12 #include <vector>
13 #include <sstream> // for to_string
14 #include <cassert>
15 
16 namespace tasks {
17 namespace tools {
18 
20 class bitset {
21  public:
22  using data_type = std::atomic<bool>;
23  using int_type = std::vector<data_type>::size_type;
24 
29  : m_bitset(std::vector<data_type>(bits)),
30  m_bits(bits) {}
31 
33  inline int_type bits() const { return m_bits; }
34 
36  inline size_t buckets() const { return m_bitset.size(); }
37 
39  inline void toggle(int_type p) {
40  assert(p < m_bits);
41  m_bitset[p] = !m_bitset[p];
42  }
43 
45  inline void set(int_type p) {
46  assert(p < m_bits);
47  m_bitset[p] = true;
48  }
49 
51  inline void unset(int_type p) {
52  assert(p < m_bits);
53  m_bitset[p] = false;
54  }
55 
57  inline bool test(int_type p) const {
58  assert(p < m_bits);
59  return m_bitset[p];
60  }
61 
63  inline bool any(int_type& offset) const {
64  offset = 0;
65  for (auto& bit : m_bitset) {
66  if (bit == true) {
67  return true;
68  }
69  offset++;
70  }
71  return false;
72  }
73 
74  // \copydoc any(int_type& offset)
75  inline bool first(int_type& idx) const {
76  return any(idx);
77  }
78 
84  inline bool next(int_type& idx, int_type start = 0) const {
85  assert(start < m_bits);
86  int_type chk_idx = start;
87  int_type chk_cnt = 0;
88  do {
89  if (test(chk_idx)) {
90  idx = chk_idx;
91  return true;
92  }
93  chk_idx++;
94  // overflow check
95  if (chk_idx == m_bits) {
96  chk_idx = 0;
97  }
98  chk_cnt++;
99  } while (chk_cnt < m_bits);
100  return false;
101  }
102 
104  std::string to_string() {
105  std::stringstream out;
106  for (int_type i = 0; i < m_bits; i++) {
107  out << (test(i) ? "1" : "0");
108  if (((i + 1) % 8) == 0) {
109  out << " ";
110  }
111  }
112  return out.str();
113  }
114 
115  private:
116  std::vector<data_type> m_bitset;
118 };
119 
120 } // tools
121 } // tasks
122 
123 #endif // _BITSET_H_
void toggle(int_type p)
Toggle a bit.
Definition: bitset.h:39
bool first(int_type &idx) const
Definition: bitset.h:75
int_type m_bits
Definition: bitset.h:117
void set(int_type p)
Set a bit.
Definition: bitset.h:45
std::vector< data_type > m_bitset
Definition: bitset.h:116
std::string to_string()
Return a string representation.
Definition: bitset.h:104
bool any(int_type &offset) const
Definition: bitset.h:63
std::vector< data_type >::size_type int_type
Definition: bitset.h:23
std::atomic< bool > data_type
Definition: bitset.h:22
void unset(int_type p)
Unset a bit.
Definition: bitset.h:51
A thread safe lock free bitset.
Definition: bitset.h:20
int_type bits() const
Definition: bitset.h:33
bool test(int_type p) const
Test if a bit is set.
Definition: bitset.h:57
size_t buckets() const
Definition: bitset.h:36
bool next(int_type &idx, int_type start=0) const
Definition: bitset.h:84
bitset(int_type bits=8)
Definition: bitset.h:28