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 |
|
117 concat (const Octave_map& ra, const Octave_map& rb, const Array<int>& ra_idx) |
4806
|
118 { |
4936
|
119 if (ra.length() != rb.length()) |
|
120 { |
|
121 error ("field name mismatch in structure concatenation"); |
|
122 return Octave_map (); |
|
123 } |
|
124 |
4915
|
125 Octave_map retval; |
4936
|
126 for (Octave_map::const_iterator pa = ra.begin (); pa != ra.end (); pa++) |
|
127 { |
|
128 Octave_map::const_iterator pb = rb.seek (ra.key(pa)); |
|
129 if (pa == rb.end()) |
|
130 { |
|
131 error ("field name mismatch in structure concatenation"); |
|
132 return Octave_map (); |
|
133 } |
|
134 |
|
135 retval.assign (ra.key(pa), ra.contents(pa).insert(rb.contents(pb), |
|
136 ra_idx)); |
|
137 } |
4915
|
138 return retval; |
4806
|
139 } |
|
140 |
4197
|
141 static string_vector |
|
142 equiv_keys (const Octave_map& a, const Octave_map& b) |
|
143 { |
|
144 string_vector retval; |
|
145 |
|
146 string_vector a_keys = a.keys().qsort (); |
|
147 string_vector b_keys = b.keys().qsort (); |
|
148 |
|
149 int a_len = a_keys.length (); |
|
150 int b_len = b_keys.length (); |
|
151 |
|
152 if (a_len == b_len) |
|
153 { |
|
154 for (int i = 0; i < a_len; i++) |
|
155 { |
|
156 if (a_keys[i] != b_keys[i]) |
|
157 return retval; |
|
158 } |
|
159 |
|
160 retval = a_keys; |
|
161 } |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 Octave_map& |
4513
|
167 Octave_map::assign (const octave_value_list& idx, const Octave_map& rhs) |
4197
|
168 { |
|
169 string_vector t_keys = empty () ? rhs.keys () : equiv_keys (*this, rhs); |
|
170 |
|
171 if (! t_keys.empty ()) |
|
172 { |
|
173 int len = t_keys.length (); |
|
174 |
|
175 for (int i = 0; i < len; i++) |
|
176 { |
4587
|
177 std::string k = t_keys[i]; |
4197
|
178 |
4675
|
179 Cell t_rhs = rhs.contents (k); |
4197
|
180 |
4587
|
181 assign (idx, k, t_rhs); |
4197
|
182 |
|
183 if (error_state) |
|
184 break; |
|
185 } |
|
186 } |
|
187 else |
|
188 error ("field name mismatch in structure assignment"); |
|
189 |
|
190 return *this; |
|
191 } |
|
192 |
4561
|
193 static dim_vector |
|
194 common_size (const dim_vector& a, const dim_vector& b) |
|
195 { |
|
196 dim_vector retval; |
|
197 |
|
198 int a_len = a.length (); |
|
199 int b_len = b.length (); |
|
200 |
|
201 int new_len = std::max (a_len, b_len); |
|
202 int min_len = std::min (a_len, b_len); |
|
203 |
|
204 retval.resize (new_len); |
|
205 |
|
206 for (int i = 0; i < min_len; i++) |
|
207 retval(i) = std::max (a(i), b(i)); |
|
208 |
|
209 if (a_len < b_len) |
|
210 { |
|
211 for (int i = min_len; i < b_len; i++) |
|
212 retval(i) = b(i); |
|
213 } |
|
214 else if (a_len > b_len) |
|
215 { |
|
216 for (int i = min_len; i < a_len; i++) |
|
217 retval(i) = a(i); |
|
218 } |
|
219 |
|
220 return retval; |
|
221 } |
|
222 |
3932
|
223 Octave_map& |
4587
|
224 Octave_map::assign (const octave_value_list& idx, const std::string& k, |
4513
|
225 const Cell& rhs) |
3932
|
226 { |
4587
|
227 Cell tmp = map[k]; |
3932
|
228 |
4216
|
229 octave_value fill_value = Matrix (); |
|
230 |
|
231 tmp.assign (idx, rhs, fill_value); |
3932
|
232 |
|
233 if (! error_state) |
|
234 { |
4561
|
235 dim_vector rhs_dims = tmp.dims (); |
3943
|
236 |
4561
|
237 dim_vector curr_dims = dims (); |
|
238 |
|
239 dim_vector new_dims = common_size (rhs_dims, curr_dims); |
3932
|
240 |
4561
|
241 if (new_dims != rhs_dims) |
3932
|
242 { |
4625
|
243 tmp.resize (new_dims, fill_value); |
3943
|
244 } |
4561
|
245 else if (new_dims != curr_dims) |
3943
|
246 { |
4219
|
247 for (iterator p = begin (); p != end (); p++) |
4625
|
248 contents(p).resize (rhs_dims, fill_value); |
4561
|
249 } |
3932
|
250 |
4561
|
251 dimensions = new_dims; |
3932
|
252 |
4587
|
253 map[k] = tmp; |
3932
|
254 } |
|
255 |
|
256 return *this; |
|
257 } |
|
258 |
3933
|
259 Octave_map& |
4675
|
260 Octave_map::assign (const std::string& k, const octave_value& rhs) |
|
261 { |
|
262 if (empty ()) |
|
263 { |
|
264 map[k] = Cell (rhs); |
|
265 |
|
266 dimensions = dim_vector (1, 1); |
|
267 } |
|
268 else |
|
269 { |
|
270 dim_vector dv = dims (); |
|
271 |
|
272 if (dv.all_ones ()) |
|
273 map[k] = Cell (rhs); |
|
274 else |
|
275 error ("invalid structure assignment"); |
|
276 } |
|
277 |
|
278 return *this; |
|
279 } |
|
280 |
|
281 Octave_map& |
4587
|
282 Octave_map::assign (const std::string& k, const Cell& rhs) |
3933
|
283 { |
4219
|
284 if (empty ()) |
4563
|
285 { |
4587
|
286 map[k] = rhs; |
4563
|
287 |
4730
|
288 dimensions = rhs.dims (); |
4563
|
289 } |
3933
|
290 else |
|
291 { |
4562
|
292 if (dims () == rhs.dims ()) |
4587
|
293 map[k] = rhs; |
3933
|
294 else |
|
295 error ("invalid structure assignment"); |
|
296 } |
|
297 |
|
298 return *this; |
|
299 } |
|
300 |
|
301 Octave_map |
4513
|
302 Octave_map::index (const octave_value_list& idx) |
3933
|
303 { |
|
304 Octave_map retval; |
|
305 |
4219
|
306 for (iterator p = begin (); p != end (); p++) |
3933
|
307 { |
4513
|
308 Cell tmp = contents(p).index (idx); |
3933
|
309 |
|
310 if (error_state) |
|
311 break; |
|
312 |
4675
|
313 retval.assign (key(p), tmp); |
3933
|
314 } |
|
315 |
|
316 return error_state ? Octave_map () : retval; |
|
317 } |
|
318 |
1278
|
319 /* |
|
320 ;;; Local Variables: *** |
|
321 ;;; mode: C++ *** |
|
322 ;;; End: *** |
|
323 */ |