transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / transmission / libtransmission / bitset.h
blob0e9271cca1e7dc9cd98ba76652dfce0e301f742c
1 /*
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.
15 #endif
17 #ifndef TR_BITSET_H
18 #define TR_BITSET_H 1
20 #include "transmission.h"
21 #include "bitfield.h"
23 /** @brief like a tr_bitfield, but supports haveAll and haveNone */
24 typedef struct tr_bitset
26 tr_bool haveAll;
27 tr_bool haveNone;
28 tr_bitfield bitfield;
30 tr_bitset;
32 static inline void
33 tr_bitsetConstructor( tr_bitset * b, size_t size )
35 tr_bitfieldConstruct( &b->bitfield, size );
38 static inline void
39 tr_bitsetDestructor( tr_bitset * b )
41 tr_bitfieldDestruct( &b->bitfield );
44 void tr_bitsetReserve( tr_bitset * b, size_t size );
46 static inline tr_bool
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 );
55 static inline tr_bool
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 );
64 static inline void
65 tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
67 if( b->haveAll )
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' */
74 static inline void
75 tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
77 if( b->haveAll )
78 tr_bitfieldClear( a );
79 else if( !b->haveNone )
80 tr_bitfieldDifference( a, &b->bitfield );
83 static inline double
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;
92 static inline void
93 tr_bitsetSetHaveAll( tr_bitset * b )
95 b->haveAll = 1;
96 b->haveNone = 0;
99 static inline void
100 tr_bitsetSetHaveNone( tr_bitset * b )
102 b->haveAll = 0;
103 b->haveNone = 1;
106 static inline int
107 tr_bitsetAdd( tr_bitset * b, size_t i )
109 int ret = 0;
110 if( !b->haveAll ) {
111 b->haveNone = 0;
112 tr_bitsetReserve( b, i+1 );
113 ret = tr_bitfieldAdd( &b->bitfield, i );
115 return ret;
118 #endif