src/array.h

00001 /***************************************************************************
00002                           array.h  -  description
00003                              -------------------
00004     begin                : Mond Dec 29 2003
00005     copyright            : (C) 2003 by Lynn Hazan
00006     email                : lynn.hazan@myrealbox.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef ARRAY_H
00019 #define ARRAY_H
00020 
00021 //C include files
00022 #include <cstring>
00023 
00024 //General C++ include files
00025 #include <iostream>
00026 using namespace std;
00027 
00035 template <class T>
00036 class Array {
00037 public:
00038      inline Array(long nbOfRows,long nbOfColumns){
00039     nbColumns = nbOfColumns;
00040     nbRows = nbOfRows;
00041     array = new T[nbRows * nbColumns];
00042   };
00043   
00044   inline Array():nbColumns(0),nbRows(0),array(0L){};
00045   
00046      inline ~Array(){
00047     delete []array;
00048   };
00052   inline void setSize(long nbOfRows,long nbOfColumns){
00053     nbColumns = nbOfColumns;
00054     nbRows = nbOfRows;
00055     if(array) delete[]array;
00056     array = new T[nbRows * nbColumns];
00057   };
00058   
00066   inline T operator()(long row,long column) const{
00067     return array[(row - 1)*nbColumns + (column - 1)];
00068   };
00076   inline T& operator()(long row,long column){
00077     return array[(row - 1)*nbColumns + (column - 1)];
00078   };
00086   inline T& operator[](long position){
00087     return array[position];
00088   };
00093   inline long nbOfRows() const{
00094     return nbRows;
00095   };
00100   inline long nbOfColumns() const{
00101     return nbColumns;
00102   };
00103 
00104 
00109   inline void copyAndPrependColumn(Array& source){
00110     for(long i = 0;i < nbRows;++i)
00111       memcpy(&array[i * nbColumns + 1],&(source.array[i * source.nbColumns]),
00112              source.nbColumns * sizeof(T));
00113   };
00114 
00121   inline void copySubset(Array& source,long lastColumnToCopy){   
00122     for(long i = 0;i < nbRows;++i)
00123       memcpy(&array[i * lastColumnToCopy],&(source.array[i * source.nbColumns]),
00124              lastColumnToCopy * sizeof(T));
00125   };
00126 
00135   inline void copySubset(Array& source,long firstColumnToCopy,long lastColumnToCopy,long startingColumn){
00136     long nbColumnsToCopy = lastColumnToCopy - firstColumnToCopy + 1;
00137     for(long i = 0;i < nbRows;++i)
00138       memcpy(&array[i * nbColumns + startingColumn - 1],&(source.array[i * source.nbColumns + (firstColumnToCopy - 1)]),
00139              nbColumnsToCopy * sizeof(T));
00140   };
00141   
00145   inline void fillWithZeros(){
00146    memset(array,0,static_cast<unsigned int>(nbRows * nbColumns) * sizeof(T));   
00147   };
00148 
00149 
00151   inline Array<T>& operator=(const Array<T>& source){
00152    if(&source != this){
00153     nbColumns = source.nbColumns;
00154     nbRows = source.nbRows;   
00155     if(array) delete[]array;
00156     array = new T[nbRows * nbColumns];
00157     memcpy(array,source.array,nbRows * nbColumns * sizeof(T));
00158    } 
00159     return *this;
00160   };
00161     
00162 protected:
00164   long nbColumns;
00166   long nbRows;
00167   T* array;
00168 
00169 };
00170 
00171 
00178 template <class T>
00179 class pArray : public Array<T>{
00180 
00181 protected:
00182   using Array<T>::nbColumns;
00183   using Array<T>::nbRows;
00184   using Array<T>::array;
00185 
00186 public:
00187      inline pArray(){};
00188      inline ~pArray(){};
00189 
00190 
00195   inline void copyAndPrependColumn(pArray& source){
00196    for(long i = 0;i < nbRows;++i){
00197     for(int j = 0;j<nbColumns;++j){
00198      array[i*nbColumns + (j + 1)] = source.array[i*source.nbColumns + j];
00199     }
00200    }
00201   };
00202 
00209   inline void copySubset(pArray& source,long lastColumnToCopy){
00210  for(long i = 0;i < nbRows;++i){
00211     for(int j = 0;j<lastColumnToCopy;++j){
00212      array[i*lastColumnToCopy + j] = source.array[i*source.nbColumns + j];
00213     }
00214    }
00215   };
00216 
00225   inline void copySubset(pArray& source,long firstColumnToCopy,long lastColumnToCopy,long startingColumn){
00226    long nbColumnsToCopy = lastColumnToCopy - firstColumnToCopy + 1;
00227     for(long i = 0;i < nbRows;++i){
00228     for(int j = 0;j< nbColumnsToCopy;++j){
00229      array[i*nbColumns + (startingColumn - 1) + j] = source.array[i*source.nbColumns + (firstColumnToCopy - 1) + j];
00230     }
00231    }
00232   };
00233   
00234    
00235   inline pArray<T>& operator=(pArray<T>& source){
00236     nbColumns = source.nbColumns;
00237     nbRows = source.nbRows;
00238     if(array) delete[]array;
00239     array = new T[nbRows * nbColumns];
00240     for(int i = 0;i<nbRows;++i){
00241       for(int j = 0;j<nbColumns;++j){
00242       array[i*nbColumns + j] = source.array[i*nbColumns + j];
00243      } 
00244     }
00245     return *this;
00246   };
00247 
00248   inline pArray<T> operator=(pArray<T>& source) const{
00249     nbColumns = source.nbColumns;
00250     nbRows = source.nbRows;
00251     if(array) delete[]array;
00252     array = new T[nbRows * nbColumns];
00253     for(int i = 0;i<nbRows;++i){
00254      for(int j = 0;j<nbColumns;++j){
00255       array[i*nbColumns + j] = source.array[i*nbColumns + j];
00256      }
00257     }
00258     return *this;
00259   };
00260 };
00261 
00262 #endif

Generated on Mon Sep 17 20:47:30 2007 for NeuroScope by  doxygen 1.5.1