1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 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: utils.h 8685 2009-06-14 01:00:36Z charles $ |
---|
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 | #include "utils.h" /* tr_new0 */ |
---|
22 | |
---|
23 | typedef struct tr_bitfield |
---|
24 | { |
---|
25 | uint8_t * bits; |
---|
26 | size_t bitCount; |
---|
27 | size_t byteCount; |
---|
28 | } |
---|
29 | tr_bitfield; |
---|
30 | |
---|
31 | tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount ); |
---|
32 | |
---|
33 | tr_bitfield* tr_bitfieldDestruct( tr_bitfield* ); |
---|
34 | |
---|
35 | static inline tr_bitfield* tr_bitfieldNew( size_t bitcount ) |
---|
36 | { |
---|
37 | return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount ); |
---|
38 | } |
---|
39 | |
---|
40 | static inline void tr_bitfieldFree( tr_bitfield * b ) |
---|
41 | { |
---|
42 | tr_free( tr_bitfieldDestruct( b ) ); |
---|
43 | } |
---|
44 | |
---|
45 | tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC; |
---|
46 | |
---|
47 | void tr_bitfieldClear( tr_bitfield* ); |
---|
48 | |
---|
49 | int tr_bitfieldAdd( tr_bitfield*, size_t bit ); |
---|
50 | |
---|
51 | int tr_bitfieldRem( tr_bitfield*, size_t bit ); |
---|
52 | |
---|
53 | int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end ); |
---|
54 | |
---|
55 | int tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end ); |
---|
56 | |
---|
57 | void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * ); |
---|
58 | |
---|
59 | int tr_bitfieldIsEmpty( const tr_bitfield* ); |
---|
60 | |
---|
61 | size_t tr_bitfieldCountTrueBits( const tr_bitfield* ); |
---|
62 | |
---|
63 | tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); |
---|
64 | |
---|
65 | /** A stripped-down version of bitfieldHas to be used |
---|
66 | for speed when you're looping quickly. This version |
---|
67 | has none of tr_bitfieldHas()'s safety checks, so you |
---|
68 | need to call tr_bitfieldTestFast() first before you |
---|
69 | start looping. */ |
---|
70 | static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth ) |
---|
71 | { |
---|
72 | return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0; |
---|
73 | } |
---|
74 | |
---|
75 | /** @param high the highest nth bit you're going to access */ |
---|
76 | static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high ) |
---|
77 | { |
---|
78 | return ( b != NULL ) |
---|
79 | && ( b->bits != NULL ) |
---|
80 | && ( high < b->bitCount ); |
---|
81 | } |
---|
82 | |
---|
83 | static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth ) |
---|
84 | { |
---|
85 | return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth ); |
---|
86 | } |
---|
87 | |
---|
88 | #endif |
---|