228
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
1296
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
1560
|
31 #define HEAVYWEIGHT_INDEXING 1 |
|
32 |
1366
|
33 #include <cassert> |
1756
|
34 #include <cstdlib> |
228
|
35 |
448
|
36 #include "lo-error.h" |
354
|
37 |
1560
|
38 class idx_vector; |
|
39 |
238
|
40 // Classes we declare. |
228
|
41 |
238
|
42 template <class T> class Array; |
|
43 template <class T> class Array2; |
|
44 template <class T> class Array3; |
1574
|
45 |
|
46 #ifndef NO_DIAG_ARRAY |
238
|
47 template <class T> class DiagArray; |
1574
|
48 #endif |
238
|
49 |
1359
|
50 // One dimensional array class. Handles the reference counting for |
|
51 // all the derived classes. |
238
|
52 |
228
|
53 template <class T> |
|
54 class Array |
|
55 { |
1619
|
56 private: |
|
57 |
1735
|
58 // The real representation of all arrays. |
|
59 |
|
60 class ArrayRep |
|
61 { |
|
62 public: |
|
63 |
|
64 T *data; |
|
65 int len; |
|
66 int count; |
|
67 |
|
68 ArrayRep& operator = (const ArrayRep& a); |
|
69 |
|
70 ArrayRep (T *d, int l) : data (d), len (l), count (1) { } |
|
71 |
|
72 ArrayRep (void) : data (0), len (0), count (1) { } |
|
73 |
|
74 ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
|
75 |
|
76 ArrayRep (const ArrayRep& a) |
|
77 : data (new T [a.len]), len (a.len), count (1) |
|
78 { |
|
79 for (int i = 0; i < len; i++) |
|
80 data[i] = a.data[i]; |
|
81 } |
|
82 |
|
83 ~ArrayRep (void) { delete [] data; } |
|
84 |
|
85 int length (void) const { return len; } |
|
86 |
|
87 T& elem (int n) { return data[n]; } |
|
88 |
|
89 T elem (int n) const { return data[n]; } |
1756
|
90 |
|
91 void qsort (int (*compare) (const void *, const void *)) |
|
92 { |
|
93 ::qsort (data, len, sizeof (T), compare); |
|
94 } |
1735
|
95 }; |
|
96 |
1619
|
97 #ifdef HEAVYWEIGHT_INDEXING |
|
98 idx_vector *idx; |
|
99 int max_indices; |
|
100 int idx_count; |
|
101 #endif |
|
102 |
238
|
103 protected: |
|
104 |
1735
|
105 ArrayRep *rep; |
238
|
106 |
1550
|
107 Array (T *d, int l) |
|
108 { |
1735
|
109 rep = new ArrayRep (d, l); |
1619
|
110 |
|
111 #ifdef HEAVYWEIGHT_INDEXING |
|
112 idx = 0; |
|
113 max_indices = 1; |
|
114 idx_count = 0; |
|
115 #endif |
1550
|
116 } |
238
|
117 |
228
|
118 public: |
238
|
119 |
1550
|
120 Array (void) |
|
121 { |
1735
|
122 rep = new ArrayRep (); |
1619
|
123 |
|
124 #ifdef HEAVYWEIGHT_INDEXING |
|
125 idx = 0; |
|
126 max_indices = 1; |
|
127 idx_count = 0; |
|
128 #endif |
1550
|
129 } |
|
130 |
|
131 Array (int n) |
|
132 { |
1735
|
133 rep = new ArrayRep (n); |
1619
|
134 |
|
135 #ifdef HEAVYWEIGHT_INDEXING |
|
136 idx = 0; |
|
137 max_indices = 1; |
|
138 idx_count = 0; |
|
139 #endif |
1550
|
140 } |
|
141 |
238
|
142 Array (int n, const T& val); |
|
143 |
1550
|
144 Array (const Array<T>& a) |
|
145 { |
|
146 rep = a.rep; |
|
147 rep->count++; |
1619
|
148 |
|
149 #ifdef HEAVYWEIGHT_INDEXING |
|
150 max_indices = a.max_indices; |
|
151 idx_count = 0; |
|
152 idx = 0; |
|
153 #endif |
1550
|
154 } |
228
|
155 |
1619
|
156 ~Array (void); |
228
|
157 |
|
158 Array<T>& operator = (const Array<T>& a); |
238
|
159 |
1550
|
160 int capacity (void) const { return rep->length (); } |
|
161 int length (void) const { return rep->length (); } |
228
|
162 |
1550
|
163 T& elem (int n) |
|
164 { |
|
165 if (rep->count > 1) |
|
166 { |
|
167 --rep->count; |
1735
|
168 rep = new ArrayRep (*rep); |
1550
|
169 } |
|
170 return rep->elem (n); |
|
171 } |
|
172 |
228
|
173 T& checkelem (int n); |
1550
|
174 T& operator () (int n) { return checkelem (n); } |
228
|
175 |
1359
|
176 // No checking. |
|
177 |
1550
|
178 T& xelem (int n) { return rep->elem (n); } |
238
|
179 |
228
|
180 T elem (int n) const; |
|
181 T checkelem (int n) const; |
|
182 T operator () (int n) const; |
|
183 |
238
|
184 void resize (int n); |
|
185 void resize (int n, const T& val); |
|
186 |
1550
|
187 const T *data (void) const { return rep->data; } |
228
|
188 |
238
|
189 T *fortran_vec (void); |
1560
|
190 |
1781
|
191 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
192 { |
|
193 if (rep->count > 1) |
|
194 { |
|
195 --rep->count; |
|
196 rep = new ArrayRep (*rep); |
|
197 } |
|
198 |
|
199 rep->qsort (compare); |
1781
|
200 |
|
201 return *this; |
1756
|
202 } |
|
203 |
1560
|
204 #ifdef HEAVYWEIGHT_INDEXING |
1619
|
205 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
206 |
1619
|
207 void clear_index (void); |
1560
|
208 |
1619
|
209 void set_index (const idx_vector& i); |
1560
|
210 |
1619
|
211 int index_count (void) const { return idx_count; } |
1560
|
212 |
1619
|
213 idx_vector *get_idx (void) const { return idx; } |
1560
|
214 |
|
215 void maybe_delete_elements (idx_vector& i); |
|
216 |
|
217 Array<T> value (void); |
|
218 #endif |
228
|
219 }; |
|
220 |
1560
|
221 template <class LT, class RT> |
|
222 int assign (Array<LT>& lhs, const Array<RT>& rhs); |
|
223 |
1359
|
224 // Two dimensional array class. |
238
|
225 |
228
|
226 template <class T> |
|
227 class Array2 : public Array<T> |
|
228 { |
238
|
229 protected: |
|
230 |
1550
|
231 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
232 { |
|
233 d1 = n; |
|
234 d2 = m; |
1560
|
235 set_max_indices (2); |
1550
|
236 } |
238
|
237 |
228
|
238 public: |
|
239 |
1560
|
240 // These really need to be protected (and they will be in the |
|
241 // future, so don't depend on them being here!), but they can't be |
|
242 // until template friends work correctly in g++. |
|
243 |
|
244 int d1; |
|
245 int d2; |
|
246 |
1550
|
247 Array2 (void) : Array<T> () |
|
248 { |
|
249 d1 = 0; |
|
250 d2 = 0; |
1560
|
251 set_max_indices (2); |
1550
|
252 } |
|
253 |
|
254 Array2 (int n, int m) : Array<T> (n*m) |
|
255 { |
|
256 d1 = n; |
|
257 d2 = m; |
1560
|
258 set_max_indices (2); |
1550
|
259 } |
|
260 |
|
261 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
262 { |
|
263 d1 = n; |
|
264 d2 = m; |
1560
|
265 set_max_indices (2); |
1550
|
266 } |
|
267 |
|
268 Array2 (const Array2<T>& a) : Array<T> (a) |
|
269 { |
|
270 d1 = a.d1; |
|
271 d2 = a.d2; |
1560
|
272 set_max_indices (2); |
1550
|
273 } |
|
274 |
1560
|
275 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
276 { |
|
277 d1 = n; |
|
278 d2 = m; |
|
279 set_max_indices (2); |
|
280 } |
|
281 |
1574
|
282 #ifndef NO_DIAG_ARRAY |
1560
|
283 Array2 (const DiagArray<T>& a) : Array<T> (a.rows () * a.cols (), T (0)) |
1550
|
284 { |
|
285 for (int i = 0; i < a.length (); i++) |
|
286 elem (i, i) = a.elem (i, i); |
1560
|
287 |
|
288 set_max_indices (2); |
1550
|
289 } |
1574
|
290 #endif |
228
|
291 |
1230
|
292 ~Array2 (void) { } |
|
293 |
1550
|
294 Array2<T>& operator = (const Array2<T>& a) |
|
295 { |
1701
|
296 if (this != &a && rep != a.rep) |
1550
|
297 { |
|
298 Array<T>::operator = (a); |
|
299 d1 = a.d1; |
|
300 d2 = a.d2; |
|
301 } |
238
|
302 |
1550
|
303 return *this; |
|
304 } |
|
305 |
|
306 int dim1 (void) const { return d1; } |
|
307 int dim2 (void) const { return d2; } |
238
|
308 |
1550
|
309 int rows (void) const { return d1; } |
|
310 int cols (void) const { return d2; } |
|
311 int columns (void) const { return d2; } |
|
312 |
|
313 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
238
|
314 T& checkelem (int i, int j); |
1550
|
315 T& operator () (int i, int j) { return checkelem (i, j); } |
238
|
316 |
1359
|
317 // No checking. |
|
318 |
1550
|
319 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
238
|
320 |
228
|
321 T elem (int i, int j) const; |
|
322 T checkelem (int i, int j) const; |
|
323 T operator () (int i, int j) const; |
|
324 |
238
|
325 void resize (int n, int m); |
|
326 void resize (int n, int m, const T& val); |
1560
|
327 |
1561
|
328 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
329 |
1560
|
330 #ifdef HEAVYWEIGHT_INDEXING |
|
331 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
332 |
|
333 Array2<T> value (void); |
|
334 #endif |
228
|
335 }; |
|
336 |
1560
|
337 template <class LT, class RT> |
|
338 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
339 |
1359
|
340 // Three dimensional array class. |
238
|
341 |
228
|
342 template <class T> |
|
343 class Array3 : public Array2<T> |
|
344 { |
238
|
345 protected: |
|
346 |
|
347 int d3; |
|
348 |
1550
|
349 Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
350 { |
|
351 d2 = m; |
|
352 d3 = k; |
1560
|
353 set_max_indices (3); |
1550
|
354 } |
238
|
355 |
228
|
356 public: |
|
357 |
1550
|
358 Array3 (void) : Array2<T> () |
|
359 { |
|
360 d2 = 0; |
|
361 d3 = 0; |
1560
|
362 set_max_indices (3); |
1550
|
363 } |
|
364 |
|
365 Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
366 { |
|
367 d2 = m; |
|
368 d3 = k; |
1560
|
369 set_max_indices (3); |
1550
|
370 } |
|
371 |
|
372 Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
373 { |
|
374 d2 = m; |
|
375 d3 = k; |
1560
|
376 set_max_indices (3); |
1550
|
377 } |
|
378 |
|
379 Array3 (const Array3<T>& a) : Array2<T> (a) |
|
380 { |
|
381 d2 = a.d2; |
|
382 d3 = a.d3; |
1560
|
383 set_max_indices (3); |
1550
|
384 } |
228
|
385 |
1230
|
386 ~Array3 (void) { } |
|
387 |
1550
|
388 Array3<T>& operator = (const Array3<T>& a) |
|
389 { |
1701
|
390 if (this != &a && rep != a.rep) |
1550
|
391 { |
|
392 Array<T>::operator = (a); |
|
393 d1 = a.d1; |
|
394 d2 = a.d2; |
|
395 d3 = a.d3; |
|
396 } |
228
|
397 |
1550
|
398 return *this; |
|
399 } |
|
400 |
|
401 int dim3 (void) const { return d3; } |
|
402 |
|
403 T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); } |
238
|
404 T& checkelem (int i, int j, int k); |
1550
|
405 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
238
|
406 |
1359
|
407 // No checking. |
|
408 |
1550
|
409 T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); } |
238
|
410 |
228
|
411 T elem (int i, int j, int k) const; |
238
|
412 T checkelem (int i, int j, int k) const; |
|
413 T operator () (int i, int j, int k) const; |
228
|
414 |
238
|
415 void resize (int n, int m, int k); |
|
416 void resize (int n, int m, int k, const T& val); |
1560
|
417 |
|
418 #ifdef HEAVYWEIGHT_INDEXING |
|
419 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
420 |
|
421 Array3<T> value (void); |
|
422 #endif |
228
|
423 }; |
|
424 |
1560
|
425 template <class LT, class RT> |
|
426 int assign (Array3<LT>& lhs, const Array3<RT>& rhs); |
|
427 |
1359
|
428 // A two-dimensional array with diagonal elements only. |
|
429 // |
|
430 // Idea and example code for Proxy class and functions from: |
|
431 // |
|
432 // From: kanze@us-es.sel.de (James Kanze) |
|
433 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
434 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
435 // Sender: news@us-es.sel.de |
|
436 // Date: 29 Nov 1993 14:14:07 GMT |
|
437 // -- |
|
438 // James Kanze email: kanze@us-es.sel.de |
|
439 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
440 |
1574
|
441 #ifndef NO_DIAG_ARRAY |
228
|
442 template <class T> |
|
443 class DiagArray : public Array<T> |
|
444 { |
366
|
445 private: |
1560
|
446 T get (int i) { return Array<T>::elem (i); } |
|
447 void set (const T& val, int i) { Array<T>::elem (i) = val; } |
366
|
448 |
319
|
449 class Proxy |
|
450 { |
|
451 public: |
|
452 |
1560
|
453 Proxy (DiagArray<T> *ref, int r, int c) |
645
|
454 : i (r), j (c), object (ref) { } |
319
|
455 |
1560
|
456 const Proxy& operator = (const T& val) const |
|
457 { |
|
458 if (i == j) |
|
459 { |
|
460 if (object) |
|
461 object->set (val, i); |
|
462 } |
|
463 else |
|
464 (*current_liboctave_error_handler) ("invalid assignment to off-diagonal in diagonal array"); |
319
|
465 |
1560
|
466 return *this; |
|
467 } |
319
|
468 |
1560
|
469 operator T () const |
|
470 { |
|
471 if (object && i == j) |
|
472 return object->get (i); |
|
473 else |
|
474 { |
|
475 static T foo (0); |
|
476 return foo; |
|
477 } |
|
478 } |
319
|
479 |
|
480 private: |
|
481 |
1359
|
482 // XXX FIXME XXX -- this is declared private to keep the user from |
|
483 // taking the address of a Proxy. Maybe it should be implemented |
|
484 // by means of a companion function in the DiagArray class. |
319
|
485 |
1560
|
486 T *operator& () const { assert (0); return (T *) 0; } |
319
|
487 |
|
488 int i; |
|
489 int j; |
|
490 |
|
491 DiagArray<T> *object; |
|
492 |
|
493 }; |
|
494 |
|
495 friend class Proxy; |
|
496 |
238
|
497 protected: |
|
498 |
|
499 int nr; |
|
500 int nc; |
|
501 |
1550
|
502 DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
503 { |
|
504 nr = r; |
|
505 nc = c; |
1560
|
506 set_max_indices (2); |
1550
|
507 } |
238
|
508 |
228
|
509 public: |
238
|
510 |
1550
|
511 DiagArray (void) : Array<T> () |
|
512 { |
|
513 nr = 0; |
|
514 nc = 0; |
1560
|
515 set_max_indices (2); |
1550
|
516 } |
|
517 |
|
518 DiagArray (int n) : Array<T> (n) |
1560
|
519 { |
|
520 nr = n; |
|
521 nc = n; |
|
522 set_max_indices (2); |
|
523 } |
1550
|
524 |
|
525 DiagArray (int n, const T& val) : Array<T> (n, val) |
1560
|
526 { |
|
527 nr = n; |
|
528 nc = n; |
|
529 set_max_indices (2); |
|
530 } |
1550
|
531 |
|
532 DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
1560
|
533 { |
|
534 nr = r; |
|
535 nc = c; |
|
536 set_max_indices (2); |
|
537 } |
1550
|
538 |
|
539 DiagArray (int r, int c, const T& val) : Array<T> (r < c ? r : c, val) |
1560
|
540 { |
|
541 nr = r; |
|
542 nc = c; |
|
543 set_max_indices (2); |
|
544 } |
1550
|
545 |
|
546 DiagArray (const Array<T>& a) : Array<T> (a) |
1560
|
547 { |
|
548 nr = nc = a.length (); |
|
549 set_max_indices (2); |
|
550 } |
1550
|
551 |
|
552 DiagArray (const DiagArray<T>& a) : Array<T> (a) |
1560
|
553 { |
|
554 nr = a.nr; |
|
555 nc = a.nc; |
|
556 set_max_indices (2); |
|
557 } |
228
|
558 |
1230
|
559 ~DiagArray (void) { } |
|
560 |
1550
|
561 DiagArray<T>& operator = (const DiagArray<T>& a) |
|
562 { |
1560
|
563 if (this != &a) |
|
564 { |
|
565 Array<T>::operator = (a); |
|
566 nr = a.nr; |
|
567 nc = a.nc; |
|
568 } |
|
569 |
|
570 return *this; |
1550
|
571 } |
228
|
572 |
1550
|
573 int dim1 (void) const { return nr; } |
|
574 int dim2 (void) const { return nc; } |
|
575 |
|
576 int rows (void) const { return nr; } |
|
577 int cols (void) const { return nc; } |
|
578 int columns (void) const { return nc; } |
228
|
579 |
880
|
580 #if 0 |
1560
|
581 Proxy elem (int r, int c) |
|
582 { |
|
583 return Proxy (this, r, c); |
|
584 } |
319
|
585 |
1560
|
586 Proxy checkelem (int r, int c) |
|
587 { |
|
588 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
589 { |
|
590 (*current_liboctave_error_handler) ("range error"); |
|
591 return Proxy (0, r, c); |
|
592 } |
|
593 else |
|
594 return Proxy (this, r, c); |
|
595 } |
319
|
596 |
1560
|
597 Proxy operator () (int r, int c) |
|
598 { |
|
599 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
600 { |
|
601 (*current_liboctave_error_handler) ("range error"); |
|
602 return Proxy (0, r, c); |
|
603 } |
|
604 else |
|
605 return Proxy (this, r, c); |
319
|
606 } |
880
|
607 #else |
|
608 T& elem (int r, int c); |
|
609 T& checkelem (int r, int c); |
|
610 T& operator () (int r, int c); |
344
|
611 #endif |
228
|
612 |
1359
|
613 // No checking. |
|
614 |
238
|
615 T& xelem (int r, int c); |
|
616 |
228
|
617 T elem (int r, int c) const; |
|
618 T checkelem (int r, int c) const; |
|
619 T operator () (int r, int c) const; |
|
620 |
238
|
621 void resize (int n, int m); |
|
622 void resize (int n, int m, const T& val); |
1560
|
623 |
|
624 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
228
|
625 }; |
1574
|
626 #endif |
228
|
627 |
|
628 #endif |
|
629 |
|
630 /* |
|
631 ;;; Local Variables: *** |
|
632 ;;; mode: C++ *** |
|
633 ;;; page-delimiter: "^/\\*" *** |
|
634 ;;; End: *** |
|
635 */ |