Forum Navigation
HashMap and LinkMap Bug
Moderators:
kris
Posted: 07/15/07 22:16:53 Modified: 07/15/07 22:34:57I have found a bug in tango.util.collection.HashMap? and tango.util.collection.LinkMap?,here is the test code:
program 1 :
import tango.util.collection.HashMap; import tango.util.collection.LinkMap; import tango.io.Stdout; import tango.math.Random; import tango.util.time.Clock; import tango.io.digest.Md5; import Integer = tango.text.convert.Integer; int main() { Stdout("-- start ---"); HashMap!(char[],Person) hmap = new HashMap!(char[],Person); LinkMap!(char[],Person) lmap = new LinkMap!(char[],Person); Person p1 = new Person(Util.generateUUID()); Person p2 = new Person(Util.generateUUID()); hmap.add(p1.id,p1); lmap.add(p2.id,p2); Stdout("\n-- end ---"); return 0; } class Person { char[] id; this(char[] id) { this.id = id; } } class Util { static Md5 md5; static Random rand; static this() { md5 = new Md5(); rand = new Random(); } static char[] generateUUID() { char[] seed = Integer.toUtf8(Clock.now); for(int i=0; i<5; i++) { seed ~= Integer.toUtf8(rand.next()); } md5.update(cast(ubyte[]) seed); return md5.hexDigest; } }compile the program 1,it will work well,output is --- start --- --- end ---
program 2 :
import tango.util.collection.HashMap; import tango.util.collection.LinkMap; import tango.io.Stdout; import tango.math.Random; import tango.util.time.Clock; import tango.io.digest.Md5; import Integer = tango.text.convert.Integer; int main() { Stdout("-- start ---"); HashMap!(char[],Person) hmap = new HashMap!(char[],Person); LinkMap!(char[],Person) lmap = new LinkMap!(char[],Person); Person p1 = new Person(Util.generateUUID()); Person p2 = new Person(Util.generateUUID()); hmap.add(p1.id,p1); lmap.add(p2.id,p2); for(int i=0;i<30;i++) { Person p = new Person(Util.generateUUID()); Stdout.formatln("--- add a new person id {0} , i is {1} ---",p.id,i); hmap.add(p.id,p); } Stdout("\n-- end ---"); return 0; } class Person { char[] id; this(char[] id) { this.id = id; } } class Util { static Md5 md5; static Random rand; static this() { md5 = new Md5(); rand = new Random(); } static char[] generateUUID() { char[] seed = Integer.toUtf8(Clock.now); for(int i=0; i<5; i++) { seed ~= Integer.toUtf8(rand.next()); } md5.update(cast(ubyte[]) seed); return md5.hexDigest; } }compile program 2 and run it, it will paused at " hmap.add(p.id,p); " in for loop, you can find the point in Stdout contents,and the same time the cpu's rate is very high, almost 100%, the difference with program 1 is that add more elements into map instance, I test that in Windows and linux,all occured this problem, now I don't understand the implemetation of HashMap? and linkMap,so hope the authors can deal this program,thank you!












