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)
|
2009-08-10 20:04:08 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2009-06-15 00:11:06 +00:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __TRANSMISSION__
|
2010-01-14 14:20:49 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-06-15 00:11:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TR_BITFIELD_H
|
|
|
|
#define TR_BITFIELD_H 1
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Implementation of the BitTorrent spec's Bitfield array of bits */
|
2009-06-15 00:11:06 +00:00
|
|
|
typedef struct tr_bitfield
|
|
|
|
{
|
|
|
|
uint8_t * bits;
|
|
|
|
size_t bitCount;
|
|
|
|
size_t byteCount;
|
|
|
|
}
|
|
|
|
tr_bitfield;
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
extern const tr_bitfield TR_BITFIELD_INIT;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitCount );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitfield* tr_bitfieldNew( size_t bitCount );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
void tr_bitfieldFree( tr_bitfield * b );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
void tr_bitfieldClear( tr_bitfield* );
|
|
|
|
|
|
|
|
int tr_bitfieldAdd( tr_bitfield*, size_t bit );
|
|
|
|
|
|
|
|
int tr_bitfieldRem( tr_bitfield*, size_t bit );
|
|
|
|
|
|
|
|
int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
|
|
|
|
|
|
|
|
int tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
|
|
|
|
|
|
|
|
size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
size_t tr_bitfieldCountRange( const tr_bitfield * b, size_t begin, size_t end );
|
|
|
|
|
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
|
|
|
|
|
|
|
|
/** A stripped-down version of bitfieldHas to be used
|
2010-12-27 19:18:17 +00:00
|
|
|
for speed when you're looping quickly. This version
|
2009-06-15 00:11:06 +00:00
|
|
|
has none of tr_bitfieldHas()'s safety checks, so you
|
|
|
|
need to call tr_bitfieldTestFast() first before you
|
|
|
|
start looping. */
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
|
|
|
return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param high the highest nth bit you're going to access */
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
|
|
|
return ( b != NULL )
|
|
|
|
&& ( b->bits != NULL )
|
|
|
|
&& ( high < b->bitCount );
|
|
|
|
}
|
|
|
|
|
2010-01-01 22:26:35 +00:00
|
|
|
static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
|
|
|
return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|