2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-06-15 00:11:06 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-06-15 00:11:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bitfield.h"
|
2010-10-11 15:41:27 +00:00
|
|
|
#include "bitset.h"
|
2011-02-23 03:54:04 +00:00
|
|
|
#include "utils.h" /* tr_new0() */
|
|
|
|
|
|
|
|
const tr_bitfield TR_BITFIELD_INIT = { NULL, 0, 0 };
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
tr_bitfield*
|
|
|
|
tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount )
|
|
|
|
{
|
|
|
|
b->bitCount = bitCount;
|
|
|
|
b->byteCount = ( bitCount + 7u ) / 8u;
|
|
|
|
b->bits = tr_new0( uint8_t, b->byteCount );
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfield*
|
|
|
|
tr_bitfieldDestruct( tr_bitfield * b )
|
|
|
|
{
|
|
|
|
if( b )
|
|
|
|
tr_free( b->bits );
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfield*
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitfieldNew( size_t bitCount )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
return tr_bitfieldConstruct( tr_new( tr_bitfield, 1 ), bitCount );
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldFree( tr_bitfield * b )
|
|
|
|
{
|
|
|
|
tr_free( tr_bitfieldDestruct( b ) );
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bitfieldClear( tr_bitfield * bitfield )
|
|
|
|
{
|
|
|
|
memset( bitfield->bits, 0, bitfield->byteCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_bitfieldAdd( tr_bitfield * bitfield,
|
|
|
|
size_t nth )
|
|
|
|
{
|
|
|
|
assert( bitfield );
|
|
|
|
assert( bitfield->bits );
|
|
|
|
|
|
|
|
if( nth >= bitfield->bitCount )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bitfield->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets bit range [begin, end) to 1 */
|
|
|
|
int
|
|
|
|
tr_bitfieldAddRange( tr_bitfield * b,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
|
|
|
{
|
|
|
|
size_t sb, eb;
|
|
|
|
unsigned char sm, em;
|
|
|
|
|
|
|
|
end--;
|
|
|
|
|
|
|
|
if( ( end >= b->bitCount ) || ( begin > end ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );
|
|
|
|
eb = end >> 3;
|
|
|
|
em = 0xff << ( 7 - ( end & 7 ) );
|
|
|
|
|
|
|
|
if( sb == eb )
|
|
|
|
{
|
|
|
|
b->bits[sb] |= ( sm & em );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b->bits[sb] |= sm;
|
|
|
|
b->bits[eb] |= em;
|
|
|
|
if( ++sb < eb )
|
|
|
|
memset ( b->bits + sb, 0xff, eb - sb );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_bitfieldRem( tr_bitfield * bitfield,
|
|
|
|
size_t nth )
|
|
|
|
{
|
|
|
|
assert( bitfield );
|
|
|
|
assert( bitfield->bits );
|
|
|
|
|
|
|
|
if( nth >= bitfield->bitCount )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bitfield->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clears bit range [begin, end) to 0 */
|
|
|
|
int
|
|
|
|
tr_bitfieldRemRange( tr_bitfield * b,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
|
|
|
{
|
|
|
|
size_t sb, eb;
|
|
|
|
unsigned char sm, em;
|
|
|
|
|
|
|
|
end--;
|
|
|
|
|
|
|
|
if( ( end >= b->bitCount ) || ( begin > end ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = 0xff << ( 8 - ( begin & 7 ) );
|
|
|
|
eb = end >> 3;
|
|
|
|
em = ~( 0xff << ( 7 - ( end & 7 ) ) );
|
|
|
|
|
|
|
|
if( sb == eb )
|
|
|
|
{
|
|
|
|
b->bits[sb] &= ( sm | em );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b->bits[sb] &= sm;
|
|
|
|
b->bits[eb] &= em;
|
|
|
|
if( ++sb < eb )
|
|
|
|
memset ( b->bits + sb, 0, eb - sb );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfield*
|
2009-12-10 04:54:48 +00:00
|
|
|
tr_bitfieldOr( tr_bitfield * a, const tr_bitfield * b )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2009-12-10 04:54:48 +00:00
|
|
|
uint8_t * ait = a->bits;
|
|
|
|
const uint8_t * aend = ait + a->byteCount;
|
|
|
|
const uint8_t * bit = b->bits;
|
|
|
|
const uint8_t * bend = bit + b->byteCount;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2009-12-10 04:54:48 +00:00
|
|
|
while( ait!=aend && bit!=bend )
|
2009-06-15 00:11:06 +00:00
|
|
|
*ait++ |= *bit++;
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
static const int 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
|
|
|
|
};
|
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
size_t
|
|
|
|
tr_bitfieldCountTrueBits( const tr_bitfield* b )
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
const uint8_t * it, *end;
|
|
|
|
|
|
|
|
if( !b )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for( it = b->bits, end = it + b->byteCount; it != end; ++it )
|
|
|
|
ret += trueBitCount[*it];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
|
|
|
|
size_t
|
|
|
|
tr_bitfieldCountRange( const tr_bitfield * b, size_t begin, size_t end )
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
const int first_byte = begin >> 3u;
|
|
|
|
const int last_byte = ( end - 1 ) >> 3u;
|
|
|
|
|
|
|
|
assert( begin < end );
|
|
|
|
|
|
|
|
if( first_byte == last_byte )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t val = b->bits[first_byte];
|
|
|
|
|
|
|
|
i = begin - (first_byte * 8);
|
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
i = (last_byte+1)*8 - end;
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
|
|
|
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
/* 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<last_byte; ++i )
|
|
|
|
ret += trueBitCount[b->bits[i]];
|
|
|
|
|
|
|
|
/* last byte */
|
|
|
|
i = (last_byte+1)*8 - end;
|
|
|
|
val = b->bits[last_byte];
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( ret <= ( begin - end ) );
|
|
|
|
return ret;
|
|
|
|
}
|