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