1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
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> |
3473
|
32 #include <climits> |
|
33 #include <cmath> |
1988
|
34 #include <cstdlib> |
|
35 |
|
36 #include "Array.h" |
|
37 #include "lo-error.h" |
|
38 |
|
39 class idx_vector; |
|
40 |
|
41 // Two dimensional array class. |
|
42 |
|
43 template <class T> |
|
44 class Array2 : public Array<T> |
|
45 { |
3492
|
46 protected: |
3473
|
47 |
|
48 int get_size (int r, int c) const; |
|
49 |
|
50 Array2 (T *d, int n, int m) : Array<T> (d, get_size (n, m)) |
1988
|
51 { |
|
52 d1 = n; |
|
53 d2 = m; |
|
54 set_max_indices (2); |
|
55 } |
|
56 |
|
57 public: |
|
58 |
|
59 // These really need to be protected (and they will be in the |
|
60 // future, so don't depend on them being here!), but they can't be |
|
61 // until template friends work correctly in g++. |
|
62 |
|
63 int d1; |
|
64 int d2; |
|
65 |
|
66 Array2 (void) : Array<T> () |
|
67 { |
|
68 d1 = 0; |
|
69 d2 = 0; |
|
70 set_max_indices (2); |
|
71 } |
|
72 |
3473
|
73 Array2 (int n, int m) : Array<T> (get_size (n, m)) |
1988
|
74 { |
|
75 d1 = n; |
|
76 d2 = m; |
|
77 set_max_indices (2); |
|
78 } |
|
79 |
3473
|
80 Array2 (int n, int m, const T& val) : Array<T> (get_size (n, m), val) |
1988
|
81 { |
|
82 d1 = n; |
|
83 d2 = m; |
|
84 set_max_indices (2); |
|
85 } |
|
86 |
|
87 Array2 (const Array2<T>& a) : Array<T> (a) |
|
88 { |
|
89 d1 = a.d1; |
|
90 d2 = a.d2; |
|
91 set_max_indices (2); |
|
92 } |
|
93 |
|
94 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
95 { |
|
96 d1 = n; |
|
97 d2 = m; |
|
98 set_max_indices (2); |
|
99 } |
|
100 |
|
101 ~Array2 (void) { } |
|
102 |
|
103 Array2<T>& operator = (const Array2<T>& a) |
|
104 { |
|
105 if (this != &a && rep != a.rep) |
|
106 { |
|
107 Array<T>::operator = (a); |
|
108 d1 = a.d1; |
|
109 d2 = a.d2; |
|
110 } |
|
111 |
|
112 return *this; |
|
113 } |
|
114 |
|
115 int dim1 (void) const { return d1; } |
|
116 int dim2 (void) const { return d2; } |
|
117 |
|
118 int rows (void) const { return d1; } |
|
119 int cols (void) const { return d2; } |
|
120 int columns (void) const { return d2; } |
|
121 |
2109
|
122 // No checking of any kind, ever. |
|
123 |
|
124 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
|
125 T xelem (int i, int j) const { return Array<T>::xelem (d1*j+i); } |
|
126 |
2306
|
127 // Note that the following element selection methods don't use |
|
128 // xelem() because they need to make use of the code in |
|
129 // Array<T>::elem() that checks the reference count. |
2006
|
130 |
|
131 T& checkelem (int i, int j) |
|
132 { |
|
133 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
134 { |
2109
|
135 (*current_liboctave_error_handler) |
|
136 ("T& Array2<T>::checkelem (%d, %d): range error", i, j); |
2006
|
137 static T foo; |
|
138 return foo; |
|
139 } |
|
140 else |
2109
|
141 return Array<T>::elem (d1*j+i); |
2006
|
142 } |
|
143 |
2306
|
144 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
|
145 |
2109
|
146 #if defined (BOUNDS_CHECKING) |
2306
|
147 T& operator () (int i, int j) { return checkelem (i, j); } |
2006
|
148 #else |
2306
|
149 T& operator () (int i, int j) { return elem (i, j); } |
2006
|
150 #endif |
|
151 |
|
152 T checkelem (int i, int j) const |
|
153 { |
|
154 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
155 { |
2109
|
156 (*current_liboctave_error_handler) |
|
157 ("T Array2<T>::checkelem (%d, %d): range error", i, j); |
2440
|
158 return T (); |
2006
|
159 } |
|
160 else |
2109
|
161 return Array<T>::elem (d1*j+i); |
2006
|
162 } |
1988
|
163 |
2306
|
164 T elem (int i, int j) const { return Array<T>::elem (d1*j+i); } |
|
165 |
2109
|
166 #if defined (BOUNDS_CHECKING) |
2306
|
167 T operator () (int i, int j) const { return checkelem (i, j); } |
2006
|
168 #else |
2306
|
169 T operator () (int i, int j) const { return elem (i, j); } |
2006
|
170 #endif |
|
171 |
2109
|
172 T range_error (const char *fcn, int i, int j) const; |
|
173 T& range_error (const char *fcn, int i, int j); |
1988
|
174 |
|
175 void resize (int n, int m); |
|
176 void resize (int n, int m, const T& val); |
|
177 |
|
178 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
179 |
2815
|
180 bool is_square (void) const { return (d1 == d2); } |
|
181 |
3225
|
182 Array2<T> transpose (void) const; |
|
183 |
1988
|
184 #ifdef HEAVYWEIGHT_INDEXING |
3472
|
185 void maybe_delete_elements (idx_vector& i); |
|
186 |
1988
|
187 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
188 |
|
189 Array2<T> value (void); |
2382
|
190 |
|
191 Array2<T> index (idx_vector& i) const; |
|
192 |
|
193 Array2<T> index (idx_vector& i, idx_vector& j) const; |
1988
|
194 #endif |
|
195 }; |
|
196 |
|
197 template <class LT, class RT> |
|
198 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
199 |
|
200 #endif |
|
201 |
|
202 /* |
|
203 ;;; Local Variables: *** |
|
204 ;;; mode: C++ *** |
|
205 ;;; End: *** |
|
206 */ |