Changeset 6520


Ignore:
Timestamp:
Aug 14, 2008, 11:31:25 AM (13 years ago)
Author:
muks
Message:

Optimize tr_bitFieldAddRange()

Also add comments documenting the usage of these functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r6517 r6520  
    780780}
    781781
    782 int
    783 tr_bitfieldAddRange( tr_bitfield  * bitfield,
     782/* Sets bit range [begin, end) to 1 */
     783int
     784tr_bitfieldAddRange( tr_bitfield  * b,
    784785                     size_t         begin,
    785786                     size_t         end )
    786787{
    787     int err = 0;
    788     size_t i;
    789     for( i=begin; i<end; ++i )
    790         if(( err = tr_bitfieldAdd( bitfield, i )))
    791             break;
    792     return err;
     788    size_t sb, eb;
     789    unsigned char sm, em;
     790
     791    end--;
     792
     793    if( ( end >= b->bitCount ) || ( begin > end ) )
     794        return -1;
     795
     796    sb = begin >> 3;
     797    sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );
     798    eb = end >> 3;
     799    em = 0xff << ( 7 - ( end & 7 ) );
     800
     801    if ( sb == eb ) {
     802        b->bits[sb] |= ( sm & em );
     803    } else {
     804        b->bits[sb] |= sm;
     805        b->bits[eb] |= em;
     806        if( ++sb < eb )
     807            memset (b->bits + sb, 0xff, eb - sb);
     808    }
     809
     810    return 0;
    793811}
    794812
     
    807825}
    808826
     827/* Clears bit range [begin, end) to 0 */
    809828int
    810829tr_bitfieldRemRange ( tr_bitfield  * b,
     
    815834    unsigned char sm, em;
    816835
     836    end--;
     837
    817838    if( ( end >= b->bitCount ) || ( begin > end ) )
    818839        return -1;
     
    821842    sm = 0xff << ( 8 - ( begin & 7 ) );
    822843    eb = end >> 3;
    823     em = ~( 0xff << ( 8 - ( end & 7 ) ) );
     844    em = ~( 0xff << ( 7 - ( end & 7 ) ) );
    824845
    825846    if ( sb == eb ) {
     
    833854
    834855    return 0;
    835 
    836856}
    837857
Note: See TracChangeset for help on using the changeset viewer.