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