Vector.pas


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

PascalでのVectorクラス


unit Vector;

interface

uses
 Messages, SysUtils, Variants, Classes, Dialogs, DataType;


type
 TIntVector = class
 private
   memMax, num: integer;
   procedure reAlloc;
 public
   v: array of Integer;
   max: integer;
   constructor Create;
   function get(ix: integer): Integer;
   procedure push(a: Integer);
   destructor destroy; override;
 end;

type
 TSingleVector = class
 private
   memMax, num: integer;
   procedure reAlloc;
 public
   v: array of Single;
   max: integer;
   constructor Create;
   function get(ix: integer): Single;
   procedure push(a: Single);
   destructor destroy; override;
 end;

type
 TVertexVector = class
 private
   memMax, num: Integer;
   procedure reAlloc;
 public
   v: array of Vertex;
   max: Integer;
   constructor Create;
   function get(ix: Integer): Vertex;
   procedure push(var a: Vertex);
   destructor destroy; override;
 end;

implementation

const
 mMax1: integer = 100;
 mMax2: integer = 500;
 mMax3: integer = 2500;
 mMax4: integer = 12500;
 mMax5: integer = 62500;
 mMax6: integer = 312500;
 mMax7: integer = 1562500;



procedure TIntVector.reAlloc;
begin
 if (memMax+1) > mMax7 then
 begin
   ShowMessage('Caution! Memory Over: TIntVector');
 end;
 memMax := memMax * 5;
 SetLength(v, memMax);
end;

constructor TIntVector.Create;
begin
 SetLength(v, mMax1);
 memMax := mMax1;
 num := 0;
end;

function TIntVector.get(ix: integer): Integer;
begin
 result := 0;
 if ix <= max then result := v[ix];
end;


procedure TIntVector.push(a: Integer);
begin
 if (num+1) > (memMax-20) then reAlloc;
 v[num] := a;
 Inc(num);
 max := num -1;
end;

destructor TIntVector.destroy;
begin
 SetLength(v, 0);
 v := nil;
end;



procedure TSingleVector.reAlloc;
begin
 if (memMax+1) > mMax7 then
 begin
   ShowMessage('Caution! Memory Over: TSingleVector');
 end;
 memMax := memMax * 5;
 SetLength(v, memMax);
end;

constructor TSingleVector.Create;
begin
 SetLength(v, mMax1);
 memMax := mMax1;
 num := 0;
end;

function TSingleVector.get(ix: integer): Single;
begin
 result := 0;
 if ix <= max then result := v[ix];
end;


procedure TSingleVector.push(a: Single);
begin
 if (num+1) > (memMax-20) then reAlloc;
 v[num] := a;
 Inc(num);
 max := num -1;
end;

destructor TSingleVector.destroy;
begin
 SetLength(v, 0);
 v := nil;
end;



procedure TVertexVector.reAlloc;
begin
 if (memMax+1) > mMax7 then
 begin
   ShowMessage('Caution! Memory Over: TVertexVector');
 end;
 memMax := memMax * 5;
 SetLength(v, memMax);
end;

constructor TVertexVector.Create;
begin
 SetLength(v, mMax1);
 memMax := mMax1;
 num := 0;
end;

function TVertexVector.get(ix: integer): Vertex;
begin
 if ix <= max then result := v[ix] else ShowMessage('out of index: TVertexVector.get');
end;


procedure TVertexVector.push(var a: Vertex);
begin
 if (num+1) > (memMax-20) then reAlloc;
 v[num] := a;
 Inc(num);
 max := num -1;
end;

destructor TVertexVector.destroy;
begin
 SetLength(v, 0);
 v := nil;
end;



end.
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。