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 |
4513
|
155 Array (T *d, const dim_vector& dims) |
|
156 : rep (new typename Array<T>::ArrayRep (d, get_size (dims))), |
|
157 dimensions (dims), idx (0), idx_count (0) { } |
|
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 |
|
194 Array (const dim_vector& dims) |
|
195 : rep (new typename Array<T>::ArrayRep (get_size (dims))), |
|
196 dimensions (dims), idx (0), idx_count (0) { } |
238
|
197 |
4513
|
198 Array (const dim_vector& dims, const T& val) |
|
199 : rep (new typename Array<T>::ArrayRep (get_size (dims))), |
|
200 dimensions (dims), idx (0), idx_count (0) |
1550
|
201 { |
4513
|
202 fill (val); |
|
203 } |
|
204 |
|
205 Array (const Array<T>& a, const dim_vector& dims) |
|
206 : rep (a.rep), dimensions (dims), idx (0), idx_count (0) |
|
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; |
|
251 |
4513
|
252 static int get_size (int r, int c); |
|
253 static int get_size (int r, int c, int p); |
|
254 static int get_size (const dim_vector& dims); |
228
|
255 |
4517
|
256 int compute_index (const Array<int>& ra_idx) const; |
|
257 |
3665
|
258 T range_error (const char *fcn, int n) const; |
|
259 T& range_error (const char *fcn, int n); |
|
260 |
4513
|
261 T range_error (const char *fcn, int i, int j) const; |
|
262 T& range_error (const char *fcn, int i, int j); |
|
263 |
|
264 T range_error (const char *fcn, int i, int j, int k) const; |
|
265 T& range_error (const char *fcn, int i, int j, int k); |
|
266 |
|
267 T range_error (const char *fcn, const Array<int>& ra_idx) const; |
|
268 T& range_error (const char *fcn, const Array<int>& ra_idx); |
|
269 |
2108
|
270 // No checking, even for multiple references, ever. |
|
271 |
|
272 T& xelem (int n) { return rep->elem (n); } |
|
273 T xelem (int n) const { return rep->elem (n); } |
|
274 |
4513
|
275 T& xelem (int i, int j) { return xelem (dim1()*j+i); } |
|
276 T xelem (int i, int j) const { return xelem (dim1()*j+i); } |
|
277 |
|
278 T& xelem (int i, int j, int k) { return xelem (i, dim2()*k+j); } |
|
279 T xelem (int i, int j, int k) const { return xelem (i, dim2()*k+j); } |
|
280 |
|
281 T& xelem (const Array<int>& ra_idx) |
|
282 { return xelem (compute_index (ra_idx)); } |
|
283 |
|
284 T xelem (const Array<int>& ra_idx) const |
|
285 { return xelem (compute_index (ra_idx)); } |
|
286 |
2006
|
287 // XXX FIXME XXX -- would be nice to fix this so that we don't |
|
288 // unnecessarily force a copy, but that is not so easy, and I see no |
|
289 // clean way to do it. |
|
290 |
2802
|
291 T& checkelem (int n) |
2006
|
292 { |
|
293 if (n < 0 || n >= rep->length ()) |
2109
|
294 return range_error ("T& Array<T>::checkelem", n); |
2006
|
295 else |
2108
|
296 { |
|
297 make_unique (); |
|
298 return xelem (n); |
|
299 } |
2006
|
300 } |
|
301 |
4513
|
302 T& checkelem (int i, int j) |
|
303 { |
|
304 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) |
|
305 return range_error ("T& Array<T>::checkelem", i, j); |
|
306 else |
|
307 return elem (dim1()*j+i); |
|
308 } |
|
309 |
|
310 T& checkelem (int i, int j, int k) |
|
311 { |
|
312 if (i < 0 || j < 0 || k < 0 || i >= dim1 () || j >= dim2 () || k >= dim3 ()) |
|
313 return range_error ("T& Array<T>::checkelem", i, j, k); |
|
314 else |
|
315 return elem (i, dim2()*k+j); |
|
316 } |
|
317 |
|
318 T& checkelem (const Array<int>& ra_idx) |
|
319 { |
|
320 int i = compute_index (ra_idx); |
|
321 |
|
322 if (i < 0) |
|
323 return range_error ("T& Array<T>::checkelem", ra_idx); |
|
324 else |
|
325 return elem (i); |
|
326 } |
|
327 |
2108
|
328 T& elem (int n) |
|
329 { |
|
330 make_unique (); |
2109
|
331 return xelem (n); |
2108
|
332 } |
2306
|
333 |
4513
|
334 T& elem (int i, int j) { return elem (dim1()*j+i); } |
|
335 |
|
336 T& elem (int i, int j, int k) { return elem (i, dim2()*k+j); } |
|
337 |
|
338 T& elem (const Array<int>& ra_idx) |
|
339 { return Array<T>::elem (compute_index (ra_idx)); } |
|
340 |
2306
|
341 #if defined (BOUNDS_CHECKING) |
|
342 T& operator () (int n) { return checkelem (n); } |
4513
|
343 T& operator () (int i, int j) { return checkelem (i, j); } |
|
344 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
|
345 T& operator () (const Array<int>& ra_idx) { return checkelem (ra_idx); } |
2306
|
346 #else |
|
347 T& operator () (int n) { return elem (n); } |
4513
|
348 T& operator () (int i, int j) { return elem (i, j); } |
|
349 T& operator () (int i, int j, int k) { return elem (i, j, k); } |
|
350 T& operator () (const Array<int>& ra_idx) { return elem (ra_idx); } |
2006
|
351 #endif |
|
352 |
2802
|
353 T checkelem (int n) const |
2006
|
354 { |
|
355 if (n < 0 || n >= rep->length ()) |
2109
|
356 return range_error ("T Array<T>::checkelem", n); |
2049
|
357 else |
2108
|
358 return xelem (n); |
2006
|
359 } |
1989
|
360 |
4513
|
361 T checkelem (int i, int j) const |
|
362 { |
|
363 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) |
|
364 return range_error ("T Array<T>::checkelem", i, j); |
|
365 else |
|
366 return elem (dim1()*j+i); |
|
367 } |
|
368 |
|
369 T checkelem (int i, int j, int k) const |
|
370 { |
|
371 if (i < 0 || j < 0 || k < 0 || i >= dim1 () || j >= dim2 () || k >= dim3 ()) |
|
372 return range_error ("T Array<T>::checkelem", i, j, k); |
|
373 else |
|
374 return Array<T>::elem (i, Array<T>::dim1()*k+j); |
|
375 } |
|
376 |
|
377 T checkelem (const Array<int>& ra_idx) const |
|
378 { |
|
379 int i = compute_index (ra_idx); |
|
380 |
|
381 if (i < 0) |
|
382 return range_error ("T Array<T>::checkelem", ra_idx); |
|
383 else |
|
384 return Array<T>::elem (i); |
|
385 } |
|
386 |
2802
|
387 T elem (int n) const { return xelem (n); } |
2306
|
388 |
4513
|
389 T elem (int i, int j) const { return elem (dim1()*j+i); } |
|
390 |
|
391 T elem (int i, int j, int k) const { return elem (i, dim2()*k+j); } |
|
392 |
|
393 T elem (const Array<int>& ra_idx) const |
|
394 { return Array<T>::elem (compute_index (ra_idx)); } |
|
395 |
2108
|
396 #if defined (BOUNDS_CHECKING) |
2802
|
397 T operator () (int n) const { return checkelem (n); } |
4513
|
398 T operator () (int i, int j) const { return checkelem (i, j); } |
|
399 T operator () (int i, int j, int k) const { return checkelem (i, j, k); } |
|
400 T operator () (const Array<int>& ra_idx) const { return checkelem (ra_idx); } |
2006
|
401 #else |
2802
|
402 T operator () (int n) const { return elem (n); } |
4513
|
403 T operator () (int i, int j) const { return elem (i, j); } |
|
404 T operator () (int i, int j, int k) const { return elem (i, j, k); } |
|
405 T operator () (const Array<int>& ra_idx) const { return elem (ra_idx); } |
2006
|
406 #endif |
|
407 |
4567
|
408 Array<T> reshape (const dim_vector& new_dims) const; |
|
409 |
4548
|
410 void resize_no_fill (int n); |
|
411 void resize_and_fill (int n, const T& val); |
|
412 |
4518
|
413 // !!! WARNING !!! -- the following resize_no_fill and |
|
414 // resize_and_fill functions are public because template friends |
|
415 // don't work properly with versions of gcc earlier than 3.3. You |
|
416 // should use these functions only in classes that are derived |
|
417 // from Array<T>. |
|
418 |
|
419 // protected: |
4513
|
420 |
|
421 void resize_no_fill (int r, int c); |
4548
|
422 void resize_and_fill (int r, int c, const T& val); |
4513
|
423 |
|
424 void resize_no_fill (int r, int c, int p); |
4548
|
425 void resize_and_fill (int r, int c, int p, const T& val); |
4513
|
426 |
|
427 void resize_no_fill (const dim_vector& dims); |
|
428 void resize_and_fill (const dim_vector& dims, const T& val); |
|
429 |
|
430 public: |
|
431 |
|
432 void resize (int n) { resize_no_fill (n); } |
|
433 |
|
434 // void resize (int n, const T& val) { resize_and_fill (n, val); } |
|
435 |
|
436 void resize (const dim_vector& dims) { resize_no_fill (dims); } |
|
437 |
|
438 void resize (const dim_vector& dims, const T& val) |
|
439 { resize_and_fill (dims, val); } |
|
440 |
|
441 Array<T>& insert (const Array<T>& a, int r, int c); |
|
442 |
|
443 Array<T>& insert (const Array<T>& a, const Array<int>& dims); |
|
444 |
|
445 bool is_square (void) const { return (dim1 () == dim2 ()); } |
|
446 |
4559
|
447 bool is_empty (void) const { return numel () == 0; } |
|
448 |
4513
|
449 Array<T> transpose (void) const; |
238
|
450 |
1550
|
451 const T *data (void) const { return rep->data; } |
228
|
452 |
3952
|
453 const T *fortran_vec (void) const { return data (); } |
|
454 |
238
|
455 T *fortran_vec (void); |
1560
|
456 |
1781
|
457 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
458 { |
2347
|
459 make_unique (); |
1756
|
460 |
|
461 rep->qsort (compare); |
1781
|
462 |
|
463 return *this; |
1756
|
464 } |
|
465 |
4513
|
466 int ndims (void) const { return dimensions.length (); } |
1560
|
467 |
4517
|
468 void maybe_delete_dims (void); |
|
469 |
1619
|
470 void clear_index (void); |
1560
|
471 |
1619
|
472 void set_index (const idx_vector& i); |
1560
|
473 |
1619
|
474 int index_count (void) const { return idx_count; } |
1560
|
475 |
1619
|
476 idx_vector *get_idx (void) const { return idx; } |
1560
|
477 |
|
478 void maybe_delete_elements (idx_vector& i); |
|
479 |
4513
|
480 void maybe_delete_elements_1 (idx_vector& i); |
|
481 |
|
482 void maybe_delete_elements_2 (idx_vector& i); |
|
483 |
|
484 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
485 |
|
486 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
487 |
|
488 void maybe_delete_elements (Array<idx_vector>& ra_idx, const T& rfv); |
|
489 |
1560
|
490 Array<T> value (void); |
2382
|
491 |
3933
|
492 Array<T> index (idx_vector& i, int resize_ok = 0, |
4459
|
493 const T& rfv = resize_fill_value (T ())) const; |
3933
|
494 |
4513
|
495 Array<T> index1 (idx_vector& i, int resize_ok = 0, |
|
496 const T& rfv = resize_fill_value (T ())) const; |
|
497 |
|
498 Array<T> index2 (idx_vector& i, int resize_ok = 0, |
|
499 const T& rfv = resize_fill_value (T ())) const; |
|
500 |
4530
|
501 Array<T> indexN (idx_vector& i, int resize_ok = 0, |
|
502 const T& rfv = resize_fill_value (T ())) const; |
|
503 |
4513
|
504 Array<T> index (idx_vector& i, idx_vector& j, int resize_ok = 0, |
|
505 const T& rfv = resize_fill_value (T ())) const; |
|
506 |
|
507 Array<T> index (Array<idx_vector>& ra_idx, int resize_ok = 0, |
|
508 const T& rfv = resize_fill_value (T ())) const; |
3928
|
509 |
4459
|
510 // static T resize_fill_value (void) { return T (); } |
3933
|
511 |
4517
|
512 void print_info (std::ostream& os, const std::string& prefix) const; |
4513
|
513 }; |
4459
|
514 |
4518
|
515 // NOTE: these functions should be friends of the Array<T> class and |
|
516 // Array<T>::dimensions should be protected, not public, but we can't |
|
517 // do that because of bugs in gcc prior to 3.3. |
|
518 |
|
519 template <class LT, class RT> |
|
520 /* friend */ int |
|
521 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
522 |
|
523 template <class LT, class RT> |
|
524 /* friend */ int |
|
525 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
526 |
|
527 template <class LT, class RT> |
|
528 /* friend */ int |
|
529 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
530 |
|
531 template <class LT, class RT> |
|
532 /* friend */ int |
|
533 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
|
534 |
3836
|
535 template <class LT, class RT> |
|
536 int |
|
537 assign (Array<LT>& lhs, const Array<RT>& rhs) |
|
538 { |
4459
|
539 return assign (lhs, rhs, resize_fill_value (LT ())); |
3836
|
540 } |
1560
|
541 |
228
|
542 #endif |
|
543 |
|
544 /* |
|
545 ;;; Local Variables: *** |
|
546 ;;; mode: C++ *** |
|
547 ;;; End: *** |
|
548 */ |