Ignore:
Timestamp:
Jun 15, 2009, 12:11:06 AM (13 years ago)
Author:
charles
Message:

(trunk libT) move tr_bitfield into its own, package-visible source files. this may break the mac build temporarily until the xcode file gets synced

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r8686 r8695  
    794794*****
    795795****/
    796 
    797 tr_bitfield*
    798 tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount )
    799 {
    800     b->bitCount = bitCount;
    801     b->byteCount = ( bitCount + 7u ) / 8u;
    802     b->bits = tr_new0( uint8_t, b->byteCount );
    803     return b;
    804 }
    805 
    806 tr_bitfield*
    807 tr_bitfieldDestruct( tr_bitfield * b )
    808 {
    809     if( b )
    810         tr_free( b->bits );
    811     return b;
    812 }
    813 
    814 tr_bitfield*
    815 tr_bitfieldDup( const tr_bitfield * in )
    816 {
    817     tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
    818 
    819     ret->bitCount = in->bitCount;
    820     ret->byteCount = in->byteCount;
    821     ret->bits = tr_memdup( in->bits, in->byteCount );
    822     return ret;
    823 }
    824 
    825 void
    826 tr_bitfieldClear( tr_bitfield * bitfield )
    827 {
    828     memset( bitfield->bits, 0, bitfield->byteCount );
    829 }
    830 
    831 int
    832 tr_bitfieldIsEmpty( const tr_bitfield * bitfield )
    833 {
    834     size_t i;
    835 
    836     for( i = 0; i < bitfield->byteCount; ++i )
    837         if( bitfield->bits[i] )
    838             return 0;
    839 
    840     return 1;
    841 }
    842 
    843 int
    844 tr_bitfieldAdd( tr_bitfield * bitfield,
    845                 size_t        nth )
    846 {
    847     assert( bitfield );
    848     assert( bitfield->bits );
    849 
    850     if( nth >= bitfield->bitCount )
    851         return -1;
    852 
    853     bitfield->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );
    854     return 0;
    855 }
    856 
    857 /* Sets bit range [begin, end) to 1 */
    858 int
    859 tr_bitfieldAddRange( tr_bitfield * b,
    860                      size_t        begin,
    861                      size_t        end )
    862 {
    863     size_t        sb, eb;
    864     unsigned char sm, em;
    865 
    866     end--;
    867 
    868     if( ( end >= b->bitCount ) || ( begin > end ) )
    869         return -1;
    870 
    871     sb = begin >> 3;
    872     sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );
    873     eb = end >> 3;
    874     em = 0xff << ( 7 - ( end & 7 ) );
    875 
    876     if( sb == eb )
    877     {
    878         b->bits[sb] |= ( sm & em );
    879     }
    880     else
    881     {
    882         b->bits[sb] |= sm;
    883         b->bits[eb] |= em;
    884         if( ++sb < eb )
    885             memset ( b->bits + sb, 0xff, eb - sb );
    886     }
    887 
    888     return 0;
    889 }
    890 
    891 int
    892 tr_bitfieldRem( tr_bitfield * bitfield,
    893                 size_t        nth )
    894 {
    895     assert( bitfield );
    896     assert( bitfield->bits );
    897 
    898     if( nth >= bitfield->bitCount )
    899         return -1;
    900 
    901     bitfield->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );
    902     return 0;
    903 }
    904 
    905 /* Clears bit range [begin, end) to 0 */
    906 int
    907 tr_bitfieldRemRange( tr_bitfield * b,
    908                      size_t        begin,
    909                      size_t        end )
    910 {
    911     size_t        sb, eb;
    912     unsigned char sm, em;
    913 
    914     end--;
    915 
    916     if( ( end >= b->bitCount ) || ( begin > end ) )
    917         return -1;
    918 
    919     sb = begin >> 3;
    920     sm = 0xff << ( 8 - ( begin & 7 ) );
    921     eb = end >> 3;
    922     em = ~( 0xff << ( 7 - ( end & 7 ) ) );
    923 
    924     if( sb == eb )
    925     {
    926         b->bits[sb] &= ( sm | em );
    927     }
    928     else
    929     {
    930         b->bits[sb] &= sm;
    931         b->bits[eb] &= em;
    932         if( ++sb < eb )
    933             memset ( b->bits + sb, 0, eb - sb );
    934     }
    935 
    936     return 0;
    937 }
    938 
    939 tr_bitfield*
    940 tr_bitfieldOr( tr_bitfield *       a,
    941                const tr_bitfield * b )
    942 {
    943     uint8_t *      ait;
    944     const uint8_t *aend, *bit;
    945 
    946     assert( a->bitCount == b->bitCount );
    947 
    948     for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;
    949          ait != aend; )
    950         *ait++ |= *bit++;
    951 
    952     return a;
    953 }
    954 
    955 /* set 'a' to all the flags that were in 'a' but not 'b' */
    956 void
    957 tr_bitfieldDifference( tr_bitfield *       a,
    958                        const tr_bitfield * b )
    959 {
    960     uint8_t *      ait;
    961     const uint8_t *aend, *bit;
    962 
    963     assert( a->bitCount == b->bitCount );
    964 
    965     for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;
    966          ait != aend; )
    967         *ait++ &= ~( *bit++ );
    968 }
    969 
    970 size_t
    971 tr_bitfieldCountTrueBits( const tr_bitfield* b )
    972 {
    973     size_t           ret = 0;
    974     const uint8_t *  it, *end;
    975     static const int trueBitCount[512] = {
    976         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3,
    977         4, 2, 3, 3, 4, 3, 4, 4, 5,
    978         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
    979         5, 3, 4, 4, 5, 4, 5, 5, 6,
    980         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
    981         5, 3, 4, 4, 5, 4, 5, 5, 6,
    982         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    983         6, 4, 5, 5, 6, 5, 6, 6, 7,
    984         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
    985         5, 3, 4, 4, 5, 4, 5, 5, 6,
    986         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    987         6, 4, 5, 5, 6, 5, 6, 6, 7,
    988         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    989         6, 4, 5, 5, 6, 5, 6, 6, 7,
    990         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
    991         7, 5, 6, 6, 7, 6, 7, 7, 8,
    992         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
    993         5, 3, 4, 4, 5, 4, 5, 5, 6,
    994         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    995         6, 4, 5, 5, 6, 5, 6, 6, 7,
    996         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    997         6, 4, 5, 5, 6, 5, 6, 6, 7,
    998         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
    999         7, 5, 6, 6, 7, 6, 7, 7, 8,
    1000         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
    1001         6, 4, 5, 5, 6, 5, 6, 6, 7,
    1002         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
    1003         7, 5, 6, 6, 7, 6, 7, 7, 8,
    1004         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
    1005         7, 5, 6, 6, 7, 6, 7, 7, 8,
    1006         4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, 5, 6, 6, 7, 6, 7, 7,
    1007         8, 6, 7, 7, 8, 7, 8, 8, 9
    1008     };
    1009 
    1010     if( !b )
    1011         return 0;
    1012 
    1013     for( it = b->bits, end = it + b->byteCount; it != end; ++it )
    1014         ret += trueBitCount[*it];
    1015 
    1016     return ret;
    1017 }
    1018 
    1019 /***
    1020 ****
    1021 ***/
    1022796
    1023797uint64_t
Note: See TracChangeset for help on using the changeset viewer.