source: trunk/libtransmission/bitset.h @ 11599

Last change on this file since 11599 was 11599, checked in by charles, 11 years ago

(trunk) Join the 21st century and use only 1 space at the end sentences. This commit is nearly as important as the semi-annual ones that remove trailing spaces from the ends of lines of code... :)

  • Property svn:keywords set to Date Rev Author Id
File size: 2.7 KB
Line 
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 11599 2010-12-27 19:18:17Z 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/** @brief like a tr_bitfield, but supports haveAll and haveNone */
24typedef struct tr_bitset
25{
26    tr_bool haveAll;
27    tr_bool haveNone;
28    tr_bitfield bitfield;
29}
30tr_bitset;
31
32static inline void
33tr_bitsetConstructor( tr_bitset * b, size_t size )
34{
35    tr_bitfieldConstruct( &b->bitfield, size );
36}
37
38static inline void
39tr_bitsetDestructor( tr_bitset * b )
40{
41    tr_bitfieldDestruct( &b->bitfield );
42}
43
44void tr_bitsetReserve( tr_bitset * b, size_t size );
45
46static inline tr_bool
47tr_bitsetHasFast( const tr_bitset * b, const size_t nth )
48{
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 );
53}
54
55static inline tr_bool
56tr_bitsetHas( const tr_bitset * b, const size_t nth )
57{
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 );
62}
63
64static inline void
65tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
66{
67    if( b->haveAll )
68        tr_bitfieldAddRange( a, 0, a->bitCount );
69    else if( !b->haveNone )
70        tr_bitfieldOr( a, &b->bitfield );
71}
72
73/* set 'a' to all the flags that were in 'a' but not 'b' */
74static inline void
75tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
76{
77    if( b->haveAll )
78        tr_bitfieldClear( a );
79    else if( !b->haveNone )
80        tr_bitfieldDifference( a, &b->bitfield );
81}
82
83static inline double
84tr_bitsetPercent( const tr_bitset * b )
85{
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;
90}
91
92static inline void
93tr_bitsetSetHaveAll( tr_bitset * b )
94{
95    b->haveAll = 1;
96    b->haveNone = 0;
97}
98
99static inline void
100tr_bitsetSetHaveNone( tr_bitset * b )
101{
102    b->haveAll = 0;
103    b->haveNone = 1;
104}
105
106static inline int
107tr_bitsetAdd( tr_bitset * b, size_t i )
108{
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 );
114    }
115    return ret;
116}
117
118#endif
Note: See TracBrowser for help on using the repository browser.