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