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