1
/* PeTe - Petri Engine exTremE
2
* Copyright (C) 2011 Jonas Finnemann Jensen <jopsen@gmail.com>,
3
* Thomas Søndersø Nielsen <primogens@gmail.com>,
4
* Lars Kærlund Østergaard <larsko@gmail.com>
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
namespace PetriEngine {
28
namespace Structures {
30
/** Representation of a BitField */
33
/** Definition of underlying type for the bitfield*/
34
typedef unsigned int _word;
36
/** Number of bits per word */
37
static const size_t _bitsPerWord = CHAR_BIT * sizeof (_word);
39
/** Number of words for size */
40
size_t _words() const {
41
return (_size + _bitsPerWord - 1) / _bitsPerWord;
44
/** Word offset for a given position */
45
static size_t _wordOffset(size_t pos) {
46
return pos / _bitsPerWord;
49
/** Bit offset offset for a given position */
50
static size_t _bitOffset(size_t pos) {
51
return pos % _bitsPerWord;
54
/** Bitmask for pos in a word */
55
static _word _bitmask(size_t pos) {
56
return ((_word) 1) << _bitOffset(pos);
59
/** Underlying word array */
64
/** Create a new empty bitfield */
65
BitField(size_t size) {
67
_bits = new _word[_words()];
71
/** Copy create a bitfield from another */
72
BitField(const BitField& bf) {
74
_bits = new _word[_words()];
75
for (size_t i = 0; i < _words(); i++)
76
_bits[i] = bf._bits[i];
87
/** Get the size of the bitfield*/
94
for (size_t i = 0; i < _words(); i++)
99
/** Clear bit at position pos */
100
BitField& clear(size_t pos) {
101
_bits[_wordOffset(pos)] &= ~_bitmask(pos);
105
/** Test bit at position pos */
106
bool test(size_t pos) const {
107
return (_bits[_wordOffset(pos)] & _bitmask(pos)) != (_word) 0;
112
memset(_bits, 0xff, sizeof (_word) * _words());
114
_bits[_words() - 1] = _bits[_words() - 1] & ~((~(_word) 0) << _bitOffset(_size));
118
/** Set bit a position pos */
119
BitField& set(size_t pos) {
120
_bits[_wordOffset(pos)] |= _bitmask(pos);
124
/** Set bit a position pos to value */
125
BitField& set(size_t pos, bool value) {
135
for (size_t i = 0; i < _words() - 1; i++)
136
_bits[i] = ~_bits[i];
138
_bits[_words() - 1] = ~_bits[_words() - 1] & ~((~(_word) 0) << _bitOffset(_size));
142
/** Flip bit at position pos */
143
BitField& flip(size_t pos) {
144
_bits[_wordOffset(pos)] ^= _bitmask(pos);
148
/** Test if any bit is set */
150
for (size_t i = 0; i < _words(); i++)
151
if (_bits[i] != (_word) 0)
156
/** Test if no bits are set */
161
/** Get the first set element, -1 if none */
163
for (size_t i = 0; i < _size; i++)
169
BitField& operator=(const BitField& rhs) {
170
for (size_t i = 0; i < _words(); i++)
171
_bits[i] = rhs._bits[i];
175
BitField& operator&=(const BitField& rhs) {
176
for (size_t i = 0; i < _words(); i++)
177
_bits[i] &= rhs._bits[i];
181
BitField& operator|=(const BitField& rhs) {
182
for (size_t i = 0; i < _words(); i++)
183
_bits[i] |= rhs._bits[i];
187
BitField& operator^=(const BitField& rhs) {
188
for (size_t i = 0; i < _words(); i++)
189
_bits[i] ^= rhs._bits[i];
193
BitField operator~() const {
194
return BitField(*this).flip();
197
bool operator==(const BitField& rhs) const {
198
for (size_t i = 0; i < _words(); i++)
199
if (_bits[i] != rhs._bits[i])
204
bool operator!=(const BitField& rhs) const {
205
for (size_t i = 0; i < _words(); i++)
206
if (_bits[i] != rhs._bits[i])
212
inline BitField operator&(const BitField& lhs, const BitField& rhs) {
213
BitField retval(lhs);
218
inline BitField operator|(const BitField& lhs, const BitField& rhs) {
219
BitField retval(lhs);
224
inline BitField operator^(const BitField& lhs, const BitField& rhs) {
225
BitField retval(lhs);