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 |
|
39 #include "ov-base.h" |
4749
|
40 #include "ov-re-mat.h" |
3223
|
41 #include "ov-base-scalar.h" |
2376
|
42 #include "ov-typeinfo.h" |
|
43 |
|
44 class Octave_map; |
|
45 class octave_value_list; |
|
46 |
|
47 class tree_walker; |
|
48 |
|
49 // Real scalar values. |
|
50 |
|
51 class |
3223
|
52 octave_scalar : public octave_base_scalar<double> |
2376
|
53 { |
|
54 public: |
|
55 |
|
56 octave_scalar (void) |
3223
|
57 : octave_base_scalar<double> (0.0) { } |
2376
|
58 |
|
59 octave_scalar (double d) |
3223
|
60 : octave_base_scalar<double> (d) { } |
2376
|
61 |
|
62 octave_scalar (const octave_scalar& s) |
3223
|
63 : octave_base_scalar<double> (s) { } |
2376
|
64 |
|
65 ~octave_scalar (void) { } |
|
66 |
3933
|
67 octave_value *clone (void) const { return new octave_scalar (*this); } |
4749
|
68 |
|
69 // We return an octave_matrix here instead of an octave_scalar so |
|
70 // that in expressions like A(2,2,2) = 2 (for A previously |
|
71 // undefined), A will be empty instead of a 1x1 object. |
|
72 octave_value *empty_clone (void) const { return new octave_matrix (); } |
2376
|
73 |
3933
|
74 octave_value do_index_op (const octave_value_list& idx, int resize_ok); |
2376
|
75 |
|
76 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
77 |
5110
|
78 octave_value any (int = 0) const |
|
79 { return (scalar != 0 && ! lo_ieee_isnan (scalar)); } |
|
80 |
2376
|
81 bool is_real_scalar (void) const { return true; } |
|
82 |
|
83 bool is_real_type (void) const { return true; } |
|
84 |
|
85 bool valid_as_scalar_index (void) const |
5042
|
86 { |
|
87 return (! xisnan (scalar) |
|
88 && D_NINT (scalar) == scalar |
5275
|
89 && NINTbig (scalar) == 1); |
5042
|
90 } |
2376
|
91 |
|
92 bool valid_as_zero_index (void) const |
5042
|
93 { |
|
94 return (! xisnan (scalar) |
|
95 && D_NINT (scalar) == scalar |
5275
|
96 && NINTbig (scalar) == 0); |
5042
|
97 } |
2376
|
98 |
|
99 double double_value (bool = false) const { return scalar; } |
|
100 |
2916
|
101 double scalar_value (bool = false) const { return scalar; } |
|
102 |
3145
|
103 Matrix matrix_value (bool = false) const |
|
104 { return Matrix (1, 1, scalar); } |
2376
|
105 |
4569
|
106 NDArray array_value (bool = false) const |
4630
|
107 { return NDArray (dim_vector (1, 1), scalar); } |
4505
|
108 |
4915
|
109 octave_value resize (const dim_vector& dv) const |
4944
|
110 { |
|
111 NDArray retval (dv); |
|
112 |
|
113 if (dv.numel ()) |
|
114 retval(0) = scalar; |
|
115 |
|
116 return retval; |
|
117 } |
4915
|
118 |
2376
|
119 Complex complex_value (bool = false) const { return scalar; } |
|
120 |
|
121 ComplexMatrix complex_matrix_value (bool = false) const |
|
122 { return ComplexMatrix (1, 1, Complex (scalar)); } |
|
123 |
4569
|
124 ComplexNDArray complex_array_value (bool = false) const |
4630
|
125 { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); } |
4569
|
126 |
4645
|
127 std::streamoff streamoff_value (void) const; |
|
128 |
4701
|
129 streamoff_array streamoff_array_value (void) const; |
|
130 |
5279
|
131 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
3223
|
132 |
2376
|
133 void increment (void) { ++scalar; } |
|
134 |
|
135 void decrement (void) { --scalar; } |
|
136 |
4687
|
137 bool save_ascii (std::ostream& os, bool& infnan_warned, |
|
138 bool strip_nan_and_inf); |
|
139 |
|
140 bool load_ascii (std::istream& is); |
|
141 |
|
142 bool save_binary (std::ostream& os, bool& save_as_floats); |
|
143 |
|
144 bool load_binary (std::istream& is, bool swap, |
|
145 oct_mach_info::float_format fmt); |
|
146 |
|
147 #if defined (HAVE_HDF5) |
|
148 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
|
149 |
|
150 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
|
151 #endif |
|
152 |
4944
|
153 int write (octave_stream& os, int block_size, |
|
154 oct_data_conv::data_type output_type, int skip, |
|
155 oct_mach_info::float_format flt_fmt) const |
|
156 { |
|
157 return os.write (array_value (), block_size, output_type, |
|
158 skip, flt_fmt); |
|
159 } |
|
160 |
2376
|
161 private: |
|
162 |
4612
|
163 DECLARE_OCTAVE_ALLOCATOR |
2477
|
164 |
4612
|
165 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376
|
166 }; |
|
167 |
|
168 #endif |
|
169 |
|
170 /* |
|
171 ;;; Local Variables: *** |
|
172 ;;; mode: C++ *** |
|
173 ;;; End: *** |
|
174 */ |