source: trunk/libtransmission/bitfield.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

File size: 2.5 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: utils.h 8685 2009-06-14 01:00:36Z charles $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_BITFIELD_H
18#define TR_BITFIELD_H 1
19
20#include "transmission.h"
21#include "utils.h" /* tr_new0 */
22
23typedef struct tr_bitfield
24{
25    uint8_t *  bits;
26    size_t     bitCount;
27    size_t     byteCount;
28}
29tr_bitfield;
30
31tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
32
33tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
34
35static inline tr_bitfield* tr_bitfieldNew( size_t bitcount )
36{
37    return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount );
38}
39
40static inline void tr_bitfieldFree( tr_bitfield * b )
41{
42    tr_free( tr_bitfieldDestruct( b ) );
43}
44
45tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
46
47void         tr_bitfieldClear( tr_bitfield* );
48
49int          tr_bitfieldAdd( tr_bitfield*, size_t bit );
50
51int          tr_bitfieldRem( tr_bitfield*, size_t bit );
52
53int          tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
54
55int          tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
56
57void         tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
58
59int          tr_bitfieldIsEmpty( const tr_bitfield* );
60
61size_t       tr_bitfieldCountTrueBits( const tr_bitfield* );
62
63tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
64
65/** A stripped-down version of bitfieldHas to be used
66    for speed when you're looping quickly.  This version
67    has none of tr_bitfieldHas()'s safety checks, so you
68    need to call tr_bitfieldTestFast() first before you
69    start looping. */
70static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
71{
72    return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
73}
74
75/** @param high the highest nth bit you're going to access */
76static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
77{
78    return ( b != NULL )
79        && ( b->bits != NULL )
80        && ( high < b->bitCount );
81}
82
83static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
84{
85    return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
86}
87
88#endif
Note: See TracBrowser for help on using the repository browser.