tags:

views:

242

answers:

4

Is there any kind of native map implementation in delphi 6 (map keys to values and objects) - the internet has been of no help so far. I just got a delphi project dumped on my, my background is c++ and java, and the previous dev of the delphi project does all the searching linearly.

Thanks.

+5  A: 

Depending on your exact needs, you might want to use the TStringList object.

Andreas Rejbrand
I am just looking to map a string to another string:map.put("apple","round looking fruit");map.get("apple"); //returns "round looking fruit"
LoudNPossiblyRight
TStringlist can do this: use `MyStringlist.Values['apple'] := 'round looking fruit';` to assign and `S := MyStringlist.Values['apple'];` to read
mjustin
+1 TStringList uses a binary search for look-ups after it is sorted.
cjrh
This is exactly what i was looking for. Thank you.Subsequently i found this very useful link:http://www.delphibasics.co.uk/RTL.asp?Name=TStringList
LoudNPossiblyRight
@cjrh - not if you use Names-Values. When using a stringlist this way, it has to resort to a linear search and doesn't scale at all.
Lieven
@Lieven: agreed.
cjrh
+3  A: 

The Jedi Code Library contains some advanced container classes. Interfaces are declared in JclContainerIntf.pas, for example:

  IJclMap = interface(IJclContainer)
    ['{A7D0A882-6952-496D-A258-23D47DDCCBC4}']
    procedure Clear;
    function ContainsKey(Key: TObject): Boolean;
    function ContainsValue(Value: TObject): Boolean;
    function Extract(Key: TObject): TObject;
    function GetValue(Key: TObject): TObject;
    function IsEmpty: Boolean;
    function KeyOfValue(Value: TObject): TObject;
    function KeySet: IJclSet;
    function MapEquals(const AMap: IJclMap): Boolean;
    procedure PutAll(const AMap: IJclMap);
    procedure PutValue(Key, Value: TObject);
    function Remove(Key: TObject): TObject;
    function Size: Integer;
    function Values: IJclCollection;
    property Items[Key: TObject]: TObject read GetValue write PutValue;
      {$IFNDEF BUGGY_DEFAULT_INDEXED_PROP} default; {$ENDIF ~BUGGY_DEFAULT_INDEXED_PROP}
  end;
mjustin
do you know if there is a native library that i can use? I am trying to avoid adding another library to the project.
LoudNPossiblyRight
Compared with Java, the core run time library is quite small in Delphi. There are some higher level container classes for objects (like TObjectList and TInterfaceList) but not much more.
mjustin
+3  A: 

I have tested TStringList and some THashTable implementations and the differences between the two implementations are minimal, and in most cases TStringList (with dicotomical sort implementation) are more efficient than THashTable.
For a small number of values TStringList is faster than Hash, and for a large number of values you must find a complex hash-function for minimize the collision, and this complexity decreases the efficiency of the HashList.

You must use the Object pointer of the StringList to store all the information that you need (the second string).

Regards.

Neftalí
Thanks Mason. ;-)
Neftalí
+1  A: 

Hi I´ve used a library called Hashes.pas from Ciaran McCreesh but since his website is no loger available you can see the single PAS file from the following URL:

Link to Cian McCreesh - Hashes.pas

You can also find it with google with the text: "Cian McCreesh Hashes"

With this library you can do the following:

aString := TStringHash.Create; aString['color'] := 'blue'; ShowMessage(aString.Items['color']); // blue

Or objects:

aObj := TObjectHash.Create; aObj['color'] := TBlueClass.Create; bcBlue:=(aObj.Items['color'] as TBlueClass); ShowMessage(bcBlue.Name); // Blue (supposing the TBLusClass as a Name property...

Hope it serves you as well as for me.

PS I think the Ares AudioGalaxy project also uses it.

Luis