Board index » delphi » help with dynamic arrays wanted (aka variant arrays??)

help with dynamic arrays wanted (aka variant arrays??)

Hi.
I am writing a program in Delphi and I need to use arrays.  My problem is that I will be running my program on
many different data sets, each with different sizes/numbers of particular variables.  What I am having to do
at the moment is declare my arrays at the beginning of my program such that it can handle any particular data
set that it encounters.

for example, I have to declare:

myArray = array[1..largestPossibleA, 1..largestPossibleB, 1..largestPossibleC] of integer;

This is making my arrays far to big however.  A lot of the time it is wasting space also, for example the
current data set might need a lot of the 1..largestPossibleA's but not many of the 1..largestPossibleB's and
C's (I hope I that last sentence makes sense :)   )

What I want is to be able to dynamically change/increase/decrease the size of the array as my program is
running (similar to what can be done in C++ I am told).  So for example I would read in my data and see that
the biggest A=10 and the biggest B = 15 and the biggest C = 10 for this data set, so I dynamically create an
array which is array[1..10, 1..15, 1..10] of integer/real/whatever.

I didnt think it was possible to do this in Pascal but someone told me that Delphi has something called
dynamic arrays.  I looked it up in the help but it was not very helpful at all.  Can someone help me with this
please???? (if you can help me it would be great if you were quite specific in your instructions as I am new
to Delphi)

Another thought I had to solve this problem was to:

a)read in the data
b)find out the ranges I need (ie the A and B and C etc..)
c)run a little program that read in my program and altered the constants A B and C (etc..) to be the size I
wanted and then
d)recompiled my program and ran it

Has someone written a unit that does this type of thing to help with array size problems??

all replies greatly appreciated

regards
andrew coyle

PS: please reply to mail (as well as news if you want) as my access to newsgroups is irregular :)

 

Re:help with dynamic arrays wanted (aka variant arrays??)


Quote
Sheryl Coyle wrote in message <35230D13.4...@iprolink.co.nz>...
>I am writing a program in Delphi and I need to use arrays.  My problem is

that I will be running my program on
Quote
>many different data sets, each with different sizes/numbers of particular

variables.  What I am having to do
Quote
>at the moment is declare my arrays at the beginning of my program such that

it can handle any particular data
Quote
>set that it encounters.

>for example, I have to declare:

>myArray = array[1..largestPossibleA, 1..largestPossibleB,

1..largestPossibleC] of integer;

Quote
>This is making my arrays far to big however.  A lot of the time it is

wasting space also, for example the
Quote
>current data set might need a lot of the 1..largestPossibleA's but not many

of the 1..largestPossibleB's and

Quote
>C's (I hope I that last sentence makes sense :)   )

If you're new at this, I'd recommend mastering a 2D case before starting on
the 3D case.  In C for a 2D array, you can setup an array of pointers, with
each of these pointers pointing to a dynamically allocated array.  (For 3D,
this first array of pointers would point to a 2D matrix of values).

Quote
>What I want is to be able to dynamically change/increase/decrease the size

of the array as my program is
Quote
>running (similar to what can be done in C++ I am told).  So for example I

would read in my data and see that
Quote
>the biggest A=10 and the biggest B = 15 and the biggest C = 10 for this

data set, so I dynamically create an

Quote
>array which is array[1..10, 1..15, 1..10] of integer/real/whatever.

Unless you're using Delphi 1, you have few memory limitations.

The memory requirements for a 10 x 15 x 10 array of number is not very
great.

Quote

>I didnt think it was possible to do this in Pascal but someone told me that

Delphi has something called
Quote
>dynamic arrays.  I looked it up in the help but it was not very helpful at

all.  Can someone help me with this
Quote
>please???? (if you can help me it would be great if you were quite specific

in your instructions as I am new

Quote
>to Delphi)

Study this example:

==============================================

// Note:  the following looks indented properly with a fixed-width font

// Delphi 3 Example of 2-Dimensional Dynamic Array
CONST
    ArrayMaxCount = 65536;

TYPE
    pDoubleArray = ^TDoubleArray;
    TDoubleArray = ARRAY[0..ArrayMaxCount-1] OF DOUBLE;
    pMatrix = ^TMatrix;
    TMatrix = ARRAY[0..ArrayMaxCount-1] OF pDoubleArray;

procedure TForm1.Button1Click(Sender: TObject);
    CONST
        iCountMax = 500; // columns
        jCountMax = 1000; // rows
    VAR
        i : 0..iCountMax-1;
        j : 0..jCountMax-1;
        k : INTEGER;

        // Let's make Matrix 1000 rows of 500 columns =
        // 500,000 elements = 4,000,000 bytes
        Matrix: pMatrix;

        Sum : DOUBLE;
begin
    // Allocate the pointers to each row
    GetMem(Matrix, jCountMax*SizeOf(pMatrix));
    TRY

        // Allocate each row of DOUBLEs
        FOR j := 0 TO jCountMax-1 DO
            GetMem(Matrix[j], iCountMax*SizeOf(DOUBLE));

        TRY
            // Assign value to each Matrix element
            k := 1;
            FOR j := 0 TO jCountMax - 1 DO
            BEGIN
                FOR i := 0 TO iCountMax -1 DO
                BEGIN
                    Matrix[j]^[i] := k;
                    INC(k);
                END;
            END;

            // Add up all values in Matrix. The sum of 1..N = N*(N+1)/2, so
            // the sum should be (iCountMax*jCountMax)*(iCountMax*jCountMax
+ 1) / 2,
            // or in this case SUM(1..500,000) = 125,000,250,000.
            Sum := 0;
            FOR j := 0 TO jCountMax - 1 DO
                FOR i := 0 TO iCountMax -1 DO
                    Sum := Sum + Matrix[j]^[i];

            Button1.Caption := FloatToStr(Sum);

        FINALLY
            // Free each row of DOUBLEs
            FOR j := jCountMax-1 DOWNTO 0 DO
            FreeMem(Matrix[j]);

        END
    FINALLY
        // Free array of pointers
        FreeMem(Matrix)
    END
end;

Normally, I'd suggest that you also look at Items D-8 and D-10 in my Delphi
Math
FAQ and Links page.  (My web server is down right now, however.)

efg
_________________________________________
efg's Computer Lab:  http://infomaster.net/external/efg

Earl F. Glynn                 E-Mail:  EarlGl...@att.net
MedTech Research Corporation, Lenexa, KS  USA

Re:help with dynamic arrays wanted (aka variant arrays??)


On Thu, 02 Apr 1998 15:59:15 +1200, Sheryl Coyle

Quote
<swco...@iprolink.co.nz> wrote:
>Hi.
-snip-
>I didnt think it was possible to do this in Pascal but someone told me that Delphi has something called
>dynamic arrays.  I looked it up in the help but it was not very helpful at all.  Can someone help me with this
>please???? (if you can help me it would be great if you were quite specific in your instructions as I am new
>to Delphi)

type
    // dynamically sized array of integers
   TarInteger = array [1..$FFFF] of integer;
   // pointer to type TarInteger
   ptrTarInteger = ^TarInteger;
   TMyClass = class(TComponent)
   private
       pIntArray: ptrTarInteger;
      ...
   end;

regards
A.G.

cc'd: yes

Re:help with dynamic arrays wanted (aka variant arrays??)


The answer is typecasting and dynamic memory allocating :

You get a pointer with the good size :

GetMem(MyP, MySize);
The Size should be calculated correctly, for example :
MySize:=20*5*20*SizeOf(Double);

Then you create the good type :
TMyArray = Array[0..0] of Double;

And if I need to get an element :
MyElem:=TMyArray(MyP^)[10,3,1];

Well, this is something like that. As my Delphi is out, I can't try this
now. It's like a jigsaw ;-)

Good luke, and ask me if more explanations are needed.

Regards,
--
Frdric GUILLIEN

Here is my Web site. Yet all in french (sorry, time missing...)
http://wwwperso.hol.fr/~fguillei

Re:help with dynamic arrays wanted (aka variant arrays??)


You might check out our Matrix Math Toolkit v 3 at our
website if you plan to do 'matrix' kinds of calcs...

--
Grace + Peace | Peter N Roth | Engineering Objects Int'l
         Now shipping version 4 of ClassBuilder++
Visit our website at http://www.inconresearch.com/eoi
"Random numbers are too important to be left to chance." - anon

Re:help with dynamic arrays wanted (aka variant arrays??)


Quote
Sheryl Coyle wrote in message <35230D13.4...@iprolink.co.nz>...
>What I want is to be able to dynamically change/increase/decrease the size

of the array as my program is
Quote
>running (similar to what can be done in C++ I am told).  So for example I

would read in my data and see that
Quote
>the biggest A=10 and the biggest B = 15 and the biggest C = 10 for this

data set, so I dynamically create an

Quote
>array which is array[1..10, 1..15, 1..10] of integer/real/whatever.

I would really recommend developing a class for this purpose:

type
  TMultiMatrix = class (TObject)
    constructor Create (dimensions: Integer);
    destructor Destroy; override;

    function GetX (index: array of Integer): Extended;
    procedure SetX (index: array of Integer; value: Extended);
  end;

You would use this class like this:

var
  m: TMultiMatrix;
  i, j, k: Integer;

begin
  m := TMultiMatrix.Create (3); // you need a 3-d matrix
  for i := 0 to 50 do // I assume zero-based indices
    for j := 0 to 12 do
      for k := 0 to 17 do
        m.SetX ([i, j, k], Extended (i + j + k) / 2);
//...
end;

The class is the tricky part :-) if you can't succeed in writing it (using
others' help) I will try to do it...

Mark

Re:help with dynamic arrays wanted (aka variant arrays??)


Quote
"Marcel Popescu" <mdpope...@geocities.com> wrote...
>>What I want is to be able to dynamically change/increase/decrease the size
>of the array as my program is
>>running (similar to what can be done in C++ I am told).  So for example I
>would read in my data and see that
>>the biggest A=10 and the biggest B = 15 and the biggest C = 10 for this
>data set, so I dynamically create an
>>array which is array[1..10, 1..15, 1..10] of integer/real/whatever.

The best solution is a component which creates, manages and destroys objects of
the desired type using a TList object to hold onto the object.  An array
property can than be used to assign and retrieve the data elements (make it the
default property and the syntax gets very clean).

We have a Dynamic Array in our product but it simulates an XBASE array (very
dynamic access, mixed type elements and coordinates of either numeric or string
values) but its not as fast as the solution above for a strickly numeric array
(due to all the other stuff its doing) .  I've seen alot of interest in this
sort of thing though so who knows.

I know that a number of Matrix components are out there already if thats what
you really need.  If you do the component and have questions, I'll try to stay
tuned and try to help out.

Nate

 Foolproof systems always underestimate
 the ingenuity of a fool.

 Clipper Functions 3.0 for Delphi
 http://members.aol.com/clipfunc

Other Threads