ov-range.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_range_h)
00024 #define octave_range_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "Range.h"
00032 
00033 #include "lo-mappers.h"
00034 #include "lo-utils.h"
00035 #include "mx-base.h"
00036 #include "oct-alloc.h"
00037 #include "str-vec.h"
00038 
00039 #include "error.h"
00040 #include "oct-stream.h"
00041 #include "ov-base.h"
00042 #include "ov-re-mat.h"
00043 #include "ov-typeinfo.h"
00044 
00045 class octave_value_list;
00046 
00047 class tree_walker;
00048 
00049 // Range values.
00050 
00051 class
00052 octave_range : public octave_base_value
00053 {
00054 public:
00055 
00056   octave_range (void)
00057     : octave_base_value (), range (), idx_cache () { }
00058 
00059   octave_range (double base, double limit, double inc)
00060     : octave_base_value (), range (base, limit, inc), idx_cache ()
00061       {
00062         if (range.nelem () < 0)
00063           ::error ("invalid range");
00064       }
00065 
00066   octave_range (const Range& r)
00067     : octave_base_value (), range (r), idx_cache ()
00068       {
00069         if (range.nelem () < 0 && range.nelem () != -2)
00070           ::error ("invalid range");
00071       }
00072 
00073   octave_range (const octave_range& r)
00074     : octave_base_value (), range (r.range),
00075       idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : 0)
00076     { }
00077 
00078   octave_range (const Range& r, const idx_vector& cache)
00079     : octave_base_value (), range (r), idx_cache ()
00080       {
00081         set_idx_cache (cache);
00082       }
00083 
00084   ~octave_range (void) { clear_cached_info (); }
00085 
00086   octave_base_value *clone (void) const { return new octave_range (*this); }
00087 
00088   // A range is really just a special kind of real matrix object.  In
00089   // the places where we need to call empty_clone, it makes more sense
00090   // to create an empty matrix (0x0) instead of an empty range (1x0).
00091   octave_base_value *empty_clone (void) const { return new octave_matrix (); }
00092 
00093   type_conv_info numeric_conversion_function (void) const;
00094 
00095   octave_base_value *try_narrowing_conversion (void);
00096 
00097   octave_value subsref (const std::string& type,
00098                         const std::list<octave_value_list>& idx);
00099 
00100   octave_value_list subsref (const std::string& type,
00101                              const std::list<octave_value_list>& idx, int)
00102     { return subsref (type, idx); }
00103 
00104   octave_value do_index_op (const octave_value_list& idx,
00105                             bool resize_ok = false);
00106 
00107   idx_vector index_vector (void) const;
00108 
00109   dim_vector dims (void) const
00110     {
00111       octave_idx_type n = range.nelem ();
00112       return dim_vector (n > 0, n);
00113     }
00114 
00115   octave_value resize (const dim_vector& dv, bool fill = false) const;
00116 
00117 
00118   size_t byte_size (void) const { return 3 * sizeof (double); }
00119 
00120   octave_value reshape (const dim_vector& new_dims) const
00121     { return NDArray (array_value().reshape (new_dims)); }
00122 
00123   octave_value permute (const Array<int>& vec, bool inv = false) const
00124     { return NDArray (array_value().permute (vec, inv)); }
00125 
00126   octave_value squeeze (void) const { return range; }
00127 
00128   octave_value full_value (void) const { return range.matrix_value (); }
00129 
00130   bool is_defined (void) const { return true; }
00131 
00132   bool is_constant (void) const { return true; }
00133 
00134   bool is_range (void) const { return true; }
00135 
00136   octave_value all (int dim = 0) const;
00137 
00138   octave_value any (int dim = 0) const;
00139 
00140   octave_value diag (octave_idx_type k = 0) const;
00141 
00142   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
00143     { return range.sort (dim, mode); }
00144 
00145   octave_value sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
00146                      sortmode mode = ASCENDING) const
00147     { return range.sort (sidx, dim, mode); }
00148 
00149   sortmode is_sorted (sortmode mode = UNSORTED) const
00150     { return range.is_sorted (mode); }
00151 
00152   Array<octave_idx_type> sort_rows_idx (sortmode) const
00153     { return Array<octave_idx_type> (dim_vector (1, 0)); }
00154 
00155   sortmode is_sorted_rows (sortmode mode = UNSORTED) const
00156     { return mode ? mode : ASCENDING; }
00157 
00158   builtin_type_t builtin_type (void) const { return btyp_double; }
00159 
00160   bool is_real_type (void) const { return true; }
00161 
00162   bool is_double_type (void) const { return true; }
00163 
00164   bool is_float_type (void) const { return true; }
00165 
00166   bool is_numeric_type (void) const { return true; }
00167 
00168   bool is_true (void) const;
00169 
00170   double double_value (bool = false) const;
00171 
00172   float float_value (bool = false) const;
00173 
00174   double scalar_value (bool frc_str_conv = false) const
00175     { return double_value (frc_str_conv); }
00176 
00177   float float_scalar_value (bool frc_str_conv = false) const
00178     { return float_value (frc_str_conv); }
00179 
00180   Matrix matrix_value (bool = false) const
00181     { return range.matrix_value (); }
00182 
00183   FloatMatrix float_matrix_value (bool = false) const
00184     { return range.matrix_value (); }
00185 
00186   NDArray array_value (bool = false) const
00187     { return range.matrix_value (); }
00188 
00189   FloatNDArray float_array_value (bool = false) const
00190     { return FloatMatrix (range.matrix_value ()); }
00191 
00192   charNDArray char_array_value (bool = false) const;
00193 
00194   // FIXME -- it would be better to have Range::intXNDArray_value
00195   // functions to avoid the intermediate conversion to a matrix
00196   // object.
00197 
00198   int8NDArray
00199   int8_array_value (void) const { return int8NDArray (array_value ()); }
00200 
00201   int16NDArray
00202   int16_array_value (void) const { return int16NDArray (array_value ()); }
00203 
00204   int32NDArray
00205   int32_array_value (void) const { return int32NDArray (array_value ()); }
00206 
00207   int64NDArray
00208   int64_array_value (void) const { return int64NDArray (array_value ()); }
00209 
00210   uint8NDArray
00211   uint8_array_value (void) const { return uint8NDArray (array_value ()); }
00212 
00213   uint16NDArray
00214   uint16_array_value (void) const { return uint16NDArray (array_value ()); }
00215 
00216   uint32NDArray
00217   uint32_array_value (void) const { return uint32NDArray (array_value ()); }
00218 
00219   uint64NDArray
00220   uint64_array_value (void) const { return uint64NDArray (array_value ()); }
00221 
00222   SparseMatrix sparse_matrix_value (bool = false) const
00223     { return SparseMatrix (range.matrix_value ()); }
00224 
00225   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00226     { return SparseComplexMatrix (sparse_matrix_value ()); }
00227 
00228   Complex complex_value (bool = false) const;
00229 
00230   FloatComplex float_complex_value (bool = false) const;
00231 
00232   boolNDArray bool_array_value (bool warn = false) const;
00233 
00234   ComplexMatrix complex_matrix_value (bool = false) const
00235     { return ComplexMatrix (range.matrix_value ()); }
00236 
00237   FloatComplexMatrix float_complex_matrix_value (bool = false) const
00238     { return FloatComplexMatrix (range.matrix_value ()); }
00239 
00240   ComplexNDArray complex_array_value (bool = false) const
00241     { return ComplexMatrix (range.matrix_value ()); }
00242 
00243   FloatComplexNDArray float_complex_array_value (bool = false) const
00244     { return FloatComplexMatrix (range.matrix_value ()); }
00245 
00246   Range range_value (void) const { return range; }
00247 
00248   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
00249 
00250   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00251 
00252   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00253 
00254   bool print_name_tag (std::ostream& os, const std::string& name) const;
00255 
00256   bool save_ascii (std::ostream& os);
00257 
00258   bool load_ascii (std::istream& is);
00259 
00260   bool save_binary (std::ostream& os, bool& save_as_floats);
00261 
00262   bool load_binary (std::istream& is, bool swap,
00263                     oct_mach_info::float_format fmt);
00264 
00265 #if defined (HAVE_HDF5)
00266   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00267 
00268   bool load_hdf5 (hid_t loc_id, const char *name);
00269 #endif
00270 
00271   int write (octave_stream& os, int block_size,
00272              oct_data_conv::data_type output_type, int skip,
00273              oct_mach_info::float_format flt_fmt) const
00274     {
00275       // FIXME -- could be more memory efficient by having a
00276       // special case of the octave_stream::write method for ranges.
00277 
00278       return os.write (matrix_value (), block_size, output_type, skip,
00279                        flt_fmt);
00280     }
00281 
00282   mxArray *as_mxArray (void) const;
00283 
00284   octave_value map (unary_mapper_t umap) const
00285     {
00286       octave_matrix m (matrix_value ());
00287       return m.map (umap);
00288     }
00289 
00290 private:
00291 
00292   Range range;
00293 
00294   idx_vector set_idx_cache (const idx_vector& idx) const
00295     {
00296       delete idx_cache;
00297       idx_cache = idx ? new idx_vector (idx) : 0;
00298       return idx;
00299     }
00300 
00301   void clear_cached_info (void) const
00302     {
00303       delete idx_cache; idx_cache = 0;
00304     }
00305 
00306   mutable idx_vector *idx_cache;
00307 
00308   // No assignment.
00309 
00310   octave_range& operator = (const octave_range&);
00311 
00312   DECLARE_OCTAVE_ALLOCATOR
00313 
00314   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00315 };
00316 
00317 // If TRUE, allow ranges with non-integer elements as array indices.
00318 extern bool Vallow_noninteger_range_as_index;
00319 
00320 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines