[ROOT] 2D and 3D arrays

From: kanaan (kanaan@astro.ufsc.br)
Date: Fri Mar 08 2002 - 14:55:12 MET


Hi Rooters,

I am cleaning up some code and wanted to replace dynamic allocation in C
style with more the civilized <vector> class.

The problem I have is I that I have 2D and 3D vectors and this is not part
of STL.

On root-talk digest I found a couple messages by Masaharu 
(Sun Jul 19 1998 - 14:37:00 MEST AND  Mon Jul 20 1998 - 14:42:00 MEST) 
where he posted some 2D and 3D classes
His examples work fine, but when I try to make these guys members of my
own classes things go bad.

So, here is an example of what I try to do and what happens:

I created a class Test which should have 'a' as a private member, however, I can' t 
declare it in the class definition.  I actually don't even know how to do
it.  

Now, if I initialize (with no previous declaration) 
it within the class constructor, as I am doing in the
attached example, no problem, it runs allright.  But well, it is only known
by the constructor, other methods know nothing about its existence (no
surprise).

So, the real question is: how do I declare one of these guys as class members?

Have any other 2D,3D,Nd classes been included as part of standard root?

I know there exist several other 2D and 3D classes around, but the old question
arises:  which one is best?  which is more "standard"? as I am using root as a
framework it better be a root class.

thanks,

Antonio

ps. repliers: would you *pleeease* send me a copy?  I am not currently subscribing
to the list.

*********here is Masaharu's class definintionos **********


// vec3d.h ///////////////////////////////////////////////////////////
// You can precompile this module by following command
//   $  makecint -mk Makefile -dl vec3d.dll -H vec3d.h
//   $  make -f Makefile
//
// 2 dimentional array
template<class T> class array2d {
 public:
  array2d(int jx,int kx) { 
    maxj=jx; maxk=kx;
    isnew=1;
    p = new T[maxj*maxk];
  }
  array2d(T* pin,int jx,int kx) { 
    maxj=jx; maxk=kx;
    isnew=0;
    p = pin;
  }
  ~array2d() {if(isnew) delete[] p;}
  T& operator()(int i,int j,int k) {return( *(p + maxk*j + k) ) ;}
  T* operator[](int j) {return(p+maxk*j);}
 private:
  T* p;
  int maxj,maxk;
  int isnew;
};

// 3 dimentional array
template<class T> class array3d {
 public:
  array3d(int ix,int jx,int kx) { 
    maxi=ix; maxj=jx; maxk=kx;
    isnew=1;
    p = new T[maxi*maxj*maxk];
  }
  array3d(T* pin,int ix,int jx,int kx) { 
    maxi=ix; maxj=jx; maxk=kx;
    isnew=0;
    p = pin;
  }
  ~array3d() {if(isnew) delete[] p;}
  T& operator()(int i,int j,int k) {
    return( *(p + ((maxj*maxk)*i + maxk*j + k)) );
  }
  array2d<T> operator[](int i) {
    array2d<T> tmp(p+maxj*maxk*i,maxj,maxk);
    return(tmp);
  }
 private:
  T* p;
  int maxi,maxj,maxk;
  int isnew;
};

typedef array3d<double> double3d;
typedef array2d<double> double2d;
typedef array3d<float> float3d;
typedef array2d<float> float2d;
typedef array3d<int> int3d;
typedef array2d<int> int2d;
/// END OF 2D/3D array classes ////////////////////////////////////////////





*******  here is my teste: *******


///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#ifdef __CINT__
#include "vec3d.dll"
#else
#include "vec3d.h"
#endif


class Test
{
public:
  Test();
  ~Test();
  ShowVec();
  
private:
  int x,y,z;
  // My goal is to declare a here
};

Test::Test()
{
  int i,j,k;
  x=5;
  y=5;
  z=5;
  // And having declared a above initialize it here once I know what
  // x,y,z are.
  // ideally I should be able to resize a too
  float3d a(const(x),const(y),const(z));
  for(i=0;i<x;i++) {
    for(j=0;j<y;j++) {
      for(k=0;k<=z;k++) {
        a(i,j,k) = i+j+k; // a(i,j,k) notation is faster
      }
    }
  }

  for(i=0;i<x;i++) {
    for(j=0;j<y;j++) {
      for(k=0;k<z;k++) {
        printf("i=%d j=%d k=%d %g\n",i,j,k,a(i,j,k));
      }
    }
  }

}

Test::ShowVec()
{
  int i,j,k ;
  for(i=0;i<x;i++) {
    for(j=0;j<y;j++) {
      for(k=0;k<z;k++) {
        // printf("i=%d j=%d k=%d %g\n",i,j,k,a(i,j,k));
      }
    }
  }
}



main() {
  Test *lixo;
  lixo = new Test() ;
  
  //test1();
  //test2();
}



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:45 MET