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