529
|
1 // tree-const.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
581
|
28 #include <iostream.h> |
1199
|
29 #include <strstream.h> |
581
|
30 |
493
|
31 #include "tree-const.h" |
1199
|
32 #include "user-prefs.h" |
|
33 #include "pager.h" |
1
|
34 #include "error.h" |
|
35 #include "gripes.h" |
747
|
36 #include "oct-map.h" |
|
37 |
1168
|
38 // The following three variables could be made static members of the |
|
39 // tree_constant class. |
|
40 |
|
41 // Pointer to the blocks of memory we manage. |
1170
|
42 static tree_constant *newlist = 0; |
1168
|
43 |
|
44 // Multiplier for allocating new blocks. |
|
45 static const int newlist_grow_size = 128; |
|
46 |
747
|
47 Octave_map |
|
48 tree_constant::map_value (void) const |
|
49 { |
|
50 return rep->map_value (); |
|
51 } |
1
|
52 |
|
53 tree_constant::~tree_constant (void) |
|
54 { |
|
55 #if defined (MDEBUG) |
|
56 cerr << "~tree_constant: rep: " << rep |
|
57 << " rep->count: " << rep->count << "\n"; |
|
58 #endif |
|
59 |
|
60 if (--rep->count <= 0) |
|
61 { |
|
62 delete rep; |
531
|
63 rep = 0; |
1
|
64 } |
|
65 } |
|
66 |
|
67 void * |
|
68 tree_constant::operator new (size_t size) |
|
69 { |
1168
|
70 assert (size == sizeof (tree_constant)); |
|
71 |
|
72 if (! newlist) |
|
73 { |
|
74 int block_size = newlist_grow_size * sizeof (tree_constant); |
|
75 newlist = (tree_constant *) new char [block_size]; |
|
76 |
|
77 for (int i = 0; i < newlist_grow_size - 1; i++) |
|
78 newlist[i].freeptr = &newlist[i+1]; |
|
79 |
|
80 newlist[i].freeptr = 0; |
|
81 } |
|
82 |
|
83 tree_constant *tmp = newlist; |
|
84 newlist = newlist->freeptr; |
|
85 return tmp; |
1
|
86 } |
|
87 |
|
88 void |
|
89 tree_constant::operator delete (void *p, size_t size) |
|
90 { |
1168
|
91 tree_constant *tmp = (tree_constant *) p; |
|
92 tmp->freeptr = newlist; |
|
93 newlist = tmp; |
1
|
94 } |
|
95 |
747
|
96 // Simple assignment. |
|
97 |
|
98 tree_constant |
|
99 tree_constant::operator = (const tree_constant& a) |
|
100 { |
|
101 if (rep != a.rep) |
|
102 { |
|
103 if (--rep->count <= 0) |
|
104 delete rep; |
|
105 rep = a.rep; |
|
106 rep->count++; |
|
107 } |
|
108 return *this; |
|
109 } |
|
110 |
|
111 tree_constant |
|
112 tree_constant::lookup_map_element (SLList<char*>& list) |
|
113 { |
|
114 tree_constant retval; |
|
115 |
|
116 tree_constant_rep *tmp_rep = rep; |
|
117 |
|
118 Pix p = list.first (); |
|
119 while (p) |
|
120 { |
|
121 char *elt = list (p); |
|
122 |
|
123 list.next (p); |
|
124 |
|
125 tree_constant tmp = tmp_rep->lookup_map_element (elt); |
|
126 |
|
127 if (error_state) |
|
128 break; |
|
129 |
|
130 tmp_rep = tmp.rep; |
|
131 |
|
132 if (! p) |
|
133 retval = tmp; |
|
134 } |
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
1199
|
139 void |
|
140 tree_constant::print (void) |
|
141 { |
|
142 ostrstream output_buf; |
|
143 print (output_buf); |
|
144 output_buf << ends; |
|
145 maybe_page_output (output_buf); |
|
146 } |
|
147 |
747
|
148 // Simple structure assignment. |
|
149 |
|
150 void |
|
151 tree_constant::make_unique (void) |
|
152 { |
|
153 if (rep->count > 1) |
|
154 { |
|
155 --rep->count; |
|
156 rep = new tree_constant_rep (*rep); |
|
157 rep->count = 1; |
|
158 } |
|
159 |
|
160 if (rep->is_map ()) |
|
161 { |
|
162 for (Pix p = rep->a_map->first (); p != 0; rep->a_map->next (p)) |
|
163 { |
|
164 rep->a_map->contents (p) . make_unique (); |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 tree_constant::tree_constant_rep * |
|
170 tree_constant::make_unique_map (void) |
|
171 { |
|
172 if (! rep->is_map ()) |
|
173 { |
|
174 if (--rep->count <= 0) |
|
175 delete rep; |
|
176 |
|
177 Octave_map m; |
|
178 rep = new tree_constant_rep (m); |
|
179 rep->count = 1; |
|
180 } |
|
181 |
|
182 make_unique (); |
|
183 |
|
184 return rep; |
|
185 } |
|
186 |
|
187 tree_constant |
780
|
188 tree_constant::assign_map_element (SLList<char*>& list, |
782
|
189 tree_constant& rhs) |
747
|
190 { |
|
191 tree_constant_rep *tmp_rep = make_unique_map (); |
|
192 |
|
193 if (rhs.is_map ()) |
|
194 rhs.make_unique (); |
|
195 |
|
196 Pix p = list.first (); |
|
197 while (p) |
|
198 { |
|
199 char *elt = list (p); |
|
200 |
|
201 list.next (p); |
|
202 |
|
203 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
204 |
|
205 if (! p) |
|
206 { |
|
207 tmp = rhs; |
|
208 return tmp; |
|
209 } |
|
210 |
|
211 tmp_rep = tmp.make_unique_map (); |
|
212 } |
|
213 |
|
214 return tree_constant (); |
|
215 } |
|
216 |
|
217 // Indexed structure assignment. |
|
218 |
|
219 tree_constant |
780
|
220 tree_constant::assign_map_element (SLList<char*>& list, |
782
|
221 tree_constant& rhs, |
747
|
222 const Octave_object& args) |
|
223 { |
|
224 tree_constant_rep *tmp_rep = make_unique_map (); |
|
225 |
|
226 if (rhs.is_map ()) |
|
227 rhs.make_unique (); |
|
228 |
|
229 Pix p = list.first (); |
|
230 while (p) |
|
231 { |
|
232 char *elt = list (p); |
|
233 |
|
234 list.next (p); |
|
235 |
|
236 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
237 |
|
238 if (! p) |
|
239 { |
|
240 tmp.assign (rhs, args); |
|
241 return tmp; |
|
242 } |
|
243 |
|
244 tmp_rep = tmp.make_unique_map (); |
|
245 } |
|
246 |
|
247 return tree_constant (); |
|
248 } |
|
249 |
|
250 void |
|
251 tree_constant::print_code (ostream& os) |
|
252 { |
|
253 print_code_indent (os); |
|
254 |
|
255 if (in_parens) |
|
256 os << "("; |
|
257 |
|
258 if (rep) |
|
259 rep->print_code (os); |
|
260 |
|
261 if (in_parens) |
|
262 os << ")"; |
|
263 } |
|
264 |
1199
|
265 int |
|
266 print_as_scalar (const tree_constant& val) |
|
267 { |
|
268 int nr = val.rows (); |
|
269 int nc = val.columns (); |
|
270 return (val.is_scalar_type () |
|
271 || val.is_string () |
|
272 || (val.is_matrix_type () |
|
273 && ((nr == 1 && nc == 1) |
|
274 || nr == 0 |
|
275 || nc == 0))); |
|
276 } |
|
277 |
|
278 int |
|
279 print_as_structure (const tree_constant& val) |
|
280 { |
|
281 return val.is_map (); |
|
282 } |
|
283 |
581
|
284 // Construct return vector of empty matrices. Return empty matrices |
|
285 // and/or gripe when appropriate. |
|
286 |
500
|
287 Octave_object |
164
|
288 vector_of_empties (int nargout, const char *fcn_name) |
96
|
289 { |
500
|
290 Octave_object retval; |
96
|
291 |
|
292 // Got an empty argument, check if should gripe/return empty values. |
|
293 |
|
294 int flag = user_pref.propagate_empty_matrices; |
|
295 if (flag != 0) |
|
296 { |
|
297 if (flag < 0) |
|
298 gripe_empty_arg (fcn_name, 0); |
|
299 |
|
300 Matrix m; |
500
|
301 retval.resize (nargout ? nargout : 1); |
96
|
302 for (int i = 0; i < nargout; i++) |
529
|
303 retval(i) = m; |
96
|
304 } |
|
305 else |
|
306 gripe_empty_arg (fcn_name, 1); |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 /* |
1
|
312 ;;; Local Variables: *** |
|
313 ;;; mode: C++ *** |
|
314 ;;; page-delimiter: "^/\\*" *** |
|
315 ;;; End: *** |
|
316 */ |