source: trunk/libtransmission/bitset.h @ 9558

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

(trunk libT) fix bitset crash. add bitset.h to the automake file.

File size: 3.0 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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:$
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 TR_INLINE void
35tr_bitsetConstructor( tr_bitset * b, int size )
36{
37    tr_bitfieldConstruct( &b->bitfield, size );
38}
39
40static TR_INLINE void
41tr_bitsetDestructor( tr_bitset * b )
42{
43    tr_bitfieldDestruct( &b->bitfield );
44}
45
46static TR_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        assert( b->bitfield.byteCount >= tmp->byteCount );
56        memcpy( &b->bitfield.bits, tmp->bits, tmp->byteCount );
57
58        tr_bitfieldFree( tmp );
59    }
60}
61
62static TR_INLINE tr_bool
63tr_bitsetHasFast( const tr_bitset * b, const size_t nth )
64{
65    if( b->haveAll ) return TRUE;
66    if( b->haveNone ) return FALSE;
67    return tr_bitfieldHasFast( &b->bitfield, nth );
68}
69
70static TR_INLINE tr_bool
71tr_bitsetHas( const tr_bitset * b, const size_t nth )
72{
73    if( b->haveAll ) return TRUE;
74    if( b->haveNone ) return FALSE;
75    return tr_bitfieldHas( &b->bitfield, nth );
76}
77
78static TR_INLINE void
79tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
80{
81    if( b->haveAll )
82        tr_bitfieldAddRange( a, 0, a->bitCount );
83    else if( !b->haveNone )
84        tr_bitfieldOr( a, &b->bitfield );
85}
86
87/* set 'a' to all the flags that were in 'a' but not 'b' */
88static TR_INLINE void
89tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
90{
91    if( b->haveAll )
92        tr_bitfieldClear( a );
93    else if( !b->haveNone )
94        tr_bitfieldDifference( a, &b->bitfield );
95}
96
97static TR_INLINE double
98tr_bitsetPercent( const tr_bitset * b )
99{
100    if( b->haveAll ) return 1.0;
101    if( b->haveNone ) return 0.0;
102    if( b->bitfield.bitCount == 0 ) return 0.0;
103    return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount;
104}
105
106static TR_INLINE void
107tr_bitsetSetHaveAll( tr_bitset * b )
108{
109    b->haveAll = 1;
110    b->haveNone = 0;
111}
112
113static TR_INLINE void
114tr_bitsetSetHaveNone( tr_bitset * b )
115{
116    b->haveAll = 0;
117    b->haveNone = 1;
118}
119
120static TR_INLINE int
121tr_bitsetAdd( tr_bitset * b, int i )
122{
123    int ret = 0;
124    if( !b->haveAll ) {
125        b->haveNone = 0;
126        tr_bitsetReserve( b, i );
127        ret = tr_bitfieldAdd( &b->bitfield, i );
128    }
129    return ret;
130}
131
132#endif
Note: See TracBrowser for help on using the repository browser.