www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - inout foreach on BitArrays

reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
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.
Mar 02 2006
parent reply xs0 <xs0 xs0.com> writes:
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
Mar 02 2006
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
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.
Mar 02 2006