Tstringlist is a commonly used string list type. Its usage is not described here. However, when the number of data items increases, tstringlist searches (mainly name/key searches and indexof searches) the performance will drop sharply because the internal storage of tstringlist uses the linked list, while the search operation uses the loop Traversal method.
Fortunately, in the inifiles unit, Delphi provides us with the thashedstringlist type, that is, the tstringlist with hash processing. It inherits from the tstringlist and only optimizes the search method. Therefore, we can use it to replace the tstringlist when searching a large number of strings with confidence. All we need to change is to use thashedstringlist after: =. create to replace tstringlist. create, but the speed is increased by an order of magnitude.
There is a thashedstringlist class in Delphi. This class can be used to implement hash table operations. To use this class, you need to reference 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: Create a hash table.
ScHash:=THashedStringlist.Create;
2: add the data structure to the hash table.
var
Index:Integer;
p_Test:PTest;
Index:=ScHash.IndexOf(IntToStr(p_Test.Key));
if Index=-1 then
begin
ScHash.AddObject(IntToStr(p_Test.Key),TObject(Integer(p_Test)));
end;
When adding a hash table, we first check whether the key is in the hash table. If Index =-1, the key is not in the hash table, then we add this structure pointer to the hash table.
Delete the data structure from the hash table.
var
Index:Integer;
t_Object: TObject;
Index:=ScHash.IndexOf(IntToStr(p_Test.Key));
if Index<>-1 then
begin
t_Object:=ScHash.Objects[Index];
ScHash.Delete(Index);
end;
Delete A hash table
When deleting a hash table, use free.
ScHash.Free;