source: trunk/libtransmission/bitset.h @ 9931

Last change on this file since 9931 was 9931, checked in by charles, 13 years ago

(trunk) housekeeping: (1) add standard svn properties for $Id$ substitution in some files (2) add #include guards in a couple of libtransmission headers (3) refresh build instructions in README

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