Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3085

Show
Ignore:
Timestamp:
01/11/08 14:59:56 (11 months ago)
Author:
sean
Message:

Updated the documentation for BitArray? with complete descriptions of all routines. No examples are supplied, but I think the explanation should make things fairly obvious. This closes #275.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/core/BitArray.d

    r2897 r3085  
    1515 
    1616/** 
    17  * An array of bits. 
     17 * This struct represents an array of boolean values, each of which occupy one 
     18 * bit of memory for storage.  Thus an array of 32 bits would occupy the same 
     19 * space as one integer value.  The typical array operations--such as indexing 
     20 * and sorting--are supported, as well as bitwise operations such as and, or, 
     21 * xor, and complement. 
    1822 */ 
    1923struct BitArray 
     
    2428 
    2529    /** 
    26      * 
     30     * This initializes a BitArray of bits.length bits, where each bit value 
     31     * matches the corresponding boolean value in bits. 
     32     * 
     33     * Params: 
     34     *  bits = The initialization value. 
     35     * 
     36     * Returns: 
     37     *  A BitArray with the same number and sequence of elements as bits. 
    2738     */ 
    2839    static BitArray opCall( bool[] bits ) 
     
    3748 
    3849    /** 
    39      * 
     50     * Get the number of bits in this array. 
     51     * 
     52     * Returns: 
     53     *  The number of bits in this array. 
    4054     */ 
    4155    size_t length() 
     
    4660 
    4761    /** 
    48      * 
    49      */ 
    50     void length(size_t newlen) 
    51     { 
    52         if (newlen != len) 
    53         { 
    54             size_t olddim = dim(); 
    55             size_t newdim = (newlen + 31) / 32; 
    56  
    57             if (newdim != olddim) 
     62     * Resizes this array to newlen bits.  If newlen is larger than the current 
     63     * length, the new bits will be initialized to zero. 
     64     * 
     65     * Params: 
     66     *  newlen = The number of bits this array should contain. 
     67     */ 
     68    void length( size_t newlen ) 
     69    { 
     70        if( newlen != len ) 
     71        { 
     72            auto olddim = dim(); 
     73            auto newdim = (newlen + 31) / 32; 
     74 
     75            if( newdim != olddim ) 
    5876            { 
    5977                // Create a fake array so we can use D's realloc machinery 
    60                 uint[] b = ptr[0 .. olddim]; 
    61  
    62                 b.length = newdim; // realloc 
    63                 ptr = b.ptr; 
    64                 if (newdim & 31
     78                uint[] buf = ptr[0 .. olddim]; 
     79 
     80                buf.length = newdim; // realloc 
     81                ptr = buf.ptr; 
     82                if( newdim & 31
    6583                { 
    6684                    // Set any pad bits to 0 
     
    7492 
    7593    /** 
    76      * 
     94     * Gets the length of a uint array large enough to hold all stored bits. 
     95     * 
     96     * Returns: 
     97     *  The size a uint array would have to be to store this array. 
    7798     */ 
    7899    size_t dim() 
     
    83104 
    84105    /** 
    85      * Support for array.dup property for BitArray. 
     106     * Duplicates this array, much like the dup property for built-in arrays. 
     107     * 
     108     * Returns: 
     109     *  A duplicate of this array. 
    86110     */ 
    87111    BitArray dup() 
     
    89113        BitArray ba; 
    90114 
    91         uint[] b = ptr[0 .. dim].dup; 
     115        uint[] buf = ptr[0 .. dim].dup; 
    92116        ba.len = len; 
    93         ba.ptr = b.ptr; 
     117        ba.ptr = buf.ptr; 
    94118        return ba; 
    95119    } 
     
    102126        BitArray a; 
    103127        BitArray b; 
    104         int i; 
    105128 
    106129        a.length = 3; 
    107130        a[0] = 1; a[1] = 0; a[2] = 1; 
    108131        b = a.dup; 
    109         assert(b.length == 3); 
    110         for (i = 0; i < 3; i++) 
    111         { 
    112             //printf("b[%d] = %d\n", i, b[i]); 
    113             assert(b[i] == (((i ^ 1) & 1) ? true : false)); 
    114         } 
    115       } 
    116     } 
    117  
    118  
    119     /** 
    120      * Set BitArray to contents of ba[] 
    121      */ 
    122     void opAssign(bool[] ba) 
    123     { 
    124         length = ba.length; 
    125         foreach (i, b; ba) 
     132        assert( b.length == 3 ); 
     133        for( int i = 0; i < 3; ++i ) 
     134        { 
     135            assert( b[i] == (((i ^ 1) & 1) ? true : false) ); 
     136        } 
     137      } 
     138    } 
     139 
     140 
     141    /** 
     142     * Resets the length of this array to bits.length and then initializes this 
     143     * 
     144     * Resizes this array to hold bits.length bits and initializes each bit 
     145     * value to match the corresponding boolean value in bits. 
     146     * 
     147     * Params: 
     148     *  bits = The initialization value. 
     149     */ 
     150    void opAssign( bool[] bits ) 
     151    { 
     152        length = bits.length; 
     153        foreach( i, b; bits ) 
    126154        { 
    127155            (*this)[i] = b; 
     
    131159 
    132160    /** 
    133      * Map BitArray onto v[], with numbits being the number of bits 
    134      * in the array. Does not copy the data. 
    135      * 
    136      * This is the inverse of opCast. 
    137      */ 
    138     void init(void[] v, size_t numbits) 
     161     * Map BitArray onto target, with numbits being the number of bits in the 
     162     * array. Does not copy the data.  This is the inverse of opCast. 
     163     * 
     164     * Params: 
     165     *  target  = The array to map. 
     166     *  numbits = The number of bits to map in target. 
     167     */ 
     168    void init( void[] target, size_t numbits ) 
    139169    in 
    140170    { 
    141         assert(numbits <= v.length * 8); 
    142         assert((v.length & 3) == 0); 
    143     } 
    144     body 
    145     { 
    146         ptr = cast(uint*)v.ptr; 
     171        assert( numbits <= target.length * 8 ); 
     172        assert( (target.length & 3) == 0 ); 
     173    } 
     174    body 
     175    { 
     176        ptr = cast(uint*)target.ptr; 
    147177        len = numbits; 
    148178    } 
     
    155185        BitArray a = [1,0,1,0,1]; 
    156186        BitArray b; 
    157         void[] v
    158  
    159         v = cast(void[])a; 
    160         b.init(v, a.length); 
    161  
    162         assert(b[0] == 1); 
    163         assert(b[1] == 0); 
    164         assert(b[2] == 1); 
    165         assert(b[3] == 0); 
    166         assert(b[4] == 1); 
     187        void[] buf
     188 
     189        buf = cast(void[])a; 
     190        b.init( buf, a.length ); 
     191 
     192        assert( b[0] == 1 ); 
     193        assert( b[1] == 0 ); 
     194        assert( b[2] == 1 ); 
     195        assert( b[3] == 0 ); 
     196        assert( b[4] == 1 ); 
    167197 
    168198        a[0] = 0; 
    169         assert(b[0] == 0); 
    170  
    171         assert(a == b); 
    172       } 
    173     } 
    174  
    175  
    176     /** 
    177      * Support for array.reverse property for BitArray. 
     199        assert( b[0] == 0 ); 
     200 
     201        assert( a == b ); 
     202      } 
     203    } 
     204 
     205 
     206    /** 
     207     * Reverses the contents of this array in place, much like the reverse 
     208     * property for built-in arrays. 
     209     * 
     210     * Returns: 
     211     *  A shallow copy of this array. 
    178212     */ 
    179213    BitArray reverse() 
    180     out (result
    181     { 
    182         assert(result == *this); 
    183     } 
    184     body 
    185     { 
    186         if (len >= 2
     214    out( result
     215    { 
     216        assert( result == *this ); 
     217    } 
     218    body 
     219    { 
     220        if( len >= 2
    187221        { 
    188222            bool t; 
     
    191225            lo = 0; 
    192226            hi = len - 1; 
    193             for (; lo < hi; lo++, hi--
     227            for( ; lo < hi; ++lo, --hi
    194228            { 
    195229                t = (*this)[lo]; 
     
    206240      unittest 
    207241      { 
    208         BitArray b; 
    209242        static bool[5] data = [1,0,1,1,0]; 
    210         int i; 
    211  
    212         b = data; 
     243        BitArray b = data; 
    213244        b.reverse; 
    214         for (i = 0; i < data.length; i++) 
    215         { 
    216             assert(b[i] == data[4 - i]); 
    217         } 
    218       } 
    219     } 
    220  
    221  
    222     /** 
    223      * Support for array.sort property for BitArray. 
     245 
     246        for( size_t i = 0; i < data.length; ++i ) 
     247        { 
     248            assert( b[i] == data[4 - i] ); 
     249        } 
     250      } 
     251    } 
     252 
     253 
     254    /** 
     255     * Sorts this array in place, with zero entries sorting before one.  This 
     256     * is equivalent to the sort property for built-in arrays. 
     257     * 
     258     * Returns: 
     259     *  A shallow copy of this array. 
    224260     */ 
    225261    BitArray sort() 
    226     out (result
    227     { 
    228         assert(result == *this); 
    229     } 
    230     body 
    231     { 
    232         if (len >= 2
     262    out( result
     263    { 
     264        assert( result == *this ); 
     265    } 
     266    body 
     267    { 
     268        if( len >= 2
    233269        { 
    234270            size_t lo, hi; 
     
    236272            lo = 0; 
    237273            hi = len - 1; 
    238             while (1
     274            while( true
    239275            { 
    240                 while (1
     276                while( true
    241277                { 
    242                     if (lo >= hi
     278                    if( lo >= hi
    243279                        goto Ldone; 
    244                     if ((*this)[lo] == true
     280                    if( (*this)[lo] == true
    245281                        break; 
    246                     lo++
     282                    ++lo
    247283                } 
    248284 
    249                 while (1
     285                while( true
    250286                { 
    251                     if (lo >= hi
     287                    if( lo >= hi
    252288                        goto Ldone; 
    253                     if ((*this)[hi] == false
     289                    if( (*this)[hi] == false
    254290                        break; 
    255                     hi--
     291                    --hi
    256292                } 
    257293 
     
    259295                (*this)[hi] = true; 
    260296 
    261                 lo++
    262                 hi--
     297                ++lo
     298                --hi
    263299            } 
    264300            Ldone: 
     
    277313 
    278314        ba.sort; 
    279         for (size_t i = 0; i < 6; i++) 
    280             assert(ba[i] == false); 
    281         for (size_t i = 6; i < 10; i++) 
    282             assert(ba[i] == true); 
    283       } 
    284     } 
    285  
    286  
    287     /** 
    288      * Support for foreach loops for BitArray. 
    289      */ 
    290     int opApply(int delegate(inout bool) dg) 
     315        for( size_t i = 0; i < 6; ++i ) 
     316            assert( ba[i] == false ); 
     317        for( size_t i = 6; i < 10; ++i ) 
     318            assert( ba[i] == true ); 
     319      } 
     320    } 
     321 
     322 
     323    /** 
     324     * Operates on all bits in this array. 
     325     * 
     326     * Params: 
     327     *  dg = The supplied code as a delegate. 
     328     */ 
     329    int opApply( int delegate(inout bool) dg ) 
    291330    { 
    292331        int result; 
    293332 
    294         for (size_t i = 0; i < len; i++
    295         { 
    296             bool b = opIndex(i); 
    297             result = dg(b); 
    298             opIndexAssign(b,i); 
    299             if (result
     333        for( size_t i = 0; i < len; ++i
     334        { 
     335            bool b = opIndex( i ); 
     336            result = dg( b ); 
     337            opIndexAssign( b, i ); 
     338            if( result
    300339                break; 
    301340        } 
     
    303342    } 
    304343 
     344 
    305345    /** ditto */ 
    306     int opApply(int delegate(inout size_t, inout bool) dg
     346    int opApply( int delegate(inout size_t, inout bool) dg
    307347    { 
    308348        int result; 
    309349 
    310         for (size_t i = 0; i < len; i++
    311         { 
    312             bool b = opIndex(i); 
    313             result = dg(i, b); 
    314             opIndexAssign(b,i); 
    315             if (result
     350        for( size_t i = 0; i < len; ++i
     351        { 
     352            bool b = opIndex( i ); 
     353            result = dg( i, b ); 
     354            opIndexAssign( b, i ); 
     355            if( result
    316356                break; 
    317357        } 
     
    327367 
    328368        int i; 
    329         foreach (b;a
    330         { 
    331             switch (i
     369        foreach( b; a
     370        { 
     371            switch( i
    332372            { 
    333             case 0: assert(b == true);  break; 
    334             case 1: assert(b == false); break; 
    335             case 2: assert(b == true);  break; 
    336             default: assert(0); 
     373            case 0: assert( b == true );  break; 
     374            case 1: assert( b == false ); break; 
     375            case 2: assert( b == true );  break; 
     376            default: assert( false ); 
    337377            } 
    338378            i++; 
    339379        } 
    340380 
    341         foreach (j,b;a
    342         { 
    343             switch (j
     381        foreach( j, b; a
     382        { 
     383            switch( j
    344384            { 
    345             case 0: assert(b == true);  break; 
    346             case 1: assert(b == false); break; 
    347             case 2: assert(b == true);  break; 
    348             default: assert(0); 
     385            case 0: assert( b == true );  break; 
     386            case 1: assert( b == false ); break; 
     387            case 2: assert( b == true );  break; 
     388            default: assert( false ); 
    349389            } 
    350390        } 
     
    354394 
    355395    /** 
    356      * Support for operators == and != for bit arrays. 
    357      */ 
    358     int opEquals(BitArray a2) 
    359     { 
    360         int i; 
    361  
    362         if (this.length != a2.length) 
    363             return 0;       // not equal 
    364         byte *p1 = cast(byte*)this.ptr; 
    365         byte *p2 = cast(byte*)a2.ptr; 
    366         uint n = this.length / 8; 
    367         for (i = 0; i < n; i++) 
    368         { 
    369             if (p1[i] != p2[i]) 
    370             return 0;       // not equal 
    371         } 
    372  
    373         ubyte mask; 
    374  
     396     * Compares this array to another for equality.  Two bit arrays are equal 
     397     * if they are the same size and contain the same series of bits. 
     398     * 
     399     * Params: 
     400     *  rhs = The array to compare against. 
     401     * 
     402     * Returns: 
     403     *  zero if not equal and non-zero otherwise. 
     404     */ 
     405    int opEquals( BitArray rhs ) 
     406    { 
     407        if( this.length != rhs.length ) 
     408            return 0; // not equal 
     409        byte* p1 = cast(byte*)this.ptr; 
     410        byte* p2 = cast(byte*)rhs.ptr; 
     411        size_t n = this.length / 8; 
     412        size_t i; 
     413        for( i = 0; i < n; ++i ) 
     414        { 
     415            if( p1[i] != p2[i] ) 
     416            return 0; // not equal 
     417        } 
    375418        n = this.length & 7; 
    376     mask = cast(ubyte)((1 << n) - 1); 
    377         //printf("i = %d, n = %d, mask = %x, %x, %x\n", i, n, mask, p1[i], p2[i]); 
     419        ubyte mask = cast(ubyte)((1 << n) - 1); 
    378420        return (mask == 0) || (p1[i] & mask) == (p2[i] & mask); 
    379421    } 
     
    398440 
    399441    /** 
    400      * Implement comparison operators. 
    401      */ 
    402     int opCmp(BitArray a2) 
    403     { 
    404         uint len; 
    405         uint i; 
    406  
    407         len = this.length; 
    408         if (a2.length < len) 
    409             len = a2.length; 
     442     * Performs a lexicographical comparison of this array to the supplied 
     443     * array. 
     444     * 
     445     * Params: 
     446     *  rhs = The array to compare against. 
     447     * 
     448     * Returns: 
     449     *  A value less than zero if this array sorts before the supplied array, 
     450     *  zero if the arrays are equavalent, and a value greater than zero if 
     451     *  this array sorts after the supplied array. 
     452     */ 
     453    int opCmp( BitArray rhs ) 
     454    { 
     455        auto len = this.length; 
     456        if( rhs.length < len ) 
     457            len = rhs.length; 
    410458        ubyte* p1 = cast(ubyte*)this.ptr; 
    411         ubyte* p2 = cast(ubyte*)a2.ptr; 
    412         uint n = len / 8; 
    413         for (i = 0; i < n; i++) 
    414         { 
    415             if (p1[i] != p2[i]) 
    416             break;      // not equal 
    417         } 
    418         for (uint j = i * 8; j < len; j++) 
    419     {   ubyte mask = cast(ubyte)(1 << j); 
     459        ubyte* p2 = cast(ubyte*)rhs.ptr; 
     460        size_t n = len / 8; 
     461        size_t i; 
     462        for( i = 0; i < n; ++i ) 
     463        { 
     464            if( p1[i] != p2[i] ) 
     465            break; // not equal 
     466        } 
     467        for( size_t j = i * 8; j < len; ++j ) 
     468        { 
     469            ubyte mask = cast(ubyte)(1 << j); 
    420470            int c; 
    421471 
    422472            c = cast(int)(p1[i] & mask) - cast(int)(p2[i] & mask); 
    423             if (c
    424             return c; 
    425         } 
    426         return cast(int)this.len - cast(int)a2.length; 
     473            if( c
     474                return c; 
     475        } 
     476        return cast(int)this.len - cast(int)rhs.length; 
    427477    } 
    428478 
     
    437487        BitArray e = [1,0,1,0,1]; 
    438488 
    439         assert(a >  b); 
    440         assert(a >= b); 
    441         assert(a <  c); 
    442         assert(a <= c); 
    443         assert(a <  d); 
    444         assert(a <= d); 
    445         assert(a == e); 
    446         assert(a <= e); 
    447         assert(a >= e); 
    448       } 
    449     } 
    450  
    451  
    452     /** 
    453      * Convert to void[]. 
     489        assert( a >  b ); 
     490        assert( a >= b ); 
     491        assert( a <  c ); 
     492        assert( a <= c ); 
     493        assert( a <  d ); 
     494        assert( a <= d ); 
     495        assert( a == e ); 
     496        assert( a <= e ); 
     497        assert( a >= e ); 
     498      } 
     499    } 
     500 
     501 
     502    /** 
     503     * Convert this array to a void array. 
     504     * 
     505     * Returns: 
     506     *  This array represented as a void array. 
    454507     */ 
    455508    void[] opCast() 
     
    466519        void[] v = cast(void[])a; 
    467520 
    468         assert(v.length == a.dim * uint.sizeof); 
    469       } 
    470     } 
    471  
    472  
    473     /** 
    474      * Support for [$(I index)] operation for BitArray. 
    475      */ 
    476     bool opIndex(size_t i) 
     521        assert( v.length == a.dim * uint.sizeof ); 
     522      } 
     523    } 
     524 
     525 
     526    /** 
     527     * Support for index operations, much like the behavior of built-in arrays. 
     528     * 
     529     * Params: 
     530     *  pos = The desired index position. 
     531     * 
     532     * In: 
     533     *  pos must be less than the length of this array. 
     534     * 
     535     * Returns: 
     536     *  The value of the bit at pos. 
     537     */ 
     538    bool opIndex( size_t pos ) 
    477539    in 
    478540    { 
    479         assert(i < len); 
    480     } 
    481     body 
    482     { 
    483         return cast(bool)bt(ptr, i); 
    484     } 
    485  
    486  
    487     /** 
    488      * Support for unary operator ~ for bit arrays. 
     541        assert( pos < len ); 
     542    } 
     543    body 
     544    { 
     545        return cast(bool)bt( ptr, pos ); 
     546    } 
     547 
     548 
     549    /** 
     550     * Generates a copy of this array with the unary complement operation 
     551     * applied. 
     552     * 
     553     * Returns: 
     554     *  A new array which is the complement of this array. 
    489555     */ 
    490556    BitArray opCom() 
     
    495561 
    496562        result.length = len; 
    497         for (size_t i = 0; i < dim; i++
     563        for( size_t i = 0; i < dim; ++i
    498564            result.ptr[i] = ~this.ptr[i]; 
    499         if (len & 31
     565        if( len & 31
    500566            result.ptr[dim - 1] &= ~(~0 << (len & 31)); 
    501567        return result; 
     
    520586 
    521587    /** 
    522      * Support for binary operator & for bit arrays. 
    523      */ 
    524     BitArray opAnd(BitArray e2) 
     588     * Generates a new array which is the result of a bitwise and operation 
     589     * between this array and the supplied array. 
     590     * 
     591     * Params: 
     592     *  rhs = The array with which to perform the bitwise and operation. 
     593     * 
     594     * In: 
     595     *  rhs.length must equal the length of this array. 
     596     * 
     597     * Returns: 
     598     *  A new array which is the result of a bitwise and with this array and 
     599     *  the supplied array. 
     600     */ 
     601    BitArray opAnd( BitArray rhs ) 
    525602    in 
    526603    { 
    527         assert(len == e2.length); 
     604        assert( len == rhs.length ); 
    528605    } 
    529606    body 
     
    534611 
    535612        result.length = len; 
    536         for (size_t i = 0; i < dim; i++
    537             result.ptr[i] = this.ptr[i] & e2.ptr[i]; 
     613        for( size_t i = 0; i < dim; ++i
     614            result.ptr[i] = this.ptr[i] & rhs.ptr[i]; 
    538615        return result; 
    539616    } 
     
    559636 
    560637    /** 
    561      * Support for binary operator | for bit arrays. 
    562      */ 
    563     BitArray opOr(BitArray e2) 
     638     * Generates a new array which is the result of a bitwise or operation 
     639     * between this array and the supplied array. 
     640     * 
     641     * Params: 
     642     *  rhs = The array with which to perform the bitwise or operation. 
     643     * 
     644     * In: 
     645     *  rhs.length must equal the length of this array. 
     646     * 
     647     * Returns: 
     648     *  A new array which is the result of a bitwise or with this array and 
     649     *  the supplied array. 
     650     */ 
     651    BitArray opOr( BitArray rhs ) 
    564652    in 
    565653    { 
    566         assert(len == e2.length); 
     654        assert( len == rhs.length ); 
    567655    } 
    568656    body 
     
    573661 
    574662        result.length = len; 
    575         for (size_t i = 0; i < dim; i++
    576             result.ptr[i] = this.ptr[i] | e2.ptr[i]; 
     663        for( size_t i = 0; i < dim; ++i
     664            result.ptr[i] = this.ptr[i] | rhs.ptr[i]; 
    577665        return result; 
    578666    } 
     
    598686 
    599687    /** 
    600      * Support for binary operator ^ for bit arrays. 
    601      */ 
    602     BitArray opXor(BitArray e2) 
     688     * Generates a new array which is the result of a bitwise xor operation 
     689     * between this array and the supplied array. 
     690     * 
     691     * Params: 
     692     *  rhs = The array with which to perform the bitwise xor operation. 
     693     * 
     694     * In: 
     695     *  rhs.length must equal the length of this array. 
     696     * 
     697     * Returns: 
     698     *  A new array which is the result of a bitwise xor with this array and 
     699     *  the supplied array. 
     700     */ 
     701    BitArray opXor( BitArray rhs ) 
    603702    in 
    604703    { 
    605         assert(len == e2.length); 
     704        assert( len == rhs.length ); 
    606705    } 
    607706    body 
     
    612711 
    613712        result.length = len; 
    614         for (size_t i = 0; i < dim; i++
    615             result.ptr[i] = this.ptr[i] ^ e2.ptr[i]; 
     713        for( size_t i = 0; i < dim; ++i
     714            result.ptr[i] = this.ptr[i] ^ rhs.ptr[i]; 
    616715        return result; 
    617716    } 
     
    637736 
    638737    /** 
    639      * Support for binary operator - for bit arrays. 
    640      * 
    641      * $(I a - b) for BitArrays means the same thing as $(I a &amp; ~b). 
    642      */ 
    643     BitArray opSub(BitArray e2) 
     738     * Generates a new array which is the result of this array minus the 
     739     * supplied array.  $(I a - b) for BitArrays means the same thing as 
     740     * $(I a &amp; ~b). 
     741     * 
     742     * Params: 
     743     *  rhs = The array with which to perform the subtraction operation. 
     744     * 
     745     * In: 
     746     *  rhs.length must equal the length of this array. 
     747     * 
     748     * Returns: 
     749     *  A new array which is the result of this array minus the supplied array. 
     750     */ 
     751    BitArray opSub( BitArray rhs ) 
    644752    in 
    645753    { 
    646         assert(len == e2.length); 
     754        assert( len == rhs.length ); 
    647755    } 
    648756    body 
     
    653761 
    654762        result.length = len; 
    655         for (size_t i = 0; i < dim; i++
    656             result.ptr[i] = this.ptr[i] & ~e2.ptr[i]; 
     763        for( size_t i = 0; i < dim; ++i
     764            result.ptr[i] = this.ptr[i] & ~rhs.ptr[i]; 
    657765        return result; 
    658766    } 
     
    668776        BitArray c = a - b; 
    669777 
    670         assert(c[0] == 0); 
    671         assert(c[1] == 0); 
    672         assert(c[2] == 0); 
    673         assert(c[3] == 0); 
    674         assert(c[4] == 1); 
    675       } 
    676     } 
    677  
    678  
    679     /** 
    680      * Support for binary operator ~ for bit arrays. 
    681      */ 
    682     BitArray opCat(bool b) 
    683     { 
    684         BitArray r; 
    685  
    686         r = this.dup; 
    687         r.length = len + 1; 
    688         r[len] = b; 
    689         return r; 
     778        assert( c[0] == 0 ); 
     779        assert( c[1] == 0 ); 
     780        assert( c[2] == 0 ); 
     781        assert( c[3] == 0 ); 
     782        assert( c[4] == 1 ); 
     783      } 
     784    } 
     785 
     786 
     787    /** 
     788     * Generates a new array which is the result of this array concatenated 
     789     * with the supplied array. 
     790     * 
     791     * Params: 
     792     *  rhs = The array with which to perform the concatenation operation. 
     793     * 
     794     * Returns: 
     795     *  A new array which is the result of this array concatenated with the 
     796     *  supplied array. 
     797     */ 
     798    BitArray opCat( bool rhs ) 
     799    { 
     800        BitArray result; 
     801 
     802        result = this.dup; 
     803        result.length = len + 1; 
     804        result[len] = rhs; 
     805        return result; 
    690806    } 
    691807 
    692808 
    693809    /** ditto */ 
    694     BitArray opCat_r(bool b
    695     { 
    696         BitArray r
    697  
    698         r.length = len + 1; 
    699         r[0] = b
    700         for (size_t i = 0; i < len; i++
    701             r[1 + i] = (*this)[i]; 
    702         return r
     810    BitArray opCat_r( bool lhs
     811    { 
     812        BitArray result
     813 
     814        result.length = len + 1; 
     815        result[0] = lhs
     816        for( size_t i = 0; i < len; ++i
     817            result[1 + i] = (*this)[i]; 
     818        return result
    703819    } 
    704820 
    705821 
    706822    /** ditto */ 
    707     BitArray opCat(BitArray b
    708     { 
    709         BitArray r
    710  
    711         r = this.dup(); 
    712         r ~= b
    713         return r
     823    BitArray opCat( BitArray rhs
     824    { 
     825        BitArray result
     826 
     827        result = this.dup(); 
     828        result ~= rhs
     829        return result
    714830    } 
    715831 
     
    724840 
    725841        c = (a ~ b); 
    726         assert(c.length == 5); 
    727         assert(c[0] == 1); 
    728         assert(c[1] == 0); 
    729         assert(c[2] == 0); 
    730         assert(c[3] == 1); 
    731         assert(c[4] == 0); 
     842        assert( c.length == 5 ); 
     843        assert( c[0] == 1 ); 
     844        assert( c[1] == 0 ); 
     845        assert( c[2] == 0 ); 
     846        assert( c[3] == 1 ); 
     847        assert( c[4] == 0 ); 
    732848 
    733849        c = (a ~ true); 
    734         assert(c.length == 3); 
    735         assert(c[0] == 1); 
    736         assert(c[1] == 0); 
    737         assert(c[2] == 1); 
     850        assert( c.length == 3 ); 
     851        assert( c[0] == 1 ); 
     852        assert( c[1] == 0 ); 
     853        assert( c[2] == 1 ); 
    738854 
    739855        c = (false ~ a); 
    740         assert(c.length == 3); 
    741         assert(c[0] == 0); 
    742         assert(c[1] == 1); 
    743         assert(c[2] == 0); 
    744       } 
    745     } 
    746  
    747  
    748     /** 
    749      * Support for [$(I index)] operation for BitArray. 
    750      */ 
    751     bool opIndexAssign(bool b, size_t i) 
     856        assert( c.length == 3 ); 
     857        assert( c[0] == 0 ); 
     858        assert( c[1] == 1 ); 
     859        assert( c[2] == 0 ); 
     860      } 
     861    } 
     862 
     863 
     864    /** 
     865     * Support for index operations, much like the behavior of built-in arrays. 
     866     * 
     867     * Params: 
     868     *  b   = The new bit value to set. 
     869     *  pos = The desired index position. 
     870     * 
     871     * In: 
     872     *  pos must be less than the length of this array. 
     873     * 
     874     * Returns: 
     875     *  The new value of the bit at pos. 
     876     */ 
     877    bool opIndexAssign( bool b, size_t pos ) 
    752878    in 
    753879    { 
    754         assert(i < len); 
    755     } 
    756     body 
    757     { 
    758         if (b
    759             bts(ptr, i); 
     880        assert( pos < len ); 
     881    } 
     882    body 
     883    { 
     884        if( b
     885            bts( ptr, pos ); 
    760886        else 
    761             btr(ptr, i); 
     887            btr( ptr, pos ); 
    762888        return b; 
    763889    } 
     
    765891 
    766892    /** 
    767      * Support for operator &= bit arrays. 
    768      */ 
    769     BitArray opAndAssign(BitArray e2) 
     893     * Updates the contents of this array with the result of a bitwise and 
     894     * operation between this array and the supplied array. 
     895     * 
     896     * Params: 
     897     *  rhs = The array with which to perform the bitwise and operation. 
     898     * 
     899     * In: 
     900     *  rhs.length must equal the length of this array. 
     901     * 
     902     * Returns: 
     903     *  A shallow copy of this array. 
     904     */ 
     905    BitArray opAndAssign( BitArray rhs ) 
    770906    in 
    771907    { 
    772         assert(len == e2.length); 
     908        assert( len == rhs.length ); 
    773909    } 
    774910    body 
     
    776912        auto dim = this.dim(); 
    777913 
    778         for (size_t i = 0; i < dim; i++
    779             ptr[i] &= e2.ptr[i]; 
     914        for( size_t i = 0; i < dim; ++i
     915            ptr[i] &= rhs.ptr[i]; 
    780916        return *this; 
    781917    } 
     
    790926 
    791927        a &= b; 
    792         assert(a[0] == 1); 
    793         assert(a[1] == 0); 
    794         assert(a[2] == 1); 
    795         assert(a[3] == 0); 
    796         assert(a[4] == 0); 
    797       } 
    798     } 
    799  
    800  
    801     /** 
    802      * Support for operator |= for bit arrays. 
    803      */ 
    804     BitArray opOrAssign(BitArray e2) 
     928        assert( a[0] == 1 ); 
     929        assert( a[1] == 0 ); 
     930        assert( a[2] == 1 ); 
     931        assert( a[3] == 0 ); 
     932        assert( a[4] == 0 ); 
     933      } 
     934    } 
     935 
     936 
     937    /** 
     938     * Updates the contents of this array with the result of a bitwise or 
     939     * operation between this array and the supplied array. 
     940     * 
     941     * Params: 
     942     *  rhs = The array with which to perform the bitwise or operation. 
     943     * 
     944     * In: 
     945     *  rhs.length must equal the length of this array. 
     946     * 
     947     * Returns: 
     948     *  A shallow copy of this array. 
     949     */ 
     950    BitArray opOrAssign( BitArray rhs ) 
    805951    in 
    806952    { 
    807         assert(len == e2.length); 
     953        assert( len == rhs.length ); 
    808954    } 
    809955    body 
     
    811957        auto dim = this.dim(); 
    812958 
    813         for (size_t i = 0; i < dim; i++
    814             ptr[i] |= e2.ptr[i]; 
     959        for( size_t i = 0; i < dim; ++i
     960            ptr[i] |= rhs.ptr[i]; 
    815961        return *this; 
    816962    } 
     
    825971 
    826972        a |= b; 
    827         assert(a[0] == 1); 
    828         assert(a[1] == 0); 
    829         assert(a[2] == 1); 
    830         assert(a[3] == 1);