1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an "as is"
* basis, without warranty of any kind, either expressed, implied, or
* statutory, including, without limitation, warranties that the
* Covered Software is free of defects, merchantable, fit for a
* particular purpose or non-infringing.
* See the Mozilla Public License v. 2.0 for more details.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "misc/bitset.h"
namespace MR {
const uint8_t BitSet::masks[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
BitSet::BitSet (const size_t b, const bool allocator) :
bits (b),
bytes ((bits + 7) / 8),
data (new uint8_t[bytes])
{
memset (data, (allocator ? 0xFF : 0x00), bytes);
}
BitSet::BitSet (const BitSet& that) :
bits (that.bits),
bytes (that.bytes),
data (new uint8_t[bytes])
{
memcpy (data, that.data, bytes);
}
BitSet::~BitSet() {
delete[] data; data = nullptr;
}
void BitSet::resize (const size_t new_size, const bool allocator)
{
size_t new_bits;
new_bits = new_size;
const size_t new_bytes = (new_bits + 7) / 8;
uint8_t* new_data = new uint8_t[new_bytes];
if (bytes) {
if (new_bytes > bytes) {
memcpy (new_data, data, bytes);
memset (new_data + bytes, (allocator ? 0xFF : 0x00), new_bytes - bytes);
if (have_excess_bits())
new_data[bytes - 1] = allocator ?
(data[bytes - 1] | excess_bit_mask()) :
(data[bytes - 1] & ~excess_bit_mask());
} else {
memcpy (new_data, data, new_bytes);
}
} else {
memset (new_data, (allocator ? 0xFF : 0x00), new_bytes);
}
delete[] data;
bits = new_bits;
bytes = new_bytes;
data = new_data;
new_data = nullptr;
}
void BitSet::clear (const bool allocator)
{
memset(data, (allocator ? 0xFF : 0x00), bytes);
}
bool BitSet::full() const
{
const size_t bytes_to_test = have_excess_bits() ? bytes - 1 : bytes;
for (size_t i = 0; i != bytes_to_test; ++i) {
if (data[i] != 0xFF)
return false;
}
if (!have_excess_bits())
return true;
if ((data[bytes - 1] | excess_bit_mask()) != 0xFF)
return false;
return true;
}
bool BitSet::empty() const
{
const size_t bytes_to_test = have_excess_bits() ? bytes - 1 : bytes;
for (size_t i = 0; i != bytes_to_test; ++i) {
if (data[i])
return false;
}
if (!have_excess_bits())
return true;
if (data[bytes - 1] & ~excess_bit_mask())
return false;
return true;
}
size_t BitSet::count () const
{
static const uint8_t byte_to_count[256] = /*0x00-0x0F*/ { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
/*0x10-0x1F*/ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
/*0x20-0x2F*/ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
/*0x30-0x3F*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0x40-0x3F*/ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
/*0x50-0x5F*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0x60-0x6F*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0x70-0x7F*/ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
/*0x80-0x8F*/ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
/*0x90-0x9F*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0xA0-0xAF*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0xB0-0xBF*/ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
/*0xC0-0xCF*/ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
/*0xD0-0xDF*/ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
/*0xE0-0xEF*/ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
/*0xF0-0xFF*/ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };
size_t count = 0;
const size_t bytes_to_test = have_excess_bits() ? bytes - 1 : bytes;
for (size_t i = 0; i != bytes_to_test; ++i)
count += byte_to_count[data[i]];
for (size_t i = 8 * bytes_to_test; i != bits; ++i) {
if ((*this)[i])
++count;
}
return count;
}
std::ostream& operator<< (std::ostream& stream, const BitSet& d)
{
if (!d.bytes)
return stream;
static const char dbyte_to_hex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
auto byte_to_hex = [] (const uint8_t d)
{
std::string out;
for (size_t i = 0; i != 2; ++i) {
const uint8_t dm = i ? (d & 0x0F) : ((d & 0xF0) >> 4);
out.push_back (dbyte_to_hex[dm]);
}
return out;
};
stream << "0x";
if (d.have_excess_bits()) {
stream << byte_to_hex (d.data[d.bytes - 1] & (0xFF >> d.excess_bits()));
for (ssize_t i = d.bytes - 2; i >= 0; --i)
stream << byte_to_hex (d.data[i]);
} else {
for (ssize_t i = d.bytes - 1; i >= 0; --i)
stream << byte_to_hex (d.data[i]);
}
return stream;
}
BitSet& BitSet::operator= (const BitSet& that)
{
delete[] data;
bits = that.bits;
bytes = that.bytes;
data = new uint8_t[bytes];
memcpy (data, that.data, bytes);
return *this;
}
bool BitSet::operator== (const BitSet& that) const
{
if (bits != that.bits)
return false;
if (have_excess_bits()) {
if (memcmp (data, that.data, bytes - 1))
return false;
return ((data[bytes - 1] & ~excess_bit_mask()) == (that.data[bytes - 1] & ~excess_bit_mask()));
} else {
return (!memcmp (data, that.data, bytes));
}
}
bool BitSet::operator!= (const BitSet& that) const
{
return (!(*this == that));
}
BitSet& BitSet::operator|= (const BitSet& that)
{
assert (bits == that.bits);
for (size_t i = 0; i != bytes; ++i)
data[i] |= that.data[i];
return *this;
}
BitSet& BitSet::operator&= (const BitSet& that)
{
assert (bits == that.bits);
for (size_t i = 0; i != bytes; ++i)
data[i] &= that.data[i];
return *this;
}
BitSet& BitSet::operator^= (const BitSet& that)
{
assert (bits == that.bits);
for (size_t i = 0; i != bytes; ++i)
data[i] ^= that.data[i];
return *this;
}
BitSet BitSet::operator| (const BitSet& that) const
{
BitSet result (*this);
result |= that;
return result;
}
BitSet BitSet::operator& (const BitSet& that) const
{
BitSet result (*this);
result &= that;
return result;
}
BitSet BitSet::operator^ (const BitSet& that) const
{
BitSet result (*this);
result ^= that;
return result;
}
BitSet BitSet::operator~() const
{
BitSet result (*this);
for (size_t i = 0; i != bytes; ++i)
result.data[i] = ~data[i];
return result;
}
}
|