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 |
|
24 #if defined (__GNUG__) |
|
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 |
|
72 template <class T> |
|
73 int |
3775
|
74 ArrayN<T>::get_size (const Array<int>& arr_idx) |
3665
|
75 { |
|
76 // XXX KLUGE XXX |
|
77 |
|
78 // If an allocation of an array with r * c elements of type T |
|
79 // would cause an overflow in the allocator when computing the |
|
80 // size of the allocation, then return a value which, although |
|
81 // not equivalent to the actual request, should be too large for |
|
82 // most current hardware, but not so large to cause the |
|
83 // allocator to barf on computing retval * sizeof (T). |
|
84 |
|
85 // A guess (should be quite conservative). |
|
86 static const int MALLOC_OVERHEAD = 1024; |
|
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 |
|
134 template <class T> |
|
135 T |
3775
|
136 ArrayN<T>::range_error (const char *fcn, const Array<int>& arr_idx) const |
3665
|
137 { |
|
138 // XXX FIXME XXX -- report index values too! |
|
139 |
3928
|
140 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
141 |
|
142 return T (); |
|
143 } |
|
144 |
|
145 template <class T> |
|
146 T& |
3775
|
147 ArrayN<T>::range_error (const char *fcn, const Array<int>& arr_idx) |
3665
|
148 { |
|
149 // XXX FIXME XXX -- report index values too! |
|
150 |
3928
|
151 (*current_liboctave_error_handler) ("range error in ArrayN"); |
3665
|
152 |
|
153 static T foo; |
|
154 return foo; |
|
155 } |
|
156 |
|
157 static inline bool |
3775
|
158 index_in_bounds (const Array<int>& arr_idx, const Array<int>& dimensions) |
3665
|
159 { |
|
160 bool retval = true; |
|
161 |
3775
|
162 int n = arr_idx.length (); |
3665
|
163 |
|
164 if (n == dimensions.length ()) |
|
165 { |
|
166 for (int i = 0; i < n; i++) |
|
167 { |
3775
|
168 if (arr_idx(i) < 0 || arr_idx(i) >= dimensions (i)) |
3665
|
169 { |
|
170 retval = false; |
|
171 break; |
|
172 } |
|
173 } |
|
174 } |
|
175 else |
|
176 retval = false; |
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
|
181 static inline void |
3775
|
182 increment_index (Array<int>& arr_idx, const Array<int>& dimensions) |
3665
|
183 { |
3775
|
184 arr_idx(0)++; |
3665
|
185 |
3775
|
186 int n = arr_idx.length () - 1; |
3665
|
187 |
|
188 for (int i = 0; i < n; i++) |
|
189 { |
3775
|
190 if (arr_idx(i) < dimensions(i)) |
3665
|
191 break; |
|
192 else |
|
193 { |
3775
|
194 arr_idx(i) = 0; |
|
195 arr_idx(i+1)++; |
3665
|
196 } |
|
197 } |
|
198 } |
|
199 |
|
200 template <class T> |
|
201 void |
|
202 ArrayN<T>::resize (const Array<int>& dims) |
|
203 { |
|
204 int n = dims.length (); |
|
205 |
|
206 for (int i = 0; i < n; i++) |
|
207 { |
|
208 if (dims(i) < 0) |
|
209 { |
|
210 (*current_liboctave_error_handler) |
|
211 ("can't resize to negative dimension"); |
|
212 return; |
|
213 } |
|
214 } |
|
215 |
|
216 bool no_change = true; |
|
217 |
|
218 for (int i = 0; i < n; i++) |
|
219 { |
|
220 if (dims(i) != dimensions(i)) |
|
221 { |
|
222 no_change = false; |
|
223 break; |
|
224 } |
|
225 } |
|
226 |
|
227 if (no_change) |
|
228 return; |
|
229 |
4053
|
230 typename Array<T>::ArrayRep *old_rep = rep; |
3665
|
231 const T *old_data = data (); |
|
232 |
|
233 rep = new Array<T>::ArrayRep (get_size (dims)); |
|
234 |
|
235 Array<int> old_dimensions = dimensions; |
|
236 |
|
237 int old_len = length (); |
|
238 |
|
239 dimensions = dims; |
|
240 |
3775
|
241 Array<int> arr_idx (dimensions.length (), 0); |
3665
|
242 |
|
243 for (int i = 0; i < old_len; i++) |
|
244 { |
3775
|
245 if (index_in_bounds (arr_idx, dimensions)) |
|
246 xelem (arr_idx) = old_data[i]; |
3665
|
247 |
3775
|
248 increment_index (arr_idx, dimensions); |
3665
|
249 } |
|
250 |
|
251 if (--old_rep->count <= 0) |
|
252 delete old_rep; |
|
253 } |
|
254 |
|
255 template <class T> |
|
256 void |
|
257 ArrayN<T>::resize (const Array<int>& dims, const T& val) |
|
258 { |
|
259 int n = dims.length (); |
|
260 |
|
261 for (int i = 0; i < n; i++) |
|
262 { |
|
263 if (dims(i) < 0) |
|
264 { |
|
265 (*current_liboctave_error_handler) |
|
266 ("can't resize to negative dimension"); |
|
267 return; |
|
268 } |
|
269 } |
|
270 |
|
271 bool no_change = true; |
|
272 |
|
273 for (int i = 0; i < n; i++) |
|
274 { |
|
275 if (dims(i) != dimensions(i)) |
|
276 { |
|
277 no_change = false; |
|
278 break; |
|
279 } |
|
280 } |
|
281 |
|
282 if (no_change) |
|
283 return; |
|
284 |
4053
|
285 typename Array<T>::ArrayRep *old_rep = rep; |
3665
|
286 const T *old_data = data (); |
|
287 |
|
288 int len = get_size (dims); |
|
289 |
|
290 rep = new Array<T>::ArrayRep (len); |
|
291 |
|
292 Array<int> old_dimensions = dimensions; |
|
293 |
|
294 int old_len = length (); |
|
295 |
|
296 dimensions = dims; |
|
297 |
3775
|
298 Array<int> arr_idx (dimensions.length (), 0); |
3665
|
299 |
|
300 for (int i = 0; i < len; i++) |
|
301 rep->elem (i) = val; |
|
302 |
|
303 for (int i = 0; i < old_len; i++) |
|
304 { |
3775
|
305 if (index_in_bounds (arr_idx, dimensions)) |
|
306 xelem (arr_idx) = old_data[i]; |
3665
|
307 |
3775
|
308 increment_index (arr_idx, dimensions); |
3665
|
309 } |
|
310 |
|
311 if (--old_rep->count <= 0) |
|
312 delete old_rep; |
|
313 } |
|
314 |
|
315 template <class T> |
|
316 ArrayN<T>& |
3775
|
317 ArrayN<T>::insert (const ArrayN<T>& a, const Array<int>& arr_idx) |
3665
|
318 { |
3775
|
319 int n = arr_idx.length (); |
3665
|
320 |
|
321 if (n == dimensions.length ()) |
|
322 { |
|
323 Array<int> a_dims = a.dims (); |
|
324 |
|
325 for (int i = 0; i < n; i++) |
|
326 { |
3775
|
327 if (arr_idx(i) < 0 || arr_idx(i) + a_dims(i) > dimensions(i)) |
3665
|
328 { |
|
329 (*current_liboctave_error_handler) |
|
330 ("ArrayN<T>::insert: range error for insert"); |
|
331 return *this; |
|
332 } |
|
333 } |
|
334 |
|
335 #if 0 |
|
336 // XXX FIXME XXX -- need to copy elements |
|
337 |
|
338 for (int j = 0; j < a_cols; j++) |
|
339 for (int i = 0; i < a_rows; i++) |
|
340 elem (r+i, c+j) = a.elem (i, j); |
|
341 #endif |
|
342 |
|
343 } |
|
344 else |
|
345 (*current_liboctave_error_handler) |
|
346 ("ArrayN<T>::insert: invalid indexing operation"); |
|
347 |
|
348 return *this; |
|
349 } |
|
350 |
|
351 template <class T> |
|
352 std::ostream& |
|
353 operator << (std::ostream& os, const ArrayN<T>& a) |
|
354 { |
|
355 Array<int> dims = a.dimensions; |
|
356 |
|
357 int n_dims = dims.length (); |
|
358 |
|
359 os << n_dims << "-dimensional array ("; |
|
360 |
|
361 for (int i = 0; i < n_dims - 1; i++) |
|
362 os << dims(i) << "x"; |
|
363 os << dims(n_dims-1) << ")\n\n"; |
|
364 |
|
365 os << "data:\n"; |
|
366 |
|
367 int n = ArrayN<T>::get_size (dims); |
|
368 |
|
369 // for (int i = 0; i < n; i++) |
|
370 // os << a.elem (i) << "\n"; |
|
371 |
|
372 return os; |
|
373 } |
|
374 |
|
375 /* |
|
376 ;;; Local Variables: *** |
|
377 ;;; mode: C++ *** |
|
378 ;;; End: *** |
|
379 */ |