2376
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2376
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2376
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_scalar_h) |
|
25 #define octave_scalar_h 1 |
|
26 |
|
27 #include <cstdlib> |
|
28 |
3503
|
29 #include <iostream> |
2376
|
30 #include <string> |
|
31 |
5110
|
32 #include "lo-ieee.h" |
2889
|
33 #include "lo-mappers.h" |
2376
|
34 #include "lo-utils.h" |
|
35 #include "mx-base.h" |
2477
|
36 #include "oct-alloc.h" |
2376
|
37 #include "str-vec.h" |
|
38 |
5943
|
39 #include "gripes.h" |
2376
|
40 #include "ov-base.h" |
4749
|
41 #include "ov-re-mat.h" |
3223
|
42 #include "ov-base-scalar.h" |
2376
|
43 #include "ov-typeinfo.h" |
|
44 |
|
45 class Octave_map; |
|
46 class octave_value_list; |
|
47 |
|
48 class tree_walker; |
|
49 |
|
50 // Real scalar values. |
|
51 |
|
52 class |
6153
|
53 OCTINTERP_API |
3223
|
54 octave_scalar : public octave_base_scalar<double> |
2376
|
55 { |
|
56 public: |
|
57 |
|
58 octave_scalar (void) |
3223
|
59 : octave_base_scalar<double> (0.0) { } |
2376
|
60 |
|
61 octave_scalar (double d) |
3223
|
62 : octave_base_scalar<double> (d) { } |
2376
|
63 |
|
64 octave_scalar (const octave_scalar& s) |
3223
|
65 : octave_base_scalar<double> (s) { } |
2376
|
66 |
|
67 ~octave_scalar (void) { } |
|
68 |
5759
|
69 octave_base_value *clone (void) const { return new octave_scalar (*this); } |
4749
|
70 |
|
71 // We return an octave_matrix here instead of an octave_scalar so |
|
72 // that in expressions like A(2,2,2) = 2 (for A previously |
|
73 // undefined), A will be empty instead of a 1x1 object. |
5759
|
74 octave_base_value *empty_clone (void) const { return new octave_matrix (); } |
2376
|
75 |
5885
|
76 octave_value do_index_op (const octave_value_list& idx, |
|
77 bool resize_ok = false); |
2376
|
78 |
|
79 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
80 |
5110
|
81 octave_value any (int = 0) const |
|
82 { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } |
|
83 |
2376
|
84 bool is_real_scalar (void) const { return true; } |
|
85 |
|
86 bool is_real_type (void) const { return true; } |
|
87 |
5895
|
88 bool is_double_type (void) const { return true; } |
|
89 |
2376
|
90 bool valid_as_scalar_index (void) const |
5042
|
91 { |
|
92 return (! xisnan (scalar) |
|
93 && D_NINT (scalar) == scalar |
5275
|
94 && NINTbig (scalar) == 1); |
5042
|
95 } |
2376
|
96 |
|
97 bool valid_as_zero_index (void) const |
5042
|
98 { |
|
99 return (! xisnan (scalar) |
|
100 && D_NINT (scalar) == scalar |
5275
|
101 && NINTbig (scalar) == 0); |
5042
|
102 } |
2376
|
103 |
5533
|
104 int8NDArray |
|
105 int8_array_value (void) const |
|
106 { return int8NDArray (dim_vector (1, 1), scalar); } |
|
107 |
|
108 int16NDArray |
|
109 int16_array_value (void) const |
|
110 { return int16NDArray (dim_vector (1, 1), scalar); } |
|
111 |
|
112 int32NDArray |
|
113 int32_array_value (void) const |
|
114 { return int32NDArray (dim_vector (1, 1), scalar); } |
|
115 |
|
116 int64NDArray |
|
117 int64_array_value (void) const |
|
118 { return int64NDArray (dim_vector (1, 1), scalar); } |
|
119 |
|
120 uint8NDArray |
|
121 uint8_array_value (void) const |
|
122 { return uint8NDArray (dim_vector (1, 1), scalar); } |
|
123 |
|
124 uint16NDArray |
|
125 uint16_array_value (void) const |
|
126 { return uint16NDArray (dim_vector (1, 1), scalar); } |
|
127 |
|
128 uint32NDArray |
|
129 uint32_array_value (void) const |
|
130 { return uint32NDArray (dim_vector (1, 1), scalar); } |
|
131 |
|
132 uint64NDArray |
|
133 uint64_array_value (void) const |
|
134 { return uint64NDArray (dim_vector (1, 1), scalar); } |
|
135 |
2376
|
136 double double_value (bool = false) const { return scalar; } |
|
137 |
2916
|
138 double scalar_value (bool = false) const { return scalar; } |
|
139 |
3145
|
140 Matrix matrix_value (bool = false) const |
|
141 { return Matrix (1, 1, scalar); } |
2376
|
142 |
4569
|
143 NDArray array_value (bool = false) const |
4630
|
144 { return NDArray (dim_vector (1, 1), scalar); } |
4505
|
145 |
5989
|
146 SparseMatrix sparse_matrix_value (bool = false) const |
|
147 { return SparseMatrix (Matrix (1, 1, scalar)); } |
|
148 |
|
149 // XXX FIXME XXX Need SparseComplexMatrix (Matrix) constructor!!! |
|
150 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const |
|
151 { return SparseComplexMatrix (sparse_matrix_value ()); } |
|
152 |
5731
|
153 octave_value resize (const dim_vector& dv, bool fill = false) const; |
4915
|
154 |
2376
|
155 Complex complex_value (bool = false) const { return scalar; } |
|
156 |
|
157 ComplexMatrix complex_matrix_value (bool = false) const |
|
158 { return ComplexMatrix (1, 1, Complex (scalar)); } |
|
159 |
4569
|
160 ComplexNDArray complex_array_value (bool = false) const |
4630
|
161 { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); } |
4569
|
162 |
5533
|
163 charNDArray |
|
164 char_array_value (bool = false) const |
|
165 { |
|
166 charNDArray retval (dim_vector (1, 1)); |
|
167 retval(0) = static_cast<char> (scalar); |
|
168 return retval; |
|
169 } |
|
170 |
5943
|
171 bool bool_value (bool warn = false) const |
|
172 { |
|
173 if (warn && scalar != 0 && scalar != 1) |
|
174 gripe_logical_conversion (); |
|
175 |
|
176 return scalar; |
|
177 } |
5533
|
178 |
5943
|
179 boolNDArray bool_array_value (bool warn = false) const |
|
180 { |
|
181 if (warn && scalar != 0 && scalar != 1) |
|
182 gripe_logical_conversion (); |
|
183 |
|
184 return boolNDArray (dim_vector (1, 1), scalar); |
|
185 } |
5533
|
186 |
4645
|
187 std::streamoff streamoff_value (void) const; |
|
188 |
4701
|
189 streamoff_array streamoff_array_value (void) const; |
|
190 |
5279
|
191 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
3223
|
192 |
2376
|
193 void increment (void) { ++scalar; } |
|
194 |
|
195 void decrement (void) { --scalar; } |
|
196 |
6974
|
197 bool save_ascii (std::ostream& os); |
4687
|
198 |
|
199 bool load_ascii (std::istream& is); |
|
200 |
|
201 bool save_binary (std::ostream& os, bool& save_as_floats); |
|
202 |
|
203 bool load_binary (std::istream& is, bool swap, |
|
204 oct_mach_info::float_format fmt); |
|
205 |
|
206 #if defined (HAVE_HDF5) |
|
207 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
|
208 |
|
209 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
|
210 #endif |
|
211 |
4944
|
212 int write (octave_stream& os, int block_size, |
|
213 oct_data_conv::data_type output_type, int skip, |
|
214 oct_mach_info::float_format flt_fmt) const |
|
215 { |
|
216 return os.write (array_value (), block_size, output_type, |
|
217 skip, flt_fmt); |
|
218 } |
|
219 |
5900
|
220 mxArray *as_mxArray (void) const; |
|
221 |
2376
|
222 private: |
|
223 |
4612
|
224 DECLARE_OCTAVE_ALLOCATOR |
2477
|
225 |
4612
|
226 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376
|
227 }; |
|
228 |
|
229 #endif |
|
230 |
|
231 /* |
|
232 ;;; Local Variables: *** |
|
233 ;;; mode: C++ *** |
|
234 ;;; End: *** |
|
235 */ |