Advertisement
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
- TAsArray=class
- 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;//получить значение по ключу (index - ключ элемента)
- procedure SetVal(index: Variant; Value: Variant);//установить значение элемента(index - ключ, value - значение)
- procedure init(_size: integer=1);//инициализация массива(_size - начальное значение размера массив, по умолчанию 1. Не менять)
- procedure Add(index: Variant; Value: Variant);//добавление элемента в конец массива(index - ключ, value - значение)
- property count: integer read size;//кол-во элементов массива
- end;
- implementation
- function TAsArray.findIndex;
- var i: integer;
- exist: integer;
- begin
- exist:=-1;
- for i:=1 to Length(key)-1 do
- if index = key[i] then exist:=i;
- result:=exist;
- end;
- function TAsArray.isExist;
- begin
- if findIndex(index) = -1 then
- result:=false
- else
- result:=true;
- end;
- function TAsArray.GetVal;
- begin
- if isExist(index) then
- result:=data[findIndex(index)]
- else
- result:=0;
- end;
- procedure TAsArray.SetVal;
- var ind: integer;
- begin
- if isExist(index) then
- begin
- ind:=findIndex(index);
- data[ind]:=Value;
- end
- else
- Add(index, Value);
- end;
- procedure TAsArray.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 TAsArray.init(_size: integer=1);
- begin
- Size:=_size;
- last:=1;
- SetLength(key, size);
- SetLength(data, size);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement