Changeset 2090


Ignore:
Timestamp:
Jun 16, 2007, 2:21:59 PM (15 years ago)
Author:
charles
Message:

just cleanup

Location:
branches/file_selection/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/file_selection/libtransmission/utils.c

    r2074 r2090  
    373373}
    374374
     375/****
     376*****
     377****/
     378
     379/* note that the argument is how many bits are needed, not bytes */
     380tr_bitfield_t*
     381tr_bitfieldNew( size_t bitcount )
     382{
     383    tr_bitfield_t * ret = calloc( 1, sizeof(tr_bitfield_t) );
     384    if( NULL == ret )
     385        return NULL;
     386
     387    ret->len = ( bitcount + 7u ) / 8u;
     388    ret->bits = calloc( ret->len, 1 );
     389    if( NULL == ret->bits ) {
     390        free( ret );
     391        return NULL;
     392    }
     393
     394    return ret;
     395}
     396
     397tr_bitfield_t*
     398tr_bitfieldDup( const tr_bitfield_t * in )
     399{
     400    tr_bitfield_t * ret = calloc( 1, sizeof(tr_bitfield_t) );
     401    ret->len = in->len;
     402    ret->bits = malloc( ret->len );
     403    memcpy( ret->bits, in->bits, ret->len );
     404    return ret;
     405}
     406
     407void tr_bitfieldFree( tr_bitfield_t * bitfield )
     408{
     409    if( bitfield )
     410    {
     411        free( bitfield->bits );
     412        free( bitfield );
     413    }
     414}
     415
     416void
     417tr_bitfieldClear( tr_bitfield_t * bitfield )
     418{
     419    memset( bitfield->bits, 0, bitfield->len );
     420}
     421
     422int
     423tr_bitfieldIsEmpty( const tr_bitfield_t * bitfield )
     424{
     425    unsigned int i;
     426
     427    for( i=0; i<bitfield->len; ++i )
     428        if( *bitfield->bits )
     429            return 0;
     430
     431    return 1;
     432}
     433
     434int
     435tr_bitfieldHas( const tr_bitfield_t   * bitfield,
     436                size_t                  bit )
     437{
     438    if ( bitfield == NULL ) return 0;
     439    assert( bit / 8u < bitfield->len );
     440    return ( bitfield->bits[ bit / 8u ] & ( 1 << ( 7 - ( bit % 8 ) ) ) );
     441}
     442
     443void
     444tr_bitfieldAdd( tr_bitfield_t  * bitfield, size_t bit )
     445{
     446    assert( bit / 8u < bitfield->len );
     447    bitfield->bits[ bit / 8u ] |= ( 1u << ( 7u - ( bit % 8u ) ) );
     448}
     449
     450void
     451tr_bitfieldAddRange( tr_bitfield_t  * bitfield,
     452                     size_t           first,
     453                     size_t           last )
     454{
     455    /* TODO: there are faster ways to do this */
     456    unsigned int i;
     457    for( i=first; i<=last; ++i )
     458        tr_bitfieldAdd( bitfield, i );
     459}
     460
     461void
     462tr_bitfieldRem( tr_bitfield_t   * bitfield,
     463                size_t            bit )
     464{
     465    assert( bit / 8u < bitfield->len );
     466    bitfield->bits[ bit / 8u ] &= ~( 1u << ( 7u - ( bit % 8u ) ) );
     467}
     468
     469void
     470tr_bitfieldRemRange ( tr_bitfield_t  * b,
     471                      size_t           first,
     472                      size_t           last )
     473{
     474    /* TODO: there are faster ways to do this */
     475    unsigned int i;
     476    for( i=first; i<=last; ++i )
     477        tr_bitfieldRem( b, i );
     478}
     479
     480tr_bitfield_t*
     481tr_bitfieldNegate( tr_bitfield_t * b )
     482{
     483    uint8_t *it;
     484    const uint8_t *end;
     485
     486    for( it=b->bits, end=it+b->len; it!=end; ++it )
     487        *it = ~*it;
     488
     489    return b;
     490}
     491
     492tr_bitfield_t*
     493tr_bitfieldAnd( tr_bitfield_t * a, const tr_bitfield_t * b )
     494{
     495    uint8_t *ait;
     496    const uint8_t *aend, *bit;
     497
     498    assert( a->len == b->len );
     499
     500    for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; ++ait, ++bit )
     501        *ait &= *bit;
     502
     503    return a;
     504}
     505
     506size_t
     507tr_bitfieldCountTrueBits( const tr_bitfield_t* b )
     508{
     509    size_t ret = 0;
     510    const uint8_t *it, *end;
     511    static const int trueBitCount[512] = {
     512        0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
     513        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
     514        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
     515        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     516        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
     517        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     518        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     519        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
     520        1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
     521        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     522        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     523        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
     524        2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
     525        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
     526        3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
     527        4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9
     528    };
     529
     530    for( it=b->bits, end=it+b->len; it!=end; ++it )
     531        ret += trueBitCount[*it];
     532
     533    return ret;
     534}
  • branches/file_selection/libtransmission/utils.h

    r2074 r2090  
    104104}
    105105
    106 struct tr_bitfield_s
    107 {
    108     uint8_t * bits;
    109     size_t len;
    110 };
    111 
    112 /* note that the argument is how many bits are needed, not bytes */
    113 static inline tr_bitfield_t * tr_bitfieldNew( unsigned int bitcount )
    114 {
    115     tr_bitfield_t * ret;
    116 
    117     ret = calloc( 1, sizeof *ret );
    118     if( NULL == ret )
    119     {
    120         return NULL;
    121     }
    122     ret->len = ( bitcount + 7u ) / 8u;
    123     ret->bits = calloc( ret->len, 1 );
    124     if( NULL == ret->bits )
    125     {
    126         free( ret );
    127         return NULL;
    128     }
    129 
    130     return ret;
    131 }
    132 
    133 static inline void tr_bitfieldFree( tr_bitfield_t * bitfield )
    134 {
    135     if( bitfield )
    136     {
    137         free( bitfield->bits );
    138         free( bitfield );
    139     }
    140 }
    141 
    142 static inline void tr_bitfieldClear( tr_bitfield_t * bitfield )
    143 {
    144     memset( bitfield->bits, 0, bitfield->len );
    145 }
    146 
    147 static inline int tr_bitfieldIsEmpty( const tr_bitfield_t * bitfield )
    148 {
    149     unsigned int i;
    150 
    151     for( i=0; i<bitfield->len; ++i )
    152         if( *bitfield->bits )
    153             return 0;
    154 
    155     return 1;
    156 }
    157 
    158 /***********************************************************************
    159  * tr_bitfieldHas
    160  **********************************************************************/
    161 static inline int tr_bitfieldHas( const tr_bitfield_t   * bitfield,
    162                                   unsigned int            piece )
    163 {
    164     if ( bitfield == NULL ) return 0;
    165     assert( piece / 8u < bitfield->len );
    166     return ( bitfield->bits[ piece / 8u ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
    167 }
    168 
    169 /***********************************************************************
    170  * tr_bitfieldAdd
    171  **********************************************************************/
    172 static inline void tr_bitfieldAdd( tr_bitfield_t  * bitfield,
    173                                    unsigned int     piece )
    174 {
    175     assert( piece / 8u < bitfield->len );
    176     bitfield->bits[ piece / 8u ] |= ( 1u << ( 7u - ( piece % 8u ) ) );
    177 }
    178 
    179 static inline void tr_bitfieldAddRange( tr_bitfield_t  * bitfield,
    180                                         unsigned int     first,
    181                                         unsigned int     last )
    182 {
    183     /* TODO: there are faster ways to do this */
    184     unsigned int i;
    185     for( i=first; i<=last; ++i )
    186         tr_bitfieldAdd( bitfield, i );
    187 }
    188 
    189 static inline void tr_bitfieldRem( tr_bitfield_t   * bitfield,
    190                                    unsigned int      piece )
    191 {
    192     assert( piece / 8u < bitfield->len );
    193     bitfield->bits[ piece / 8u ] &= ~( 1u << ( 7u - ( piece % 8u ) ) );
    194 }
    195 
    196 static inline void tr_bitfieldRemRange ( tr_bitfield_t  * b,
    197                                          unsigned int     first,
    198                                          unsigned int     last )
    199 {
    200     /* TODO: there are faster ways to do this */
    201     unsigned int i;
    202     for( i=first; i<=last; ++i )
    203         tr_bitfieldRem( b, i );
    204 }
    205106
    206107#define tr_blockPiece(a) _tr_blockPiece(tor,a)
     
    273174}
    274175
     176/***
     177****
     178***/
     179
     180struct tr_bitfield_s
     181{
     182    uint8_t * bits;
     183    size_t len;
     184};
     185
     186tr_bitfield_t* tr_bitfieldNew( size_t bitcount );
     187tr_bitfield_t* tr_bitfieldDup( const tr_bitfield_t* );
     188void tr_bitfieldFree( tr_bitfield_t*);
     189
     190void tr_bitfieldClear( tr_bitfield_t* );
     191void tr_bitfieldAdd( tr_bitfield_t*, size_t bit );
     192void tr_bitfieldRem( tr_bitfield_t*, size_t bit );
     193void tr_bitfieldAddRange( tr_bitfield_t *, size_t first, size_t last );
     194void tr_bitfieldRemRange ( tr_bitfield_t*, size_t first, size_t last );
     195
     196int    tr_bitfieldIsEmpty( const tr_bitfield_t* );
     197int    tr_bitfieldHas( const tr_bitfield_t *, size_t bit );
     198size_t tr_bitfieldCountTrueBits( const tr_bitfield_t* );
     199
     200tr_bitfield_t* tr_bitfieldNegate( tr_bitfield_t* );
     201tr_bitfield_t* tr_bitfieldAnd( tr_bitfield_t*, const tr_bitfield_t* );
     202
     203
     204
    275205#endif
Note: See TracChangeset for help on using the changeset viewer.