The use method of hash table in Delphi

Source: Internet
Author: User
Tags hash

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;

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.