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