How to Use the hash table in Delphi (thashedstringlist)

Source: Internet
Author: User

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;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.