libtasks Documentation
1.6
Main Page
Namespaces
Classes
Files
File List
File Members
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
28
bitset
(
int_type
bits
= 8)
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
;
117
int_type
m_bits
;
118
};
119
120
}
// tools
121
}
// tasks
122
123
#endif // _BITSET_H_
tasks::tools::bitset::toggle
void toggle(int_type p)
Toggle a bit.
Definition:
bitset.h:39
tasks::tools::bitset::first
bool first(int_type &idx) const
Definition:
bitset.h:75
tasks::tools::bitset::m_bits
int_type m_bits
Definition:
bitset.h:117
tasks::tools::bitset::set
void set(int_type p)
Set a bit.
Definition:
bitset.h:45
tasks::tools::bitset::m_bitset
std::vector< data_type > m_bitset
Definition:
bitset.h:116
tasks::tools::bitset::to_string
std::string to_string()
Return a string representation.
Definition:
bitset.h:104
tasks::tools::bitset::any
bool any(int_type &offset) const
Definition:
bitset.h:63
tasks::tools::bitset::int_type
std::vector< data_type >::size_type int_type
Definition:
bitset.h:23
tasks::tools::bitset::data_type
std::atomic< bool > data_type
Definition:
bitset.h:22
tasks::tools::bitset::unset
void unset(int_type p)
Unset a bit.
Definition:
bitset.h:51
tasks::tools::bitset
A thread safe lock free bitset.
Definition:
bitset.h:20
tasks::tools::bitset::bits
int_type bits() const
Definition:
bitset.h:33
tasks::tools::bitset::test
bool test(int_type p) const
Test if a bit is set.
Definition:
bitset.h:57
tasks::tools::bitset::buckets
size_t buckets() const
Definition:
bitset.h:36
tasks::tools::bitset::next
bool next(int_type &idx, int_type start=0) const
Definition:
bitset.h:84
tasks::tools::bitset::bitset
bitset(int_type bits=8)
Definition:
bitset.h:28
include
tasks
tools
bitset.h
Generated by
1.8.6