1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_Array2_h) |
|
25 #define octave_Array2_h 1 |
|
26 |
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <cassert> |
|
32 #include <cstdlib> |
|
33 |
|
34 #include "Array.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 class idx_vector; |
|
38 |
|
39 // Two dimensional array class. |
|
40 |
|
41 template <class T> |
|
42 class Array2 : public Array<T> |
|
43 { |
|
44 protected: |
|
45 |
|
46 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
47 { |
|
48 d1 = n; |
|
49 d2 = m; |
|
50 set_max_indices (2); |
|
51 } |
|
52 |
|
53 public: |
|
54 |
|
55 // These really need to be protected (and they will be in the |
|
56 // future, so don't depend on them being here!), but they can't be |
|
57 // until template friends work correctly in g++. |
|
58 |
|
59 int d1; |
|
60 int d2; |
|
61 |
|
62 Array2 (void) : Array<T> () |
|
63 { |
|
64 d1 = 0; |
|
65 d2 = 0; |
|
66 set_max_indices (2); |
|
67 } |
|
68 |
|
69 Array2 (int n, int m) : Array<T> (n*m) |
|
70 { |
|
71 d1 = n; |
|
72 d2 = m; |
|
73 set_max_indices (2); |
|
74 } |
|
75 |
|
76 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
77 { |
|
78 d1 = n; |
|
79 d2 = m; |
|
80 set_max_indices (2); |
|
81 } |
|
82 |
|
83 Array2 (const Array2<T>& a) : Array<T> (a) |
|
84 { |
|
85 d1 = a.d1; |
|
86 d2 = a.d2; |
|
87 set_max_indices (2); |
|
88 } |
|
89 |
|
90 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
91 { |
|
92 d1 = n; |
|
93 d2 = m; |
|
94 set_max_indices (2); |
|
95 } |
|
96 |
|
97 ~Array2 (void) { } |
|
98 |
|
99 Array2<T>& operator = (const Array2<T>& a) |
|
100 { |
|
101 if (this != &a && rep != a.rep) |
|
102 { |
|
103 Array<T>::operator = (a); |
|
104 d1 = a.d1; |
|
105 d2 = a.d2; |
|
106 } |
|
107 |
|
108 return *this; |
|
109 } |
|
110 |
|
111 int dim1 (void) const { return d1; } |
|
112 int dim2 (void) const { return d2; } |
|
113 |
|
114 int rows (void) const { return d1; } |
|
115 int cols (void) const { return d2; } |
|
116 int columns (void) const { return d2; } |
|
117 |
2109
|
118 // No checking of any kind, ever. |
|
119 |
|
120 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
|
121 T xelem (int i, int j) const { return Array<T>::xelem (d1*j+i); } |
|
122 |
2306
|
123 // Note that the following element selection methods don't use |
|
124 // xelem() because they need to make use of the code in |
|
125 // Array<T>::elem() that checks the reference count. |
2006
|
126 |
|
127 T& checkelem (int i, int j) |
|
128 { |
|
129 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
130 { |
2109
|
131 (*current_liboctave_error_handler) |
|
132 ("T& Array2<T>::checkelem (%d, %d): range error", i, j); |
2006
|
133 static T foo; |
|
134 return foo; |
|
135 } |
|
136 else |
2109
|
137 return Array<T>::elem (d1*j+i); |
2006
|
138 } |
|
139 |
2306
|
140 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
|
141 |
2109
|
142 #if defined (BOUNDS_CHECKING) |
2306
|
143 T& operator () (int i, int j) { return checkelem (i, j); } |
2006
|
144 #else |
2306
|
145 T& operator () (int i, int j) { return elem (i, j); } |
2006
|
146 #endif |
|
147 |
|
148 T checkelem (int i, int j) const |
|
149 { |
|
150 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
151 { |
2109
|
152 (*current_liboctave_error_handler) |
|
153 ("T Array2<T>::checkelem (%d, %d): range error", i, j); |
2006
|
154 T foo; |
|
155 static T *bar = &foo; |
|
156 return foo; |
|
157 } |
|
158 else |
2109
|
159 return Array<T>::elem (d1*j+i); |
2006
|
160 } |
1988
|
161 |
2306
|
162 T elem (int i, int j) const { return Array<T>::elem (d1*j+i); } |
|
163 |
2109
|
164 #if defined (BOUNDS_CHECKING) |
2306
|
165 T operator () (int i, int j) const { return checkelem (i, j); } |
2006
|
166 #else |
2306
|
167 T operator () (int i, int j) const { return elem (i, j); } |
2006
|
168 #endif |
|
169 |
2109
|
170 T range_error (const char *fcn, int i, int j) const; |
|
171 T& range_error (const char *fcn, int i, int j); |
1988
|
172 |
|
173 void resize (int n, int m); |
|
174 void resize (int n, int m, const T& val); |
|
175 |
|
176 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
177 |
|
178 #ifdef HEAVYWEIGHT_INDEXING |
|
179 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
180 |
|
181 Array2<T> value (void); |
|
182 #endif |
|
183 }; |
|
184 |
|
185 template <class LT, class RT> |
|
186 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
187 |
|
188 #endif |
|
189 |
|
190 /* |
|
191 ;;; Local Variables: *** |
|
192 ;;; mode: C++ *** |
|
193 ;;; End: *** |
|
194 */ |