develooper Front page | perl.perl6.internals | Postings from July 2002

Re: multidimensional array

Thread Previous
Josef Höök
July 8, 2002 03:39
Re: multidimensional array
Message ID:

On Tue, 2 Jul 2002, Dan Sugalski wrote:

> At 9:45 PM +0100 7/2/02, Nicholas Clark wrote:
> >On Thu, Jun 27, 2002 at 05:42:10PM +0200, Josef Höök wrote:
> >>  I've been thinking abit on howto implement multidimensional arrays and
> >>  i found that its quite tricky :). I'm currently thinking of having
> >>  a structure that contains a data pointer and its location in every
> >>  dimension something like this:
> >>
> >>  typedef struct CELL {
> >>    int dim[100];
> >>    int *data;
> >>  } cell;
> >>
> >>  cell *a = (cell *)malloc((unsigned) (2)*sizeof(cell *));
> >>
> >>  // A cell with location in a 3 dim space a(2;4;6)
> >>   a->dim[0] = 2;
> >>   a->dim[1] = 4;
> >>   a->dim[2] = 6;
> >>   a->data = data1;
> >
> >I wouldn't like to do it quite like that, as you've got a hard coded constant
> >in there. Why should we prevent crazy people making arrays with dimensions
> >greater than 100, and why should we have most people who only want a 2D array
> >having to carry 98 unused ints around as baggage.
> Nick makes a number of good points here (including ones I've 
> chopped). It's not unreasonable to have matrices of fixed 
> dimensionality, at least to start, even if the size in each dimension 
> is variable. Which is to say, you have to know that it's a 3D matrix, 
> even if you don't know how far it goes in each of the 3 dimensions.

I agree with that but i tried to implement it and i found it much
nicer to have it this way ( note i've changed the name cell to point ) 

// Each point consists of a pointer to data and its location ( position )
typedef struct _position POS;
typedef struct _point POINT;

struct _position {
  POS *next;
  INTVAL val;
  INTVAL upper_init_val;

struct _point {
  POS pos;
  INTVAL *data;

Position structure consist of:
	a pointer to next position structure in dimension+1 this way its
        fairly easy to assign from a KEY structure.
	val is its position in its dimension eg: x = 2
	upper_init_val is the upper boundry value that we get when
        initialising a POINT
	eg: Marray[2][3]
	will store upper_init_val == 2 in first position and
	pos->next.upper_init_val == 3	
 	I've been thinking about this and i dont think its the best way to
        store init values .. i am currently thinking of creating a
        seperate POS at begining of the PMC Buffer that needs to be

 Point structure:
         holds its position.
	 and a pointer to its data.

Having it this way we can eassily represent a matrix and a
multidimensional array with the same structures plus as i see it we will
be compatible with CBLAS and maybe other matrix packages. 

I have a working code that can do 
        new P0, .Matrix[3][1]
	new P0, .Matrix

I added a "op new(out PMC, in INT, in INT, in INT)" in core ops ( just a
fast hack since we cant yet handle new_p_i_k ). 
keyed unique void init() in vtable.
I also added a keyed function in pmc.c. These where just some fast hack
to get around non existing key operations. Who is working on key
operations ?
Anyway i could make a patch with my work, though not complete its a 


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About