Sign in Join
FPDoc Onlineexperiment

TPointerPointerArray class

In unit DynamicArray

Implements a resizable two-dimensional array using Pointers to column and row values.

Declaration

pascal
TPointerPointerArray = class

Description

Nothing more than the line above yet.

Example: tarrayexample.pas

pascal
program tarrayexample;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  strings,
  DynamicArray;
  
type
  TArrayExampleClass = class
  private
    procedure doDestroyItem(Sender: Tobject; Col,Row: Integer;var Item: Pointer);
  end;

procedure TArrayExampleClass.doDestroyItem(Sender: Tobject; Col,Row: Integer;
  var Item: Pointer);
begin
  StrDispose(Item);
end;


var
  FCols: TPointerPointerArray;
  ex: TArrayExampleClass;

begin
  FCols := TPointerPointerArray.Create;
  ex    := TArrayExampleClass.Create;
  FCols.OnDestroyItem := @ex.doDestroyItem;

  FCols.SetLength(8,8);
  FCols.arr[0,0] := StrNew('string1');
  FCols.arr[4,7] := StrNew('string2');
  FCols.arr[4,3] := StrNew('string3');

  writeln('0,0:' + Pchar(FCols.arr[0,0]));
  writeln('4,7:' + Pchar(FCols.arr[4,7]));

  FCols.MoveColRow(True,4,5);
  writeln('after moving column 4 to 5');
  writeln('5,7:' + Pchar(FCols.arr[5,7]));
  writeln('before exchanging row 7 and 3:');
  writeln('5,3:' + Pchar(FCols.arr[5,3]));
  writeln('5,7:' + Pchar(FCols.arr[5,7]));

  FCols.ExchangeColRow(False,7,3);
  writeln('after exchanging row 7 and 3:');
  writeln('5,3:' + Pchar(FCols.arr[5,3]));
  writeln('5,7:' + Pchar(FCols.arr[5,7]));

  FCols.DeleteColRow(true,5);
  writeln('after deleting column 5:');

  try
    writeln('5,3:' + Pchar(FCols.arr[5,3])); //this raises an exception
  except
    writeln ('An exception has taken place be because 5,3 does not exist.');
  end;

  try
    writeln('5,7:' + Pchar(FCols.arr[5,7])); //this raises an exception
  except
    writeln ('An exception has taken place be because 5,7 does not exist.');
  end;

  FCols.Clear; writeln('after clear:');
  try
    writeln('4,7:' + Pchar(FCols.arr[4,7])); //this raises an exception
  except
    writeln ('An exception has taken place be because 4,7 does not exist.');
  end;

  FCols.Destroy;
  ex.Destroy;
  readln;
end.

Properties

ArrpublicProvides indexed access Pointer values in the elements for the array.

Methods

CreatepublicConstructor for the class instance.
DestroypublicDestructor for the class instance.
SetLengthpublicSets the dimensions for the array to the specified number of columns and rows.
DeleteColRowpublicDeletes a column or row at the specified ordinal position in the array.
MoveColRowpublicMoves a column or row at the specified ordinal position to a new location in the array.
ExchangeColRowpublicExchanges values for array elements in the specified columns or rows in the array.
ClearpublicRemoves pointers and frees resources for all columns and rows in the two-dimensional array.

Events

OnDestroyItempublicPerforms actions needed when the Pointer in an array element is removed from the array.
OnNewItempublicPerforms actions needed when a new Pointer is needed for an array element added to a column or row in the array.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.