In the game development often need to save some data structure, and in the use of time need to find out quickly. Hash table is designed to improve the speed of the search. Below I will I use the Delphi development using the method of hash table to write out, I hope to have a certain help!
There is a Thashedstringlist class in Delphi that uses this class to implement hash table operations. Use this class to refer to the Inifiles header file.
For example: The data structure we define is:
以下是引用片段:
RTest=record
Key:Integer;
Name:String[20];
Sex:Boolean;
Age:Integer;
end;
PTest=^RTest;
1:创建Hash表.
ScHash:=THashedStringlist.Create;
2:将数据结构加入Hash表中.
var
Index:Integer;
p_Test:PTest;
Index:=ScHash.IndexOf(IntToStr(p_Test.Key));
ifIndex=-1then
begin
ScHash.AddObject(IntToStr(p_Test.Key),TObject(Integer(p_Test)));
end;
When adding a hash table, we first check to see if the key is in the hash table, and if index=-1 indicates that the key is not in the hash table, we add the struct pointer to the hash table.
3: Delete the data structure from the hash table.
以下是引用片段:
var
Index:Integer;
t_Object:TObject;
Index:=ScHash.IndexOf(IntToStr(p_Test.Key));
ifIndex<>-1then
begin
t_Object:=ScHash.Objects[Index];
ScHash.Delete(Index);
end;
4: Delete hash table
Use free when deleting a hash table, as with a normal tlist deletion.
Schash.free;