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