8c323f4034ca45f3b1449b3ba3f2b50c7db3c2ed
[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.8 2000/09/02 05:10:39 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         : m_nx(x), m_ny(y), array_data(0)
39         {
40             allocArray();
41         }
42
43     Array2d ()
44         : m_nx(0), m_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             m_nx = x;
55             m_ny = y;
56             deleteArray();
57             allocArray();
58         }
59
60     T** getArray () 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 () const 
70         {  return sizeof(T); }
71
72     unsigned int sizeofColumn () const
73         { return (sizeof(T) * m_ny); }
74
75     unsigned int sizeofArray () const
76         { return (sizeof(T) * m_nx * m_ny); }
77
78
79  private:
80     unsigned int m_nx;
81     unsigned int m_ny;
82     T** array_data;
83
84     void allocArray ()
85         {
86             if (array_data)
87                 deleteArray();
88
89             array_data = new T*[m_nx];
90             
91             for (unsigned int i = 0; i < m_nx; i++)
92                 array_data[i] = new T[m_ny];
93         }
94
95     void deleteArray ()
96         {
97             if (array_data) {
98                 for (unsigned int i = 0; i < m_nx; i++)
99                     delete array_data [i];
100                 delete array_data;
101                 array_data = NULL;
102             }
103         }
104
105
106     Array2d& operator= (const Array2d& rhs); //assignment operator
107     Array2d (const Array2d& rhs);  // copy constructor
108 };
109
110
111 #endif