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 valid_as_scalar_index (void) const |
|
125 { return rep->valid_as_scalar_index (); } |
1
|
126 |
605
|
127 // What type of constant am I? |
|
128 |
|
129 int is_unknown (void) const { return rep->is_unknown (); } |
|
130 int is_scalar (void) const { return rep->is_scalar (); } |
|
131 int is_matrix (void) const { return rep->is_matrix (); } |
|
132 int is_complex_scalar (void) const { return rep->is_complex_scalar (); } |
|
133 int is_complex_matrix (void) const { return rep->is_complex_matrix (); } |
|
134 int is_string (void) const { return rep->is_string (); } |
|
135 int is_range (void) const { return rep->is_range (); } |
|
136 |
164
|
137 int is_defined (void) const { return rep->is_defined (); } |
|
138 int is_undefined (void) const { return rep->is_undefined (); } |
1
|
139 |
164
|
140 double to_scalar (void) const { return rep->to_scalar (); } |
|
141 ColumnVector to_vector (void) const { return rep->to_vector (); } |
|
142 Matrix to_matrix (void) const { return rep->to_matrix (); } |
1
|
143 |
581
|
144 void stash_original_text (char *s) |
|
145 { rep->stash_original_text (s); } |
|
146 |
1
|
147 tree_constant_rep::constant_type force_numeric (int force_str_conv = 0) |
|
148 { return rep->force_numeric (force_str_conv); } |
|
149 |
164
|
150 tree_constant make_numeric (int force_str_conv = 0) const |
1
|
151 { |
|
152 if (is_numeric_type ()) |
|
153 return *this; |
|
154 else |
|
155 return rep->make_numeric (force_str_conv); |
|
156 } |
|
157 |
164
|
158 tree_constant make_numeric_or_range (void) const |
1
|
159 { |
|
160 if (is_numeric_type () |
|
161 || rep->type_tag == tree_constant_rep::range_constant) |
|
162 return *this; |
|
163 else |
|
164 return rep->make_numeric (); |
|
165 } |
|
166 |
164
|
167 tree_constant make_numeric_or_magic (void) const |
1
|
168 { |
|
169 if (is_numeric_type () |
|
170 || rep->type_tag == tree_constant_rep::magic_colon) |
|
171 return *this; |
|
172 else |
|
173 return rep->make_numeric (); |
|
174 } |
|
175 |
164
|
176 tree_constant make_numeric_or_range_or_magic (void) const |
1
|
177 { |
|
178 if (is_numeric_type () |
|
179 || rep->type_tag == tree_constant_rep::magic_colon |
|
180 || rep->type_tag == tree_constant_rep::range_constant) |
|
181 return *this; |
|
182 else |
|
183 return rep->make_numeric (); |
|
184 } |
|
185 |
506
|
186 tree_constant assign (tree_constant& rhs, const Octave_object& args) |
1
|
187 { |
|
188 if (rep->count > 1) |
|
189 { |
|
190 --rep->count; |
|
191 rep = new tree_constant_rep (*rep); |
|
192 rep->count = 1; |
|
193 } |
506
|
194 rep->assign (rhs, args); |
1
|
195 return *this; |
|
196 } |
|
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 |
581
|
286 void print_code (ostream& os); |
|
287 |
1
|
288 private: |
|
289 tree_constant_rep *rep; |
|
290 }; |
|
291 |
529
|
292 // XXX FIXME XXX -- this is not used very much now. Perhaps it can be |
|
293 // eliminated. |
500
|
294 extern Octave_object vector_of_empties (int nargout, const char *fcn_name); |
94
|
295 |
1
|
296 #endif |
|
297 |
|
298 /* |
|
299 ;;; Local Variables: *** |
|
300 ;;; mode: C++ *** |
|
301 ;;; page-delimiter: "^/\\*" *** |
|
302 ;;; End: *** |
|
303 */ |