Range.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1993-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 <iosfwd>
00027 
00028 #include "dMatrix.h"
00029 #include "oct-sort.h"
00030 
00031 class
00032 OCTAVE_API
00033 Range
00034 {
00035  public:
00036 
00037   Range (void)
00038     : rng_base (0), rng_limit (0), rng_inc (0), rng_nelem (0), cache (1, 0) { }
00039 
00040   Range (const Range& r)
00041     : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc),
00042       rng_nelem (r.rng_nelem), cache (r.cache) { }
00043 
00044   Range (double b, double l)
00045     : rng_base (b), rng_limit (l), rng_inc (1),
00046       rng_nelem (nelem_internal ()), cache () { }
00047 
00048   Range (double b, double l, double i)
00049     : rng_base (b), rng_limit (l), rng_inc (i),
00050       rng_nelem (nelem_internal ()), cache () { }
00051 
00052   // For operators' usage (to preserve element count).
00053   Range (double b, double i, octave_idx_type n);
00054 
00055   double base (void) const { return rng_base; }
00056   double limit (void) const { return rng_limit; }
00057   double inc (void) const { return rng_inc; }
00058   octave_idx_type nelem (void) const { return rng_nelem; }
00059 
00060   bool all_elements_are_ints (void) const;
00061 
00062   Matrix matrix_value (void) const;
00063 
00064   double min (void) const;
00065   double max (void) const;
00066 
00067   void sort_internal (bool ascending = true);
00068   void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true);
00069 
00070   Matrix diag (octave_idx_type k = 0) const;
00071 
00072   Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
00073 
00074   Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
00075               sortmode mode = ASCENDING) const;
00076 
00077   sortmode is_sorted (sortmode mode = ASCENDING) const;
00078 
00079   // Support for single-index subscripting, without generating matrix cache.
00080 
00081   double checkelem (octave_idx_type i) const;
00082 
00083   double elem (octave_idx_type i) const
00084     {
00085 #if defined (BOUNDS_CHECKING)
00086       return checkelem (i);
00087 #else
00088       return rng_base + rng_inc * i;
00089 #endif
00090     }
00091 
00092   Array<double> index (const idx_vector& i) const;
00093 
00094   void set_base (double b)
00095   {
00096     if (rng_base != b)
00097       {
00098         rng_base = b;
00099         clear_cache ();
00100       }
00101   }
00102 
00103   void set_limit (double l)
00104   {
00105     if (rng_limit != l)
00106       {
00107         rng_limit = l;
00108         clear_cache ();
00109       }
00110   }
00111 
00112   void set_inc (double i)
00113   {
00114     if (rng_inc != i)
00115       {
00116         rng_inc = i;
00117         clear_cache ();
00118       }
00119   }
00120 
00121   friend OCTAVE_API std::ostream& operator << (std::ostream& os, const Range& r);
00122   friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
00123 
00124   friend OCTAVE_API Range operator - (const Range& r);
00125   friend OCTAVE_API Range operator + (double x, const Range& r);
00126   friend OCTAVE_API Range operator + (const Range& r, double x);
00127   friend OCTAVE_API Range operator - (double x, const Range& r);
00128   friend OCTAVE_API Range operator - (const Range& r, double x);
00129   friend OCTAVE_API Range operator * (double x, const Range& r);
00130   friend OCTAVE_API Range operator * (const Range& r, double x);
00131 
00132   void print_range (void);
00133 
00134  private:
00135 
00136   double rng_base;
00137   double rng_limit;
00138   double rng_inc;
00139 
00140   octave_idx_type rng_nelem;
00141 
00142   mutable Matrix cache;
00143 
00144   octave_idx_type nelem_internal (void) const;
00145 
00146   void clear_cache (void) const { cache.resize (0, 0); }
00147 
00148 };
00149 
00150 extern OCTAVE_API Range operator - (const Range& r);
00151 
00152 extern OCTAVE_API Range operator + (double x, const Range& r);
00153 
00154 extern OCTAVE_API Range operator + (const Range& r, double x);
00155 
00156 extern OCTAVE_API Range operator - (double x, const Range& r);
00157 
00158 extern OCTAVE_API Range operator - (const Range& r, double x);
00159 
00160 extern OCTAVE_API Range operator * (double x, const Range& r);
00161 
00162 extern OCTAVE_API Range operator * (const Range& r, double x);
00163 
00164 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines