529
|
1 // tree-const.h -*- 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 |
383
|
24 #if !defined (octave_tree_const_h) |
|
25 #define octave_tree_const_h 1 |
1
|
26 |
455
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
1
|
31 #include <stdlib.h> |
|
32 |
500
|
33 #include "mx-base.h" |
|
34 #include "Range.h" |
|
35 |
1
|
36 #include "tree-base.h" |
495
|
37 #include "tree-expr.h" |
|
38 #include "tc-rep.h" |
500
|
39 #include "oct-obj.h" |
164
|
40 |
|
41 class idx_vector; |
1
|
42 |
529
|
43 struct Mapper_fcn; |
|
44 |
1
|
45 /* |
495
|
46 * Constants. |
1
|
47 */ |
|
48 class |
495
|
49 tree_constant : public tree_fvc |
1
|
50 { |
|
51 friend class tree_constant_rep; |
|
52 |
|
53 public: |
|
54 tree_constant (void) |
|
55 { rep = new tree_constant_rep (); rep->count = 1; } |
|
56 |
|
57 tree_constant (double d) |
|
58 { rep = new tree_constant_rep (d); rep->count = 1; } |
161
|
59 tree_constant (const Matrix& m) |
1
|
60 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
61 tree_constant (const DiagMatrix& d) |
1
|
62 { rep = new tree_constant_rep (d); rep->count = 1; } |
455
|
63 tree_constant (const RowVector& v, int pcv = -1) |
1
|
64 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
455
|
65 tree_constant (const ColumnVector& v, int pcv = -1) |
1
|
66 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
67 |
161
|
68 tree_constant (const Complex& c) |
1
|
69 { rep = new tree_constant_rep (c); rep->count = 1; } |
161
|
70 tree_constant (const ComplexMatrix& m) |
1
|
71 { rep = new tree_constant_rep (m); rep->count = 1; } |
161
|
72 tree_constant (const ComplexDiagMatrix& d) |
1
|
73 { rep = new tree_constant_rep (d); rep->count = 1; } |
455
|
74 tree_constant (const ComplexRowVector& v, int pcv = -1) |
1
|
75 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
455
|
76 tree_constant (const ComplexColumnVector& v, int pcv = -1) |
1
|
77 { rep = new tree_constant_rep (v, pcv); rep->count = 1; } |
|
78 |
|
79 tree_constant (const char *s) |
|
80 { rep = new tree_constant_rep (s); rep->count = 1; } |
|
81 |
|
82 tree_constant (double base, double limit, double inc) |
|
83 { rep = new tree_constant_rep (base, limit, inc); rep->count = 1; } |
161
|
84 tree_constant (const Range& r) |
1
|
85 { rep = new tree_constant_rep (r); rep->count = 1; } |
|
86 |
|
87 tree_constant (tree_constant_rep::constant_type t) |
|
88 { rep = new tree_constant_rep (t); rep->count = 1; } |
|
89 |
161
|
90 tree_constant (const tree_constant& a) |
1
|
91 { rep = a.rep; rep->count++; } |
|
92 tree_constant (tree_constant_rep& r) |
|
93 { rep = &r; rep->count++; } |
|
94 |
|
95 ~tree_constant (void); |
|
96 |
|
97 #if defined (MDEBUG) |
|
98 void *operator new (size_t size); |
|
99 void operator delete (void *p, size_t size); |
|
100 #endif |
|
101 |
529
|
102 tree_constant operator = (const tree_constant& a) |
1
|
103 { |
|
104 if (--rep->count <= 0 && rep != a.rep) |
|
105 delete rep; |
|
106 |
|
107 rep = a.rep; |
|
108 rep->count++; |
|
109 return *this; |
|
110 } |
|
111 |
164
|
112 int is_constant (void) const { return 1; } |
1
|
113 |
164
|
114 int is_scalar_type (void) const { return rep->is_scalar_type (); } |
|
115 int is_matrix_type (void) const { return rep->is_matrix_type (); } |
1
|
116 |
164
|
117 int is_real_type (void) const { return rep->is_real_type (); } |
|
118 int is_complex_type (void) const { return rep->is_complex_type (); } |
1
|
119 |
164
|
120 int is_numeric_type (void) const { return rep->is_numeric_type (); } |
1
|
121 |
164
|
122 int is_numeric_or_range_type (void) const |
1
|
123 { return rep->is_numeric_or_range_type (); } |
|
124 |
164
|
125 int is_string_type (void) const { return rep->is_string_type (); } |
1
|
126 |
164
|
127 int valid_as_scalar_index (void) const |
|
128 { return rep->valid_as_scalar_index (); } |
1
|
129 |
164
|
130 int is_defined (void) const { return rep->is_defined (); } |
|
131 int is_undefined (void) const { return rep->is_undefined (); } |
1
|
132 |
164
|
133 double to_scalar (void) const { return rep->to_scalar (); } |
|
134 ColumnVector to_vector (void) const { return rep->to_vector (); } |
|
135 Matrix to_matrix (void) const { return rep->to_matrix (); } |
1
|
136 |
|
137 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0) |
|
138 { return rep->force_numeric (force_str_conv); } |
|
139 |
164
|
140 tree_constant make_numeric (int force_str_conv = 0) const |
1
|
141 { |
|
142 if (is_numeric_type ()) |
|
143 return *this; |
|
144 else |
|
145 return rep->make_numeric (force_str_conv); |
|
146 } |
|
147 |
164
|
148 tree_constant make_numeric_or_range (void) const |
1
|
149 { |
|
150 if (is_numeric_type () |
|
151 || rep->type_tag == tree_constant_rep::range_constant) |
|
152 return *this; |
|
153 else |
|
154 return rep->make_numeric (); |
|
155 } |
|
156 |
164
|
157 tree_constant make_numeric_or_magic (void) const |
1
|
158 { |
|
159 if (is_numeric_type () |
|
160 || rep->type_tag == tree_constant_rep::magic_colon) |
|
161 return *this; |
|
162 else |
|
163 return rep->make_numeric (); |
|
164 } |
|
165 |
164
|
166 tree_constant make_numeric_or_range_or_magic (void) const |
1
|
167 { |
|
168 if (is_numeric_type () |
|
169 || rep->type_tag == tree_constant_rep::magic_colon |
|
170 || rep->type_tag == tree_constant_rep::range_constant) |
|
171 return *this; |
|
172 else |
|
173 return rep->make_numeric (); |
|
174 } |
|
175 |
506
|
176 tree_constant assign (tree_constant& rhs, const Octave_object& args) |
1
|
177 { |
|
178 if (rep->count > 1) |
|
179 { |
|
180 --rep->count; |
|
181 rep = new tree_constant_rep (*rep); |
|
182 rep->count = 1; |
|
183 } |
506
|
184 rep->assign (rhs, args); |
1
|
185 return *this; |
|
186 } |
|
187 |
276
|
188 int save (ostream& os, int mark_as_global = 0, int precision = 17) |
|
189 { return rep->save (os, mark_as_global, precision); } |
1
|
190 int save_three_d (ostream& os, int parametric = 0) |
|
191 { return rep->save_three_d (os, parametric); } |
|
192 |
|
193 int load (istream& is) { return rep->load (is); } |
|
194 tree_constant_rep::constant_type load |
|
195 (istream& is, tree_constant_rep::constant_type t) |
|
196 { return rep->load (is, t); } |
|
197 |
164
|
198 double double_value (void) const { return rep->double_value (); } |
|
199 Matrix matrix_value (void) const { return rep->matrix_value (); } |
|
200 Complex complex_value (void) const { return rep->complex_value (); } |
|
201 ComplexMatrix complex_matrix_value (void) const |
1
|
202 { return rep->complex_matrix_value (); } |
164
|
203 char *string_value (void) const { return rep->string_value (); } |
|
204 Range range_value (void) const { return rep->range_value (); } |
1
|
205 |
164
|
206 int rows (void) const { return rep->rows (); } |
|
207 int columns (void) const { return rep->columns (); } |
1
|
208 |
208
|
209 int is_empty (void) const |
|
210 { |
|
211 return (rep->type_tag != tree_constant_rep::magic_colon |
|
212 && rep->type_tag != tree_constant_rep::unknown_constant |
|
213 && (rows () == 0 || columns () == 0)); |
|
214 } |
|
215 |
|
216 int is_zero_by_zero (void) const |
212
|
217 { |
|
218 return (rep->type_tag != tree_constant_rep::magic_colon |
|
219 && rep->type_tag != tree_constant_rep::unknown_constant |
|
220 && rows () == 0 |
|
221 && columns () == 0); |
|
222 } |
208
|
223 |
93
|
224 |
164
|
225 tree_constant all (void) const { return rep->all (); } |
|
226 tree_constant any (void) const { return rep->any (); } |
|
227 tree_constant isstr (void) const { return rep->isstr (); } |
1
|
228 |
|
229 tree_constant convert_to_str (void) { return rep->convert_to_str (); } |
|
230 |
435
|
231 void convert_to_row_or_column_vector (void) |
455
|
232 { rep->convert_to_row_or_column_vector (); } |
435
|
233 |
428
|
234 int is_true (void) const { return rep->is_true (); } |
|
235 |
164
|
236 tree_constant cumprod (void) const { return rep->cumprod (); } |
|
237 tree_constant cumsum (void) const { return rep->cumsum (); } |
|
238 tree_constant prod (void) const { return rep->prod (); } |
|
239 tree_constant sum (void) const { return rep->sum (); } |
|
240 tree_constant sumsq (void) const { return rep->sumsq (); } |
1
|
241 |
164
|
242 tree_constant diag (void) const { return rep->diag (); } |
|
243 tree_constant diag (const tree_constant& a) const { return rep->diag (a); } |
1
|
244 |
164
|
245 tree_constant_rep::constant_type const_type (void) const |
1
|
246 { return rep->const_type (); } |
|
247 |
164
|
248 tree_constant mapper (Mapper_fcn& m_fcn, int print) const |
1
|
249 { return rep->mapper (m_fcn, print); } |
|
250 |
578
|
251 void bump_value (tree_expression::type et) |
1
|
252 { |
|
253 if (rep->count > 1) |
|
254 { |
|
255 --rep->count; |
|
256 rep = new tree_constant_rep (*rep); |
|
257 rep->count = 1; |
|
258 } |
|
259 rep->bump_value (et); |
|
260 } |
|
261 |
|
262 tree_constant eval (int print) |
495
|
263 { |
|
264 rep->maybe_mutate (); |
|
265 if (print) |
|
266 rep->print (); |
|
267 return *this; |
|
268 } |
1
|
269 |
506
|
270 Octave_object eval (int print, int nargout, const Octave_object& args) |
1
|
271 { |
565
|
272 Octave_object retval; |
495
|
273 |
506
|
274 // XXX FIXME XXX -- make it safe to call do_index() with |
|
275 // args.length () == 0 |
|
276 if (args.length () > 0) |
|
277 retval(0) = rep->do_index (args); |
495
|
278 else |
500
|
279 retval(0) = *this; |
495
|
280 |
500
|
281 if (retval(0).is_defined ()) |
|
282 retval(0).eval (print); |
1
|
283 return retval; |
|
284 } |
|
285 |
|
286 private: |
|
287 tree_constant_rep *rep; |
|
288 }; |
|
289 |
529
|
290 // XXX FIXME XXX -- this is not used very much now. Perhaps it can be |
|
291 // eliminated. |
500
|
292 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
293 |
1
|
294 #endif |
|
295 |
|
296 /* |
|
297 ;;; Local Variables: *** |
|
298 ;;; mode: C++ *** |
|
299 ;;; page-delimiter: "^/\\*" *** |
|
300 ;;; End: *** |
|
301 */ |