Advertisement
Guill

String Hash

Nov 4th, 2014
176
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program Test;
  2. uses Vcl.Dialogs, Classes;
  3.     type
  4.         TMyArray = array[0..4] of String;
  5.         TStringHash = class
  6.         private
  7.             FKeys : TStringList;
  8.             FValues : Array of TMyArray;
  9.             function GetContent(Key: String; Index : Integer): String;
  10.             procedure SetContent(Key : String; Index : Integer; Value: String);
  11.         public
  12.             property Contents[Key : String; Index : Integer] : String read GetContent write SetContent; Default;
  13.             constructor Create;
  14.             function Count : Integer;
  15.         end;
  16.  
  17. constructor TStringHash.Create;
  18. begin
  19.     inherited;
  20.     FKeys := TStringList.Create;
  21.     FKeys.Add('');
  22.     SetLength(FValues, 1);
  23.     FValues[0][0] := '';
  24. end;
  25.  
  26. function TStringHash.Count: Integer;
  27. begin
  28.     Result := FKeys.Count;
  29. end;
  30.  
  31. function TStringHash.GetContent(Key: String; Index : Integer) : String;
  32. var
  33.     I : Integer;
  34. begin
  35.     Result := '';
  36.     I := FKeys.IndexOf(Key) ;
  37.     if I > -1 then
  38.         Result := FValues[I][Index];
  39. end;
  40.  
  41. procedure TStringHash.SetContent(Key : String; Index : Integer; Value: String);
  42. var
  43.     I : Integer;
  44. begin
  45.     I := FKeys.IndexOf(Key) ;
  46.     if I > -1 then
  47.         FValues[I][Index] := Value
  48.     else
  49.     begin
  50.         SetLength(FValues, Length(FValues) + 1);
  51.         FValues[Length(FValues) - 1][Index] := Value;
  52.         FKeys.Add(Key);
  53.     end;
  54. end;
  55.  
  56. var
  57.     MinhaHash : TStringHash;
  58. begin
  59.     MinhaHash := TStringHash.Create;
  60.     MinhaHash['Teste', 0] := 'Olá Mundo!';
  61.     ShowMessage(MinhaHash['Teste', 0]);
  62. end.
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement