2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2901
|
32 |
2376
|
33 #include "lo-ieee.h" |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "gripes.h" |
|
37 #include "ops.h" |
3933
|
38 #include "oct-obj.h" |
2376
|
39 #include "ov-range.h" |
|
40 #include "ov-re-mat.h" |
2410
|
41 #include "ov-scalar.h" |
2376
|
42 #include "pr-output.h" |
|
43 |
3219
|
44 DEFINE_OCTAVE_ALLOCATOR (octave_range); |
2376
|
45 |
3219
|
46 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range"); |
2376
|
47 |
|
48 static octave_value * |
|
49 default_numeric_conversion_function (const octave_value& a) |
|
50 { |
|
51 CAST_CONV_ARG (const octave_range&); |
|
52 |
|
53 return new octave_matrix (v.matrix_value ()); |
|
54 } |
|
55 |
2427
|
56 type_conv_fcn |
2376
|
57 octave_range::numeric_conversion_function (void) const |
|
58 { |
|
59 return default_numeric_conversion_function; |
|
60 } |
|
61 |
2410
|
62 octave_value * |
|
63 octave_range::try_narrowing_conversion (void) |
|
64 { |
|
65 octave_value *retval = 0; |
|
66 |
|
67 switch (range.nelem ()) |
|
68 { |
|
69 case 1: |
|
70 retval = new octave_scalar (range.base ()); |
|
71 break; |
|
72 |
|
73 case 0: |
4417
|
74 retval = new octave_matrix (Matrix (1, 0)); |
2410
|
75 break; |
|
76 |
|
77 default: |
|
78 break; |
|
79 } |
|
80 |
|
81 return retval; |
|
82 } |
|
83 |
2436
|
84 octave_value |
4247
|
85 octave_range::subsref (const std::string& type, |
4219
|
86 const std::list<octave_value_list>& idx) |
3933
|
87 { |
|
88 octave_value retval; |
|
89 |
|
90 switch (type[0]) |
|
91 { |
|
92 case '(': |
|
93 retval = do_index_op (idx.front ()); |
|
94 break; |
|
95 |
|
96 case '{': |
|
97 case '.': |
|
98 { |
|
99 std::string nm = type_name (); |
|
100 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
101 } |
|
102 break; |
|
103 |
|
104 default: |
|
105 panic_impossible (); |
|
106 } |
|
107 |
|
108 return retval.next_subsref (type, idx); |
|
109 } |
|
110 |
|
111 octave_value |
|
112 octave_range::do_index_op (const octave_value_list& idx, int resize_ok) |
2436
|
113 { |
|
114 // XXX FIXME XXX -- this doesn't solve the problem of |
|
115 // |
|
116 // a = 1:5; a(1, 1, 1) |
|
117 // |
|
118 // and similar constructions. Hmm... |
|
119 |
|
120 // XXX FIXME XXX -- using this constructor avoids possibly narrowing |
|
121 // the range to a scalar value. Need a better solution to this |
|
122 // problem. |
|
123 |
|
124 octave_value tmp (new octave_matrix (range.matrix_value ())); |
|
125 |
3933
|
126 return tmp.do_index_op (idx, resize_ok); |
2436
|
127 } |
|
128 |
2376
|
129 double |
|
130 octave_range::double_value (bool) const |
|
131 { |
4102
|
132 double retval = lo_ieee_nan_value (); |
2376
|
133 |
|
134 int nel = range.nelem (); |
|
135 |
4455
|
136 if (nel > 0) |
|
137 { |
|
138 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
139 if (Vwarn_fortran_indexing) |
|
140 gripe_implicit_conversion ("range", "real scalar"); |
|
141 |
|
142 retval = range.base (); |
|
143 } |
2376
|
144 else |
|
145 gripe_invalid_conversion ("range", "real scalar"); |
|
146 |
|
147 return retval; |
|
148 } |
|
149 |
|
150 octave_value |
4017
|
151 octave_range::all (int dim) const |
2376
|
152 { |
2436
|
153 // XXX FIXME XXX -- this is a potential waste of memory. |
|
154 |
|
155 Matrix m = range.matrix_value (); |
|
156 |
4017
|
157 return m.all (dim); |
2376
|
158 } |
|
159 |
|
160 octave_value |
4017
|
161 octave_range::any (int dim) const |
2376
|
162 { |
4017
|
163 // XXX FIXME XXX -- this is a potential waste of memory. |
|
164 |
|
165 Matrix m = range.matrix_value (); |
|
166 |
|
167 return m.any (dim); |
2376
|
168 } |
|
169 |
|
170 bool |
|
171 octave_range::is_true (void) const |
|
172 { |
|
173 bool retval = false; |
2436
|
174 |
|
175 if (range.nelem () == 0) |
|
176 { |
|
177 int flag = Vpropagate_empty_matrices; |
|
178 |
|
179 if (flag < 0) |
|
180 warning ("empty range used in conditional expression"); |
|
181 else if (flag == 0) |
|
182 error ("empty range used in conditional expression"); |
|
183 } |
|
184 else |
|
185 { |
|
186 // XXX FIXME XXX -- this is a potential waste of memory. |
|
187 |
|
188 Matrix m ((range.matrix_value () . all ()) . all ()); |
|
189 |
|
190 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
191 } |
|
192 |
2376
|
193 return retval; |
|
194 } |
|
195 |
|
196 Complex |
|
197 octave_range::complex_value (bool) const |
|
198 { |
4102
|
199 double tmp = lo_ieee_nan_value (); |
|
200 |
|
201 Complex retval (tmp, tmp); |
2376
|
202 |
|
203 int nel = range.nelem (); |
|
204 |
4455
|
205 if (nel > 0) |
|
206 { |
|
207 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
208 if (Vwarn_fortran_indexing) |
|
209 gripe_implicit_conversion ("range", "complex scalar"); |
|
210 |
|
211 retval = range.base (); |
|
212 } |
2376
|
213 else |
|
214 gripe_invalid_conversion ("range", "complex scalar"); |
|
215 |
|
216 return retval; |
|
217 } |
|
218 |
|
219 octave_value |
4457
|
220 octave_range::convert_to_str_internal (bool pad, bool force) const |
2449
|
221 { |
|
222 octave_value tmp (range.matrix_value ()); |
4457
|
223 return tmp.convert_to_str (pad, force); |
2449
|
224 } |
|
225 |
2376
|
226 void |
3523
|
227 octave_range::print (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
228 { |
|
229 print_raw (os, pr_as_read_syntax); |
|
230 newline (os); |
|
231 } |
|
232 |
|
233 void |
3523
|
234 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
235 { |
|
236 octave_print_internal (os, range, pr_as_read_syntax, |
|
237 current_print_indent_level ()); |
|
238 } |
|
239 |
|
240 bool |
3523
|
241 octave_range::print_name_tag (std::ostream& os, const std::string& name) const |
2376
|
242 { |
2901
|
243 bool retval = false; |
|
244 |
|
245 int n = range.nelem (); |
|
246 |
|
247 indent (os); |
|
248 |
|
249 if (n == 0 || n == 1) |
|
250 os << name << " = "; |
|
251 else |
|
252 { |
|
253 os << name << " ="; |
|
254 newline (os); |
|
255 newline (os); |
|
256 retval = true; |
|
257 } |
|
258 |
|
259 return retval; |
2376
|
260 } |
|
261 |
|
262 /* |
|
263 ;;; Local Variables: *** |
|
264 ;;; mode: C++ *** |
|
265 ;;; End: *** |
|
266 */ |