1278
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1278
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1278
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
1278
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3932
|
31 #include "error.h" |
1755
|
32 #include "str-vec.h" |
|
33 |
1278
|
34 #include "oct-map.h" |
|
35 #include "utils.h" |
|
36 |
4513
|
37 Cell |
4675
|
38 Octave_map::contents (const std::string& k) const |
4197
|
39 { |
4587
|
40 const_iterator p = seek (k); |
4197
|
41 |
4513
|
42 return p != end () ? p->second : Cell (); |
4197
|
43 } |
|
44 |
1755
|
45 string_vector |
3933
|
46 Octave_map::keys (void) const |
1278
|
47 { |
1287
|
48 int len = length (); |
|
49 |
1755
|
50 string_vector names (len); |
1278
|
51 |
|
52 int i = 0; |
4219
|
53 for (const_iterator p = begin (); p != end (); p++) |
1755
|
54 names[i++] = key (p); |
1278
|
55 |
|
56 return names; |
|
57 } |
|
58 |
4567
|
59 Octave_map |
|
60 Octave_map::reshape (const dim_vector& new_dims) const |
|
61 { |
|
62 Octave_map retval; |
|
63 |
|
64 if (new_dims != dims ()) |
|
65 { |
|
66 for (const_iterator p = begin (); p != end (); p++) |
4675
|
67 retval.assign (key(p), contents(p).reshape (new_dims)); |
4567
|
68 |
4936
|
69 retval.dimensions = new_dims; |
4567
|
70 } |
4936
|
71 else |
|
72 retval = *this; |
4567
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
3932
|
77 int |
4561
|
78 Octave_map::numel (void) const |
3932
|
79 { |
4561
|
80 int retval; |
|
81 |
|
82 if (empty ()) |
|
83 retval = 0; |
|
84 else |
3932
|
85 { |
4561
|
86 Cell tmp = contents (begin ()); |
|
87 retval = tmp.numel (); |
3932
|
88 } |
4121
|
89 |
4561
|
90 return retval; |
3932
|
91 } |
|
92 |
4936
|
93 Octave_map |
|
94 Octave_map::resize (const dim_vector& dv) const |
|
95 { |
|
96 Octave_map retval; |
|
97 |
|
98 if (dv != dims ()) |
|
99 { |
|
100 for (const_iterator p = begin (); p != end (); p++) |
|
101 { |
|
102 Cell tmp = contents(p); |
|
103 tmp.resize(dv); |
|
104 retval.assign (key(p), tmp); |
|
105 } |
|
106 |
|
107 retval.dimensions = dv; |
|
108 } |
|
109 else |
|
110 retval = *this; |
|
111 |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
4915
|
116 Octave_map |
5073
|
117 Octave_map::concat (const Octave_map& rb, const Array<int>& ra_idx) |
4806
|
118 { |
4937
|
119 Octave_map retval; |
4936
|
120 |
5073
|
121 if (length() == rb.length()) |
4936
|
122 { |
5073
|
123 for (Octave_map::const_iterator pa = begin (); pa != end (); pa++) |
4936
|
124 { |
5073
|
125 Octave_map::const_iterator pb = rb.seek (key(pa)); |
4937
|
126 |
5003
|
127 if (pb == rb.end ()) |
4937
|
128 { |
|
129 error ("field name mismatch in structure concatenation"); |
|
130 break; |
|
131 } |
4936
|
132 |
5073
|
133 retval.assign (key(pa), |
|
134 contents(pa).insert (rb.contents(pb), ra_idx)); |
4937
|
135 } |
4936
|
136 } |
4937
|
137 else |
|
138 error ("field name mismatch in structure concatenation"); |
|
139 |
4915
|
140 return retval; |
4806
|
141 } |
|
142 |
4197
|
143 static string_vector |
|
144 equiv_keys (const Octave_map& a, const Octave_map& b) |
|
145 { |
|
146 string_vector retval; |
|
147 |
|
148 string_vector a_keys = a.keys().qsort (); |
|
149 string_vector b_keys = b.keys().qsort (); |
|
150 |
|
151 int a_len = a_keys.length (); |
|
152 int b_len = b_keys.length (); |
|
153 |
|
154 if (a_len == b_len) |
|
155 { |
|
156 for (int i = 0; i < a_len; i++) |
|
157 { |
|
158 if (a_keys[i] != b_keys[i]) |
|
159 return retval; |
|
160 } |
|
161 |
|
162 retval = a_keys; |
|
163 } |
|
164 |
|
165 return retval; |
|
166 } |
|
167 |
|
168 Octave_map& |
4513
|
169 Octave_map::assign (const octave_value_list& idx, const Octave_map& rhs) |
4197
|
170 { |
|
171 string_vector t_keys = empty () ? rhs.keys () : equiv_keys (*this, rhs); |
|
172 |
|
173 if (! t_keys.empty ()) |
|
174 { |
|
175 int len = t_keys.length (); |
|
176 |
|
177 for (int i = 0; i < len; i++) |
|
178 { |
4587
|
179 std::string k = t_keys[i]; |
4197
|
180 |
4675
|
181 Cell t_rhs = rhs.contents (k); |
4197
|
182 |
4587
|
183 assign (idx, k, t_rhs); |
4197
|
184 |
|
185 if (error_state) |
|
186 break; |
|
187 } |
|
188 } |
|
189 else |
|
190 error ("field name mismatch in structure assignment"); |
|
191 |
|
192 return *this; |
|
193 } |
|
194 |
4561
|
195 static dim_vector |
|
196 common_size (const dim_vector& a, const dim_vector& b) |
|
197 { |
|
198 dim_vector retval; |
|
199 |
|
200 int a_len = a.length (); |
|
201 int b_len = b.length (); |
|
202 |
|
203 int new_len = std::max (a_len, b_len); |
|
204 int min_len = std::min (a_len, b_len); |
|
205 |
|
206 retval.resize (new_len); |
|
207 |
|
208 for (int i = 0; i < min_len; i++) |
|
209 retval(i) = std::max (a(i), b(i)); |
|
210 |
|
211 if (a_len < b_len) |
|
212 { |
|
213 for (int i = min_len; i < b_len; i++) |
|
214 retval(i) = b(i); |
|
215 } |
|
216 else if (a_len > b_len) |
|
217 { |
|
218 for (int i = min_len; i < a_len; i++) |
|
219 retval(i) = a(i); |
|
220 } |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
3932
|
225 Octave_map& |
4587
|
226 Octave_map::assign (const octave_value_list& idx, const std::string& k, |
4513
|
227 const Cell& rhs) |
3932
|
228 { |
4587
|
229 Cell tmp = map[k]; |
3932
|
230 |
4216
|
231 octave_value fill_value = Matrix (); |
|
232 |
|
233 tmp.assign (idx, rhs, fill_value); |
3932
|
234 |
|
235 if (! error_state) |
|
236 { |
4561
|
237 dim_vector rhs_dims = tmp.dims (); |
3943
|
238 |
4561
|
239 dim_vector curr_dims = dims (); |
|
240 |
|
241 dim_vector new_dims = common_size (rhs_dims, curr_dims); |
3932
|
242 |
4561
|
243 if (new_dims != rhs_dims) |
3932
|
244 { |
4625
|
245 tmp.resize (new_dims, fill_value); |
3943
|
246 } |
4561
|
247 else if (new_dims != curr_dims) |
3943
|
248 { |
4219
|
249 for (iterator p = begin (); p != end (); p++) |
4625
|
250 contents(p).resize (rhs_dims, fill_value); |
4561
|
251 } |
3932
|
252 |
4561
|
253 dimensions = new_dims; |
3932
|
254 |
4587
|
255 map[k] = tmp; |
3932
|
256 } |
|
257 |
|
258 return *this; |
|
259 } |
|
260 |
3933
|
261 Octave_map& |
4675
|
262 Octave_map::assign (const std::string& k, const octave_value& rhs) |
|
263 { |
|
264 if (empty ()) |
|
265 { |
|
266 map[k] = Cell (rhs); |
|
267 |
|
268 dimensions = dim_vector (1, 1); |
|
269 } |
|
270 else |
|
271 { |
|
272 dim_vector dv = dims (); |
|
273 |
|
274 if (dv.all_ones ()) |
|
275 map[k] = Cell (rhs); |
|
276 else |
|
277 error ("invalid structure assignment"); |
|
278 } |
|
279 |
|
280 return *this; |
|
281 } |
|
282 |
|
283 Octave_map& |
4587
|
284 Octave_map::assign (const std::string& k, const Cell& rhs) |
3933
|
285 { |
4219
|
286 if (empty ()) |
4563
|
287 { |
4587
|
288 map[k] = rhs; |
4563
|
289 |
4730
|
290 dimensions = rhs.dims (); |
4563
|
291 } |
3933
|
292 else |
|
293 { |
4562
|
294 if (dims () == rhs.dims ()) |
4587
|
295 map[k] = rhs; |
3933
|
296 else |
|
297 error ("invalid structure assignment"); |
|
298 } |
|
299 |
|
300 return *this; |
|
301 } |
|
302 |
|
303 Octave_map |
4513
|
304 Octave_map::index (const octave_value_list& idx) |
3933
|
305 { |
|
306 Octave_map retval; |
|
307 |
4219
|
308 for (iterator p = begin (); p != end (); p++) |
3933
|
309 { |
4513
|
310 Cell tmp = contents(p).index (idx); |
3933
|
311 |
|
312 if (error_state) |
|
313 break; |
|
314 |
4675
|
315 retval.assign (key(p), tmp); |
3933
|
316 } |
|
317 |
|
318 return error_state ? Octave_map () : retval; |
|
319 } |
|
320 |
1278
|
321 /* |
|
322 ;;; Local Variables: *** |
|
323 ;;; mode: C++ *** |
|
324 ;;; End: *** |
|
325 */ |