Recent

Author Topic: removing elements from dynamic array  (Read 13312 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
removing elements from dynamic array
« on: December 01, 2010, 03:22:01 pm »
Hi, I've got a problem I don't know how to solve, I have a 3 dimensional array, (say, cols,rows, depths?)

For each col/row the number of depths varies. I'm trying to delete from the array a whole row using a function deletex which deletes an element from a one dimensional array.

So, say I want to delete the third row from the array, first I delete all the depth elements from the second row of all the cols, this is successful.

then I "try" to delete the third element from all the cols , this is where I get a SIGSEGV fault. Any ideas how to do this, or alternate strategies?   I did think of just using a one dimensional array, but, as depth varies, indexing would be a nightmare.

Best regards

Albert

Code: [Select]
type
        TdoubleArray = array of double;
type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }

    procedure DeleteX(var A: tdoublearray; const Index: Cardinal);
  end;

var
  Form1: TForm1;
  x:array of array of array of double;
  //x:tintarray;

implementation

{$R *.lfm}

{ TForm1 }


procedure TForm1.Button1Click(Sender: TObject);
var
  i, k :byte;
begin

    // delete elements in first position of third dimension of the  first, second and third cols; third row
    deletex(tdoublearray(x[0, 2]), 0);
    deletex(tdoublearray(x[1, 2]), 0);
    deletex(tdoublearray(x[2, 2]), 0);

    // delete elements in second position of third dimension of the  first, second and third cols; third row
    deletex(tdoublearray(x[0, 2]), 1);
    deletex(tdoublearray(x[1, 2]), 1);
    deletex(tdoublearray(x[2, 2]), 1);

    // now delete third row from first, second and third cols,
    deletex(tdoublearray(x[0]), 2);
    deletex(tdoublearray(x[1]), 2);  // gives SIGSEGV error here
    deletex(tdoublearray(x[2]), 2);

end;

procedure tform1.DeleteX(var A: Tdoublearray; const Index: Cardinal);

var
  ALength: Cardinal;
  TailElements: Cardinal;
begin
  ALength := Length(A);
  Assert(ALength > 0);
  Assert(Index < ALength);
  Finalize(A[Index]);
  TailElements := ALength - Index;
  if TailElements > 0 then
     Move(A[Index + 1], A[Index],  SizeOf(double)* TailElements);  //
  Initialize(A[ALength - 1]);
  SetLength(A, ALength - 1);
end;

procedure TForm1.FormShow(Sender: TObject);
var
  i,k:byte;
begin
    setlength(x, 3, 10, 2); // three dimensional array, three cols, 10 rows, 2 depths?
    for k:= 0 to 2 do       // fill with something
      for i := 0 to 9 do begin
        x[k,i,0]:= i*(k+1);
        x[k,i,1]:= i*2*(k+1);
      end;
end;

end.                                                         

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: removing elements from dynamic array
« Reply #1 on: December 02, 2010, 12:37:51 am »
Code: [Select]
for i := 0 to number.of.columns-1 do begin
    SetLength(x[i, row.to.delete], 0); //start with emptying the arrays you want delete
    move the remaining rows up // for-loop
    SetLength(x[i], Length(x[i])-1);  // reduce number of items in column i
end;

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: removing elements from dynamic array
« Reply #2 on: December 02, 2010, 08:33:06 am »
Thanks Ivan17, I see I was approaching this the wrong way, I just read that only the last dimension of dynamic array contains the data, the other dimensions just contain pointers to the last dimension.

Regards

Albert

 

TinyPortal © 2005-2018