3665
|
1 // Template array classes |
|
2 /* |
|
3 |
|
4 Copyright (C) 2000 John W. Eaton |
|
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 |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
3665
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
|
34 #include <iostream> |
|
35 |
4588
|
36 #include "Array-util.h" |
3665
|
37 #include "ArrayN.h" |
4513
|
38 #include "ArrayN-idx.h" |
3665
|
39 #include "idx-vector.h" |
|
40 #include "lo-error.h" |
|
41 |
|
42 // N-dimensional array class. |
|
43 |
|
44 template <class T> |
|
45 std::ostream& |
|
46 operator << (std::ostream& os, const ArrayN<T>& a) |
|
47 { |
4513
|
48 dim_vector a_dims = a.dims (); |
4473
|
49 |
|
50 int n_dims = a_dims.length (); |
|
51 |
|
52 os << n_dims << "-dimensional array"; |
|
53 |
|
54 if (n_dims) |
4543
|
55 os << " (" << a_dims.str () << ")"; |
4473
|
56 |
|
57 os <<"\n\n"; |
3665
|
58 |
4473
|
59 if (n_dims) |
|
60 { |
|
61 os << "data:"; |
|
62 |
4513
|
63 Array<int> ra_idx (n_dims, 0); |
4473
|
64 |
|
65 // Number of times the first 2d-array is to be displayed. |
3665
|
66 |
4473
|
67 int m = 1; |
|
68 for (int i = 2; i < n_dims; i++) |
|
69 m *= a_dims(i); |
|
70 |
|
71 if (m == 1) |
|
72 { |
|
73 int rows = 0; |
|
74 int cols = 0; |
|
75 |
|
76 switch (n_dims) |
|
77 { |
|
78 case 2: |
|
79 rows = a_dims(0); |
|
80 cols = a_dims(1); |
3665
|
81 |
4473
|
82 for (int j = 0; j < rows; j++) |
|
83 { |
|
84 ra_idx(0) = j; |
|
85 for (int k = 0; k < cols; k++) |
|
86 { |
|
87 ra_idx(1) = k; |
|
88 os << " " << a.elem(ra_idx); |
|
89 } |
|
90 os << "\n"; |
|
91 } |
|
92 break; |
|
93 |
4476
|
94 default: |
4473
|
95 rows = a_dims(0); |
|
96 |
|
97 for (int k = 0; k < rows; k++) |
|
98 { |
|
99 ra_idx(0) = k; |
|
100 os << " " << a.elem(ra_idx); |
|
101 } |
|
102 break; |
|
103 } |
3665
|
104 |
4473
|
105 os << "\n"; |
|
106 } |
|
107 else |
|
108 { |
|
109 int rows = a_dims(0); |
|
110 int cols = a_dims(1); |
|
111 |
|
112 for (int i = 0; i < m; i++) |
|
113 { |
|
114 os << "\n(:,:,"; |
|
115 |
|
116 for (int j = 2; j < n_dims - 1; j++) |
|
117 os << ra_idx(j) + 1 << ","; |
|
118 |
|
119 os << ra_idx(n_dims - 1) + 1 << ") = \n"; |
3665
|
120 |
4473
|
121 for (int j = 0; j < rows; j++) |
|
122 { |
|
123 ra_idx(0) = j; |
|
124 |
|
125 for (int k = 0; k < cols; k++) |
|
126 { |
|
127 ra_idx(1) = k; |
|
128 os << " " << a.elem(ra_idx); |
|
129 } |
3665
|
130 |
4473
|
131 os << "\n"; |
|
132 } |
|
133 |
|
134 os << "\n"; |
|
135 |
|
136 if (i != m - 1) |
|
137 increment_index (ra_idx, a_dims, 2); |
|
138 } |
|
139 } |
|
140 } |
3665
|
141 |
|
142 return os; |
|
143 } |
|
144 |
|
145 /* |
|
146 ;;; Local Variables: *** |
|
147 ;;; mode: C++ *** |
|
148 ;;; End: *** |
|
149 */ |