529
|
1 // tree-const.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
455
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
581
|
32 #include <iostream.h> |
|
33 |
493
|
34 #include "tree-const.h" |
1
|
35 #include "error.h" |
|
36 #include "gripes.h" |
529
|
37 #include "user-prefs.h" |
747
|
38 #include "oct-map.h" |
|
39 |
|
40 Octave_map |
|
41 tree_constant::map_value (void) const |
|
42 { |
|
43 return rep->map_value (); |
|
44 } |
1
|
45 |
|
46 tree_constant::~tree_constant (void) |
|
47 { |
|
48 #if defined (MDEBUG) |
|
49 cerr << "~tree_constant: rep: " << rep |
|
50 << " rep->count: " << rep->count << "\n"; |
|
51 #endif |
|
52 |
|
53 if (--rep->count <= 0) |
|
54 { |
|
55 delete rep; |
531
|
56 rep = 0; |
1
|
57 } |
|
58 } |
|
59 |
|
60 #if defined (MDEBUG) |
|
61 void * |
|
62 tree_constant::operator new (size_t size) |
|
63 { |
|
64 tree_constant *p = ::new tree_constant; |
|
65 cerr << "tree_constant::new(): " << p << "\n"; |
|
66 return p; |
|
67 } |
|
68 |
|
69 void |
|
70 tree_constant::operator delete (void *p, size_t size) |
|
71 { |
|
72 cerr << "tree_constant::delete(): " << p << "\n"; |
|
73 ::delete p; |
|
74 } |
|
75 #endif |
|
76 |
747
|
77 // Simple assignment. |
|
78 |
|
79 tree_constant |
|
80 tree_constant::operator = (const tree_constant& a) |
|
81 { |
|
82 if (rep != a.rep) |
|
83 { |
|
84 if (--rep->count <= 0) |
|
85 delete rep; |
|
86 rep = a.rep; |
|
87 rep->count++; |
|
88 } |
|
89 return *this; |
|
90 } |
|
91 |
|
92 tree_constant |
|
93 tree_constant::lookup_map_element (SLList<char*>& list) |
|
94 { |
|
95 tree_constant retval; |
|
96 |
|
97 tree_constant_rep *tmp_rep = rep; |
|
98 |
|
99 Pix p = list.first (); |
|
100 while (p) |
|
101 { |
|
102 char *elt = list (p); |
|
103 |
|
104 list.next (p); |
|
105 |
|
106 tree_constant tmp = tmp_rep->lookup_map_element (elt); |
|
107 |
|
108 if (error_state) |
|
109 break; |
|
110 |
|
111 tmp_rep = tmp.rep; |
|
112 |
|
113 if (! p) |
|
114 retval = tmp; |
|
115 } |
|
116 |
|
117 return retval; |
|
118 } |
|
119 |
|
120 // Simple structure assignment. |
|
121 |
|
122 void |
|
123 tree_constant::make_unique (void) |
|
124 { |
|
125 if (rep->count > 1) |
|
126 { |
|
127 --rep->count; |
|
128 rep = new tree_constant_rep (*rep); |
|
129 rep->count = 1; |
|
130 } |
|
131 |
|
132 if (rep->is_map ()) |
|
133 { |
|
134 for (Pix p = rep->a_map->first (); p != 0; rep->a_map->next (p)) |
|
135 { |
|
136 rep->a_map->contents (p) . make_unique (); |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 tree_constant::tree_constant_rep * |
|
142 tree_constant::make_unique_map (void) |
|
143 { |
|
144 if (! rep->is_map ()) |
|
145 { |
|
146 if (--rep->count <= 0) |
|
147 delete rep; |
|
148 |
|
149 Octave_map m; |
|
150 rep = new tree_constant_rep (m); |
|
151 rep->count = 1; |
|
152 } |
|
153 |
|
154 make_unique (); |
|
155 |
|
156 return rep; |
|
157 } |
|
158 |
|
159 tree_constant |
780
|
160 tree_constant::assign_map_element (SLList<char*>& list, |
782
|
161 tree_constant& rhs) |
747
|
162 { |
|
163 tree_constant_rep *tmp_rep = make_unique_map (); |
|
164 |
|
165 if (rhs.is_map ()) |
|
166 rhs.make_unique (); |
|
167 |
|
168 Pix p = list.first (); |
|
169 while (p) |
|
170 { |
|
171 char *elt = list (p); |
|
172 |
|
173 list.next (p); |
|
174 |
|
175 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
176 |
|
177 if (! p) |
|
178 { |
|
179 tmp = rhs; |
|
180 return tmp; |
|
181 } |
|
182 |
|
183 tmp_rep = tmp.make_unique_map (); |
|
184 } |
|
185 |
|
186 return tree_constant (); |
|
187 } |
|
188 |
|
189 // Indexed structure assignment. |
|
190 |
|
191 tree_constant |
780
|
192 tree_constant::assign_map_element (SLList<char*>& list, |
782
|
193 tree_constant& rhs, |
747
|
194 const Octave_object& args) |
|
195 { |
|
196 tree_constant_rep *tmp_rep = make_unique_map (); |
|
197 |
|
198 if (rhs.is_map ()) |
|
199 rhs.make_unique (); |
|
200 |
|
201 Pix p = list.first (); |
|
202 while (p) |
|
203 { |
|
204 char *elt = list (p); |
|
205 |
|
206 list.next (p); |
|
207 |
|
208 tree_constant& tmp = tmp_rep->lookup_map_element (elt, 1); |
|
209 |
|
210 if (! p) |
|
211 { |
|
212 tmp.assign (rhs, args); |
|
213 return tmp; |
|
214 } |
|
215 |
|
216 tmp_rep = tmp.make_unique_map (); |
|
217 } |
|
218 |
|
219 return tree_constant (); |
|
220 } |
|
221 |
|
222 void |
|
223 tree_constant::print_code (ostream& os) |
|
224 { |
|
225 print_code_indent (os); |
|
226 |
|
227 if (in_parens) |
|
228 os << "("; |
|
229 |
|
230 if (rep) |
|
231 rep->print_code (os); |
|
232 |
|
233 if (in_parens) |
|
234 os << ")"; |
|
235 } |
|
236 |
581
|
237 // Construct return vector of empty matrices. Return empty matrices |
|
238 // and/or gripe when appropriate. |
|
239 |
500
|
240 Octave_object |
164
|
241 vector_of_empties (int nargout, const char *fcn_name) |
96
|
242 { |
500
|
243 Octave_object retval; |
96
|
244 |
|
245 // Got an empty argument, check if should gripe/return empty values. |
|
246 |
|
247 int flag = user_pref.propagate_empty_matrices; |
|
248 if (flag != 0) |
|
249 { |
|
250 if (flag < 0) |
|
251 gripe_empty_arg (fcn_name, 0); |
|
252 |
|
253 Matrix m; |
500
|
254 retval.resize (nargout ? nargout : 1); |
96
|
255 for (int i = 0; i < nargout; i++) |
529
|
256 retval(i) = m; |
96
|
257 } |
|
258 else |
|
259 gripe_empty_arg (fcn_name, 1); |
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
|
264 /* |
1
|
265 ;;; Local Variables: *** |
|
266 ;;; mode: C++ *** |
|
267 ;;; page-delimiter: "^/\\*" *** |
|
268 ;;; End: *** |
|
269 */ |