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