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