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