Add main page for Doxygen generated documentation.
[tairent.git] / src / core / bitfield.cpp
blobd5c80759adc932431b9a044a3f52c1a1f0d797d9
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #include "bitfield.h"
19 namespace Tairent
22 namespace Core
25 /* {{{ BitField::BitField(size_t) */
26 BitField::BitField(size_t len) : length(len), remaining(length)
28 size_t s = (len >> 3) + (len & 0x07 ? 1 : 0);
29 data = new char[s];
30 memset(data, 0, s);
32 /* }}} */
34 /* {{{ BitField::BitField(const char *, size_t) */
35 BitField::BitField(const char *mem, size_t len) : length(len), remaining(length)
37 size_t s = (len >> 3) + (len & 0x07 ? 1 : 0);
38 data = new char[s];
39 memcpy(data, mem, s);
41 for (size_t i = 0; i < len; ++i)
42 if (getBit(i))
43 remaining -= 1;
45 /* }}} */
47 /* {{{ BitField::~BitField() */
48 BitField::~BitField()
50 delete [] data;
52 /* }}} */
54 /* {{{ BitField::getBit(size_t) */
55 bool BitField::getBit(size_t index)
57 return data[index >> 3] & (0x80 >> (index & 0x07));
59 /* }}} */
61 /* {{{ BitField::setBit(size_t, bool) */
62 void BitField::setBit(size_t index, bool value)
64 char orig = data[index >> 3];
65 if (value) {
66 data[index >> 3] |= 0x80 >> (index & 0x07);
67 remaining += orig ^ data[index >> 3] ? 1 : 0;
68 } else {
69 data[index >> 3] &= 0x7f >> (index & 0x07);
70 remaining -= orig ^ data[index >> 3] ? 1 : 0;
73 /* }}} */
75 }; // namespace Core
77 }; // namespace Tairent
79 // vim: ai sw=4 ts=4 noet fdm=marker