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