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