www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - A zeroing sort - sort_zeroing.zip

reply Marcello Gnani <Marcello_member pathlink.com> writes:
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>
begin 0644 sort_zeroing.zip

M+.P_I+QKS`=8BMA:^`.Y.$H MPF;O4+$?S<B'$SQAN$Q:[UO!3Y5L9 ER_.P
MQ(XC$U,(%P T%I_%H(^8P+2C/_T5Z'0N6"%V>S7,3&\F%\)5:X-:1A^U;4O)
M:<!O<[OH>U6;YG'F/DPC7U!+`P04``(`"`"OD2,T6JX[U=`!```J!```"P``
M`$1U;6UY36%I;BYDO5/!;IPP$#TOTO[#E
M;17&R)ANMU'^O6.CL"12M3E%0L(:WKSWY WNC?K-K% 'C^M I;I>&PM<HV4*
M%1[2P3+^*S]_&FQ-M5IIJCVM W5P??UU[+H3=-2P#A1
M$_O1*"N:*+QM6\V951I!-\"0,(:=W)G:AS1-]QC&3I/$K>*NZHG, [[3_:G8
M;3XFFYOD)DOH_:GR[LZ0>[)9S."T'OM\UFXQ"DO1:"- (! -^'E2HE(T.O.J
MR'+U929*6X$'*W-U=45#K!8T,V07)BH)JR),SB55>=8SC5-[:>.VL<*\AXM%

MT..WI2X8EZX;&+?)-'M>OAJR7`S'K>\L?:)1?#G3GXJXVS8K-I<4'R^JKE03
MN;,[KCQIYLI/[ >D+U1XY7R/WT:YA0<Y3PY_A=&4K(O9I[N%'Y)9^`ZL UH[
MQ-%H/&PG]Z(=Q'\HU0#TH+9P=`22];U`8J9;Y:\CEPH%[8ZH):M!_.F%49U`
MOU=OPXG1[ E-*0B#K)T7/JD;84>#D$UW_1]02P,$%``"`` `SI$C-)R<A^6Z
M`0``NP0```<```!3=&%C:RYDK52Q;MLP$)TE0/_`;A022,[2H;:'H.W H6B`
M%%T"#S1UD0
MD9`OZRS-TK+<G1ICR65I8_&7(,C2/UF:8(A.,0?AP$.ZB'KL.S"IA//(\&$A
MQ+\J.(&F'^<&<O:)?8X-P'Z892))6?X4%L5! 6^2--802(+*G_MLTJ(F=F^M
M..^TM`&\O?NX'E,/QB&AT=M5B$WZ/^W93A-8+=0C&2OJ7GG2!<H':QJPA)&S
M/2B4%\)_B&7.6\'SOB*4)8O>19\/$A(+U%K-Z( N!+J1]AO0T50NG#V/(]M*
M#W?E%QC/KP3U??A\KN'F)G)&'Z1PQ'MK<KZP[8(J%.B:CGE4.R_:;+9WZVOS
M;37\9E,5<]S^*NAI510+UOTHXUIRX==W_TCBJBEE^6PL"'D4!U1(9\9-<]\T


MN+?X7 -WX5?S%U!+`0(4`!0`` `(`$>1(S0Y95$:<P```(T````&````````
M``$`(`"` 0````!"87-E+F102P$"%``4``(`"`"OD2,T6JX[U=`!```J!```

MG)R'Y;H!``"[!```!P`````````!`"`` (&0` ``4W1A8VLN9%!+!08`````

`
end
Jan 03 2006
parent Marcello Gnani <Marcello_member pathlink.com> writes:
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>
Jan 04 2006