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 |
|
69 dimensions = new_dims; |
|
70 } |
|
71 |
|
72 return retval; |
|
73 } |
|
74 |
3932
|
75 int |
4561
|
76 Octave_map::numel (void) const |
3932
|
77 { |
4561
|
78 int retval; |
|
79 |
|
80 if (empty ()) |
|
81 retval = 0; |
|
82 else |
3932
|
83 { |
4561
|
84 Cell tmp = contents (begin ()); |
|
85 retval = tmp.numel (); |
3932
|
86 } |
4121
|
87 |
4561
|
88 return retval; |
3932
|
89 } |
|
90 |
4197
|
91 static string_vector |
|
92 equiv_keys (const Octave_map& a, const Octave_map& b) |
|
93 { |
|
94 string_vector retval; |
|
95 |
|
96 string_vector a_keys = a.keys().qsort (); |
|
97 string_vector b_keys = b.keys().qsort (); |
|
98 |
|
99 int a_len = a_keys.length (); |
|
100 int b_len = b_keys.length (); |
|
101 |
|
102 if (a_len == b_len) |
|
103 { |
|
104 for (int i = 0; i < a_len; i++) |
|
105 { |
|
106 if (a_keys[i] != b_keys[i]) |
|
107 return retval; |
|
108 } |
|
109 |
|
110 retval = a_keys; |
|
111 } |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 Octave_map& |
4513
|
117 Octave_map::assign (const octave_value_list& idx, const Octave_map& rhs) |
4197
|
118 { |
|
119 string_vector t_keys = empty () ? rhs.keys () : equiv_keys (*this, rhs); |
|
120 |
|
121 if (! t_keys.empty ()) |
|
122 { |
|
123 int len = t_keys.length (); |
|
124 |
|
125 for (int i = 0; i < len; i++) |
|
126 { |
4587
|
127 std::string k = t_keys[i]; |
4197
|
128 |
4675
|
129 Cell t_rhs = rhs.contents (k); |
4197
|
130 |
4587
|
131 assign (idx, k, t_rhs); |
4197
|
132 |
|
133 if (error_state) |
|
134 break; |
|
135 } |
|
136 } |
|
137 else |
|
138 error ("field name mismatch in structure assignment"); |
|
139 |
|
140 return *this; |
|
141 } |
|
142 |
4561
|
143 static dim_vector |
|
144 common_size (const dim_vector& a, const dim_vector& b) |
|
145 { |
|
146 dim_vector retval; |
|
147 |
|
148 int a_len = a.length (); |
|
149 int b_len = b.length (); |
|
150 |
|
151 int new_len = std::max (a_len, b_len); |
|
152 int min_len = std::min (a_len, b_len); |
|
153 |
|
154 retval.resize (new_len); |
|
155 |
|
156 for (int i = 0; i < min_len; i++) |
|
157 retval(i) = std::max (a(i), b(i)); |
|
158 |
|
159 if (a_len < b_len) |
|
160 { |
|
161 for (int i = min_len; i < b_len; i++) |
|
162 retval(i) = b(i); |
|
163 } |
|
164 else if (a_len > b_len) |
|
165 { |
|
166 for (int i = min_len; i < a_len; i++) |
|
167 retval(i) = a(i); |
|
168 } |
|
169 |
|
170 return retval; |
|
171 } |
|
172 |
3932
|
173 Octave_map& |
4587
|
174 Octave_map::assign (const octave_value_list& idx, const std::string& k, |
4513
|
175 const Cell& rhs) |
3932
|
176 { |
4587
|
177 Cell tmp = map[k]; |
3932
|
178 |
4216
|
179 octave_value fill_value = Matrix (); |
|
180 |
|
181 tmp.assign (idx, rhs, fill_value); |
3932
|
182 |
|
183 if (! error_state) |
|
184 { |
4561
|
185 dim_vector rhs_dims = tmp.dims (); |
3943
|
186 |
4561
|
187 dim_vector curr_dims = dims (); |
|
188 |
|
189 dim_vector new_dims = common_size (rhs_dims, curr_dims); |
3932
|
190 |
4561
|
191 if (new_dims != rhs_dims) |
3932
|
192 { |
4625
|
193 tmp.resize (new_dims, fill_value); |
3943
|
194 } |
4561
|
195 else if (new_dims != curr_dims) |
3943
|
196 { |
4219
|
197 for (iterator p = begin (); p != end (); p++) |
4625
|
198 contents(p).resize (rhs_dims, fill_value); |
4561
|
199 } |
3932
|
200 |
4561
|
201 dimensions = new_dims; |
3932
|
202 |
4587
|
203 map[k] = tmp; |
3932
|
204 } |
|
205 |
|
206 return *this; |
|
207 } |
|
208 |
3933
|
209 Octave_map& |
4675
|
210 Octave_map::assign (const std::string& k, const octave_value& rhs) |
|
211 { |
|
212 if (empty ()) |
|
213 { |
|
214 map[k] = Cell (rhs); |
|
215 |
|
216 dimensions = dim_vector (1, 1); |
|
217 } |
|
218 else |
|
219 { |
|
220 dim_vector dv = dims (); |
|
221 |
|
222 if (dv.all_ones ()) |
|
223 map[k] = Cell (rhs); |
|
224 else |
|
225 error ("invalid structure assignment"); |
|
226 } |
|
227 |
|
228 return *this; |
|
229 } |
|
230 |
|
231 Octave_map& |
4587
|
232 Octave_map::assign (const std::string& k, const Cell& rhs) |
3933
|
233 { |
4219
|
234 if (empty ()) |
4563
|
235 { |
4587
|
236 map[k] = rhs; |
4563
|
237 |
|
238 dimensions = dim_vector (1, 1); |
|
239 } |
3933
|
240 else |
|
241 { |
4562
|
242 if (dims () == rhs.dims ()) |
4587
|
243 map[k] = rhs; |
3933
|
244 else |
|
245 error ("invalid structure assignment"); |
|
246 } |
|
247 |
|
248 return *this; |
|
249 } |
|
250 |
|
251 Octave_map |
4513
|
252 Octave_map::index (const octave_value_list& idx) |
3933
|
253 { |
|
254 Octave_map retval; |
|
255 |
4219
|
256 for (iterator p = begin (); p != end (); p++) |
3933
|
257 { |
4513
|
258 Cell tmp = contents(p).index (idx); |
3933
|
259 |
|
260 if (error_state) |
|
261 break; |
|
262 |
4675
|
263 retval.assign (key(p), tmp); |
3933
|
264 } |
|
265 |
|
266 return error_state ? Octave_map () : retval; |
|
267 } |
|
268 |
1278
|
269 /* |
|
270 ;;; Local Variables: *** |
|
271 ;;; mode: C++ *** |
|
272 ;;; End: *** |
|
273 */ |