BitArray/BitFields - Review
Era Scarecrow
rtcvb32 at yahoo.com
Sat Jul 28 14:39:55 PDT 2012
On Saturday, 28 July 2012 at 21:30:21 UTC, Jonathan M Davis wrote:
> Well, it seems that my comment was somewhat misplaced in that
> BitArray isn't a range but a container. My comment was directed
> at opSlice on ranges. Container types should return whatever
> range type is appropriate. That will usually be a struct of
> some variety. I'd have to study BitArray and your changes to
> determine what the appropriate type was here, but it sounds
> like you were talking about duping the contents with opSlice,
> which would be highly irregular. Normally, ranges over
> containers are light wrappers which can iterate through and
> potentially alter the elements in the container, and duping up
> an array as the range type doesn't work like that. But
> depending on what BitArray is doing exactly, it might be
> appropriate. I don't know.
An array suggests you can control the size of the array, however
if you are forced to work in 32increments (or more if 64bit),
that makes it highly hard to control without making it a slice to
begin with. Consider without slices (range control)
BitArray ba; //size 32/64bit
ba.length = 42; //life, the universe and everything
assert(ba.length == 42); //fails, can't set length except by
increments of 32/64
ba ~= true; //what's the size now? Or are all appending
operators invalid unless it's a slice?
foreach(b; ba) {
//fails, can't go over range.
}
auto slice = ba[]; //converts to BitSlice
foreach(b; slice) {
//works
}
BitSlice func(BitArray ba);
BitSlice func(BitSlice ba); //two versions of function needed?
If convertable between the two, then you only lose the
begining/ending length offsets.
T func(T)(T ba) if (is(T : BitArray)) // correct? Potential loss
of information.
BitSlice x = ba[1 .. 10];
BitArray y = func(x); //range lost, beginning/ending at max
regardless of size
Honestly that doesn't add up to what I call an 'array'. It
should look like an array, act like an array, and besides not
being able to specify the array as separate from the entire
BitArray (Ie const(int)) it's otherwise should not be
distinguishable from an array.
More information about the Digitalmars-d-learn
mailing list