2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2021-10-24 20:43:36 +00:00
|
|
|
#include <vector>
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2021-10-24 20:43:36 +00:00
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
#include "bitfield.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2021-10-24 20:43:36 +00:00
|
|
|
#include "utils.h" /* tr_new0() */
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
constexpr size_t getBytesNeeded(size_t bit_count)
|
|
|
|
{
|
|
|
|
return (bit_count >> 3) + ((bit_count & 7) != 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setAllTrue(uint8_t* array, size_t bit_count)
|
|
|
|
{
|
|
|
|
uint8_t constexpr Val = 0xFF;
|
|
|
|
size_t const n = getBytesNeeded(bit_count);
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
std::fill_n(array, n, Val);
|
|
|
|
array[n - 1] = Val << (n * 8 - bit_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int8_t const trueBitCount[256] = {
|
|
|
|
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
|
|
|
|
3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
|
|
|
|
3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
|
|
|
|
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
|
|
|
|
5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
|
|
|
|
6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
|
2011-03-28 16:31:05 +00:00
|
|
|
};
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
size_t tr_bitfield::countFlags() const
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
|
|
|
|
for (auto ch : flags_)
|
|
|
|
{
|
|
|
|
ret += trueBitCount[ch];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t tr_bitfield::countFlags(size_t begin, size_t end) const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t ret = 0;
|
2019-03-17 04:07:48 +00:00
|
|
|
size_t const first_byte = begin >> 3U;
|
|
|
|
size_t const last_byte = (end - 1) >> 3U;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-10-16 14:04:19 +00:00
|
|
|
if (bit_count_ == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (first_byte >= std::size(flags_))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-29 16:39:30 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(begin < end);
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(!std::empty(flags_));
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (first_byte == last_byte)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
uint8_t val = flags_[first_byte];
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-26 18:02:07 +00:00
|
|
|
int i = begin - (first_byte * 8);
|
2017-04-19 12:04:45 +00:00
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
i = (last_byte + 1) * 8 - end;
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
ret += trueBitCount[val];
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t const walk_end = std::min(std::size(flags_), last_byte);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* first byte */
|
2017-05-13 22:38:31 +00:00
|
|
|
size_t const first_shift = begin - (first_byte * 8);
|
2021-10-26 18:02:07 +00:00
|
|
|
uint8_t val = flags_[first_byte];
|
2017-05-13 22:38:31 +00:00
|
|
|
val <<= first_shift;
|
|
|
|
val >>= first_shift;
|
2021-10-24 20:43:36 +00:00
|
|
|
ret += trueBitCount[val];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* middle bytes */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = first_byte + 1; i < walk_end; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
ret += trueBitCount[flags_[i]];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* last byte */
|
2021-10-24 20:43:36 +00:00
|
|
|
if (last_byte < std::size(flags_))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
size_t const last_shift = (last_byte + 1) * 8 - end;
|
2021-10-24 20:43:36 +00:00
|
|
|
val = flags_[last_byte];
|
2017-05-13 22:38:31 +00:00
|
|
|
val >>= last_shift;
|
|
|
|
val <<= last_shift;
|
2021-10-24 20:43:36 +00:00
|
|
|
ret += trueBitCount[val];
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(ret <= (begin - end));
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t tr_bitfield::count(size_t begin, size_t end) const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
if (hasAll())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return end - begin;
|
|
|
|
}
|
2012-12-13 02:00:45 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (hasNone())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-13 02:00:45 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
return countFlags(begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_bitfield::testFlag(size_t n) const
|
|
|
|
{
|
|
|
|
if (n >> 3U >= std::size(flags_))
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
bool ret = (flags_[n >> 3U] << (n & 7U) & 0x80) != 0;
|
|
|
|
return ret;
|
2012-12-13 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2015-05-31 22:13:31 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
bool tr_bitfield::assertValid() const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(std::empty(flags_) || true_count_ == countFlags());
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
std::vector<uint8_t> tr_bitfield::raw() const
|
2011-04-05 00:29:42 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
auto const n = getBytesNeeded(bit_count_);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (!std::empty(flags_))
|
|
|
|
{
|
|
|
|
return flags_;
|
|
|
|
}
|
2011-04-05 00:29:42 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
auto raw = std::vector<uint8_t>(n);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (hasAll())
|
2013-07-29 04:19:15 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
setAllTrue(std::data(raw), std::size(raw));
|
2013-07-29 04:19:15 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
return raw;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void tr_bitfield::ensureBitsAlloced(size_t n)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
bool const has_all = hasAll();
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t const bytes_needed = has_all ? getBytesNeeded(std::max(n, true_count_)) : getBytesNeeded(n);
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (std::size(flags_) < bytes_needed)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
flags_.resize(bytes_needed);
|
|
|
|
|
|
|
|
if (has_all)
|
|
|
|
{
|
|
|
|
setAllTrue(std::data(flags_), true_count_);
|
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_bitfield::ensureNthBitAlloced(size_t nth)
|
|
|
|
{
|
|
|
|
// count is zero-based, so we need to allocate nth+1 bits before setting the nth */
|
|
|
|
if (nth == SIZE_MAX)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
ensureBitsAlloced(nth + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_bitfield::freeArray()
|
|
|
|
{
|
|
|
|
flags_ = std::vector<uint8_t>{};
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void tr_bitfield::setTrueCount(size_t n)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(bit_count_ == 0 || n <= bit_count_);
|
2015-12-27 16:34:47 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
true_count_ = n;
|
|
|
|
have_all_hint_ = n == bit_count_;
|
|
|
|
have_none_hint_ = n == 0;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (hasAll() || hasNone())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
freeArray();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
TR_ASSERT(assertValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_bitfield::rebuildTrueCount()
|
|
|
|
{
|
|
|
|
setTrueCount(countFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_bitfield::incrementTrueCount(size_t inc)
|
|
|
|
{
|
|
|
|
TR_ASSERT(bit_count_ == 0 || inc <= bit_count_);
|
|
|
|
TR_ASSERT(bit_count_ == 0 || true_count_ <= bit_count_ - inc);
|
|
|
|
|
|
|
|
setTrueCount(true_count_ + inc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_bitfield::decrementTrueCount(size_t dec)
|
|
|
|
{
|
|
|
|
TR_ASSERT(bit_count_ == 0 || dec <= bit_count_);
|
|
|
|
TR_ASSERT(bit_count_ == 0 || true_count_ >= dec);
|
|
|
|
|
|
|
|
setTrueCount(true_count_ - dec);
|
2015-12-27 16:34:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
tr_bitfield::tr_bitfield(size_t bit_count)
|
|
|
|
: bit_count_{ bit_count }
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(assertValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_bitfield::setHasNone()
|
|
|
|
{
|
|
|
|
freeArray();
|
2021-10-16 14:04:19 +00:00
|
|
|
true_count_ = 0;
|
2021-10-24 20:43:36 +00:00
|
|
|
have_all_hint_ = false;
|
|
|
|
have_none_hint_ = true;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(assertValid());
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void tr_bitfield::setHasAll()
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
freeArray();
|
|
|
|
true_count_ = bit_count_;
|
|
|
|
have_all_hint_ = true;
|
|
|
|
have_none_hint_ = false;
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
TR_ASSERT(assertValid());
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:25:45 +00:00
|
|
|
void tr_bitfield::setRaw(uint8_t const* raw, size_t byte_count)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
flags_ = std::vector<uint8_t>(raw, raw + byte_count);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-11-08 00:25:45 +00:00
|
|
|
// ensure any excess bits at the end of the array are set to '0'.
|
|
|
|
if (byte_count == getBytesNeeded(bit_count_))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
int const excess_bit_count = byte_count * 8 - bit_count_;
|
|
|
|
|
|
|
|
TR_ASSERT(excess_bit_count >= 0);
|
|
|
|
TR_ASSERT(excess_bit_count <= 7);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
if (excess_bit_count != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
flags_.back() &= 0xff << excess_bit_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-09-26 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
rebuildTrueCount();
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void tr_bitfield::setFromBools(bool const* flags, size_t n)
|
2011-09-20 23:39:40 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t trueCount = 0;
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
freeArray();
|
|
|
|
ensureBitsAlloced(n);
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
if (flags[i])
|
2020-09-10 23:50:46 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
++trueCount;
|
|
|
|
flags_[i >> 3U] |= (0x80 >> (i & 7U));
|
2020-09-10 23:50:46 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
setTrueCount(trueCount);
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void tr_bitfield::set(size_t nth, bool value)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
if (test(nth) == value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-10-17 15:34:36 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (!ensureNthBitAlloced(nth))
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
flags_[nth >> 3U] |= 0x80 >> (nth & 7U);
|
|
|
|
incrementTrueCount(1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
else
|
2021-10-16 14:04:19 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
flags_[nth >> 3U] &= 0xff7f >> (nth & 7U);
|
|
|
|
decrementTrueCount(1);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/* Sets bit range [begin, end) to 1 */
|
|
|
|
void tr_bitfield::setRange(size_t begin, size_t end, bool value)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
// did anything change?
|
|
|
|
size_t const old_count = count(begin, end);
|
|
|
|
size_t const new_count = value ? (end - begin) : 0;
|
|
|
|
if (old_count == new_count)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-10-17 15:34:36 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// bounds check
|
|
|
|
--end;
|
|
|
|
if (end >= bit_count_ || begin > end)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (!ensureNthBitAlloced(end))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
size_t walk = begin >> 3;
|
|
|
|
size_t const last_byte = end >> 3;
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (value)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
unsigned char const first_mask = ~(0xff << (8 - (begin & 7)));
|
|
|
|
unsigned char const last_mask = 0xff << (7 - (end & 7));
|
|
|
|
|
|
|
|
if (walk == last_byte)
|
|
|
|
{
|
|
|
|
flags_[walk] |= first_mask & last_mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags_[walk] |= first_mask;
|
|
|
|
flags_[last_byte] |= last_mask;
|
|
|
|
|
|
|
|
if (++walk < last_byte)
|
|
|
|
{
|
|
|
|
std::fill_n(std::begin(flags_) + walk, last_byte - walk, 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementTrueCount(new_count - old_count);
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned char const first_mask = 0xff << (8 - (begin & 7));
|
|
|
|
unsigned char const last_mask = ~(0xff << (7 - (end & 7)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
if (walk == last_byte)
|
|
|
|
{
|
|
|
|
flags_[walk] &= first_mask | last_mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags_[walk] &= first_mask;
|
|
|
|
flags_[last_byte] &= last_mask;
|
|
|
|
|
|
|
|
if (++walk < last_byte)
|
|
|
|
{
|
|
|
|
std::fill_n(std::begin(flags_) + walk, last_byte - walk, 0);
|
|
|
|
}
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
decrementTrueCount(old_count);
|
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|