source: trunk/libtransmission/bitset.h @ 9934

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

(trunk libT) remove the r9933 assertions and maybe fix Yada's bug

  • 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 9934 2010-01-15 00:20:48Z 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        if( ( tmp->bits != NULL ) && ( tmp->byteCount > 0 ) )
57            memcpy( b->bitfield.bits, tmp->bits, tmp->byteCount );
58
59        tr_bitfieldFree( tmp );
60    }
61}
62
63static inline tr_bool
64tr_bitsetHasFast( const tr_bitset * b, const size_t nth )
65{
66    if( b->haveAll ) return TRUE;
67    if( b->haveNone ) return FALSE;
68    if( nth >= b->bitfield.bitCount ) return FALSE;
69    return tr_bitfieldHasFast( &b->bitfield, nth );
70}
71
72static inline tr_bool
73tr_bitsetHas( const tr_bitset * b, const size_t nth )
74{
75    if( b->haveAll ) return TRUE;
76    if( b->haveNone ) return FALSE;
77    if( nth >= b->bitfield.bitCount ) return FALSE;
78    return tr_bitfieldHas( &b->bitfield, nth );
79}
80
81static inline void
82tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
83{
84    if( b->haveAll )
85        tr_bitfieldAddRange( a, 0, a->bitCount );
86    else if( !b->haveNone )
87        tr_bitfieldOr( a, &b->bitfield );
88}
89
90/* set 'a' to all the flags that were in 'a' but not 'b' */
91static inline void
92tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
93{
94    if( b->haveAll )
95        tr_bitfieldClear( a );
96    else if( !b->haveNone )
97        tr_bitfieldDifference( a, &b->bitfield );
98}
99
100static inline double
101tr_bitsetPercent( const tr_bitset * b )
102{
103    if( b->haveAll ) return 1.0;
104    if( b->haveNone ) return 0.0;
105    if( b->bitfield.bitCount == 0 ) return 0.0;
106    return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount;
107}
108
109static inline void
110tr_bitsetSetHaveAll( tr_bitset * b )
111{
112    b->haveAll = 1;
113    b->haveNone = 0;
114}
115
116static inline void
117tr_bitsetSetHaveNone( tr_bitset * b )
118{
119    b->haveAll = 0;
120    b->haveNone = 1;
121}
122
123static inline int
124tr_bitsetAdd( tr_bitset * b, int i )
125{
126    int ret = 0;
127    if( !b->haveAll ) {
128        b->haveNone = 0;
129        tr_bitsetReserve( b, i+1 );
130        ret = tr_bitfieldAdd( &b->bitfield, i );
131    }
132    return ret;
133}
134
135#endif
Note: See TracBrowser for help on using the repository browser.