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> |
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 |
1756
|
191 void qsort (int (*compare) (const void *, const void *)) |
|
192 { |
|
193 if (rep->count > 1) |
|
194 { |
|
195 --rep->count; |
|
196 rep = new ArrayRep (*rep); |
|
197 } |
|
198 |
|
199 rep->qsort (compare); |
|
200 } |
|
201 |
1560
|
202 #ifdef HEAVYWEIGHT_INDEXING |
1619
|
203 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
204 |
1619
|
205 void clear_index (void); |
1560
|
206 |
1619
|
207 void set_index (const idx_vector& i); |
1560
|
208 |
1619
|
209 int index_count (void) const { return idx_count; } |
1560
|
210 |
1619
|
211 idx_vector *get_idx (void) const { return idx; } |
1560
|
212 |
|
213 void maybe_delete_elements (idx_vector& i); |
|
214 |
|
215 Array<T> value (void); |
|
216 #endif |
228
|
217 }; |
|
218 |
1560
|
219 template <class LT, class RT> |
|
220 int assign (Array<LT>& lhs, const Array<RT>& rhs); |
|
221 |
1359
|
222 // Two dimensional array class. |
238
|
223 |
228
|
224 template <class T> |
|
225 class Array2 : public Array<T> |
|
226 { |
238
|
227 protected: |
|
228 |
1550
|
229 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
230 { |
|
231 d1 = n; |
|
232 d2 = m; |
1560
|
233 set_max_indices (2); |
1550
|
234 } |
238
|
235 |
228
|
236 public: |
|
237 |
1560
|
238 // These really need to be protected (and they will be in the |
|
239 // future, so don't depend on them being here!), but they can't be |
|
240 // until template friends work correctly in g++. |
|
241 |
|
242 int d1; |
|
243 int d2; |
|
244 |
1550
|
245 Array2 (void) : Array<T> () |
|
246 { |
|
247 d1 = 0; |
|
248 d2 = 0; |
1560
|
249 set_max_indices (2); |
1550
|
250 } |
|
251 |
|
252 Array2 (int n, int m) : Array<T> (n*m) |
|
253 { |
|
254 d1 = n; |
|
255 d2 = m; |
1560
|
256 set_max_indices (2); |
1550
|
257 } |
|
258 |
|
259 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
260 { |
|
261 d1 = n; |
|
262 d2 = m; |
1560
|
263 set_max_indices (2); |
1550
|
264 } |
|
265 |
|
266 Array2 (const Array2<T>& a) : Array<T> (a) |
|
267 { |
|
268 d1 = a.d1; |
|
269 d2 = a.d2; |
1560
|
270 set_max_indices (2); |
1550
|
271 } |
|
272 |
1560
|
273 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
274 { |
|
275 d1 = n; |
|
276 d2 = m; |
|
277 set_max_indices (2); |
|
278 } |
|
279 |
1574
|
280 #ifndef NO_DIAG_ARRAY |
1560
|
281 Array2 (const DiagArray<T>& a) : Array<T> (a.rows () * a.cols (), T (0)) |
1550
|
282 { |
|
283 for (int i = 0; i < a.length (); i++) |
|
284 elem (i, i) = a.elem (i, i); |
1560
|
285 |
|
286 set_max_indices (2); |
1550
|
287 } |
1574
|
288 #endif |
228
|
289 |
1230
|
290 ~Array2 (void) { } |
|
291 |
1550
|
292 Array2<T>& operator = (const Array2<T>& a) |
|
293 { |
1701
|
294 if (this != &a && rep != a.rep) |
1550
|
295 { |
|
296 Array<T>::operator = (a); |
|
297 d1 = a.d1; |
|
298 d2 = a.d2; |
|
299 } |
238
|
300 |
1550
|
301 return *this; |
|
302 } |
|
303 |
|
304 int dim1 (void) const { return d1; } |
|
305 int dim2 (void) const { return d2; } |
238
|
306 |
1550
|
307 int rows (void) const { return d1; } |
|
308 int cols (void) const { return d2; } |
|
309 int columns (void) const { return d2; } |
|
310 |
|
311 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
238
|
312 T& checkelem (int i, int j); |
1550
|
313 T& operator () (int i, int j) { return checkelem (i, j); } |
238
|
314 |
1359
|
315 // No checking. |
|
316 |
1550
|
317 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
238
|
318 |
228
|
319 T elem (int i, int j) const; |
|
320 T checkelem (int i, int j) const; |
|
321 T operator () (int i, int j) const; |
|
322 |
238
|
323 void resize (int n, int m); |
|
324 void resize (int n, int m, const T& val); |
1560
|
325 |
1561
|
326 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
327 |
1560
|
328 #ifdef HEAVYWEIGHT_INDEXING |
|
329 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
330 |
|
331 Array2<T> value (void); |
|
332 #endif |
228
|
333 }; |
|
334 |
1560
|
335 template <class LT, class RT> |
|
336 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
337 |
1359
|
338 // Three dimensional array class. |
238
|
339 |
228
|
340 template <class T> |
|
341 class Array3 : public Array2<T> |
|
342 { |
238
|
343 protected: |
|
344 |
|
345 int d3; |
|
346 |
1550
|
347 Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, m*k) |
|
348 { |
|
349 d2 = m; |
|
350 d3 = k; |
1560
|
351 set_max_indices (3); |
1550
|
352 } |
238
|
353 |
228
|
354 public: |
|
355 |
1550
|
356 Array3 (void) : Array2<T> () |
|
357 { |
|
358 d2 = 0; |
|
359 d3 = 0; |
1560
|
360 set_max_indices (3); |
1550
|
361 } |
|
362 |
|
363 Array3 (int n, int m, int k) : Array2<T> (n, m*k) |
|
364 { |
|
365 d2 = m; |
|
366 d3 = k; |
1560
|
367 set_max_indices (3); |
1550
|
368 } |
|
369 |
|
370 Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
371 { |
|
372 d2 = m; |
|
373 d3 = k; |
1560
|
374 set_max_indices (3); |
1550
|
375 } |
|
376 |
|
377 Array3 (const Array3<T>& a) : Array2<T> (a) |
|
378 { |
|
379 d2 = a.d2; |
|
380 d3 = a.d3; |
1560
|
381 set_max_indices (3); |
1550
|
382 } |
228
|
383 |
1230
|
384 ~Array3 (void) { } |
|
385 |
1550
|
386 Array3<T>& operator = (const Array3<T>& a) |
|
387 { |
1701
|
388 if (this != &a && rep != a.rep) |
1550
|
389 { |
|
390 Array<T>::operator = (a); |
|
391 d1 = a.d1; |
|
392 d2 = a.d2; |
|
393 d3 = a.d3; |
|
394 } |
228
|
395 |
1550
|
396 return *this; |
|
397 } |
|
398 |
|
399 int dim3 (void) const { return d3; } |
|
400 |
|
401 T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); } |
238
|
402 T& checkelem (int i, int j, int k); |
1550
|
403 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
238
|
404 |
1359
|
405 // No checking. |
|
406 |
1550
|
407 T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); } |
238
|
408 |
228
|
409 T elem (int i, int j, int k) const; |
238
|
410 T checkelem (int i, int j, int k) const; |
|
411 T operator () (int i, int j, int k) const; |
228
|
412 |
238
|
413 void resize (int n, int m, int k); |
|
414 void resize (int n, int m, int k, const T& val); |
1560
|
415 |
|
416 #ifdef HEAVYWEIGHT_INDEXING |
|
417 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
418 |
|
419 Array3<T> value (void); |
|
420 #endif |
228
|
421 }; |
|
422 |
1560
|
423 template <class LT, class RT> |
|
424 int assign (Array3<LT>& lhs, const Array3<RT>& rhs); |
|
425 |
1359
|
426 // A two-dimensional array with diagonal elements only. |
|
427 // |
|
428 // Idea and example code for Proxy class and functions from: |
|
429 // |
|
430 // From: kanze@us-es.sel.de (James Kanze) |
|
431 // Subject: Re: How to overload [] to do READ/WRITE differently ? |
|
432 // Message-ID: <KANZE.93Nov29151407@slsvhdt.us-es.sel.de> |
|
433 // Sender: news@us-es.sel.de |
|
434 // Date: 29 Nov 1993 14:14:07 GMT |
|
435 // -- |
|
436 // James Kanze email: kanze@us-es.sel.de |
|
437 // GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France |
238
|
438 |
1574
|
439 #ifndef NO_DIAG_ARRAY |
228
|
440 template <class T> |
|
441 class DiagArray : public Array<T> |
|
442 { |
366
|
443 private: |
1560
|
444 T get (int i) { return Array<T>::elem (i); } |
|
445 void set (const T& val, int i) { Array<T>::elem (i) = val; } |
366
|
446 |
319
|
447 class Proxy |
|
448 { |
|
449 public: |
|
450 |
1560
|
451 Proxy (DiagArray<T> *ref, int r, int c) |
645
|
452 : i (r), j (c), object (ref) { } |
319
|
453 |
1560
|
454 const Proxy& operator = (const T& val) const |
|
455 { |
|
456 if (i == j) |
|
457 { |
|
458 if (object) |
|
459 object->set (val, i); |
|
460 } |
|
461 else |
|
462 (*current_liboctave_error_handler) ("invalid assignment to off-diagonal in diagonal array"); |
319
|
463 |
1560
|
464 return *this; |
|
465 } |
319
|
466 |
1560
|
467 operator T () const |
|
468 { |
|
469 if (object && i == j) |
|
470 return object->get (i); |
|
471 else |
|
472 { |
|
473 static T foo (0); |
|
474 return foo; |
|
475 } |
|
476 } |
319
|
477 |
|
478 private: |
|
479 |
1359
|
480 // XXX FIXME XXX -- this is declared private to keep the user from |
|
481 // taking the address of a Proxy. Maybe it should be implemented |
|
482 // by means of a companion function in the DiagArray class. |
319
|
483 |
1560
|
484 T *operator& () const { assert (0); return (T *) 0; } |
319
|
485 |
|
486 int i; |
|
487 int j; |
|
488 |
|
489 DiagArray<T> *object; |
|
490 |
|
491 }; |
|
492 |
|
493 friend class Proxy; |
|
494 |
238
|
495 protected: |
|
496 |
|
497 int nr; |
|
498 int nc; |
|
499 |
1550
|
500 DiagArray (T *d, int r, int c) : Array<T> (d, r < c ? r : c) |
|
501 { |
|
502 nr = r; |
|
503 nc = c; |
1560
|
504 set_max_indices (2); |
1550
|
505 } |
238
|
506 |
228
|
507 public: |
238
|
508 |
1550
|
509 DiagArray (void) : Array<T> () |
|
510 { |
|
511 nr = 0; |
|
512 nc = 0; |
1560
|
513 set_max_indices (2); |
1550
|
514 } |
|
515 |
|
516 DiagArray (int n) : Array<T> (n) |
1560
|
517 { |
|
518 nr = n; |
|
519 nc = n; |
|
520 set_max_indices (2); |
|
521 } |
1550
|
522 |
|
523 DiagArray (int n, const T& val) : Array<T> (n, val) |
1560
|
524 { |
|
525 nr = n; |
|
526 nc = n; |
|
527 set_max_indices (2); |
|
528 } |
1550
|
529 |
|
530 DiagArray (int r, int c) : Array<T> (r < c ? r : c) |
1560
|
531 { |
|
532 nr = r; |
|
533 nc = c; |
|
534 set_max_indices (2); |
|
535 } |
1550
|
536 |
|
537 DiagArray (int r, int c, const T& val) : Array<T> (r < c ? r : c, val) |
1560
|
538 { |
|
539 nr = r; |
|
540 nc = c; |
|
541 set_max_indices (2); |
|
542 } |
1550
|
543 |
|
544 DiagArray (const Array<T>& a) : Array<T> (a) |
1560
|
545 { |
|
546 nr = nc = a.length (); |
|
547 set_max_indices (2); |
|
548 } |
1550
|
549 |
|
550 DiagArray (const DiagArray<T>& a) : Array<T> (a) |
1560
|
551 { |
|
552 nr = a.nr; |
|
553 nc = a.nc; |
|
554 set_max_indices (2); |
|
555 } |
228
|
556 |
1230
|
557 ~DiagArray (void) { } |
|
558 |
1550
|
559 DiagArray<T>& operator = (const DiagArray<T>& a) |
|
560 { |
1560
|
561 if (this != &a) |
|
562 { |
|
563 Array<T>::operator = (a); |
|
564 nr = a.nr; |
|
565 nc = a.nc; |
|
566 } |
|
567 |
|
568 return *this; |
1550
|
569 } |
228
|
570 |
1550
|
571 int dim1 (void) const { return nr; } |
|
572 int dim2 (void) const { return nc; } |
|
573 |
|
574 int rows (void) const { return nr; } |
|
575 int cols (void) const { return nc; } |
|
576 int columns (void) const { return nc; } |
228
|
577 |
880
|
578 #if 0 |
1560
|
579 Proxy elem (int r, int c) |
|
580 { |
|
581 return Proxy (this, r, c); |
|
582 } |
319
|
583 |
1560
|
584 Proxy checkelem (int r, int c) |
|
585 { |
|
586 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
587 { |
|
588 (*current_liboctave_error_handler) ("range error"); |
|
589 return Proxy (0, r, c); |
|
590 } |
|
591 else |
|
592 return Proxy (this, r, c); |
|
593 } |
319
|
594 |
1560
|
595 Proxy operator () (int r, int c) |
|
596 { |
|
597 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
598 { |
|
599 (*current_liboctave_error_handler) ("range error"); |
|
600 return Proxy (0, r, c); |
|
601 } |
|
602 else |
|
603 return Proxy (this, r, c); |
319
|
604 } |
880
|
605 #else |
|
606 T& elem (int r, int c); |
|
607 T& checkelem (int r, int c); |
|
608 T& operator () (int r, int c); |
344
|
609 #endif |
228
|
610 |
1359
|
611 // No checking. |
|
612 |
238
|
613 T& xelem (int r, int c); |
|
614 |
228
|
615 T elem (int r, int c) const; |
|
616 T checkelem (int r, int c) const; |
|
617 T operator () (int r, int c) const; |
|
618 |
238
|
619 void resize (int n, int m); |
|
620 void resize (int n, int m, const T& val); |
1560
|
621 |
|
622 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
228
|
623 }; |
1574
|
624 #endif |
228
|
625 |
|
626 #endif |
|
627 |
|
628 /* |
|
629 ;;; Local Variables: *** |
|
630 ;;; mode: C++ *** |
|
631 ;;; page-delimiter: "^/\\*" *** |
|
632 ;;; End: *** |
|
633 */ |