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