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 |
|
136 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
137 retval = range.base (); |
|
138 else |
|
139 gripe_invalid_conversion ("range", "real scalar"); |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
|
144 octave_value |
4017
|
145 octave_range::all (int dim) const |
2376
|
146 { |
2436
|
147 // XXX FIXME XXX -- this is a potential waste of memory. |
|
148 |
|
149 Matrix m = range.matrix_value (); |
|
150 |
4017
|
151 return m.all (dim); |
2376
|
152 } |
|
153 |
|
154 octave_value |
4017
|
155 octave_range::any (int dim) const |
2376
|
156 { |
4017
|
157 // XXX FIXME XXX -- this is a potential waste of memory. |
|
158 |
|
159 Matrix m = range.matrix_value (); |
|
160 |
|
161 return m.any (dim); |
2376
|
162 } |
|
163 |
|
164 bool |
|
165 octave_range::is_true (void) const |
|
166 { |
|
167 bool retval = false; |
2436
|
168 |
|
169 if (range.nelem () == 0) |
|
170 { |
|
171 int flag = Vpropagate_empty_matrices; |
|
172 |
|
173 if (flag < 0) |
|
174 warning ("empty range used in conditional expression"); |
|
175 else if (flag == 0) |
|
176 error ("empty range used in conditional expression"); |
|
177 } |
|
178 else |
|
179 { |
|
180 // XXX FIXME XXX -- this is a potential waste of memory. |
|
181 |
|
182 Matrix m ((range.matrix_value () . all ()) . all ()); |
|
183 |
|
184 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
185 } |
|
186 |
2376
|
187 return retval; |
|
188 } |
|
189 |
|
190 Complex |
|
191 octave_range::complex_value (bool) const |
|
192 { |
4102
|
193 double tmp = lo_ieee_nan_value (); |
|
194 |
|
195 Complex retval (tmp, tmp); |
2376
|
196 |
|
197 int nel = range.nelem (); |
|
198 |
|
199 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
200 retval = range.base (); |
|
201 else |
|
202 gripe_invalid_conversion ("range", "complex scalar"); |
|
203 |
|
204 return retval; |
|
205 } |
|
206 |
|
207 octave_value |
4452
|
208 octave_range::convert_to_str_internal (bool pad) const |
2449
|
209 { |
|
210 octave_value tmp (range.matrix_value ()); |
4358
|
211 return tmp.convert_to_str (pad); |
2449
|
212 } |
|
213 |
2376
|
214 void |
3523
|
215 octave_range::print (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
216 { |
|
217 print_raw (os, pr_as_read_syntax); |
|
218 newline (os); |
|
219 } |
|
220 |
|
221 void |
3523
|
222 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
223 { |
|
224 octave_print_internal (os, range, pr_as_read_syntax, |
|
225 current_print_indent_level ()); |
|
226 } |
|
227 |
|
228 bool |
3523
|
229 octave_range::print_name_tag (std::ostream& os, const std::string& name) const |
2376
|
230 { |
2901
|
231 bool retval = false; |
|
232 |
|
233 int n = range.nelem (); |
|
234 |
|
235 indent (os); |
|
236 |
|
237 if (n == 0 || n == 1) |
|
238 os << name << " = "; |
|
239 else |
|
240 { |
|
241 os << name << " ="; |
|
242 newline (os); |
|
243 newline (os); |
|
244 retval = true; |
|
245 } |
|
246 |
|
247 return retval; |
2376
|
248 } |
|
249 |
|
250 /* |
|
251 ;;; Local Variables: *** |
|
252 ;;; mode: C++ *** |
|
253 ;;; End: *** |
|
254 */ |