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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bitfield.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "utils.h" /* tr_new0() */
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_bitfield const TR_BITFIELD_INIT = { NULL, 0, 0, 0, false, false };
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int8_t const trueBitCount[256] =
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static size_t countArray(tr_bitfield const* b)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
size_t i = b->alloc_count;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
ret += trueBitCount[b->bits[--i]];
|
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static size_t countRange(tr_bitfield const* b, size_t begin, size_t end)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t ret = 0;
|
2017-04-20 16:02:19 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!b->bit_count)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (first_byte >= b->alloc_count)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-29 16:39:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(begin < end);
|
|
|
|
assert(b->bits != NULL);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i;
|
|
|
|
uint8_t val = b->bits[first_byte];
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
i = begin - (first_byte * 8);
|
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
i = (last_byte + 1) * 8 - end;
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t i;
|
|
|
|
uint8_t val;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const walk_end = MIN(b->alloc_count, last_byte);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* first byte */
|
|
|
|
i = begin - (first_byte * 8);
|
|
|
|
val = b->bits[first_byte];
|
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
|
|
|
|
/* middle bytes */
|
|
|
|
for (i = first_byte + 1; i < walk_end; ++i)
|
|
|
|
{
|
|
|
|
ret += trueBitCount[b->bits[i]];
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* last byte */
|
|
|
|
if (last_byte < b->alloc_count)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
i = (last_byte + 1) * 8 - end;
|
|
|
|
val = b->bits[last_byte];
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
|
|
|
ret += trueBitCount[val];
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(ret <= (begin - end));
|
|
|
|
return ret;
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_bitfieldCountRange(tr_bitfield const* b, size_t begin, size_t end)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasAll(b))
|
|
|
|
{
|
|
|
|
return end - begin;
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasNone(b))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return countRange(b, begin, end);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_bitfieldHas(tr_bitfield const* b, size_t n)
|
2012-12-13 02:00:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasAll(b))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-13 02:00:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasNone(b))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-13 02:00:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n >> 3u >= b->alloc_count)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-13 02:00:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return (b->bits[n >> 3u] << (n & 7u) & 0x80) != 0;
|
2012-12-13 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool tr_bitfieldIsValid(tr_bitfield const* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b != NULL);
|
|
|
|
assert((b->alloc_count == 0) == (b->bits == 0));
|
|
|
|
assert(!b->bits || (b->true_count == countArray(b)));
|
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
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_bitfieldCountTrueBits(tr_bitfield const* b)
|
2011-04-05 00:29:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(tr_bitfieldIsValid(b));
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return b->true_count;
|
2011-04-05 00:29:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static size_t get_bytes_needed(size_t bit_count)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return (bit_count >> 3) + (bit_count & 7 ? 1 : 0);
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_all_true(uint8_t* array, size_t bit_count)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const val = 0xFF;
|
|
|
|
size_t const n = get_bytes_needed(bit_count);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n > 0)
|
2013-07-29 04:19:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(array, val, n - 1);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
array[n - 1] = val << (n * 8 - bit_count);
|
2013-07-29 04:19:15 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_bitfieldGetRaw(tr_bitfield const* b, size_t* byte_count)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const n = get_bytes_needed(b->bit_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* bits = tr_new0(uint8_t, n);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b->bit_count > 0);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (b->alloc_count)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b->alloc_count <= n);
|
|
|
|
memcpy(bits, b->bits, b->alloc_count);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_bitfieldHasAll(b))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_all_true(bits, b->bit_count);
|
2011-09-28 16:07:35 +00:00
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*byte_count = n;
|
|
|
|
return bits;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldEnsureBitsAlloced(tr_bitfield* b, size_t n)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t bytes_needed;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const has_all = tr_bitfieldHasAll(b);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (has_all)
|
|
|
|
{
|
|
|
|
bytes_needed = get_bytes_needed(MAX(n, b->true_count));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes_needed = get_bytes_needed(n);
|
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (b->alloc_count < bytes_needed)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits = tr_renew(uint8_t, b->bits, bytes_needed);
|
|
|
|
memset(b->bits + b->alloc_count, 0, bytes_needed - b->alloc_count);
|
|
|
|
b->alloc_count = bytes_needed;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (has_all)
|
|
|
|
{
|
|
|
|
set_all_true(b->bits, b->true_count);
|
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool tr_bitfieldEnsureNthBitAlloced(tr_bitfield* b, size_t nth)
|
2011-09-28 16:07:35 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* count is zero-based, so we need to allocate nth+1 bits before setting the nth */
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (nth == SIZE_MAX)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced(b, nth + 1);
|
|
|
|
return true;
|
2011-09-28 16:07:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldFreeArray(tr_bitfield* b)
|
2011-03-30 04:14:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(b->bits);
|
|
|
|
b->bits = NULL;
|
|
|
|
b->alloc_count = 0;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldSetTrueCount(tr_bitfield* b, size_t n)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b->bit_count == 0 || n <= b->bit_count);
|
2015-12-27 16:34:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
b->true_count = n;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasAll(b) || tr_bitfieldHasNone(b))
|
|
|
|
{
|
|
|
|
tr_bitfieldFreeArray(b);
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(tr_bitfieldIsValid(b));
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldRebuildTrueCount(tr_bitfield* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetTrueCount(b, countArray(b));
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldIncTrueCount(tr_bitfield* b, size_t i)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b->bit_count == 0 || i <= b->bit_count);
|
|
|
|
assert(b->bit_count == 0 || b->true_count <= b->bit_count - i);
|
2015-12-27 16:34:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetTrueCount(b, b->true_count + i);
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_bitfieldDecTrueCount(tr_bitfield* b, size_t i)
|
2015-12-27 16:34:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(b->bit_count == 0 || i <= b->bit_count);
|
|
|
|
assert(b->bit_count == 0 || b->true_count >= i);
|
2015-12-27 16:34:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetTrueCount(b, b->true_count - i);
|
2015-12-27 16:34:47 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldConstruct(tr_bitfield* b, size_t bit_count)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bit_count = bit_count;
|
|
|
|
b->true_count = 0;
|
|
|
|
b->bits = NULL;
|
|
|
|
b->alloc_count = 0;
|
|
|
|
b->have_all_hint = false;
|
|
|
|
b->have_none_hint = false;
|
|
|
|
|
|
|
|
assert(tr_bitfieldIsValid(b));
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldSetHasNone(tr_bitfield* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldFreeArray(b);
|
|
|
|
b->true_count = 0;
|
|
|
|
b->have_all_hint = false;
|
|
|
|
b->have_none_hint = true;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(tr_bitfieldIsValid(b));
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldSetHasAll(tr_bitfield* b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldFreeArray(b);
|
|
|
|
b->true_count = b->bit_count;
|
|
|
|
b->have_all_hint = true;
|
|
|
|
b->have_none_hint = false;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(tr_bitfieldIsValid(b));
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetFromBitfield(tr_bitfield* b, tr_bitfield const* src)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHasAll(src))
|
|
|
|
{
|
|
|
|
tr_bitfieldSetHasAll(b);
|
|
|
|
}
|
|
|
|
else if (tr_bitfieldHasNone(src))
|
|
|
|
{
|
|
|
|
tr_bitfieldSetHasNone(b);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_bitfieldSetRaw(b, src->bits, src->alloc_count, true);
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetRaw(tr_bitfield* b, void const* bits, size_t byte_count, bool bounded)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldFreeArray(b);
|
|
|
|
b->true_count = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bounded)
|
|
|
|
{
|
|
|
|
byte_count = MIN(byte_count, get_bytes_needed(b->bit_count));
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits = tr_memdup(bits, byte_count);
|
|
|
|
b->alloc_count = byte_count;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bounded)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* ensure the excess bits are set to '0' */
|
2017-04-20 16:02:19 +00:00
|
|
|
int const excess_bit_count = byte_count * 8 - b->bit_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(excess_bit_count >= 0);
|
|
|
|
assert(excess_bit_count <= 7);
|
|
|
|
|
|
|
|
if (excess_bit_count)
|
|
|
|
{
|
|
|
|
b->bits[b->alloc_count - 1] &= ((0xff) << excess_bit_count);
|
|
|
|
}
|
2011-09-26 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldRebuildTrueCount(b);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_bitfieldSetFromFlags(tr_bitfield* b, bool const* flags, size_t n)
|
2011-09-20 23:39:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t i;
|
|
|
|
size_t trueCount = 0;
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldFreeArray(b);
|
|
|
|
tr_bitfieldEnsureBitsAlloced(b, n);
|
2011-09-20 23:39:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (i = 0; i < n; ++i)
|
2011-09-20 23:39:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (flags[i])
|
2011-09-20 23:39:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++trueCount;
|
|
|
|
b->bits[i >> 3u] |= (0x80 >> (i & 7u));
|
2011-09-20 23:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetTrueCount(b, trueCount);
|
2011-09-20 23:39:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldAdd(tr_bitfield* b, size_t nth)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_bitfieldHas(b, nth) && tr_bitfieldEnsureNthBitAlloced(b, nth))
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits[nth >> 3u] |= (0x80 >> (nth & 7u));
|
|
|
|
tr_bitfieldIncTrueCount(b, 1);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets bit range [begin, end) to 1 */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldAddRange(tr_bitfield* b, size_t begin, size_t end)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t sb, eb;
|
|
|
|
unsigned char sm, em;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const diff = (end - begin) - tr_bitfieldCountRange(b, begin, end);
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (diff == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
end--;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((end >= b->bit_count) || (begin > end))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sb = begin >> 3;
|
|
|
|
sm = ~(0xff << (8 - (begin & 7)));
|
|
|
|
eb = end >> 3;
|
|
|
|
em = 0xff << (7 - (end & 7));
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_bitfieldEnsureNthBitAlloced(b, end))
|
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
|
|
|
|
|
|
|
if (sb == eb)
|
|
|
|
{
|
|
|
|
b->bits[sb] |= (sm & em);
|
|
|
|
}
|
|
|
|
else
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits[sb] |= sm;
|
|
|
|
b->bits[eb] |= em;
|
|
|
|
|
|
|
|
if (++sb < eb)
|
|
|
|
{
|
|
|
|
memset(b->bits + sb, 0xff, eb - sb);
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldIncTrueCount(b, diff);
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldRem(tr_bitfield* b, size_t nth)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(tr_bitfieldIsValid(b));
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_bitfieldHas(b, nth) && tr_bitfieldEnsureNthBitAlloced(b, nth))
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits[nth >> 3u] &= (0xff7f >> (nth & 7u));
|
|
|
|
tr_bitfieldDecTrueCount(b, 1);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clears bit range [begin, end) to 0 */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_bitfieldRemRange(tr_bitfield* b, size_t begin, size_t end)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t sb, eb;
|
|
|
|
unsigned char sm, em;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const diff = tr_bitfieldCountRange(b, begin, end);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!diff)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
end--;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((end >= b->bit_count) || (begin > end))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sb = begin >> 3;
|
|
|
|
sm = 0xff << (8 - (begin & 7));
|
|
|
|
eb = end >> 3;
|
|
|
|
em = ~(0xff << (7 - (end & 7)));
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_bitfieldEnsureNthBitAlloced(b, end))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-29 01:43:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (sb == eb)
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits[sb] &= (sm | em);
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
b->bits[sb] &= sm;
|
|
|
|
b->bits[eb] &= em;
|
|
|
|
|
|
|
|
if (++sb < eb)
|
|
|
|
{
|
|
|
|
memset(b->bits + sb, 0, eb - sb);
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldDecTrueCount(b, diff);
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|