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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
3223
|
53 octave_scalar : public octave_base_scalar<double> |
2376
|
54 { |
|
55 public: |
|
56 |
|
57 octave_scalar (void) |
3223
|
58 : octave_base_scalar<double> (0.0) { } |
2376
|
59 |
|
60 octave_scalar (double d) |
3223
|
61 : octave_base_scalar<double> (d) { } |
2376
|
62 |
|
63 octave_scalar (const octave_scalar& s) |
3223
|
64 : octave_base_scalar<double> (s) { } |
2376
|
65 |
|
66 ~octave_scalar (void) { } |
|
67 |
5759
|
68 octave_base_value *clone (void) const { return new octave_scalar (*this); } |
4749
|
69 |
|
70 // We return an octave_matrix here instead of an octave_scalar so |
|
71 // that in expressions like A(2,2,2) = 2 (for A previously |
|
72 // undefined), A will be empty instead of a 1x1 object. |
5759
|
73 octave_base_value *empty_clone (void) const { return new octave_matrix (); } |
2376
|
74 |
5885
|
75 octave_value do_index_op (const octave_value_list& idx, |
|
76 bool resize_ok = false); |
2376
|
77 |
|
78 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
79 |
5110
|
80 octave_value any (int = 0) const |
|
81 { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } |
|
82 |
2376
|
83 bool is_real_scalar (void) const { return true; } |
|
84 |
|
85 bool is_real_type (void) const { return true; } |
|
86 |
5895
|
87 bool is_double_type (void) const { return true; } |
|
88 |
2376
|
89 bool valid_as_scalar_index (void) const |
5042
|
90 { |
|
91 return (! xisnan (scalar) |
|
92 && D_NINT (scalar) == scalar |
5275
|
93 && NINTbig (scalar) == 1); |
5042
|
94 } |
2376
|
95 |
|
96 bool valid_as_zero_index (void) const |
5042
|
97 { |
|
98 return (! xisnan (scalar) |
|
99 && D_NINT (scalar) == scalar |
5275
|
100 && NINTbig (scalar) == 0); |
5042
|
101 } |
2376
|
102 |
5533
|
103 int8NDArray |
|
104 int8_array_value (void) const |
|
105 { return int8NDArray (dim_vector (1, 1), scalar); } |
|
106 |
|
107 int16NDArray |
|
108 int16_array_value (void) const |
|
109 { return int16NDArray (dim_vector (1, 1), scalar); } |
|
110 |
|
111 int32NDArray |
|
112 int32_array_value (void) const |
|
113 { return int32NDArray (dim_vector (1, 1), scalar); } |
|
114 |
|
115 int64NDArray |
|
116 int64_array_value (void) const |
|
117 { return int64NDArray (dim_vector (1, 1), scalar); } |
|
118 |
|
119 uint8NDArray |
|
120 uint8_array_value (void) const |
|
121 { return uint8NDArray (dim_vector (1, 1), scalar); } |
|
122 |
|
123 uint16NDArray |
|
124 uint16_array_value (void) const |
|
125 { return uint16NDArray (dim_vector (1, 1), scalar); } |
|
126 |
|
127 uint32NDArray |
|
128 uint32_array_value (void) const |
|
129 { return uint32NDArray (dim_vector (1, 1), scalar); } |
|
130 |
|
131 uint64NDArray |
|
132 uint64_array_value (void) const |
|
133 { return uint64NDArray (dim_vector (1, 1), scalar); } |
|
134 |
2376
|
135 double double_value (bool = false) const { return scalar; } |
|
136 |
2916
|
137 double scalar_value (bool = false) const { return scalar; } |
|
138 |
3145
|
139 Matrix matrix_value (bool = false) const |
|
140 { return Matrix (1, 1, scalar); } |
2376
|
141 |
4569
|
142 NDArray array_value (bool = false) const |
4630
|
143 { return NDArray (dim_vector (1, 1), scalar); } |
4505
|
144 |
5731
|
145 octave_value resize (const dim_vector& dv, bool fill = false) const; |
4915
|
146 |
2376
|
147 Complex complex_value (bool = false) const { return scalar; } |
|
148 |
|
149 ComplexMatrix complex_matrix_value (bool = false) const |
|
150 { return ComplexMatrix (1, 1, Complex (scalar)); } |
|
151 |
4569
|
152 ComplexNDArray complex_array_value (bool = false) const |
4630
|
153 { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); } |
4569
|
154 |
5533
|
155 charNDArray |
|
156 char_array_value (bool = false) const |
|
157 { |
|
158 charNDArray retval (dim_vector (1, 1)); |
|
159 retval(0) = static_cast<char> (scalar); |
|
160 return retval; |
|
161 } |
|
162 |
5943
|
163 bool bool_value (bool warn = false) const |
|
164 { |
|
165 if (warn && scalar != 0 && scalar != 1) |
|
166 gripe_logical_conversion (); |
|
167 |
|
168 return scalar; |
|
169 } |
5533
|
170 |
5943
|
171 boolNDArray bool_array_value (bool warn = false) const |
|
172 { |
|
173 if (warn && scalar != 0 && scalar != 1) |
|
174 gripe_logical_conversion (); |
|
175 |
|
176 return boolNDArray (dim_vector (1, 1), scalar); |
|
177 } |
5533
|
178 |
4645
|
179 std::streamoff streamoff_value (void) const; |
|
180 |
4701
|
181 streamoff_array streamoff_array_value (void) const; |
|
182 |
5279
|
183 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
3223
|
184 |
2376
|
185 void increment (void) { ++scalar; } |
|
186 |
|
187 void decrement (void) { --scalar; } |
|
188 |
5958
|
189 bool save_ascii (std::ostream& os, bool& infnan_warned); |
4687
|
190 |
|
191 bool load_ascii (std::istream& is); |
|
192 |
|
193 bool save_binary (std::ostream& os, bool& save_as_floats); |
|
194 |
|
195 bool load_binary (std::istream& is, bool swap, |
|
196 oct_mach_info::float_format fmt); |
|
197 |
|
198 #if defined (HAVE_HDF5) |
|
199 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
|
200 |
|
201 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
|
202 #endif |
|
203 |
4944
|
204 int write (octave_stream& os, int block_size, |
|
205 oct_data_conv::data_type output_type, int skip, |
|
206 oct_mach_info::float_format flt_fmt) const |
|
207 { |
|
208 return os.write (array_value (), block_size, output_type, |
|
209 skip, flt_fmt); |
|
210 } |
|
211 |
5900
|
212 mxArray *as_mxArray (void) const; |
|
213 |
2376
|
214 private: |
|
215 |
4612
|
216 DECLARE_OCTAVE_ALLOCATOR |
2477
|
217 |
4612
|
218 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376
|
219 }; |
|
220 |
|
221 #endif |
|
222 |
|
223 /* |
|
224 ;;; Local Variables: *** |
|
225 ;;; mode: C++ *** |
|
226 ;;; End: *** |
|
227 */ |