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