3665
|
1 // Template array classes |
|
2 /* |
|
3 |
|
4 Copyright (C) 2000 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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3665
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
|
34 #include <iostream> |
|
35 |
|
36 #include "ArrayN.h" |
|
37 |
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "ArrayN-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
|
45 // N-dimensional array class. |
|
46 |
|
47 template <class T> |
|
48 int |
3775
|
49 ArrayN<T>::compute_index (const Array<int>& arr_idx) const |
3665
|
50 { |
|
51 int retval = -1; |
|
52 |
|
53 int n = dimensions.length (); |
|
54 |
3775
|
55 if (n > 0 && n == arr_idx.length ()) |
3665
|
56 { |
3775
|
57 retval = arr_idx(--n); |
3665
|
58 |
|
59 while (--n >= 0) |
|
60 { |
|
61 retval *= dimensions(n); |
3775
|
62 retval += arr_idx(n); |
3665
|
63 } |
|
64 } |
|
65 else |
|
66 (*current_liboctave_error_handler) |
3775
|
67 ("ArrayN<T>::compute_index: invalid arr_idxing operation"); |
3665
|
68 |
|
69 return retval; |
|
70 } |
|
71 |
4142
|
72 // A guess (should be quite conservative). |
|
73 #define MALLOC_OVERHEAD 1024 |
|
74 |
3665
|
75 template <class T> |
|
76 int |
3775
|
77 ArrayN<T>::get_size (const Array<int>& arr_idx) |
3665
|
78 { |
|
79 // XXX KLUGE XXX |
|
80 |
|
81 // If an allocation of an array with r * c elements of type T |
|
82 // would cause an overflow in the allocator when computing the |
|
83 // size of the allocation, then return a value which, although |
|
84 // not equivalent to the actual request, should be too large for |
|
85 // most current hardware, but not so large to cause the |
|
86 // allocator to barf on computing retval * sizeof (T). |
|
87 |
|
88 static int nl; |
|
89 static double dl |
|
90 = frexp (static_cast<double> |
|
91 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
92 |
|
93 // This value should be an integer. If we return this value and |
|
94 // things work the way we expect, we should be paying a visit to |
|
95 // new_handler in no time flat. |
|
96 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
97 |
|
98 int retval = max_items; |
|
99 |
3775
|
100 int n = arr_idx.length (); |
3665
|
101 |
|
102 int nt = 0; |
|
103 double dt = 1; |
|
104 |
|
105 for (int i = 0; i < n; i++) |
|
106 { |
3775
|
107 int narr_idx; |
|
108 double darr_idx = frexp (static_cast<double> (arr_idx(i)), &narr_idx); |
3665
|
109 |
3775
|
110 nt += narr_idx; |
|
111 dt *= darr_idx; |
3665
|
112 } |
|
113 |
|
114 if (dt <= 0.5) |
|
115 { |
|
116 nt--; |
|
117 dt *= 2; |
|
118 |
|
119 if (dt <= 0.5) |
|
120 nt--; |
|
121 } |
|
122 |
|
123 if (nt < nl || (nt == nl && dt < dl)) |
|
124 { |
|
125 retval = 1; |
|
126 |
|
127 for (int i = 0; i < n; i++) |
3775
|
128 retval *= arr_idx(i); |
3665
|
129 } |
|
130 |
|
131 return retval; |
|
132 } |
|
133 |
4142
|
134 #undef MALLOC_OVERHEAD |
|
135 |
3665
|
136 template <class T> |
|
137 T |
3775
|
138 ArrayN<T>::range_error (const char *fcn, const Array<int>& arr_idx) const |
3665
|
139 { |
|
140 // XXX FIXME XXX -- report index values too! |
|
141 |
3928
|
142 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
143 |
|
144 return T (); |
|
145 } |
|
146 |
|
147 template <class T> |
|
148 T& |
3775
|
149 ArrayN<T>::range_error (const char *fcn, const Array<int>& arr_idx) |
3665
|
150 { |
|
151 // XXX FIXME XXX -- report index values too! |
|
152 |
3928
|
153 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
154 |
|
155 static T foo; |
|
156 return foo; |
|
157 } |
|
158 |
|
159 static inline bool |
3775
|
160 index_in_bounds (const Array<int>& arr_idx, const Array<int>& dimensions) |
3665
|
161 { |
|
162 bool retval = true; |
|
163 |
3775
|
164 int n = arr_idx.length (); |
3665
|
165 |
|
166 if (n == dimensions.length ()) |
|
167 { |
|
168 for (int i = 0; i < n; i++) |
|
169 { |
3775
|
170 if (arr_idx(i) < 0 || arr_idx(i) >= dimensions (i)) |
3665
|
171 { |
|
172 retval = false; |
|
173 break; |
|
174 } |
|
175 } |
|
176 } |
|
177 else |
|
178 retval = false; |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 static inline void |
3775
|
184 increment_index (Array<int>& arr_idx, const Array<int>& dimensions) |
3665
|
185 { |
3775
|
186 arr_idx(0)++; |
3665
|
187 |
3775
|
188 int n = arr_idx.length () - 1; |
3665
|
189 |
|
190 for (int i = 0; i < n; i++) |
|
191 { |
3775
|
192 if (arr_idx(i) < dimensions(i)) |
3665
|
193 break; |
|
194 else |
|
195 { |
3775
|
196 arr_idx(i) = 0; |
|
197 arr_idx(i+1)++; |
3665
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 template <class T> |
|
203 void |
|
204 ArrayN<T>::resize (const Array<int>& dims) |
|
205 { |
|
206 int n = dims.length (); |
|
207 |
|
208 for (int i = 0; i < n; i++) |
|
209 { |
|
210 if (dims(i) < 0) |
|
211 { |
|
212 (*current_liboctave_error_handler) |
|
213 ("can't resize to negative dimension"); |
|
214 return; |
|
215 } |
|
216 } |
|
217 |
|
218 bool no_change = true; |
|
219 |
|
220 for (int i = 0; i < n; i++) |
|
221 { |
|
222 if (dims(i) != dimensions(i)) |
|
223 { |
|
224 no_change = false; |
|
225 break; |
|
226 } |
|
227 } |
|
228 |
|
229 if (no_change) |
|
230 return; |
|
231 |
4053
|
232 typename Array<T>::ArrayRep *old_rep = rep; |
3665
|
233 const T *old_data = data (); |
|
234 |
4054
|
235 rep = new typename Array<T>::ArrayRep (get_size (dims)); |
3665
|
236 |
|
237 Array<int> old_dimensions = dimensions; |
|
238 |
|
239 int old_len = length (); |
|
240 |
|
241 dimensions = dims; |
|
242 |
3775
|
243 Array<int> arr_idx (dimensions.length (), 0); |
3665
|
244 |
|
245 for (int i = 0; i < old_len; i++) |
|
246 { |
3775
|
247 if (index_in_bounds (arr_idx, dimensions)) |
|
248 xelem (arr_idx) = old_data[i]; |
3665
|
249 |
3775
|
250 increment_index (arr_idx, dimensions); |
3665
|
251 } |
|
252 |
|
253 if (--old_rep->count <= 0) |
|
254 delete old_rep; |
|
255 } |
|
256 |
|
257 template <class T> |
|
258 void |
|
259 ArrayN<T>::resize (const Array<int>& dims, const T& val) |
|
260 { |
|
261 int n = dims.length (); |
|
262 |
|
263 for (int i = 0; i < n; i++) |
|
264 { |
|
265 if (dims(i) < 0) |
|
266 { |
|
267 (*current_liboctave_error_handler) |
|
268 ("can't resize to negative dimension"); |
|
269 return; |
|
270 } |
|
271 } |
|
272 |
|
273 bool no_change = true; |
|
274 |
|
275 for (int i = 0; i < n; i++) |
|
276 { |
|
277 if (dims(i) != dimensions(i)) |
|
278 { |
|
279 no_change = false; |
|
280 break; |
|
281 } |
|
282 } |
|
283 |
|
284 if (no_change) |
|
285 return; |
|
286 |
4053
|
287 typename Array<T>::ArrayRep *old_rep = rep; |
3665
|
288 const T *old_data = data (); |
|
289 |
|
290 int len = get_size (dims); |
|
291 |
4054
|
292 rep = new typename Array<T>::ArrayRep (len); |
3665
|
293 |
|
294 Array<int> old_dimensions = dimensions; |
|
295 |
|
296 int old_len = length (); |
|
297 |
|
298 dimensions = dims; |
|
299 |
3775
|
300 Array<int> arr_idx (dimensions.length (), 0); |
3665
|
301 |
|
302 for (int i = 0; i < len; i++) |
|
303 rep->elem (i) = val; |
|
304 |
|
305 for (int i = 0; i < old_len; i++) |
|
306 { |
3775
|
307 if (index_in_bounds (arr_idx, dimensions)) |
|
308 xelem (arr_idx) = old_data[i]; |
3665
|
309 |
3775
|
310 increment_index (arr_idx, dimensions); |
3665
|
311 } |
|
312 |
|
313 if (--old_rep->count <= 0) |
|
314 delete old_rep; |
|
315 } |
|
316 |
|
317 template <class T> |
|
318 ArrayN<T>& |
3775
|
319 ArrayN<T>::insert (const ArrayN<T>& a, const Array<int>& arr_idx) |
3665
|
320 { |
3775
|
321 int n = arr_idx.length (); |
3665
|
322 |
|
323 if (n == dimensions.length ()) |
|
324 { |
|
325 Array<int> a_dims = a.dims (); |
|
326 |
|
327 for (int i = 0; i < n; i++) |
|
328 { |
3775
|
329 if (arr_idx(i) < 0 || arr_idx(i) + a_dims(i) > dimensions(i)) |
3665
|
330 { |
|
331 (*current_liboctave_error_handler) |
|
332 ("ArrayN<T>::insert: range error for insert"); |
|
333 return *this; |
|
334 } |
|
335 } |
|
336 |
|
337 #if 0 |
|
338 // XXX FIXME XXX -- need to copy elements |
|
339 |
|
340 for (int j = 0; j < a_cols; j++) |
|
341 for (int i = 0; i < a_rows; i++) |
|
342 elem (r+i, c+j) = a.elem (i, j); |
|
343 #endif |
|
344 |
|
345 } |
|
346 else |
|
347 (*current_liboctave_error_handler) |
|
348 ("ArrayN<T>::insert: invalid indexing operation"); |
|
349 |
|
350 return *this; |
|
351 } |
|
352 |
|
353 template <class T> |
|
354 std::ostream& |
|
355 operator << (std::ostream& os, const ArrayN<T>& a) |
|
356 { |
|
357 Array<int> dims = a.dimensions; |
|
358 |
|
359 int n_dims = dims.length (); |
|
360 |
|
361 os << n_dims << "-dimensional array ("; |
|
362 |
|
363 for (int i = 0; i < n_dims - 1; i++) |
|
364 os << dims(i) << "x"; |
|
365 os << dims(n_dims-1) << ")\n\n"; |
|
366 |
|
367 os << "data:\n"; |
|
368 |
|
369 int n = ArrayN<T>::get_size (dims); |
|
370 |
|
371 // for (int i = 0; i < n; i++) |
|
372 // os << a.elem (i) << "\n"; |
|
373 |
|
374 return os; |
|
375 } |
|
376 |
|
377 /* |
|
378 ;;; Local Variables: *** |
|
379 ;;; mode: C++ *** |
|
380 ;;; End: *** |
|
381 */ |