logoWinsbydefault


Delphi hashmaps

Posted in Coding by xealot on the February 1st, 2009
0 votes

Hashmaps are nice, everyone likes hashmaps.

I’ve wanted one for Delphi for a while, for no real reason. Been thinking of making one, how hard can it be?

(it turned out to be very easy heh heh)

I ended up with some spare time over this weekend, so I threw together a quick hashmap, written in Delphi 7.

The usage is pretty clear here. The hashmap will store a pointer to  whatever value it wants to hold. Ill provide an example:


var
  hashmap: THashmap;
  ptr: ^TTest;
begin
  hashmap := THashmap.Create();
  new(ptr);
  ptr^ := TTest.Create();
  hashmap.SetValue('somekey', ptr);
  ptr := nil;
  ptr := hashmap.GetValue('somekey');
  FreeAndNil(ptr^);
  FreeAndNil(hashmap)
end;

The unit is available on my SVN here

Its worth noting that, due to the genericness of the hasmap, It only keeps a pointer to its value therefor has no clue
what data it points to and can thusly not be responsible for freeing the object it reference to, this has to be the responsibility
of the programmer.

Also, The unit is still very much work-in-progress, allthough it works, one sould be aware that I will update it as I get time to and
full usage will be available in the units comment at the top.

Leave a Reply

You must be logged in to post a comment.