Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-12

Schema Types

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kevan Hashemi

Dear FPC,

I'm considering movingn from GPC to FPC. One thing I like very much about GPC is its schema types. I can declare a matrix like this:

matrix_type(num_rows,num_columns:integer)=
   array [1..num_rows,1..num_columns] of real;

and then create a new matrix with:

var
   M:^matrix_ptr;
   num_rows,num_columns:integer;
begin
   readln(num_rows,num_columns);
   M:=new(M,num_rows,num_columns);

So far as I can tell, FPC supports one-dimentional dynamic arrays, but not more complicated schema types. Is that true?

Yours, Kevan

Kevan Hashemi, President Open Source Instruments Inc. www.opensourceinstruments.com

Reply by email

Florian Klaempfl
to Kevan Hashemi
Show quoted text 20 lines
Dear FPC, I'm considering movingn from GPC to FPC. One thing I like very much about GPC is its schema types. I can declare a matrix like this: matrix_type(num_rows,num_columns:integer)= array [1..num_rows,1..num_columns] of real; and then create a new matrix with: var M:^matrix_ptr; num_rows,num_columns:integer; begin readln(num_rows,num_columns); M:=new(M,num_rows,num_columns); So far as I can tell, FPC supports one-dimentional dynamic arrays, but not more complicated schema types. Is that true?

FPC supports multi dimensional dyn. arrays with

var
  a : array of array of real;
begin
  setlength(a,num_rows,num_columns);

...

Reply by email

Kevan Hashemi
to Florian Klaempfl

Dear Florian,

Show quoted text 5 lines
var a : array of array of real; begin setlength(a,num_rows,num_columns);

Interesting. What about schema types that are no simple arrays? Like this one:

type
fancy_type(a,b,c:integer)=record
   first:array [0..a,0..b] of integer;
   second:array [0..a,0..b] of real;
   third: array [0..c] of char;
end;
var
   fancy:^fancy_type;
begin
   fancy:=new(fancy,10,20,300);

How do I apply setlength to "first" and "second" after declaring the fancy_type?

Yours, Kevan

Reply by email

Felipe Monteiro de Carvalho
to Kevan Hashemi
fancy:=new(fancy_type); // or something similar, I usually use GetMem not New
SetLength(fancy.first, 10, 20);
SetLength(fancy.second, 10, 20);
SetLength(fancy.third, 300);
Felipe Monteiro de Carvalho

Reply by email

Kevan Hashemi
to Felipe Monteiro de Carvalho

Dear FPC,

Show quoted text 5 lines
fancy:=new(fancy_type); // or something similar, I usually use GetMem not New SetLength(fancy.first, 10, 20); SetLength(fancy.second, 10, 20); SetLength(fancy.third, 300);

Nice. I see how it works. Thank you very much (Felipe and Florian) for your answers. I'll try porting some code to FPC.

Yours, Kevan

Reply by email

Jonas Maebe
to Kevan Hashemi: Nice. I see how it works. Thank you very much (Felipe and Florian) for your answers. I'll…
Kevan Hashemi wrote 10 lines
Dear FPC, > fancy:=new(fancy_type); // or something similar, I usually use > GetMem not New > SetLength(fancy.first, 10, 20); > SetLength(fancy.second, 10, 20); > SetLength(fancy.third, 300); Nice. I see how it works. Thank you very much (Felipe and Florian) for your answers. I'll try porting some code to FPC.

One important different to keep in mind between dynamic arrays and regular arrays (including those defined using schema types): dynamic arrays are reference counted and copies are always shallow copies. This means that in this code:

var
   a, b: array of real;
begin
   setlength(a,10);
   b:=a;
   a[5]:=1.0;
end.

at the end, b[5] will also be 1.0. To make a deep copy, use the following code after assignment:

setlength(b,length(b));

Note that this will only make the "first" level unique. I.e., if you have:

var
   a, b: array of array of real;
begin
   setlength(a,10,10);
   b:=a;
   setlength(b,length(b));
   a[5][5]:=1.0;
   writeln(b[5][5]);
end.

then this will still print 1.0. I don't think there is an easier way to make a full deep copy than using a loop to iteratively call setlength on all sub-arrays as well:

var
   a, b: array of array of real;
   i: longint;
begin
   setlength(a,10,10);
   b:=a;
   setlength(b,length(b));
   for i:=0 to high(b) do
    setlength(b[i],length(b[i]));
   a[5][5]:=1.0;
   writeln(b[5][5]);
end.

Jonas

Reply by email

Florian Klaempfl
to Jonas Maebe
Show quoted text 11 lines
One important different to keep in mind between dynamic arrays and regular arrays (including those defined using schema types): dynamic arrays are reference counted and copies are always shallow copies. This means that in this code: var a, b: array of real; begin setlength(a,10); b:=a;

The proper way to do a copy is
b:=copy(a);
:)

It's only a one level deep copy though.

Reply by email

Andrew Brunner
to Florian Klaempfl

I would also add that it's wise to employ overloaded commands for each data structure that contains dynamic arrays. There may be nested dynamic arrays, and Delphi at least would not respect copying over nested data structures that contained Dynamic Arrays, so I write Copy, Empty, SetSize routines for all my data types that contain dynamic arrays.

I would like to point out that it would be nice if we could have lamda expressions. Does FPC already have this or is it planned?

Florian Klaempfl wrote 23 lines
Jonas Maebe schrieb: > > One important different to keep in mind between dynamic arrays and regular > arrays (including those defined using schema types): dynamic arrays are > reference counted and copies are always shallow copies. This means that in > this code: > > var > a, b: array of real; > begin > setlength(a,10); > b:=a; The proper way to do a copy is b:=copy(a); :) It's only a one level deep copy though. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email