Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
33
Добавлен:
28.06.2014
Размер:
4.03 Кб
Скачать
unit Unit5;

interface
uses SysUtils;
type
ExceptArrClass = class(Exception)
end;

PDynElem = ^TDynElem;
TDynElem = record
val: double;
next: PDynElem;
pred: PDynElem;
ind: integer;
end;

TDynArr = class
private
start: PDynElem;
this: PDynElem;
last, first:PDynElem;
maxLen: integer;
function GetArr(index: integer): double;
procedure SetArr(index: integer; const Value: Double);
function GetPred: PDynElem;
function GetNext: PDynElem;
public
// procedure Delete(index: integer);
function Length:integer;
function High:integer;
function low:integer;
property Arr[index: integer]: double read GetArr write SetArr; Default;
constructor Create;
end;

implementation
constructor TDynArr.Create;
begin
start:=nil;
last:=start;
first:=start;
maxLen:=1500;
end;

function TDynArr.GetArr(index: integer): double;
procedure rec(Base: PDynElem; ind: integer);
begin
if ind = 0 then begin
result:=base.val;
this :=base;
end else if ind > 0 then
rec(base.next, ind -1)
else if ind < 0 then
rec(base.pred, ind +1)
end;
begin
if (last = nil) or (first = nil )then
raise ExceptArrClass.Create('Неверный индекс ' + inttostr(index))
else if index > last.ind then
raise ExceptArrClass.Create('Неверный индекс ' + inttostr(index))
else if index < first.ind then
raise ExceptArrClass.Create('Неверный индекс ' + inttostr(index))
else if index = last.ind then
Result:=last.val
else if index = first.ind then
Result:=first.val
else
if abs(index - this.ind) < abs(index - start.ind) then
rec(this, index-this.ind)
else
rec(start, index-start.ind);

end;

function TDynArr.GetNext: PDynElem;
begin
result:= start;
while result.next <> nil do
begin
Result:=Result.next;
end;
end;

function TDynArr.GetPred: PDynElem;
begin
result := start;
while result.pred <> nil do
begin
Result:=Result.pred;
end;
end;

function TDynArr.High: integer;
begin
result:=last.ind;

end;

function TDynArr.Length: integer;
begin
result:=last.ind-first.ind;
end;

function TDynArr.low: integer;
begin
result:=first.ind;
end;

procedure TDynArr.SetArr(index: integer; const Value: double);
procedure rec(Base: PDynElem; ind: integer);
begin
if ind = 0 then begin
base.val:=value;
this :=base;
end else if ind > 0 then
rec(base.next, ind-1)
else
rec(base.pred, ind+1)
end;
begin
if start = nil then begin
new(start);
start.val:=Value;
start.next:=nil;
start.pred:=nil;
start.ind:=index;
first:=start;
last:=start;
this :=start;
end else if index > last.ind+1 then
raise ExceptArrClass.Create('Неверный индекс ' + inttostr(index))
else if index < first.ind-1 then
raise ExceptArrClass.Create('Неверный индекс ' + inttostr(index))
else if index = last.ind+1 then begin
new(last.next);
last.next.pred:=last;
last.next.next:=nil;
last.next.val:=Value;
last:=last.next;
last.ind:=index;
this :=last;
if last.ind - first.ind >= maxLen then begin
first:=first.next;
if start = first.pred then start:=first;
dispose(first.pred);
first.pred:=nil;

end
end else if index = first.ind-1 then begin
new(first.pred);
first.pred.next:=first;
first.pred.pred:=nil;
first.pred.val:=Value;
first:=first.pred;
first.ind:=index;
this :=first;
if last.ind - first.ind >= maxLen then begin
last:=last.pred;
if start = last.next then start:=last;
dispose(last.next);
last.next:=nil;
end
end else
if abs(this.ind - index) < abs(start.ind-index) then
rec(this, index-this.ind)
else
rec(start, index-start.ind);
end;

end.
Соседние файлы в папке Runge