Delphi中类似Map的HashTable

来源:互联网 发布:大数据平台和云平台 编辑:程序博客网 时间:2024/05/22 04:39

From: http://topic.csdn.net/t/20050518/10/4015559.html 

unit   UnitHasedTable;         interface     uses         Classes;         type         TStringHashedTable   =   class(TPersistent)             FKeyList:   TStrings;             FStrList:   TStrings;         private             function   GetItems(Key:   string):   string;             procedure   SetItems(Key:   string;   const   Value:   string);             function   GetCount:   Integer;         public             constructor   Create;             destructor   Destroy;   override;             property   Items[Key:   string]:   string   read   GetItems   write   SetItems;   default;             property   Count:   Integer   read   GetCount;             procedure   Add(Key,   Str:   string);             procedure   Delete(Key:String);             end;         implementation         {   TStringHashedTable   }         procedure   TStringHashedTable.Add(Key,   Str:   string);     begin         if   FKeyList.IndexOf(Key)   <>   -1   then         begin                 Exit;         end;         FKeyList.Add(Key);         FStrList.Add(Str);     end;         constructor   TStringHashedTable.Create;     begin             FKeyList   :=   TStringList.Create;         FStrList   :=   TStringList.Create;     end;         procedure   TStringHashedTable.Delete(Key:String);     var         KeyIndex:Integer;     begin         KeyIndex   :=   FKeyList.IndexOf(Key);         FKeyList.Delete(KeyIndex);         FStrList.Delete(KeyIndex);     end;         destructor   TStringHashedTable.Destroy;     begin         FStrList.Free;         FKeyList.Free;         inherited   Destroy;     end;         function   TStringHashedTable.GetCount:   Integer;     begin         Result   :=   FKeyList.Count;     end;         function   TStringHashedTable.GetItems(Key:   string):   string;     var         KeyIndex                                   :   Integer;     begin         KeyIndex     :=FKeyList.IndexOf(Key);         Result   :=   FStrList[KeyIndex];     end;         procedure   TStringHashedTable.SetItems(Key:   string;   const   Value:   string);     var         KeyIndex                                   :   Integer;     begin         KeyIndex     :=FKeyList.IndexOf(Key);         FStrList[KeyIndex]   :=   Value;     end;         end.

原创粉丝点击