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 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
2901
|
31 #include <iostream.h> |
|
32 |
2376
|
33 #include "lo-ieee.h" |
|
34 #include "lo-utils.h" |
|
35 |
|
36 #include "gripes.h" |
|
37 #include "ops.h" |
|
38 #include "ov-range.h" |
|
39 #include "ov-re-mat.h" |
2410
|
40 #include "ov-scalar.h" |
2376
|
41 #include "pr-output.h" |
|
42 |
3219
|
43 DEFINE_OCTAVE_ALLOCATOR (octave_range); |
2376
|
44 |
3219
|
45 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range"); |
2376
|
46 |
|
47 static octave_value * |
|
48 default_numeric_conversion_function (const octave_value& a) |
|
49 { |
|
50 CAST_CONV_ARG (const octave_range&); |
|
51 |
|
52 return new octave_matrix (v.matrix_value ()); |
|
53 } |
|
54 |
2427
|
55 type_conv_fcn |
2376
|
56 octave_range::numeric_conversion_function (void) const |
|
57 { |
|
58 return default_numeric_conversion_function; |
|
59 } |
|
60 |
2410
|
61 octave_value * |
|
62 octave_range::try_narrowing_conversion (void) |
|
63 { |
|
64 octave_value *retval = 0; |
|
65 |
|
66 switch (range.nelem ()) |
|
67 { |
|
68 case 1: |
|
69 retval = new octave_scalar (range.base ()); |
|
70 break; |
|
71 |
|
72 case 0: |
|
73 retval = new octave_matrix (Matrix ()); |
|
74 break; |
|
75 |
|
76 default: |
|
77 break; |
|
78 } |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
2436
|
83 octave_value |
2974
|
84 octave_range::do_index_op (const octave_value_list& idx) |
2436
|
85 { |
|
86 // XXX FIXME XXX -- this doesn't solve the problem of |
|
87 // |
|
88 // a = 1:5; a(1, 1, 1) |
|
89 // |
|
90 // and similar constructions. Hmm... |
|
91 |
|
92 // XXX FIXME XXX -- using this constructor avoids possibly narrowing |
|
93 // the range to a scalar value. Need a better solution to this |
|
94 // problem. |
|
95 |
|
96 octave_value tmp (new octave_matrix (range.matrix_value ())); |
|
97 |
2962
|
98 return tmp.do_index_op (idx); |
2436
|
99 } |
|
100 |
2376
|
101 double |
|
102 octave_range::double_value (bool) const |
|
103 { |
|
104 double retval = octave_NaN; |
|
105 |
|
106 int nel = range.nelem (); |
|
107 |
|
108 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
109 retval = range.base (); |
|
110 else |
|
111 gripe_invalid_conversion ("range", "real scalar"); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 octave_value |
|
117 octave_range::all (void) const |
|
118 { |
2436
|
119 // XXX FIXME XXX -- this is a potential waste of memory. |
|
120 |
|
121 Matrix m = range.matrix_value (); |
|
122 |
|
123 return m.all (); |
2376
|
124 } |
|
125 |
|
126 octave_value |
|
127 octave_range::any (void) const |
|
128 { |
2800
|
129 return static_cast<double> (range.base () != 0.0 || range.nelem () > 1); |
2376
|
130 } |
|
131 |
|
132 bool |
|
133 octave_range::is_true (void) const |
|
134 { |
|
135 bool retval = false; |
2436
|
136 |
|
137 if (range.nelem () == 0) |
|
138 { |
|
139 int flag = Vpropagate_empty_matrices; |
|
140 |
|
141 if (flag < 0) |
|
142 warning ("empty range used in conditional expression"); |
|
143 else if (flag == 0) |
|
144 error ("empty range used in conditional expression"); |
|
145 } |
|
146 else |
|
147 { |
|
148 // XXX FIXME XXX -- this is a potential waste of memory. |
|
149 |
|
150 Matrix m ((range.matrix_value () . all ()) . all ()); |
|
151 |
|
152 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
153 } |
|
154 |
2376
|
155 return retval; |
|
156 } |
|
157 |
|
158 Complex |
|
159 octave_range::complex_value (bool) const |
|
160 { |
|
161 Complex retval (octave_NaN, octave_NaN); |
|
162 |
|
163 int nel = range.nelem (); |
|
164 |
|
165 if (nel == 1 || (nel > 1 && Vdo_fortran_indexing)) |
|
166 retval = range.base (); |
|
167 else |
|
168 gripe_invalid_conversion ("range", "complex scalar"); |
|
169 |
|
170 return retval; |
|
171 } |
|
172 |
|
173 octave_value |
2449
|
174 octave_range::convert_to_str (void) const |
|
175 { |
|
176 octave_value tmp (range.matrix_value ()); |
|
177 return tmp.convert_to_str (); |
|
178 } |
|
179 |
2376
|
180 void |
2901
|
181 octave_range::print (ostream& os, bool pr_as_read_syntax) const |
|
182 { |
|
183 print_raw (os, pr_as_read_syntax); |
|
184 newline (os); |
|
185 } |
|
186 |
|
187 void |
|
188 octave_range::print_raw (ostream& os, bool pr_as_read_syntax) const |
|
189 { |
|
190 octave_print_internal (os, range, pr_as_read_syntax, |
|
191 current_print_indent_level ()); |
|
192 } |
|
193 |
|
194 bool |
|
195 octave_range::print_name_tag (ostream& os, const string& name) const |
2376
|
196 { |
2901
|
197 bool retval = false; |
|
198 |
|
199 int n = range.nelem (); |
|
200 |
|
201 indent (os); |
|
202 |
|
203 if (n == 0 || n == 1) |
|
204 os << name << " = "; |
|
205 else |
|
206 { |
|
207 os << name << " ="; |
|
208 newline (os); |
|
209 newline (os); |
|
210 retval = true; |
|
211 } |
|
212 |
|
213 return retval; |
2376
|
214 } |
|
215 |
|
216 /* |
|
217 ;;; Local Variables: *** |
|
218 ;;; mode: C++ *** |
|
219 ;;; End: *** |
|
220 */ |