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