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