Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit assocArray; //written by Anton Pechenev
- interface
- uses Windows, Messages, SysUtils, Classes;
- type
- asArray=class(TObject)
- private
- last, size: integer;
- key, data: array of Variant;
- function isExist(index: Variant): boolean;//ñóùåñòâóåò ëè ýëåìåíò ñ äàííûì êëþ÷åì
- function findIndex(index: Variant): integer; //ïîèñê â ìàññèâå êëþ÷åé ïî èíäåêñó
- public
- function GetVal(index: Variant): Variant;//ïîëó÷èòü çíà÷åíèå ïî êëþ÷ó
- procedure SetVal(index: Variant; const Value: Variant);//óñòàíîâèòü çíà÷åíèå ýëåìåíòà
- procedure init(_size: integer=1);//èíèöèàëèçàöèÿ ìàññèâà
- procedure Add(index: Variant; const Value: Variant);//äîáàâëåíèå ýëåìåíòà â êîíåö ìàññèâà
- property count: integer read size;//êîë-âî ýëåìåíòîâ ìàññèâà
- end;
- implementation
- function asArray.findIndex;
- var i: integer;
- exist: integer;
- begin
- exist:=-1;
- for i:=1 to Length(key) do
- if index = key[i] then exist:=i;
- result:=exist;
- end;
- function asArray.isExist;
- begin
- if findIndex(index) = -1 then
- result:=false
- else
- result:=true;
- end;
- function asArray.GetVal;
- begin
- if isExist(index) then
- result:=data[findIndex(index)]
- else
- result:=0;
- end;
- procedure asArray.SetVal;
- var ind: integer;
- begin
- if isExist(index) then
- begin
- ind:=findIndex(index);
- data[ind]:=Value;
- end
- else
- Add(index, Value);
- end;
- procedure asArray.Add;
- begin
- if not isExist(index) then
- begin
- inc(size);
- SetLength(key, size);
- SetLength(data, size);
- key[last]:=index;
- data[last]:=Value;
- inc(last);
- end;
- end;
- procedure asArray.init;
- begin
- size:=_size;
- last:=1;
- SetLength(key, size);
- SetLength(data, size);
- end;
- end.
Add Comment
Please, Sign In to add comment