January 03, 2006
I tried to write a minimal templated stack class.
I wanted my class to implement a templated interface.
In unittesting phase i found out that the sort method I implemented (simply
calling the sort method of the internal dynamic array) had the effect of zeroing
all the elements of the array.
I strongly suspect this is a bug.

Here the code (the minimum amount of code to reproduce the bug):

in file base.d (the interface):
---
module containing.base;

//General interface
interface Container(ElementType)
{
//Properties
public
{
Container sort();
}
}
---

in file Stack.d (the class):
---
module containing.stack;

//Imports
private
{
import containing.base;
}

//Stack class
class Stack(ElementType) : Container!(ElementType)
{
//Variables
protected
{
uint ArrayIncrement=16;
uint Position=0;
ElementType[] InternalStorage;
}
//Properties
public
{
Container!(ElementType) sort()
{
InternalStorage.sort;
return this;
}
}
//Methods
//Constructors/Destructors
public
{
this(ElementType[] Elements)
{
Position=cast(uint)(ArrayIncrement=Elements.length);
ArrayIncrement<<=1;
InternalStorage=new ElementType[ArrayIncrement];
InternalStorage[0..Elements.length]=Elements[0..Elements.length];
}
}
//Operators
public
{
//foreachability (opApply)
int opApply(int delegate(inout ElementType) dg)
{
int delres=0;
for(uint i=0;i<Position;i++)
{
delres=dg(InternalStorage[i]);
if(delres)
return delres;
}
return delres;
}
int opApply(int delegate(inout ElementType,inout uint) dg)
{
int delres=0;
for(uint i=cast(uint)(0);i<Position;i++)
{
delres=dg(InternalStorage[i],i);
if(delres)
return delres;
}
return delres;
}
}
}
---

in file DummyMain (a dummy main to show the bug):
---
private
{
import containing.stack;
import std.stdio;
}

//Dummy main
int main(char[][] args)
{
writef("Allocation of an array of ints...\n");
static int[] arrToCopy=[12,13,30,2,15];
int[] arrToSort=arrToCopy.dup;
writefln("Before sorting:");
for(uint i=0;i<arrToSort.length;i++)
writefln("arrToSort[",i,"]=",arrToSort[i]);
arrToSort.sort;
writefln("After sorting:");
for(uint i=0;i<arrToSort.length;i++)
writefln("arrToSort[",i,"]=",arrToSort[i]);

writef("\nConstructing a Stack of ints from the unsorted array...\n");
Stack!(int) B=new Stack!(int)(arrToCopy.dup);
writefln("Before sorting:");
foreach(int act,uint i;B)
writefln("B[",i,"]=",act);
B.sort();
writefln("After sorting:");
bit all0=1;
foreach(int act,uint i;B)
{
writefln("B[",i,"]=",act);
if(act)
all0=0;
}
if(all0)
writefln("\nHuh? The sorting zeroed the array? What I am doing wrong?");
else
writefln("\nHuh? This is not what happened on my machine... I had experimented a
zeroing of my internal array...");
return 0;
}
---

I send the three files (zipped) in attachment as well.

OS was windows xp prof.
Compiler version was 0.142

Marcello Gnani
<marcello_gnani@tiscali.it>
January 05, 2006
In article <dped3d$l7s$1@digitaldaemon.com>, Marcello Gnani says...
>
>I tried to write a minimal templated stack class.
>I wanted my class to implement a templated interface.
>In unittesting phase i found out that the sort method I implemented (simply
>calling the sort method of the internal dynamic array) had the effect of zeroing
>all the elements of the array.
>I strongly suspect this is a bug.
..

Forget (and forgive) it: i figured out by miself what the problem was; it was
not compiler's fault.

Marcello Gnani
<marcello_gnani@tiscali.it>