ov-scalar.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_scalar_h)
00024 #define octave_scalar_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "lo-ieee.h"
00032 #include "lo-mappers.h"
00033 #include "lo-utils.h"
00034 #include "mx-base.h"
00035 #include "oct-alloc.h"
00036 #include "str-vec.h"
00037 
00038 #include "gripes.h"
00039 #include "ov-base.h"
00040 #include "ov-re-mat.h"
00041 #include "ov-base-scalar.h"
00042 #include "ov-typeinfo.h"
00043 
00044 class octave_value_list;
00045 
00046 class tree_walker;
00047 
00048 // Real scalar values.
00049 
00050 class
00051 OCTINTERP_API
00052 octave_scalar : public octave_base_scalar<double>
00053 {
00054 public:
00055 
00056   octave_scalar (void)
00057     : octave_base_scalar<double> (0.0) { }
00058 
00059   octave_scalar (double d)
00060     : octave_base_scalar<double> (d) { }
00061 
00062   octave_scalar (const octave_scalar& s)
00063     : octave_base_scalar<double> (s) { }
00064 
00065   ~octave_scalar (void) { }
00066 
00067   octave_base_value *clone (void) const { return new octave_scalar (*this); }
00068 
00069   // We return an octave_matrix here instead of an octave_scalar so
00070   // that in expressions like A(2,2,2) = 2 (for A previously
00071   // undefined), A will be empty instead of a 1x1 object.
00072   octave_base_value *empty_clone (void) const { return new octave_matrix (); }
00073 
00074   octave_value do_index_op (const octave_value_list& idx,
00075                             bool resize_ok = false);
00076 
00077   type_conv_info numeric_demotion_function (void) const;
00078 
00079   idx_vector index_vector (void) const { return idx_vector (scalar); }
00080 
00081   octave_value any (int = 0) const
00082     { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
00083 
00084   builtin_type_t builtin_type (void) const { return btyp_double; }
00085 
00086   bool is_real_scalar (void) const { return true; }
00087 
00088   bool is_real_type (void) const { return true; }
00089 
00090   bool is_double_type (void) const { return true; }
00091 
00092   bool is_float_type (void) const { return true; }
00093 
00094   int8NDArray
00095   int8_array_value (void) const
00096     { return int8NDArray (dim_vector (1, 1), scalar); }
00097 
00098   int16NDArray
00099   int16_array_value (void) const
00100     { return int16NDArray (dim_vector (1, 1), scalar); }
00101 
00102   int32NDArray
00103   int32_array_value (void) const
00104     { return int32NDArray (dim_vector (1, 1), scalar); }
00105 
00106   int64NDArray
00107   int64_array_value (void) const
00108     { return int64NDArray (dim_vector (1, 1), scalar); }
00109 
00110   uint8NDArray
00111   uint8_array_value (void) const
00112     { return uint8NDArray (dim_vector (1, 1), scalar); }
00113 
00114   uint16NDArray
00115   uint16_array_value (void) const
00116     { return uint16NDArray (dim_vector (1, 1), scalar); }
00117 
00118   uint32NDArray
00119   uint32_array_value (void) const
00120     { return uint32NDArray (dim_vector (1, 1), scalar); }
00121 
00122   uint64NDArray
00123   uint64_array_value (void) const
00124     { return uint64NDArray (dim_vector (1, 1), scalar); }
00125 
00126 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
00127   octave_ ## TYPE \
00128   TYPE ## _scalar_value (void) const \
00129     { return octave_ ## TYPE (scalar); }
00130 
00131   DEFINE_INT_SCALAR_VALUE (int8)
00132   DEFINE_INT_SCALAR_VALUE (int16)
00133   DEFINE_INT_SCALAR_VALUE (int32)
00134   DEFINE_INT_SCALAR_VALUE (int64)
00135   DEFINE_INT_SCALAR_VALUE (uint8)
00136   DEFINE_INT_SCALAR_VALUE (uint16)
00137   DEFINE_INT_SCALAR_VALUE (uint32)
00138   DEFINE_INT_SCALAR_VALUE (uint64)
00139 
00140 #undef DEFINE_INT_SCALAR_VALUE
00141 
00142   double double_value (bool = false) const { return scalar; }
00143 
00144   float float_value (bool = false) const { return static_cast<float> (scalar); }
00145 
00146   double scalar_value (bool = false) const { return scalar; }
00147 
00148   float float_scalar_value (bool = false) const { return static_cast<float> (scalar); }
00149 
00150   Matrix matrix_value (bool = false) const
00151     { return Matrix (1, 1, scalar); }
00152 
00153   FloatMatrix float_matrix_value (bool = false) const
00154     { return FloatMatrix (1, 1, scalar); }
00155 
00156   NDArray array_value (bool = false) const
00157     { return NDArray (dim_vector (1, 1), scalar); }
00158 
00159   FloatNDArray float_array_value (bool = false) const
00160     { return FloatNDArray (dim_vector (1, 1), scalar); }
00161 
00162   SparseMatrix sparse_matrix_value (bool = false) const
00163     { return SparseMatrix (Matrix (1, 1, scalar)); }
00164 
00165   // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
00166   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00167     { return SparseComplexMatrix (sparse_matrix_value ()); }
00168 
00169   octave_value resize (const dim_vector& dv, bool fill = false) const;
00170 
00171   Complex complex_value (bool = false) const { return scalar; }
00172 
00173   FloatComplex float_complex_value (bool = false) const { return scalar; }
00174 
00175   ComplexMatrix complex_matrix_value (bool = false) const
00176     { return  ComplexMatrix (1, 1, Complex (scalar)); }
00177 
00178   FloatComplexMatrix float_complex_matrix_value (bool = false) const
00179     { return  FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
00180 
00181   ComplexNDArray complex_array_value (bool = false) const
00182     { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
00183 
00184   FloatComplexNDArray float_complex_array_value (bool = false) const
00185     { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
00186 
00187   charNDArray
00188   char_array_value (bool = false) const
00189   {
00190     charNDArray retval (dim_vector (1, 1));
00191     retval(0) = static_cast<char> (scalar);
00192     return retval;
00193   }
00194 
00195   bool bool_value (bool warn = false) const
00196   {
00197     if (xisnan (scalar))
00198       gripe_nan_to_logical_conversion ();
00199     else if (warn && scalar != 0 && scalar != 1)
00200       gripe_logical_conversion ();
00201 
00202     return scalar;
00203   }
00204 
00205   boolNDArray bool_array_value (bool warn = false) const
00206   {
00207     if (xisnan (scalar))
00208       gripe_nan_to_logical_conversion ();
00209     else if (warn && scalar != 0 && scalar != 1)
00210       gripe_logical_conversion ();
00211 
00212     return boolNDArray (dim_vector (1, 1), scalar);
00213   }
00214 
00215   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
00216 
00217   void increment (void) { ++scalar; }
00218 
00219   void decrement (void) { --scalar; }
00220 
00221   bool save_ascii (std::ostream& os);
00222 
00223   bool load_ascii (std::istream& is);
00224 
00225   bool save_binary (std::ostream& os, bool& save_as_floats);
00226 
00227   bool load_binary (std::istream& is, bool swap,
00228                     oct_mach_info::float_format fmt);
00229 
00230 #if defined (HAVE_HDF5)
00231   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00232 
00233   bool load_hdf5 (hid_t loc_id, const char *name);
00234 #endif
00235 
00236   int write (octave_stream& os, int block_size,
00237              oct_data_conv::data_type output_type, int skip,
00238              oct_mach_info::float_format flt_fmt) const
00239     {
00240       return os.write (array_value (), block_size, output_type,
00241                        skip, flt_fmt);
00242     }
00243 
00244   mxArray *as_mxArray (void) const;
00245 
00246   octave_value map (unary_mapper_t umap) const;
00247 
00248   bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
00249 
00250 private:
00251 
00252   DECLARE_OCTAVE_ALLOCATOR
00253 
00254   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00255 };
00256 
00257 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines