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