3665
|
1 // Template array classes |
|
2 /* |
|
3 |
9245
|
4 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2009 John W. Eaton |
3665
|
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" |
|
34 #include "idx-vector.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 // N-dimensional array class. |
|
38 |
|
39 template <class T> |
|
40 std::ostream& |
|
41 operator << (std::ostream& os, const ArrayN<T>& a) |
|
42 { |
4513
|
43 dim_vector a_dims = a.dims (); |
4473
|
44 |
|
45 int n_dims = a_dims.length (); |
|
46 |
|
47 os << n_dims << "-dimensional array"; |
|
48 |
|
49 if (n_dims) |
4543
|
50 os << " (" << a_dims.str () << ")"; |
4473
|
51 |
|
52 os <<"\n\n"; |
3665
|
53 |
4473
|
54 if (n_dims) |
|
55 { |
|
56 os << "data:"; |
|
57 |
5275
|
58 Array<octave_idx_type> ra_idx (n_dims, 0); |
4473
|
59 |
|
60 // Number of times the first 2d-array is to be displayed. |
3665
|
61 |
5275
|
62 octave_idx_type m = 1; |
4473
|
63 for (int i = 2; i < n_dims; i++) |
|
64 m *= a_dims(i); |
|
65 |
|
66 if (m == 1) |
|
67 { |
5275
|
68 octave_idx_type rows = 0; |
|
69 octave_idx_type cols = 0; |
4473
|
70 |
|
71 switch (n_dims) |
|
72 { |
|
73 case 2: |
|
74 rows = a_dims(0); |
|
75 cols = a_dims(1); |
3665
|
76 |
5275
|
77 for (octave_idx_type j = 0; j < rows; j++) |
4473
|
78 { |
|
79 ra_idx(0) = j; |
5275
|
80 for (octave_idx_type k = 0; k < cols; k++) |
4473
|
81 { |
|
82 ra_idx(1) = k; |
|
83 os << " " << a.elem(ra_idx); |
|
84 } |
|
85 os << "\n"; |
|
86 } |
|
87 break; |
|
88 |
4476
|
89 default: |
4473
|
90 rows = a_dims(0); |
|
91 |
5275
|
92 for (octave_idx_type k = 0; k < rows; k++) |
4473
|
93 { |
|
94 ra_idx(0) = k; |
|
95 os << " " << a.elem(ra_idx); |
|
96 } |
|
97 break; |
|
98 } |
3665
|
99 |
4473
|
100 os << "\n"; |
|
101 } |
|
102 else |
|
103 { |
5275
|
104 octave_idx_type rows = a_dims(0); |
|
105 octave_idx_type cols = a_dims(1); |
4473
|
106 |
|
107 for (int i = 0; i < m; i++) |
|
108 { |
|
109 os << "\n(:,:,"; |
|
110 |
|
111 for (int j = 2; j < n_dims - 1; j++) |
|
112 os << ra_idx(j) + 1 << ","; |
|
113 |
|
114 os << ra_idx(n_dims - 1) + 1 << ") = \n"; |
3665
|
115 |
5275
|
116 for (octave_idx_type j = 0; j < rows; j++) |
4473
|
117 { |
|
118 ra_idx(0) = j; |
|
119 |
5275
|
120 for (octave_idx_type k = 0; k < cols; k++) |
4473
|
121 { |
|
122 ra_idx(1) = k; |
|
123 os << " " << a.elem(ra_idx); |
|
124 } |
3665
|
125 |
4473
|
126 os << "\n"; |
|
127 } |
|
128 |
|
129 os << "\n"; |
|
130 |
|
131 if (i != m - 1) |
|
132 increment_index (ra_idx, a_dims, 2); |
|
133 } |
|
134 } |
|
135 } |
3665
|
136 |
|
137 return os; |
|
138 } |
|
139 |
|
140 /* |
|
141 ;;; Local Variables: *** |
|
142 ;;; mode: C++ *** |
|
143 ;;; End: *** |
|
144 */ |