Add missing #include to src/main/torrentclient.h.
[tairent.git] / src / core / bitfield.cpp
blob007fa64ac77673d6e8c10f8c0b00e41e87d34a27
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::setAll(bool) */
62 void BitField::setAll(bool value)
64 size_t s = (length >> 3) + (length & 0x07 ? 1 : 0);
65 memset(data, value ? 0xff : 0x00, s);
66 remaining = 0;
68 /* }}} */
70 /* {{{ BitField::setBit(size_t, bool) */
71 void BitField::setBit(size_t index, bool value)
73 char orig = data[index >> 3];
74 if (value) {
75 data[index >> 3] |= 0x80 >> (index & 0x07);
76 remaining -= orig ^ data[index >> 3] ? 1 : 0;
77 } else {
78 data[index >> 3] &= 0x7f >> (index & 0x07);
79 remaining += orig ^ data[index >> 3] ? 1 : 0;
82 /* }}} */
84 }; // namespace Core
86 }; // namespace Tairent
88 // vim: ai sw=4 ts=4 noet fdm=marker