2D/3D array classes

From: Masaharu Goto (MXJ02154@nifty.ne.jp)
Date: Sun Jul 19 1998 - 14:37:00 MEST


Rooters,

It looks like 2D/3D classes are FAQ item. Many people are still fighting with
raw pointer to make 2D/3D array which is very painful. 

In theory, you can use STL 'vector<vector<vector<float> > >' for 3D, but 
some compilers and also cint has problem dealing with such complex STL usage.
I made simple 2D/3D classes which are easier to deal with. If somebody else 
have better solution, please follow, 

Masaharu Goto

// 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 ////////////////////////////////////////////

Below is an example of using array classes.

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

// Please use following notation for now
// This is more efficient.
test1() {
  int i,j,k;
  const int x=5, y=3,z=4;
  float3d a(x,y,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));
      }
    }
  }
}

// Please don't use it as follows
// This is slightly inefficient and cint has bug now
test2() {
  int i,j,k;
  const int x=5, y=3,z=4;
  float3d a(x,y,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 works, but a bit slower
      }
    }
  }

  for(i=0;i<x;i++) {
    for(j=0;j<y;j++) {
      for(k=0;k<z;k++) {
        // following notation should work, but I found a bug in cint
        // I need to fix it
        printf("i=%d j=%d k=%d %g\n",i,j,k,a[i][j][k]);
      }
    }
  }
}

main() {
  test1();
  //test2();
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:35 MET