Golden_Rus

Dynamic associative array for Delphi

Dec 13th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.80 KB | None | 0 0
  1. unit assocArray; //written by Anton Pechenev
  2.  
  3. interface
  4. uses Windows, Messages, SysUtils, Classes;
  5.  
  6. type
  7.  asArray=class(TObject)
  8.  private
  9.   last, size: integer;
  10.   key, data: array of Variant;
  11.   function isExist(index: Variant): boolean;//ñóùåñòâóåò ëè ýëåìåíò ñ äàííûì êëþ÷åì
  12.   function findIndex(index: Variant): integer; //ïîèñê â ìàññèâå êëþ÷åé ïî èíäåêñó
  13.  public
  14.   function GetVal(index: Variant): Variant;//ïîëó÷èòü çíà÷åíèå ïî êëþ÷ó
  15.   procedure SetVal(index: Variant; const Value: Variant);//óñòàíîâèòü çíà÷åíèå ýëåìåíòà
  16.   procedure init(_size: integer=1);//èíèöèàëèçàöèÿ ìàññèâà
  17.   procedure Add(index: Variant; const Value: Variant);//äîáàâëåíèå ýëåìåíòà â êîíåö ìàññèâà
  18.   property  count: integer read size;//êîë-âî ýëåìåíòîâ ìàññèâà
  19. end;
  20. implementation
  21.  
  22. function asArray.findIndex;
  23. var i: integer;
  24.     exist: integer;
  25. begin
  26.   exist:=-1;
  27.   for i:=1 to Length(key) do
  28.     if index = key[i] then exist:=i;
  29.   result:=exist;
  30. end;
  31.  
  32. function asArray.isExist;
  33. begin
  34.   if findIndex(index) = -1 then
  35.     result:=false
  36.   else
  37.     result:=true;
  38. end;
  39.  
  40. function asArray.GetVal;
  41. begin
  42.   if isExist(index) then
  43.   result:=data[findIndex(index)]
  44.   else
  45.   result:=0;
  46. end;
  47. procedure asArray.SetVal;
  48. var ind: integer;
  49. begin
  50.   if isExist(index) then
  51.   begin
  52.     ind:=findIndex(index);
  53.     data[ind]:=Value;
  54.   end
  55.   else
  56.   Add(index, Value);
  57. end;
  58. procedure asArray.Add;
  59. begin
  60.    if not isExist(index) then
  61.    begin
  62.       inc(size);
  63.       SetLength(key, size);
  64.       SetLength(data, size);
  65.       key[last]:=index;
  66.       data[last]:=Value;
  67.       inc(last);
  68.    end;
  69. end;
  70. procedure asArray.init;
  71. begin
  72. size:=_size;
  73. last:=1;
  74. SetLength(key, size);
  75. SetLength(data, size);
  76. end;
  77. end.
Add Comment
Please, Sign In to add comment