BitArray - Is there one?
Alex Rønne Petersen
alex at lycus.org
Sat May 26 23:53:32 PDT 2012
On 27-05-2012 08:50, Era Scarecrow wrote:
> Curiously I'm making a huffman compression algo for fun, however I
> didn't see anything in std.array or anywhere that was to support bools
> specifically (in a packed form anyways). So I'm making one. So far I've
> got it saving the data as uint, 0 refers to the most significant bit and
> 7 the least significant, so A will come out as 0100001 and look right
>
> It supports a range, however foreach doesn't like using
> front/popFront/empty if I make opApply. It supports slices as well.
>
> For the BitArray, using it as follows I get an error:
>
> struct BitArray {
> alias length opDollar;
> alias length __dollar;
>
> int length();
> Range opSlice();
> Range opSlice(int s, int e);
> struct Range {
> int opDollar();
> }
> }
>
> BitArray ba = BitArray(someArrayOfUbyte);
> auto range = ba[0 .. $]; //needs to access 'this' for length? shouldn't
> it rewrite to ba[0 .. ba.opDollar()] ?
> auto range2 = ba[]; //works fine
>
> It also seems the BitArray (but not the range) requires __dollar rather
> than opDollar, however an alias seems to fix that. (Why is this?? the
> Range doesn't need a __dollar alias to work right)
>
> I also have it so you can ref the individual bools (by using an
> intermediate one in opApply). So..
>
> //perfectly fine in unittest
> foreach(ref r_b; range) {
> r_b = !r_b;
> }
std.bitmanip.BitArray.
--
Alex Rønne Petersen
alex at lycus.org
http://lycus.org
More information about the Digitalmars-d-learn
mailing list