1993
|
1 // Template array classes |
228
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
228
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
228
|
21 |
|
22 */ |
|
23 |
382
|
24 #if !defined (octave_Array_h) |
|
25 #define octave_Array_h 1 |
|
26 |
4192
|
27 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
28 #pragma interface |
|
29 #endif |
|
30 |
1366
|
31 #include <cassert> |
4152
|
32 #include <cstddef> |
3613
|
33 |
3933
|
34 #include <iostream> |
|
35 |
4513
|
36 #include "dim-vector.h" |
3613
|
37 #include "lo-utils.h" |
228
|
38 |
1560
|
39 class idx_vector; |
|
40 |
1359
|
41 // One dimensional array class. Handles the reference counting for |
|
42 // all the derived classes. |
238
|
43 |
228
|
44 template <class T> |
4459
|
45 T |
|
46 resize_fill_value (const T& x) |
|
47 { |
|
48 return x; |
|
49 } |
|
50 |
|
51 template <class T> |
3585
|
52 class |
|
53 Array |
228
|
54 { |
3504
|
55 protected: |
1619
|
56 |
4513
|
57 //-------------------------------------------------------------------- |
|
58 // The real representation of all arrays. |
|
59 //-------------------------------------------------------------------- |
1735
|
60 |
|
61 class ArrayRep |
|
62 { |
|
63 public: |
|
64 |
|
65 T *data; |
|
66 int len; |
|
67 int count; |
|
68 |
|
69 ArrayRep (T *d, int l) : data (d), len (l), count (1) { } |
|
70 |
|
71 ArrayRep (void) : data (0), len (0), count (1) { } |
|
72 |
3585
|
73 explicit ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
1735
|
74 |
4513
|
75 explicit ArrayRep (int n, const T& val) |
|
76 : data (new T [n]), len (n), count (1) |
|
77 { |
|
78 fill (val); |
|
79 } |
|
80 |
1735
|
81 ArrayRep (const ArrayRep& a) |
|
82 : data (new T [a.len]), len (a.len), count (1) |
4513
|
83 { |
|
84 for (int i = 0; i < len; i++) |
|
85 data[i] = a.data[i]; |
|
86 } |
4517
|
87 |
1735
|
88 ~ArrayRep (void) { delete [] data; } |
|
89 |
|
90 int length (void) const { return len; } |
|
91 |
4513
|
92 void fill (const T& val) |
|
93 { |
|
94 for (int i = 0; i < len; i++) |
|
95 data[i] = val; |
|
96 } |
|
97 |
1735
|
98 T& elem (int n) { return data[n]; } |
|
99 |
|
100 T elem (int n) const { return data[n]; } |
1756
|
101 |
|
102 void qsort (int (*compare) (const void *, const void *)) |
|
103 { |
3613
|
104 octave_qsort (data, static_cast<size_t> (len), sizeof (T), compare); |
1756
|
105 } |
4517
|
106 |
|
107 private: |
|
108 |
|
109 // No assignment! |
|
110 |
|
111 ArrayRep& operator = (const ArrayRep& a); |
1735
|
112 }; |
|
113 |
4513
|
114 //-------------------------------------------------------------------- |
|
115 |
2006
|
116 void make_unique (void) |
|
117 { |
|
118 if (rep->count > 1) |
|
119 { |
|
120 --rep->count; |
|
121 rep = new ArrayRep (*rep); |
|
122 } |
|
123 } |
|
124 |
4513
|
125 void make_unique (const T& val) |
|
126 { |
|
127 if (rep->count > 1) |
|
128 { |
|
129 --rep->count; |
|
130 rep = new ArrayRep (rep->length (), val); |
|
131 } |
|
132 else |
|
133 rep->fill (val); |
|
134 } |
238
|
135 |
4518
|
136 public: |
|
137 |
4902
|
138 // !!! WARNING !!! -- these should be protected, not public. You |
|
139 // should not access these data members directly! |
|
140 |
|
141 typename Array<T>::ArrayRep *rep; |
4518
|
142 |
4513
|
143 dim_vector dimensions; |
|
144 |
4518
|
145 protected: |
|
146 |
4513
|
147 idx_vector *idx; |
|
148 int idx_count; |
|
149 |
|
150 Array (T *d, int n) |
|
151 : rep (new typename Array<T>::ArrayRep (d, n)), dimensions (n), |
|
152 idx (0), idx_count (0) { } |
1619
|
153 |
4587
|
154 Array (T *d, const dim_vector& dv) |
|
155 : rep (new typename Array<T>::ArrayRep (d, get_size (dv))), |
|
156 dimensions (dv), idx (0), idx_count (0) { } |
4513
|
157 |
|
158 private: |
|
159 |
4585
|
160 typename Array<T>::ArrayRep *nil_rep (void) const |
4513
|
161 { |
|
162 static typename Array<T>::ArrayRep *nr |
|
163 = new typename Array<T>::ArrayRep (); |
|
164 |
|
165 return nr; |
1550
|
166 } |
238
|
167 |
4902
|
168 template <class U> |
|
169 T * |
|
170 coerce (const U *a, int len) |
|
171 { |
|
172 T *retval = new T [len]; |
|
173 |
|
174 for (int i = 0; i < len; i++) |
|
175 retval[i] = T (a[i]); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
228
|
180 public: |
238
|
181 |
1550
|
182 Array (void) |
4513
|
183 : rep (nil_rep ()), dimensions (), |
|
184 idx (0), idx_count (0) { rep->count++; } |
1550
|
185 |
3585
|
186 explicit Array (int n) |
4513
|
187 : rep (new typename Array<T>::ArrayRep (n)), dimensions (n), |
|
188 idx (0), idx_count (0) { } |
1619
|
189 |
4513
|
190 explicit Array (int n, const T& val) |
|
191 : rep (new typename Array<T>::ArrayRep (n)), dimensions (n), |
|
192 idx (0), idx_count (0) |
|
193 { |
|
194 fill (val); |
|
195 } |
|
196 |
4902
|
197 // Type conversion case. |
|
198 template <class U> |
|
199 Array (const Array<U>& a) |
|
200 : rep (new typename Array<T>::ArrayRep (coerce (a.data (), a.length ()), a.length ())), |
|
201 dimensions (a.dimensions), idx (0), idx_count (0) |
|
202 { |
|
203 } |
|
204 |
|
205 // No type conversion case. |
4513
|
206 Array (const Array<T>& a) |
|
207 : rep (a.rep), dimensions (a.dimensions), idx (0), idx_count (0) |
|
208 { |
|
209 rep->count++; |
1550
|
210 } |
|
211 |
4513
|
212 public: |
|
213 |
4587
|
214 Array (const dim_vector& dv) |
|
215 : rep (new typename Array<T>::ArrayRep (get_size (dv))), |
|
216 dimensions (dv), idx (0), idx_count (0) { } |
238
|
217 |
4587
|
218 Array (const dim_vector& dv, const T& val) |
|
219 : rep (new typename Array<T>::ArrayRep (get_size (dv))), |
|
220 dimensions (dv), idx (0), idx_count (0) |
1550
|
221 { |
4513
|
222 fill (val); |
|
223 } |
|
224 |
4834
|
225 Array (const Array<T>& a, const dim_vector& dv); |
228
|
226 |
1619
|
227 ~Array (void); |
228
|
228 |
4513
|
229 Array<T>& operator = (const Array<T>& a) |
|
230 { |
|
231 if (this != &a) |
|
232 { |
|
233 if (--rep->count <= 0) |
|
234 delete rep; |
|
235 |
|
236 rep = a.rep; |
|
237 rep->count++; |
|
238 |
|
239 dimensions = a.dimensions; |
|
240 } |
|
241 |
|
242 idx_count = 0; |
|
243 idx = 0; |
|
244 |
|
245 return *this; |
|
246 } |
|
247 |
|
248 void fill (const T& val) { make_unique (val); } |
238
|
249 |
1550
|
250 int capacity (void) const { return rep->length (); } |
4513
|
251 int length (void) const { return capacity (); } |
|
252 int nelem (void) const { return capacity (); } |
4559
|
253 int numel (void) const { return nelem (); } |
4513
|
254 |
|
255 int dim1 (void) const { return dimensions(0); } |
|
256 int dim2 (void) const { return dimensions(1); } |
|
257 int dim3 (void) const { return dimensions(2); } |
|
258 |
|
259 int rows (void) const { return dim1 (); } |
|
260 int cols (void) const { return dim2 (); } |
|
261 int columns (void) const { return dim2 (); } |
|
262 int pages (void) const { return dim3 (); } |
|
263 |
4902
|
264 size_t byte_size (void) const { return numel () * sizeof (T); } |
|
265 |
4513
|
266 dim_vector dims (void) const { return dimensions; } |
|
267 |
4532
|
268 Array<T> squeeze (void) const; |
4703
|
269 |
|
270 void chop_trailing_singletons (void) |
|
271 { dimensions.chop_trailing_singletons (); } |
|
272 |
4513
|
273 static int get_size (int r, int c); |
|
274 static int get_size (int r, int c, int p); |
4587
|
275 static int get_size (const dim_vector& dv); |
228
|
276 |
4517
|
277 int compute_index (const Array<int>& ra_idx) const; |
|
278 |
3665
|
279 T range_error (const char *fcn, int n) const; |
|
280 T& range_error (const char *fcn, int n); |
|
281 |
4513
|
282 T range_error (const char *fcn, int i, int j) const; |
|
283 T& range_error (const char *fcn, int i, int j); |
|
284 |
|
285 T range_error (const char *fcn, int i, int j, int k) const; |
|
286 T& range_error (const char *fcn, int i, int j, int k); |
|
287 |
|
288 T range_error (const char *fcn, const Array<int>& ra_idx) const; |
|
289 T& range_error (const char *fcn, const Array<int>& ra_idx); |
|
290 |
2108
|
291 // No checking, even for multiple references, ever. |
|
292 |
|
293 T& xelem (int n) { return rep->elem (n); } |
|
294 T xelem (int n) const { return rep->elem (n); } |
|
295 |
4513
|
296 T& xelem (int i, int j) { return xelem (dim1()*j+i); } |
|
297 T xelem (int i, int j) const { return xelem (dim1()*j+i); } |
|
298 |
|
299 T& xelem (int i, int j, int k) { return xelem (i, dim2()*k+j); } |
|
300 T xelem (int i, int j, int k) const { return xelem (i, dim2()*k+j); } |
|
301 |
|
302 T& xelem (const Array<int>& ra_idx) |
|
303 { return xelem (compute_index (ra_idx)); } |
|
304 |
|
305 T xelem (const Array<int>& ra_idx) const |
|
306 { return xelem (compute_index (ra_idx)); } |
|
307 |
2006
|
308 // XXX FIXME XXX -- would be nice to fix this so that we don't |
|
309 // unnecessarily force a copy, but that is not so easy, and I see no |
|
310 // clean way to do it. |
|
311 |
2802
|
312 T& checkelem (int n) |
2006
|
313 { |
|
314 if (n < 0 || n >= rep->length ()) |
2109
|
315 return range_error ("T& Array<T>::checkelem", n); |
2006
|
316 else |
2108
|
317 { |
|
318 make_unique (); |
|
319 return xelem (n); |
|
320 } |
2006
|
321 } |
|
322 |
4513
|
323 T& checkelem (int i, int j) |
|
324 { |
|
325 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) |
|
326 return range_error ("T& Array<T>::checkelem", i, j); |
|
327 else |
|
328 return elem (dim1()*j+i); |
|
329 } |
|
330 |
|
331 T& checkelem (int i, int j, int k) |
|
332 { |
|
333 if (i < 0 || j < 0 || k < 0 || i >= dim1 () || j >= dim2 () || k >= dim3 ()) |
|
334 return range_error ("T& Array<T>::checkelem", i, j, k); |
|
335 else |
|
336 return elem (i, dim2()*k+j); |
|
337 } |
|
338 |
|
339 T& checkelem (const Array<int>& ra_idx) |
|
340 { |
|
341 int i = compute_index (ra_idx); |
|
342 |
|
343 if (i < 0) |
|
344 return range_error ("T& Array<T>::checkelem", ra_idx); |
|
345 else |
|
346 return elem (i); |
|
347 } |
|
348 |
2108
|
349 T& elem (int n) |
|
350 { |
|
351 make_unique (); |
2109
|
352 return xelem (n); |
2108
|
353 } |
2306
|
354 |
4513
|
355 T& elem (int i, int j) { return elem (dim1()*j+i); } |
|
356 |
|
357 T& elem (int i, int j, int k) { return elem (i, dim2()*k+j); } |
|
358 |
|
359 T& elem (const Array<int>& ra_idx) |
|
360 { return Array<T>::elem (compute_index (ra_idx)); } |
|
361 |
2306
|
362 #if defined (BOUNDS_CHECKING) |
|
363 T& operator () (int n) { return checkelem (n); } |
4513
|
364 T& operator () (int i, int j) { return checkelem (i, j); } |
|
365 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
|
366 T& operator () (const Array<int>& ra_idx) { return checkelem (ra_idx); } |
2306
|
367 #else |
|
368 T& operator () (int n) { return elem (n); } |
4513
|
369 T& operator () (int i, int j) { return elem (i, j); } |
|
370 T& operator () (int i, int j, int k) { return elem (i, j, k); } |
|
371 T& operator () (const Array<int>& ra_idx) { return elem (ra_idx); } |
2006
|
372 #endif |
|
373 |
2802
|
374 T checkelem (int n) const |
2006
|
375 { |
|
376 if (n < 0 || n >= rep->length ()) |
2109
|
377 return range_error ("T Array<T>::checkelem", n); |
2049
|
378 else |
2108
|
379 return xelem (n); |
2006
|
380 } |
1989
|
381 |
4513
|
382 T checkelem (int i, int j) const |
|
383 { |
|
384 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) |
|
385 return range_error ("T Array<T>::checkelem", i, j); |
|
386 else |
|
387 return elem (dim1()*j+i); |
|
388 } |
|
389 |
|
390 T checkelem (int i, int j, int k) const |
|
391 { |
|
392 if (i < 0 || j < 0 || k < 0 || i >= dim1 () || j >= dim2 () || k >= dim3 ()) |
|
393 return range_error ("T Array<T>::checkelem", i, j, k); |
|
394 else |
|
395 return Array<T>::elem (i, Array<T>::dim1()*k+j); |
|
396 } |
|
397 |
|
398 T checkelem (const Array<int>& ra_idx) const |
|
399 { |
|
400 int i = compute_index (ra_idx); |
|
401 |
|
402 if (i < 0) |
|
403 return range_error ("T Array<T>::checkelem", ra_idx); |
|
404 else |
|
405 return Array<T>::elem (i); |
|
406 } |
|
407 |
2802
|
408 T elem (int n) const { return xelem (n); } |
2306
|
409 |
4513
|
410 T elem (int i, int j) const { return elem (dim1()*j+i); } |
|
411 |
|
412 T elem (int i, int j, int k) const { return elem (i, dim2()*k+j); } |
|
413 |
|
414 T elem (const Array<int>& ra_idx) const |
|
415 { return Array<T>::elem (compute_index (ra_idx)); } |
|
416 |
2108
|
417 #if defined (BOUNDS_CHECKING) |
2802
|
418 T operator () (int n) const { return checkelem (n); } |
4513
|
419 T operator () (int i, int j) const { return checkelem (i, j); } |
|
420 T operator () (int i, int j, int k) const { return checkelem (i, j, k); } |
|
421 T operator () (const Array<int>& ra_idx) const { return checkelem (ra_idx); } |
2006
|
422 #else |
2802
|
423 T operator () (int n) const { return elem (n); } |
4513
|
424 T operator () (int i, int j) const { return elem (i, j); } |
|
425 T operator () (int i, int j, int k) const { return elem (i, j, k); } |
|
426 T operator () (const Array<int>& ra_idx) const { return elem (ra_idx); } |
2006
|
427 #endif |
|
428 |
4567
|
429 Array<T> reshape (const dim_vector& new_dims) const; |
|
430 |
4593
|
431 Array<T> permute (const Array<int>& vec, bool inv = false) const; |
|
432 Array<T> ipermute (const Array<int>& vec) const |
|
433 { return permute (vec, true); } |
|
434 |
4548
|
435 void resize_no_fill (int n); |
|
436 void resize_and_fill (int n, const T& val); |
|
437 |
4518
|
438 // !!! WARNING !!! -- the following resize_no_fill and |
|
439 // resize_and_fill functions are public because template friends |
|
440 // don't work properly with versions of gcc earlier than 3.3. You |
|
441 // should use these functions only in classes that are derived |
|
442 // from Array<T>. |
|
443 |
|
444 // protected: |
4513
|
445 |
|
446 void resize_no_fill (int r, int c); |
4548
|
447 void resize_and_fill (int r, int c, const T& val); |
4513
|
448 |
|
449 void resize_no_fill (int r, int c, int p); |
4548
|
450 void resize_and_fill (int r, int c, int p, const T& val); |
4513
|
451 |
4587
|
452 void resize_no_fill (const dim_vector& dv); |
|
453 void resize_and_fill (const dim_vector& dv, const T& val); |
4513
|
454 |
|
455 public: |
|
456 |
|
457 void resize (int n) { resize_no_fill (n); } |
|
458 |
4625
|
459 void resize (int n, const T& val) { resize_and_fill (n, val); } |
4513
|
460 |
4587
|
461 void resize (const dim_vector& dv) { resize_no_fill (dv); } |
4513
|
462 |
4587
|
463 void resize (const dim_vector& dv, const T& val) |
|
464 { resize_and_fill (dv, val); } |
4513
|
465 |
|
466 Array<T>& insert (const Array<T>& a, int r, int c); |
4786
|
467 Array<T>& insert2 (const Array<T>& a, int r, int c); |
|
468 Array<T>& insertN (const Array<T>& a, int r, int c); |
4513
|
469 |
4786
|
470 Array<T>& insert (const Array<T>& a, const Array<int>& idx); |
4513
|
471 |
|
472 bool is_square (void) const { return (dim1 () == dim2 ()); } |
|
473 |
4559
|
474 bool is_empty (void) const { return numel () == 0; } |
|
475 |
4513
|
476 Array<T> transpose (void) const; |
238
|
477 |
1550
|
478 const T *data (void) const { return rep->data; } |
228
|
479 |
3952
|
480 const T *fortran_vec (void) const { return data (); } |
|
481 |
238
|
482 T *fortran_vec (void); |
1560
|
483 |
1781
|
484 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
485 { |
2347
|
486 make_unique (); |
1756
|
487 |
|
488 rep->qsort (compare); |
1781
|
489 |
|
490 return *this; |
1756
|
491 } |
|
492 |
4513
|
493 int ndims (void) const { return dimensions.length (); } |
1560
|
494 |
4517
|
495 void maybe_delete_dims (void); |
|
496 |
1619
|
497 void clear_index (void); |
1560
|
498 |
1619
|
499 void set_index (const idx_vector& i); |
1560
|
500 |
1619
|
501 int index_count (void) const { return idx_count; } |
1560
|
502 |
1619
|
503 idx_vector *get_idx (void) const { return idx; } |
1560
|
504 |
|
505 void maybe_delete_elements (idx_vector& i); |
|
506 |
4513
|
507 void maybe_delete_elements_1 (idx_vector& i); |
|
508 |
|
509 void maybe_delete_elements_2 (idx_vector& i); |
|
510 |
|
511 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
512 |
|
513 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
514 |
|
515 void maybe_delete_elements (Array<idx_vector>& ra_idx, const T& rfv); |
|
516 |
1560
|
517 Array<T> value (void); |
2382
|
518 |
3933
|
519 Array<T> index (idx_vector& i, int resize_ok = 0, |
4459
|
520 const T& rfv = resize_fill_value (T ())) const; |
3933
|
521 |
4513
|
522 Array<T> index1 (idx_vector& i, int resize_ok = 0, |
|
523 const T& rfv = resize_fill_value (T ())) const; |
|
524 |
|
525 Array<T> index2 (idx_vector& i, int resize_ok = 0, |
|
526 const T& rfv = resize_fill_value (T ())) const; |
|
527 |
4530
|
528 Array<T> indexN (idx_vector& i, int resize_ok = 0, |
|
529 const T& rfv = resize_fill_value (T ())) const; |
|
530 |
4513
|
531 Array<T> index (idx_vector& i, idx_vector& j, int resize_ok = 0, |
|
532 const T& rfv = resize_fill_value (T ())) const; |
|
533 |
|
534 Array<T> index (Array<idx_vector>& ra_idx, int resize_ok = 0, |
|
535 const T& rfv = resize_fill_value (T ())) const; |
3928
|
536 |
4459
|
537 // static T resize_fill_value (void) { return T (); } |
3933
|
538 |
4517
|
539 void print_info (std::ostream& os, const std::string& prefix) const; |
4513
|
540 }; |
4459
|
541 |
4518
|
542 // NOTE: these functions should be friends of the Array<T> class and |
|
543 // Array<T>::dimensions should be protected, not public, but we can't |
|
544 // do that because of bugs in gcc prior to 3.3. |
|
545 |
|
546 template <class LT, class RT> |
|
547 /* friend */ int |
|
548 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
549 |
|
550 template <class LT, class RT> |
|
551 /* friend */ int |
|
552 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
553 |
|
554 template <class LT, class RT> |
|
555 /* friend */ int |
|
556 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
557 |
|
558 template <class LT, class RT> |
|
559 /* friend */ int |
|
560 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
561 |
3836
|
562 template <class LT, class RT> |
|
563 int |
|
564 assign (Array<LT>& lhs, const Array<RT>& rhs) |
|
565 { |
4459
|
566 return assign (lhs, rhs, resize_fill_value (LT ())); |
3836
|
567 } |
1560
|
568 |
4594
|
569 #define INSTANTIATE_ARRAY_ASSIGN(LT, RT) \ |
|
570 template int assign (Array<LT>&, const Array<RT>&, const LT&); \ |
|
571 template int assign1 (Array<LT>&, const Array<RT>&, const LT&); \ |
|
572 template int assign2 (Array<LT>&, const Array<RT>&, const LT&); \ |
|
573 template int assignN (Array<LT>&, const Array<RT>&, const LT&); \ |
|
574 template int assign (Array<LT>&, const Array<RT>&) |
|
575 |
4762
|
576 |
4594
|
577 #define INSTANTIATE_ARRAY(T) \ |
|
578 template class Array<T>; \ |
4762
|
579 template T resize_fill_value (const T&); \ |
|
580 |
4594
|
581 #define INSTANTIATE_ARRAY_AND_ASSIGN(T) \ |
|
582 INSTANTIATE_ARRAY (T); \ |
|
583 INSTANTIATE_ARRAY_ASSIGN (T, T) |
|
584 |
228
|
585 #endif |
|
586 |
|
587 /* |
|
588 ;;; Local Variables: *** |
|
589 ;;; mode: C++ *** |
|
590 ;;; End: *** |
|
591 */ |