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