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