1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex4.2/CaloGrid.hh

52 lines
962 B
C++

//CaloGrid.hh
#include "CaloCell.hh"
#ifndef CALOGRID_HH
#define CALOGRID_HH
class CaloGrid
{
public:
CaloGrid( int nx, int ny ) {
init(nx, ny );
}
CaloGrid( const CaloGrid& other ) {
// Set everything using init to allocate memory
init(other.nx, other.ny ) ;
// Copy elements
// Because we make it a 1Dim array, we can simply run over all elements in one go
for (int i=0 ; i<nx*ny ; i++ ) {
cells[i] = other.cells[i] ;
}
}
~CaloGrid() {
delete[] cells;
}
CaloCell* cell(int x, int y);
const CaloCell* cell(int x, int y) const {
return const_cast<CaloCell*> (cell(x, y));
}
private:
int nx, ny ;
CaloCell* cells ;
void init( int in_nx, int in_ny ) {
nx = in_nx ;
ny = in_ny ;
// contigious cells
cells = new CaloCell[ nx*ny ];
// set Cell Ids
for ( int i = 0; i < nx ; i++ ) {
for ( int j = 0; j < ny ; j++ ) {
cells[i*ny +j].setId( i*ny+j );
}
}
}
} ;
#endif