Thread overview
inout foreach on BitArrays
Mar 02, 2006
Deewiant
Mar 02, 2006
xs0
Mar 02, 2006
Deewiant
March 02, 2006
Am I confused, or is this a bug:
--
import std.bitarray;

void main() {
	BitArray a;
	a.length = 5;
	foreach (inout bit b; a) {
		assert (b == 0);
		b = 1;
	}
	foreach (bit b; a)
		assert (b == 1); // FAILS, they're all 0
}
--
Windows XP, DMD 0.148.
March 02, 2006
Deewiant wrote:
> Am I confused, or is this a bug:
> --
> import std.bitarray;
> 
> void main() {
> 	BitArray a;
> 	a.length = 5;
> 	foreach (inout bit b; a) {
> 		assert (b == 0);
> 		b = 1;
> 	}
> 	foreach (bit b; a)
> 		assert (b == 1); // FAILS, they're all 0
> }
> --
> Windows XP, DMD 0.148.

I think it's a bug, too - looking at the source of BitArray, it would seem that the new value does not get written back to the storage, even though it should..


xs0
March 02, 2006
xs0 wrote:
> I think it's a bug, too - looking at the source of BitArray, it would seem that the new value does not get written back to the storage, even though it should..
> 
> 
> xs0

I agree, I think the problem lies in the fact that the bit retrieved is a copy got by calling opIndex(i). It'd probably be easiest to just call opIndexAssign(i, b) afterward, but it might also be possible to somehow pass the bit itself directly.

Since part of BitArray is done with std.intrinsic and particularly in opIndex we
have the functions bt(), bts() and btr() whose implementations are unavailable,
I can't really say whether the latter is possible in any reasonable way.