2 * This file Copyright (C) Mnemosyne LLC
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.
10 * $Id: bitset.h 11709 2011-01-19 13:48:47Z jordan $
13 #ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
20 #include "transmission.h"
23 /** @brief like a tr_bitfield, but supports haveAll and haveNone */
24 typedef struct tr_bitset
33 tr_bitsetConstructor( tr_bitset
* b
, size_t size
)
35 tr_bitfieldConstruct( &b
->bitfield
, size
);
39 tr_bitsetDestructor( tr_bitset
* b
)
41 tr_bitfieldDestruct( &b
->bitfield
);
44 void tr_bitsetReserve( tr_bitset
* b
, size_t size
);
47 tr_bitsetHasFast( const tr_bitset
* b
, const size_t nth
)
49 if( b
->haveAll
) return TRUE
;
50 if( b
->haveNone
) return FALSE
;
51 if( nth
>= b
->bitfield
.bitCount
) return FALSE
;
52 return tr_bitfieldHasFast( &b
->bitfield
, nth
);
56 tr_bitsetHas( const tr_bitset
* b
, const size_t nth
)
58 if( b
->haveAll
) return TRUE
;
59 if( b
->haveNone
) return FALSE
;
60 if( nth
>= b
->bitfield
.bitCount
) return FALSE
;
61 return tr_bitfieldHas( &b
->bitfield
, nth
);
65 tr_bitsetOr( tr_bitfield
* a
, const tr_bitset
* b
)
68 tr_bitfieldAddRange( a
, 0, a
->bitCount
);
69 else if( !b
->haveNone
)
70 tr_bitfieldOr( a
, &b
->bitfield
);
73 /* set 'a' to all the flags that were in 'a' but not 'b' */
75 tr_bitsetDifference( tr_bitfield
* a
, const tr_bitset
* b
)
78 tr_bitfieldClear( a
);
79 else if( !b
->haveNone
)
80 tr_bitfieldDifference( a
, &b
->bitfield
);
84 tr_bitsetPercent( const tr_bitset
* b
)
86 if( b
->haveAll
) return 1.0;
87 if( b
->haveNone
) return 0.0;
88 if( b
->bitfield
.bitCount
== 0 ) return 0.0;
89 return tr_bitfieldCountTrueBits( &b
->bitfield
) / (double)b
->bitfield
.bitCount
;
93 tr_bitsetSetHaveAll( tr_bitset
* b
)
100 tr_bitsetSetHaveNone( tr_bitset
* b
)
107 tr_bitsetAdd( tr_bitset
* b
, size_t i
)
112 tr_bitsetReserve( b
, i
+1 );
113 ret
= tr_bitfieldAdd( &b
->bitfield
, i
);