]>
sourceware.org Git - systemtap.git/blob - bpf-bitset.h
2 // Copyright (C) 2016 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
9 // This differs from std::bitset in being runtime sized, and from
10 // boost::dynamic_bitset in being space efficient in multiple dimensions.
12 // ??? Could be templatized to n-dimensions.
25 void throw_out_of_range(const char *) __attribute__((noreturn
));
29 typedef size_t word_t
;
30 static size_t const bits_per_word
= sizeof(word_t
) * CHAR_BIT
;
32 inline size_t round_words(size_t bits
)
34 return (bits
+ bits_per_word
- 1) / bits_per_word
;
43 bit_ref(); // not present
46 bit_ref(word_t
*w
, size_t i
) : word(w
), index(i
) { }
48 void reset() { *word
&= ~((word_t
)1 << index
); }
49 void set() { *word
|= (word_t
)1 << index
; }
50 void flip() { *word
^= (word_t
)1 << index
; }
51 void set(bool v
) { if (v
) set(); else reset(); }
52 bool test() const { return (*word
>> index
) & 1; }
54 operator bool() const { return test(); }
55 bool operator!() const { return !test(); }
57 bool operator|= (bool o
)
69 bool operator&= (bool o
)
80 bool operator-= (bool o
)
91 bool operator^= (bool o
)
104 friend class set1_ref
;
107 set1_const_ref(); // not present
108 set1_const_ref
& operator= (const set1_const_ref
&); // not present
115 static const size_t npos
= -1;
117 set1_const_ref(word_t
*d
, size_t w
) : data(d
), words(w
) { }
118 set1_const_ref(const set1_const_ref
&o
) : data(o
.data
), words(o
.words
) { }
120 bool operator!= (const set1_const_ref
&o
) const
122 return !(*this == o
);
125 size_t size() const { return words
* bits_per_word
; }
127 bool test(size_t i
) const
129 size_t w
= i
/ bits_per_word
;
130 size_t o
= i
% bits_per_word
;
132 throw_out_of_range("bpf::bitset::set1_ref::test");
133 return (data
[w
] >> o
) & 1;
136 bool operator[] (size_t i
) const { return test(i
); }
139 bool operator== (const set1_const_ref
&o
) const;
140 bool is_subset_of(const set1_const_ref
&o
) const;
141 size_t find_first() const;
142 size_t find_next(size_t i
) const;
143 size_t find_next_zero(size_t i
) const;
146 class set1_ref
: public set1_const_ref
149 set1_ref(); // not present
152 set1_ref(size_t *d
, size_t w
) : set1_const_ref(d
, w
) { }
153 set1_ref(const set1_ref
&o
) : set1_const_ref(o
.data
, o
.words
) { }
155 bit_ref
operator[] (size_t i
)
157 size_t w
= i
/ bits_per_word
;
158 size_t o
= i
% bits_per_word
;
160 throw_out_of_range("bpf::bitset::set1_ref::operator[]");
161 return bit_ref(data
+ w
, o
);
164 set1_ref
& operator= (const set1_const_ref
&o
)
166 if (words
!= o
.words
)
167 throw_out_of_range("bpf::bitset::set1_ref::operator=");
168 memcpy(data
, o
.data
, words
* sizeof(*data
));
172 set1_ref
& operator= (const set1_ref
&o
)
174 return operator=(static_cast<const set1_const_ref
&>(o
));
177 set1_ref
& operator|= (const set1_const_ref
&o
)
179 if (words
!= o
.words
)
180 throw_out_of_range("bpf::bitset::set1_ref::operator|=");
181 for (size_t i
= 0; i
< words
; ++i
)
182 data
[i
] |= o
.data
[i
];
186 set1_ref
& operator&= (const set1_const_ref
&o
)
188 if (words
!= o
.words
)
189 throw_out_of_range("bpf::bitset::set1_ref::operator&=");
190 for (size_t i
= 0; i
< words
; ++i
)
191 data
[i
] &= o
.data
[i
];
195 set1_ref
& operator-= (const set1_const_ref
&o
)
197 if (words
!= o
.words
)
198 throw_out_of_range("bpf::bitset::set1_ref::operator-=");
199 for (size_t i
= 0; i
< words
; ++i
)
200 data
[i
] &= ~o
.data
[i
];
204 void clear() { memset(data
, 0, words
* sizeof(*data
)); }
206 void reset(size_t i
) { (*this)[i
].reset(); }
207 void set(size_t i
) { (*this)[i
].set(); }
208 void set(size_t i
, bool v
) { (*this)[i
].set(v
); }
211 class set1
: public set1_ref
214 set1(); // not present
218 set1(const set1_const_ref
&o
);
229 set2(); // not present
232 set2(size_t m1
, size_t m2
);
236 size_t size() const { return n1
; }
238 set2
& operator= (const set2
&o
)
240 if (n1
!= o
.n1
|| w2
!= o
.w2
)
241 throw_out_of_range("bpf::bitset::set2::operator=");
242 memcpy(data
, o
.data
, n1
* w2
* sizeof(word_t
));
245 set1_ref
operator[] (size_t i
)
248 throw_out_of_range("set2::operator[]");
249 return set1_ref(data
+ w2
* i
, w2
);
252 set1_const_ref
operator[] (size_t i
) const
255 throw_out_of_range("set2::operator[]");
256 return set1_const_ref(data
+ w2
* i
, w2
);
259 void clear() { memset(data
, 0, n1
* w2
* sizeof(*data
)); }
262 std::ostream
& operator<< (std::ostream
&o
, const set1_const_ref
&s
);
264 } // namespace bitset
267 #endif // BPF_BITSET_H
This page took 0.046542 seconds and 6 git commands to generate.