00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if !defined (octave_range_h)
00025 #define octave_range_h 1
00026
00027 #include <cstdlib>
00028
00029 #include <iosfwd>
00030 #include <string>
00031
00032 #include "Range.h"
00033
00034 #include "lo-mappers.h"
00035 #include "lo-utils.h"
00036 #include "mx-base.h"
00037 #include "oct-alloc.h"
00038 #include "str-vec.h"
00039
00040 #include "error.h"
00041 #include "oct-stream.h"
00042 #include "ov-base.h"
00043 #include "ov-re-mat.h"
00044 #include "ov-typeinfo.h"
00045
00046 class Octave_map;
00047 class octave_value_list;
00048
00049 class tree_walker;
00050
00051
00052
00053 class
00054 octave_range : public octave_base_value
00055 {
00056 public:
00057
00058 octave_range (void)
00059 : octave_base_value (), idx_cache () { }
00060
00061 octave_range (double base, double limit, double inc)
00062 : octave_base_value (), range (base, limit, inc), idx_cache ()
00063 {
00064 if (range.nelem () < 0)
00065 ::error ("invalid range");
00066 }
00067
00068 octave_range (const Range& r)
00069 : octave_base_value (), range (r), idx_cache ()
00070 {
00071 if (range.nelem () < 0 && range.nelem () != -2)
00072 ::error ("invalid range");
00073 }
00074
00075 octave_range (const octave_range& r)
00076 : octave_base_value (), range (r.range),
00077 idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : 0)
00078 { }
00079
00080 octave_range (const Range& r, const idx_vector& cache)
00081 : octave_base_value (), range (r), idx_cache ()
00082 {
00083 set_idx_cache (cache);
00084 }
00085
00086 ~octave_range (void) { clear_cached_info (); }
00087
00088 octave_base_value *clone (void) const { return new octave_range (*this); }
00089
00090
00091
00092
00093 octave_base_value *empty_clone (void) const { return new octave_matrix (); }
00094
00095 type_conv_info numeric_conversion_function (void) const;
00096
00097 octave_base_value *try_narrowing_conversion (void);
00098
00099 octave_value subsref (const std::string& type,
00100 const std::list<octave_value_list>& idx);
00101
00102 octave_value_list subsref (const std::string& type,
00103 const std::list<octave_value_list>& idx, int)
00104 { return subsref (type, idx); }
00105
00106 octave_value do_index_op (const octave_value_list& idx,
00107 bool resize_ok = false);
00108
00109 idx_vector index_vector (void) const
00110 { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (range)); }
00111
00112 dim_vector dims (void) const
00113 {
00114 octave_idx_type n = range.nelem ();
00115 return dim_vector (n > 0, n);
00116 }
00117
00118 octave_value resize (const dim_vector& dv, bool fill = false) const;
00119
00120
00121 size_t byte_size (void) const { return 3 * sizeof (double); }
00122
00123 octave_value reshape (const dim_vector& new_dims) const
00124 { return NDArray (array_value().reshape (new_dims)); }
00125
00126 octave_value permute (const Array<int>& vec, bool inv = false) const
00127 { return NDArray (array_value().permute (vec, inv)); }
00128
00129 octave_value squeeze (void) const { return range; }
00130
00131 octave_value full_value (void) const { return range.matrix_value (); }
00132
00133 bool is_defined (void) const { return true; }
00134
00135 bool is_constant (void) const { return true; }
00136
00137 bool is_range (void) const { return true; }
00138
00139 octave_value all (int dim = 0) const;
00140
00141 octave_value any (int dim = 0) const;
00142
00143 octave_value diag (octave_idx_type k = 0) const;
00144
00145 octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
00146 { return range.sort (dim, mode); }
00147
00148 octave_value sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
00149 sortmode mode = ASCENDING) const
00150 { return range.sort (sidx, dim, mode); }
00151
00152 sortmode is_sorted (sortmode mode = UNSORTED) const
00153 { return range.is_sorted (mode); }
00154
00155 Array<octave_idx_type> sort_rows_idx (sortmode) const
00156 { return Array<octave_idx_type> (1, 0); }
00157
00158 sortmode is_sorted_rows (sortmode mode = UNSORTED) const
00159 { return mode ? mode : ASCENDING; }
00160
00161 builtin_type_t builtin_type (void) const { return btyp_double; }
00162
00163 bool is_real_type (void) const { return true; }
00164
00165 bool is_double_type (void) const { return true; }
00166
00167 bool is_float_type (void) const { return true; }
00168
00169 bool is_numeric_type (void) const { return true; }
00170
00171 bool is_true (void) const;
00172
00173 double double_value (bool = false) const;
00174
00175 float float_value (bool = false) const;
00176
00177 double scalar_value (bool frc_str_conv = false) const
00178 { return double_value (frc_str_conv); }
00179
00180 float float_scalar_value (bool frc_str_conv = false) const
00181 { return float_value (frc_str_conv); }
00182
00183 Matrix matrix_value (bool = false) const
00184 { return range.matrix_value (); }
00185
00186 FloatMatrix float_matrix_value (bool = false) const
00187 { return range.matrix_value (); }
00188
00189 NDArray array_value (bool = false) const
00190 { return range.matrix_value (); }
00191
00192 FloatNDArray float_array_value (bool = false) const
00193 { return FloatMatrix (range.matrix_value ()); }
00194
00195 charNDArray char_array_value (bool = false) const;
00196
00197
00198
00199
00200
00201 int8NDArray
00202 int8_array_value (void) const { return int8NDArray (array_value ()); }
00203
00204 int16NDArray
00205 int16_array_value (void) const { return int16NDArray (array_value ()); }
00206
00207 int32NDArray
00208 int32_array_value (void) const { return int32NDArray (array_value ()); }
00209
00210 int64NDArray
00211 int64_array_value (void) const { return int64NDArray (array_value ()); }
00212
00213 uint8NDArray
00214 uint8_array_value (void) const { return uint8NDArray (array_value ()); }
00215
00216 uint16NDArray
00217 uint16_array_value (void) const { return uint16NDArray (array_value ()); }
00218
00219 uint32NDArray
00220 uint32_array_value (void) const { return uint32NDArray (array_value ()); }
00221
00222 uint64NDArray
00223 uint64_array_value (void) const { return uint64NDArray (array_value ()); }
00224
00225 SparseMatrix sparse_matrix_value (bool = false) const
00226 { return SparseMatrix (range.matrix_value ()); }
00227
00228 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00229 { return SparseComplexMatrix (sparse_matrix_value ()); }
00230
00231 Complex complex_value (bool = false) const;
00232
00233 FloatComplex float_complex_value (bool = false) const;
00234
00235 boolNDArray bool_array_value (bool warn = false) const
00236 {
00237 Matrix m = range.matrix_value ();
00238
00239 if (m.any_element_is_nan ())
00240 error ("invalid conversion from NaN to logical");
00241 else if (warn && m.any_element_not_one_or_zero ())
00242 gripe_logical_conversion ();
00243
00244 return boolNDArray (m);
00245 }
00246
00247 ComplexMatrix complex_matrix_value (bool = false) const
00248 { return ComplexMatrix (range.matrix_value ()); }
00249
00250 FloatComplexMatrix float_complex_matrix_value (bool = false) const
00251 { return FloatComplexMatrix (range.matrix_value ()); }
00252
00253 ComplexNDArray complex_array_value (bool = false) const
00254 { return ComplexMatrix (range.matrix_value ()); }
00255
00256 FloatComplexNDArray float_complex_array_value (bool = false) const
00257 { return FloatComplexMatrix (range.matrix_value ()); }
00258
00259 Range range_value (void) const { return range; }
00260
00261 octave_value convert_to_str_internal (bool pad, bool force, char type) const;
00262
00263 void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00264
00265 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00266
00267 bool print_name_tag (std::ostream& os, const std::string& name) const;
00268
00269 bool save_ascii (std::ostream& os);
00270
00271 bool load_ascii (std::istream& is);
00272
00273 bool save_binary (std::ostream& os, bool& save_as_floats);
00274
00275 bool load_binary (std::istream& is, bool swap,
00276 oct_mach_info::float_format fmt);
00277
00278 #if defined (HAVE_HDF5)
00279 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00280
00281 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug);
00282 #endif
00283
00284 int write (octave_stream& os, int block_size,
00285 oct_data_conv::data_type output_type, int skip,
00286 oct_mach_info::float_format flt_fmt) const
00287 {
00288
00289
00290
00291 return os.write (matrix_value (), block_size, output_type, skip,
00292 flt_fmt);
00293 }
00294
00295 mxArray *as_mxArray (void) const;
00296
00297
00298 #define RANGE_MAPPER(MAP) \
00299 octave_value MAP (void) const \
00300 { \
00301 octave_matrix m (array_value ()); \
00302 return m.MAP (); \
00303 }
00304
00305 RANGE_MAPPER (abs)
00306 RANGE_MAPPER (acos)
00307 RANGE_MAPPER (acosh)
00308 RANGE_MAPPER (angle)
00309 RANGE_MAPPER (arg)
00310 RANGE_MAPPER (asin)
00311 RANGE_MAPPER (asinh)
00312 RANGE_MAPPER (atan)
00313 RANGE_MAPPER (atanh)
00314 RANGE_MAPPER (ceil)
00315 RANGE_MAPPER (conj)
00316 RANGE_MAPPER (cos)
00317 RANGE_MAPPER (cosh)
00318 RANGE_MAPPER (erf)
00319 RANGE_MAPPER (erfc)
00320 RANGE_MAPPER (exp)
00321 RANGE_MAPPER (expm1)
00322 RANGE_MAPPER (finite)
00323 RANGE_MAPPER (fix)
00324 RANGE_MAPPER (floor)
00325 RANGE_MAPPER (gamma)
00326 RANGE_MAPPER (imag)
00327 RANGE_MAPPER (isinf)
00328 RANGE_MAPPER (isna)
00329 RANGE_MAPPER (isnan)
00330 RANGE_MAPPER (lgamma)
00331 RANGE_MAPPER (log)
00332 RANGE_MAPPER (log2)
00333 RANGE_MAPPER (log10)
00334 RANGE_MAPPER (log1p)
00335 RANGE_MAPPER (real)
00336 RANGE_MAPPER (round)
00337 RANGE_MAPPER (roundb)
00338 RANGE_MAPPER (signum)
00339 RANGE_MAPPER (sin)
00340 RANGE_MAPPER (sinh)
00341 RANGE_MAPPER (sqrt)
00342 RANGE_MAPPER (tan)
00343 RANGE_MAPPER (tanh)
00344
00345 private:
00346
00347 Range range;
00348
00349 idx_vector set_idx_cache (const idx_vector& idx) const
00350 {
00351 delete idx_cache;
00352 idx_cache = idx ? new idx_vector (idx) : 0;
00353 return idx;
00354 }
00355
00356 void clear_cached_info (void) const
00357 {
00358 delete idx_cache; idx_cache = 0;
00359 }
00360
00361 mutable idx_vector *idx_cache;
00362
00363 DECLARE_OCTAVE_ALLOCATOR
00364
00365 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00366 };
00367
00368 #endif
00369
00370
00371
00372
00373
00374