source: trunk/libtransmission/bitfield.h @ 12012

Last change on this file since 12012 was 12012, checked in by jordan, 11 years ago

(trunk libT) #4048 "use bitsets instead of bitfield in tr_completion" -- done.

Excuse the sprawl. Much of this didn't fit into self-contained commits.

  • Property svn:keywords set to Date Rev Author Id
File size: 2.3 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: bitfield.h 12012 2011-02-23 03:54:04Z jordan $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_BITFIELD_H
18#define TR_BITFIELD_H 1
19
20#include "transmission.h"
21
22/** @brief Implementation of the BitTorrent spec's Bitfield array of bits */
23typedef struct tr_bitfield
24{
25    uint8_t *  bits;
26    size_t     bitCount;
27    size_t     byteCount;
28}
29tr_bitfield;
30
31extern const tr_bitfield TR_BITFIELD_INIT;
32
33tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitCount );
34
35tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
36
37tr_bitfield* tr_bitfieldNew( size_t bitCount );
38
39void tr_bitfieldFree( tr_bitfield * b );
40
41void         tr_bitfieldClear( tr_bitfield* );
42
43int          tr_bitfieldAdd( tr_bitfield*, size_t bit );
44
45int          tr_bitfieldRem( tr_bitfield*, size_t bit );
46
47int          tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
48
49int          tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
50
51size_t       tr_bitfieldCountTrueBits( const tr_bitfield* );
52
53size_t       tr_bitfieldCountRange( const tr_bitfield * b, size_t begin, size_t end );
54
55
56tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
57
58/** A stripped-down version of bitfieldHas to be used
59    for speed when you're looping quickly. This version
60    has none of tr_bitfieldHas()'s safety checks, so you
61    need to call tr_bitfieldTestFast() first before you
62    start looping. */
63static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
64{
65    return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
66}
67
68/** @param high the highest nth bit you're going to access */
69static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
70{
71    return ( b != NULL )
72        && ( b->bits != NULL )
73        && ( high < b->bitCount );
74}
75
76static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
77{
78    return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
79}
80
81#endif
Note: See TracBrowser for help on using the repository browser.