From: "Joselito G. Real" Subject: Tip: Initializing n-dimensional array-type constants or static arrays Date: 08 Dec 1998 00:00:00 GMT Message-ID: <01be22dd$ed6ccd40$9f24a2d1@z-reajos-pc> Content-Transfer-Encoding: 7bit Organization: n-Genius Solutions Content-Type: text/plain; charset=ISO-8859-1 Mime-Version: 1.0 Newsgroups: borland.public.delphi.objectpascal Initializing n-dimensional array-type constants or static arrays There are times when you need to store tabular values within your code so that you don't need to create an extra file to read externally from your program or simply you want to hide those tabular values from other hackers, or sometimes to dramatically boost your computational speed. For example, you may be using precomputed values from time-consuming complex graphical algorithms, or simulation programs, and storing these precomputed values into integerized tables so that you can simply interpolate from these values instead of running those huge time consuming programs every time, could increase your computational speeds of several orders of magnitude. Some tabular values can be used for storing the basic definitions for complex three dimensional objects, and still others would just want to store first and second derivatives for smoothing curves, and still others, simply tabular values for reference such as statistical tables. If you store static or constant values into local or global arrays, it would be slower and would eat up more memories compared to using array-type constants. Array-type constants are initialized during the compile process and not during the runtime so it saves you some clock cycles every time your routine is called . For example if we declare: var xi:array[1..5] of integer; begin xi[1]:=500; xi[2]:=250; xi[3]:=600; xi[4]:=900; xi[5]:=800; //other codes end; At two bytes per integer, not counting the other overhead, the above method will add at least 10 bytes for storing the index values and 10 bytes for the values themselves for a total of at least 20 bytes that will be added to the EXE file produced. Now if we know that these are necessary initial values for our program (and not necessarily constants, of course constants are okay), it would be better and simpler to do the following declaration: const xi:array[1..5] of integer = (500,250,600,900,800); The array type constant method will consume only half the memory and will result to adding only at least 10 bytes to the EXE file. And for strings, array constants of one dimension (example with 4 elements) a sample declaration would be: Const MyStrArr: array[1..4] of string = ('First string', '2nd string', '3rd string', '4th string'); Now how do we initialize n-dimensional array constants? From Delphi help file, it says that multidimensional-array constants are defined by enclosing the constants of each dimension in separate sets of parentheses, separated by commas. The innermost constants correspond to the rightmost dimensions. For two-dimensional arrays as in the case of tables, what I do is to type the tables in Excel, then insert columns for the commas and parenthesis in Excel, and then copy and paste the Excel Table right into my code. Very quick and easy. For example a table of integer of constants, 4 rows by 5 columns can be initialized as: const ix: array[1..4, 1..5] of integer = ((11, 12, 13, 14, 15) , //row one (21, 22, 23, 24, 25), //row two (31, 32, 33, 34, 35), //row three (41, 42, 43, 44, 45)); //row four thus the element ix[2,4] points to the value 24. I prefer the above method of writing in my code, it looks like an ordinary table, and I can import it directly from Excel after inserting the commas and parenthesis for large tables. Now the above method will save you 66.67% memory compared to using local or global arrays as in the bad example shown below: var ix:array[1..4,1..5] of integer; begin ix[1,1]:=11; ix[1,2]:=12; ix[1,3]:=13; ix[1,4]:=14; ix[1,5]:=15; ix[2,1]:=21; ix[2,2]:=22; ix[2,3]:=23; ix[2,4]:=14; ix[2,5]:=25; ix[3,1]:=31; ix[3,2]:=32; ix[3,3]:=33; ix[3,4]:=14; ix[3,5]:=35; ix[4,1]:=41; ix[4,2]:=42; ix[4,3]:=43; ix[4,4]:=14; ix[4,5]:=45; //some other codes end; My actual tests with integer table of 25 rows by 20 columns, the EXE file produced is 2048 bytes shorter for array constant than when I used and initialized local variables. And besides, my table is embedded in the program, with no extra file to distribute. For three or more dimensions, just follow the rules of Delphi: 'Multidimensional-array constants are defined by enclosing the constants of each dimension in separate sets of parentheses, separated by commas. The innermost constants correspond to the rightmost dimensions'. //The declaration type TCube = array[0..1, 0..1, 0..1] of Integer; const Maze: TCube = (((0, 1), (2, 3)), ((4, 5), (6, 7))); //provides an initialized array Maze with the following values: Maze[0, 0, 0] = 0 Maze[0, 0, 1] = 1 Maze[0, 1, 0] = 2 Maze[0, 1, 1] = 3 Maze[1, 0, 0] = 4 Maze[1, 0, 1] = 5 Maze[1, 1, 0] = 6 Maze[1, 1, 1] = 7 Moreover, you can also reuse the constant array by reassigning other values to the individual elements if you want to, very much like an ordinary array.