73f39093cb2da11832e3d69a8d1c87f1f59d2047
[ctsim.git] / include / array2d.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         array2d.h
5 **      Purpose:      2-dimension array classes
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: array2d.h,v 1.7 2000/07/13 07:03:21 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef ARRAY2D_H
29 #define ARRAY2D_H
30
31 #include "ctsupport.h"
32
33
34 template<class T> 
35 class Array2d {
36  public:
37     Array2d (unsigned int x, unsigned int y)
38         : nx(x), ny(y), array_data(0)
39         {
40             allocArray();
41         }
42
43     Array2d (void)
44         : nx(0), ny(0), array_data(0)
45         {}
46
47     ~Array2d ()
48         {
49             deleteArray();
50         }
51     
52     void initSetSize (unsigned int x, unsigned int y)
53         {
54             nx = x;
55             ny = y;
56             deleteArray();
57             allocArray();
58         }
59
60     T** getArray (void) const
61         { return array_data; }
62
63     T* getColumn (unsigned int x) const
64         { return (array_data ? array_data[x] : NULL); }
65
66     T getPoint (unsigned int x, unsigned int y) const
67         { return (array_data ? array_data[x][y] : NULL); }
68
69     unsigned int sizeofPixel (void) const 
70         {  return sizeof(T); }
71
72     unsigned int sizeofColumn (void) const
73         { return (sizeof(T) * ny); }
74
75     unsigned int sizeofArray (void) const
76         { return (sizeof(T) * nx * ny); }
77
78
79  private:
80     unsigned int nx;
81     unsigned int ny;
82     T** array_data;
83
84     void allocArray (void)
85         {
86             array_data = new T*[nx];
87             
88             for (unsigned int i = 0; i < nx; i++)
89                 array_data[i] = new T[ny];
90         }
91
92     void deleteArray (void)
93         {
94             if (array_data) {
95                 for (unsigned int i = 0; i < nx; i++)
96                     delete array_data[i];
97                 delete array_data;
98             }
99         }
100
101
102     Array2d& operator= (const Array2d& rhs); //assignment operator
103     Array2d (const Array2d& rhs);  // copy constructor
104 };
105
106
107 #endif