source: trunk/libtransmission/bitset.h @ 9933

Last change on this file since 9933 was 9933, checked in by charles, 12 years ago

(trunk libT) add in some assertions to try & smoke out Yada's crash

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