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: bitset.h 9931 2010-01-14 14:20:49Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef TR_BITSET_H |
---|
18 | #define TR_BITSET_H 1 |
---|
19 | |
---|
20 | #include "transmission.h" |
---|
21 | #include "bitfield.h" |
---|
22 | |
---|
23 | /** This like a tr_bitfield, but supports haveAll and haveNone */ |
---|
24 | typedef struct tr_bitset |
---|
25 | { |
---|
26 | tr_bool haveAll; |
---|
27 | tr_bool haveNone; |
---|
28 | tr_bitfield bitfield; |
---|
29 | } |
---|
30 | tr_bitset; |
---|
31 | |
---|
32 | static inline void |
---|
33 | tr_bitsetConstructor( tr_bitset * b, int size ) |
---|
34 | { |
---|
35 | tr_bitfieldConstruct( &b->bitfield, size ); |
---|
36 | } |
---|
37 | |
---|
38 | static inline void |
---|
39 | tr_bitsetDestructor( tr_bitset * b ) |
---|
40 | { |
---|
41 | tr_bitfieldDestruct( &b->bitfield ); |
---|
42 | } |
---|
43 | |
---|
44 | static inline void |
---|
45 | tr_bitsetReserve( tr_bitset * b, size_t size ) |
---|
46 | { |
---|
47 | if( b->bitfield.bitCount < size ) |
---|
48 | { |
---|
49 | tr_bitfield * tmp = tr_bitfieldDup( &b->bitfield ); |
---|
50 | |
---|
51 | tr_bitfieldDestruct( &b->bitfield ); |
---|
52 | tr_bitfieldConstruct( &b->bitfield, size ); |
---|
53 | memcpy( b->bitfield.bits, tmp->bits, tmp->byteCount ); |
---|
54 | |
---|
55 | tr_bitfieldFree( tmp ); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static inline tr_bool |
---|
60 | tr_bitsetHasFast( const tr_bitset * b, const size_t nth ) |
---|
61 | { |
---|
62 | if( b->haveAll ) return TRUE; |
---|
63 | if( b->haveNone ) return FALSE; |
---|
64 | if( nth >= b->bitfield.bitCount ) return FALSE; |
---|
65 | return tr_bitfieldHasFast( &b->bitfield, nth ); |
---|
66 | } |
---|
67 | |
---|
68 | static inline tr_bool |
---|
69 | tr_bitsetHas( const tr_bitset * b, const size_t nth ) |
---|
70 | { |
---|
71 | if( b->haveAll ) return TRUE; |
---|
72 | if( b->haveNone ) return FALSE; |
---|
73 | if( nth >= b->bitfield.bitCount ) return FALSE; |
---|
74 | return tr_bitfieldHas( &b->bitfield, nth ); |
---|
75 | } |
---|
76 | |
---|
77 | static inline void |
---|
78 | tr_bitsetOr( tr_bitfield * a, const tr_bitset * b ) |
---|
79 | { |
---|
80 | if( b->haveAll ) |
---|
81 | tr_bitfieldAddRange( a, 0, a->bitCount ); |
---|
82 | else if( !b->haveNone ) |
---|
83 | tr_bitfieldOr( a, &b->bitfield ); |
---|
84 | } |
---|
85 | |
---|
86 | /* set 'a' to all the flags that were in 'a' but not 'b' */ |
---|
87 | static inline void |
---|
88 | tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b ) |
---|
89 | { |
---|
90 | if( b->haveAll ) |
---|
91 | tr_bitfieldClear( a ); |
---|
92 | else if( !b->haveNone ) |
---|
93 | tr_bitfieldDifference( a, &b->bitfield ); |
---|
94 | } |
---|
95 | |
---|
96 | static inline double |
---|
97 | tr_bitsetPercent( const tr_bitset * b ) |
---|
98 | { |
---|
99 | if( b->haveAll ) return 1.0; |
---|
100 | if( b->haveNone ) return 0.0; |
---|
101 | if( b->bitfield.bitCount == 0 ) return 0.0; |
---|
102 | return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount; |
---|
103 | } |
---|
104 | |
---|
105 | static inline void |
---|
106 | tr_bitsetSetHaveAll( tr_bitset * b ) |
---|
107 | { |
---|
108 | b->haveAll = 1; |
---|
109 | b->haveNone = 0; |
---|
110 | } |
---|
111 | |
---|
112 | static inline void |
---|
113 | tr_bitsetSetHaveNone( tr_bitset * b ) |
---|
114 | { |
---|
115 | b->haveAll = 0; |
---|
116 | b->haveNone = 1; |
---|
117 | } |
---|
118 | |
---|
119 | static inline int |
---|
120 | tr_bitsetAdd( tr_bitset * b, int i ) |
---|
121 | { |
---|
122 | int ret = 0; |
---|
123 | if( !b->haveAll ) { |
---|
124 | b->haveNone = 0; |
---|
125 | tr_bitsetReserve( b, i+1 ); |
---|
126 | ret = tr_bitfieldAdd( &b->bitfield, i ); |
---|
127 | } |
---|
128 | return ret; |
---|
129 | } |
---|
130 | |
---|
131 | #endif |
---|