MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/util/array.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raul Durand                   *
00004  * Copyright (C) 2009 Sergio Galindo                                    *
00005  *                                                                      *
00006  * This program is free software: you can redistribute it and/or modify *
00007  * it under the terms of the GNU General Public License as published by *
00008  * the Free Software Foundation, either version 3 of the License, or    *
00009  * any later version.                                                   *
00010  *                                                                      *
00011  * This program is distributed in the hope that it will be useful,      *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00014  * GNU General Public License for more details.                         *
00015  *                                                                      *
00016  * You should have received a copy of the GNU General Public License    *
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00018  ************************************************************************/
00019 
00020 #ifndef MECHSYS_ARRAY_H
00021 #define MECHSYS_ARRAY_H
00022 
00023 // STL
00024 #include <algorithm> // for std::find, std::min_element, and std::max_element
00025 #ifdef USE_STDVECTOR
00026   #include <vector> // for std::vector
00027   #define INIT_VALUES
00028 #else
00029   #define INIT_VALUES SzFactor=1.2; _values=NULL;
00030 #endif
00031 
00032 // MechSys
00033 #include <mechsys/util/fatal.h>
00034 
00035 template<typename Value_T>
00036 class Array
00037 #ifdef USE_STDVECTOR
00038     : public std::vector<Value_T>
00039 #endif
00040 {
00041 public:
00042     // Alternative constructors
00043     Array (Value_T const & v0, bool JustOne);
00044     Array (Value_T const & v0, Value_T const & v1);
00045     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2);
00046     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3);
00047     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4);
00048     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5);
00049     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6);
00050     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7);
00051     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8);
00052     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9);
00053     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10);
00054     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11);
00055     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12);
00056     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13);
00057     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14);
00058     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15);
00059     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16);
00060     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17);
00061     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17, Value_T const & v18);
00062     Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17, Value_T const & v18, Value_T const & v19);
00063 
00064 #ifdef USE_BOOST_PYTHON
00065     // Python constructor
00066     Array (BPy::list const & Dat);
00067 #endif
00068 
00069 #ifdef USE_STDVECTOR
00070     // Constructors
00071     Array () {}                           
00072     Array (size_t Size) { Resize(Size); } 
00073 
00074     // Methods
00075     size_t          Size   () const { return  std::vector<Value_T>::size();  } 
00076     Value_T       * GetPtr ()       { return &std::vector<Value_T>::front(); } 
00077     Value_T const * GetPtr () const { return &std::vector<Value_T>::front(); } 
00078     Value_T       & Last   ()       { return  std::vector<Value_T>::back();  } 
00079     Value_T const & Last   () const { return  std::vector<Value_T>::back();  } 
00080 
00081     // NOTE:
00082     //  std::vector operators [] do not properly check for wrong indices when accessing the values.
00083     //  This sometimes causes segmentation fault.
00084     //  To solve this we could wrap operator[] and check the range of indices, but this does not work,
00085     //  since it fails to return a reference when using std::vector<bool> 
00086 
00087 #else
00088     // Constructors and destructor
00089      Array ()            { INIT_VALUES Resize(0); }     
00090      Array (size_t Size) { INIT_VALUES Resize(Size); }  
00091      Array (Array<Value_T> const & Other);              
00092     ~Array () { if (_values!=NULL) delete [] _values; } 
00093 
00094     // Methods
00095     size_t          Size   () const { return _size; }            
00096     Value_T       * GetPtr ()       { return _values; }          
00097     Value_T const * GetPtr () const { return _values; }          
00098     Value_T       & Last   ()       { return _values[_size-1]; } 
00099     Value_T const & Last   () const { return _values[_size-1]; } 
00100     size_t          size   () const { return Size(); }           
00101 
00102     // Operators
00103     Value_T       & operator[] (size_t i);                 
00104     Value_T const & operator[] (size_t i) const;           
00105     void            operator=  (Array<Value_T> const & R); 
00106 #endif
00107 
00108     // Methods
00109     void            Resize    (size_t Size);                       
00110     void            Push      (Value_T const & Value);             
00111     void            XPush     (Value_T const & Value);             
00112     void            PushN     (Value_T const & Value, size_t Num); 
00113     long            Find      (Value_T const & Value) const;       
00114     bool            Has       (Value_T const & Value) const;       
00115     Value_T const & TheMin    () const;                            
00116     Value_T const & TheMax    () const;                            
00117     Value_T         Mean      () const;                            
00118     Value_T         Norm      () const;                            
00119     void            SetValues (Value_T const & V);                 
00120     void            Clear     () { Resize(0); }                    
00121     void            DelItem   (size_t i);                          
00122     void            DelItems  (Array<int> const & Idxs);           
00123     void            DelVal    (Value_T const & Value);             
00124 
00125     // Assign values separated by commas
00126     class CommaAssign
00127     {
00128     public:
00129         CommaAssign (Array<Value_T> * ptArray, Value_T const & FirstValue) : _ptarray(ptArray), _i(0)
00130         {
00131             if (_ptarray->Size()>_i) (*_ptarray)[_i] = FirstValue;
00132             else throw new Fatal("Array::CommaAssign: The array must be resized with a size greater than or equal to %d before calling this method.",_i+1);
00133         }
00134         CommaAssign & operator, (Value_T const & Value)
00135         {
00136             _i++;
00137             if (_ptarray->Size()>_i) (*_ptarray)[_i] = Value;
00138             else throw new Fatal("Array::CommaAssign: The array must be resized with a size greater than or equal to %d before calling this method.",_i+1);
00139             return *this;
00140         }
00141     private:
00142         Array<Value_T> * _ptarray;
00143         size_t           _i;
00144     };
00145     CommaAssign operator= (Value_T const & Value) { return CommaAssign(this, Value); }
00146 
00147 #ifndef USE_STDVECTOR
00148     double    SzFactor; 
00149 private:
00150     size_t    _size;    
00151     size_t    _space;   
00152     Value_T * _values;  
00153 #endif
00154 };
00155 
00156 
00158 
00159 
00160 #ifndef USE_STDVECTOR
00161 
00162 template<typename Value_T>
00163 inline Array<Value_T>::Array(Array<Value_T> const & Other)
00164 {
00165     INIT_VALUES
00166     Resize(Other.Size());
00167     for (size_t i=0; i<_size; ++i)
00168         _values[i] = Other[i];
00169 }
00170 
00171 #endif
00172 
00173 #ifdef USE_BOOST_PYTHON
00174 
00175 template<typename Value_T>
00176 inline Array<Value_T>::Array (BPy::list const & Dat)
00177 {
00178     INIT_VALUES
00179     size_t size = BPy::len(Dat);
00180     Resize (size);
00181     for (size_t i=0; i<size; ++i)
00182         (*this)[i] = BPy::extract<Value_T>(Dat[i])();
00183 }
00184 
00185 #endif
00186 
00187 template<typename Value_T>
00188 inline Array<Value_T>::Array (Value_T const & v0, bool JustOne)
00189 {
00190     INIT_VALUES
00191     Resize(1);
00192     (*this)[0] = v0;
00193 }
00194 
00195 template<typename Value_T>
00196 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1)
00197 {
00198     INIT_VALUES
00199     Resize(2);
00200     (*this)[0] = v0;
00201     (*this)[1] = v1;
00202 }
00203 
00204 template<typename Value_T>
00205 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2)
00206 {
00207     INIT_VALUES
00208     Resize(3);
00209     (*this)[0] = v0;
00210     (*this)[1] = v1;
00211     (*this)[2] = v2;
00212 }
00213 
00214 template<typename Value_T>
00215 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3)
00216 {
00217     INIT_VALUES
00218     Resize(4);
00219     (*this)[0] = v0;
00220     (*this)[1] = v1;
00221     (*this)[2] = v2;
00222     (*this)[3] = v3;
00223 }
00224 
00225 template<typename Value_T>
00226 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4)
00227 {
00228     INIT_VALUES
00229     Resize(5);
00230     (*this)[0] = v0;
00231     (*this)[1] = v1;
00232     (*this)[2] = v2;
00233     (*this)[3] = v3;
00234     (*this)[4] = v4;
00235 }
00236 
00237 template<typename Value_T>
00238 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5)
00239 {
00240     INIT_VALUES
00241     Resize(6);
00242     (*this)[0] = v0;
00243     (*this)[1] = v1;
00244     (*this)[2] = v2;
00245     (*this)[3] = v3;
00246     (*this)[4] = v4;
00247     (*this)[5] = v5;
00248 }
00249 
00250 template<typename Value_T>
00251 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6)
00252 {
00253     INIT_VALUES
00254     Resize(7);
00255     (*this)[0] = v0;
00256     (*this)[1] = v1;
00257     (*this)[2] = v2;
00258     (*this)[3] = v3;
00259     (*this)[4] = v4;
00260     (*this)[5] = v5;
00261     (*this)[6] = v6;
00262 }
00263 
00264 template<typename Value_T>
00265 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7)
00266 {
00267     INIT_VALUES
00268     Resize(8);
00269     (*this)[0] = v0;
00270     (*this)[1] = v1;
00271     (*this)[2] = v2;
00272     (*this)[3] = v3;
00273     (*this)[4] = v4;
00274     (*this)[5] = v5;
00275     (*this)[6] = v6;
00276     (*this)[7] = v7;
00277 }
00278 
00279 template<typename Value_T>
00280 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8)
00281 {
00282     INIT_VALUES
00283     Resize(9);
00284     (*this)[0] = v0;
00285     (*this)[1] = v1;
00286     (*this)[2] = v2;
00287     (*this)[3] = v3;
00288     (*this)[4] = v4;
00289     (*this)[5] = v5;
00290     (*this)[6] = v6;
00291     (*this)[7] = v7;
00292     (*this)[8] = v8;
00293 }
00294 
00295 template<typename Value_T>
00296 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9)
00297 {
00298     INIT_VALUES
00299     Resize(10);
00300     (*this)[0] = v0;
00301     (*this)[1] = v1;
00302     (*this)[2] = v2;
00303     (*this)[3] = v3;
00304     (*this)[4] = v4;
00305     (*this)[5] = v5;
00306     (*this)[6] = v6;
00307     (*this)[7] = v7;
00308     (*this)[8] = v8;
00309     (*this)[9] = v9;
00310 }
00311 
00312 template<typename Value_T>
00313 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10)
00314 {
00315     INIT_VALUES
00316     Resize(11);
00317     (*this)[ 0] = v0;
00318     (*this)[ 1] = v1;
00319     (*this)[ 2] = v2;
00320     (*this)[ 3] = v3;
00321     (*this)[ 4] = v4;
00322     (*this)[ 5] = v5;
00323     (*this)[ 6] = v6;
00324     (*this)[ 7] = v7;
00325     (*this)[ 8] = v8;
00326     (*this)[ 9] = v9;
00327     (*this)[10] = v10;
00328 }
00329 
00330 template<typename Value_T>
00331 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11)
00332 {
00333     INIT_VALUES
00334     Resize(12);
00335     (*this)[ 0] = v0;
00336     (*this)[ 1] = v1;
00337     (*this)[ 2] = v2;
00338     (*this)[ 3] = v3;
00339     (*this)[ 4] = v4;
00340     (*this)[ 5] = v5;
00341     (*this)[ 6] = v6;
00342     (*this)[ 7] = v7;
00343     (*this)[ 8] = v8;
00344     (*this)[ 9] = v9;
00345     (*this)[10] = v10;
00346     (*this)[11] = v11;
00347 }
00348 
00349 template<typename Value_T>
00350 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12)
00351 {
00352     INIT_VALUES
00353     Resize(13);
00354     (*this)[ 0] = v0;
00355     (*this)[ 1] = v1;
00356     (*this)[ 2] = v2;
00357     (*this)[ 3] = v3;
00358     (*this)[ 4] = v4;
00359     (*this)[ 5] = v5;
00360     (*this)[ 6] = v6;
00361     (*this)[ 7] = v7;
00362     (*this)[ 8] = v8;
00363     (*this)[ 9] = v9;
00364     (*this)[10] = v10;
00365     (*this)[11] = v11;
00366     (*this)[12] = v12;
00367 }
00368 
00369 template<typename Value_T>
00370 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13)
00371 {
00372     INIT_VALUES
00373     Resize(14);
00374     (*this)[ 0] = v0;
00375     (*this)[ 1] = v1;
00376     (*this)[ 2] = v2;
00377     (*this)[ 3] = v3;
00378     (*this)[ 4] = v4;
00379     (*this)[ 5] = v5;
00380     (*this)[ 6] = v6;
00381     (*this)[ 7] = v7;
00382     (*this)[ 8] = v8;
00383     (*this)[ 9] = v9;
00384     (*this)[10] = v10;
00385     (*this)[11] = v11;
00386     (*this)[12] = v12;
00387     (*this)[13] = v13;
00388 }
00389 
00390 template<typename Value_T>
00391 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14)
00392 {
00393     INIT_VALUES
00394     Resize(15);
00395     (*this)[ 0] = v0;
00396     (*this)[ 1] = v1;
00397     (*this)[ 2] = v2;
00398     (*this)[ 3] = v3;
00399     (*this)[ 4] = v4;
00400     (*this)[ 5] = v5;
00401     (*this)[ 6] = v6;
00402     (*this)[ 7] = v7;
00403     (*this)[ 8] = v8;
00404     (*this)[ 9] = v9;
00405     (*this)[10] = v10;
00406     (*this)[11] = v11;
00407     (*this)[12] = v12;
00408     (*this)[13] = v13;
00409     (*this)[14] = v14;
00410 }
00411 
00412 template<typename Value_T>
00413 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15)
00414 {
00415     INIT_VALUES
00416     Resize(16);
00417     (*this)[ 0] = v0;
00418     (*this)[ 1] = v1;
00419     (*this)[ 2] = v2;
00420     (*this)[ 3] = v3;
00421     (*this)[ 4] = v4;
00422     (*this)[ 5] = v5;
00423     (*this)[ 6] = v6;
00424     (*this)[ 7] = v7;
00425     (*this)[ 8] = v8;
00426     (*this)[ 9] = v9;
00427     (*this)[10] = v10;
00428     (*this)[11] = v11;
00429     (*this)[12] = v12;
00430     (*this)[13] = v13;
00431     (*this)[14] = v14;
00432     (*this)[15] = v15;
00433 }
00434 
00435 template<typename Value_T>
00436 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16)
00437 {
00438     INIT_VALUES
00439     Resize(17);
00440     (*this)[ 0] = v0;
00441     (*this)[ 1] = v1;
00442     (*this)[ 2] = v2;
00443     (*this)[ 3] = v3;
00444     (*this)[ 4] = v4;
00445     (*this)[ 5] = v5;
00446     (*this)[ 6] = v6;
00447     (*this)[ 7] = v7;
00448     (*this)[ 8] = v8;
00449     (*this)[ 9] = v9;
00450     (*this)[10] = v10;
00451     (*this)[11] = v11;
00452     (*this)[12] = v12;
00453     (*this)[13] = v13;
00454     (*this)[14] = v14;
00455     (*this)[15] = v15;
00456     (*this)[16] = v16;
00457 }
00458 
00459 template<typename Value_T>
00460 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17)
00461 {
00462     INIT_VALUES
00463     Resize(18);
00464     (*this)[ 0] = v0;
00465     (*this)[ 1] = v1;
00466     (*this)[ 2] = v2;
00467     (*this)[ 3] = v3;
00468     (*this)[ 4] = v4;
00469     (*this)[ 5] = v5;
00470     (*this)[ 6] = v6;
00471     (*this)[ 7] = v7;
00472     (*this)[ 8] = v8;
00473     (*this)[ 9] = v9;
00474     (*this)[10] = v10;
00475     (*this)[11] = v11;
00476     (*this)[12] = v12;
00477     (*this)[13] = v13;
00478     (*this)[14] = v14;
00479     (*this)[15] = v15;
00480     (*this)[16] = v16;
00481     (*this)[17] = v17;
00482 }
00483 
00484 template<typename Value_T>
00485 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17, Value_T const & v18)
00486 {
00487     INIT_VALUES
00488     Resize(19);
00489     (*this)[ 0] = v0;
00490     (*this)[ 1] = v1;
00491     (*this)[ 2] = v2;
00492     (*this)[ 3] = v3;
00493     (*this)[ 4] = v4;
00494     (*this)[ 5] = v5;
00495     (*this)[ 6] = v6;
00496     (*this)[ 7] = v7;
00497     (*this)[ 8] = v8;
00498     (*this)[ 9] = v9;
00499     (*this)[10] = v10;
00500     (*this)[11] = v11;
00501     (*this)[12] = v12;
00502     (*this)[13] = v13;
00503     (*this)[14] = v14;
00504     (*this)[15] = v15;
00505     (*this)[16] = v16;
00506     (*this)[17] = v17;
00507     (*this)[18] = v18;
00508 }
00509 
00510 template<typename Value_T>
00511 inline Array<Value_T>::Array (Value_T const & v0, Value_T const & v1, Value_T const & v2, Value_T const & v3, Value_T const & v4, Value_T const & v5, Value_T const & v6, Value_T const & v7, Value_T const & v8, Value_T const & v9, Value_T const & v10, Value_T const & v11, Value_T const & v12, Value_T const & v13, Value_T const & v14, Value_T const & v15, Value_T const & v16, Value_T const & v17, Value_T const & v18, Value_T const & v19)
00512 {
00513     INIT_VALUES
00514     Resize(20);
00515     (*this)[ 0] = v0;
00516     (*this)[ 1] = v1;
00517     (*this)[ 2] = v2;
00518     (*this)[ 3] = v3;
00519     (*this)[ 4] = v4;
00520     (*this)[ 5] = v5;
00521     (*this)[ 6] = v6;
00522     (*this)[ 7] = v7;
00523     (*this)[ 8] = v8;
00524     (*this)[ 9] = v9;
00525     (*this)[10] = v10;
00526     (*this)[11] = v11;
00527     (*this)[12] = v12;
00528     (*this)[13] = v13;
00529     (*this)[14] = v14;
00530     (*this)[15] = v15;
00531     (*this)[16] = v16;
00532     (*this)[17] = v17;
00533     (*this)[18] = v18;
00534     (*this)[19] = v19;
00535 }
00536 
00537 
00538 // Methods
00539 
00540 template<typename Value_T>
00541 inline void Array<Value_T>::Resize (size_t Size)
00542 {
00543 #ifdef USE_STDVECTOR
00544     std::vector<Value_T>::resize (Size);
00545 #else
00546     // Check
00547     if (SzFactor<1.0) throw new Fatal("Array::Resize: SzFactor==%f must be greater than 1.0", SzFactor);
00548 
00549     // Clear previous memory
00550     if (_values!=NULL) delete [] _values;
00551 
00552     // Allocate new memory
00553     _size   = Size;
00554     _space  = static_cast<size_t>((_size+1)*SzFactor+1);
00555     _values = new Value_T [_space];
00556 #endif
00557 }
00558 
00559 template<typename Value_T>
00560 inline void Array<Value_T>::Push (Value_T const & Value)
00561 {
00562 #ifdef USE_STDVECTOR
00563     std::vector<Value_T>::push_back (Value);
00564 #else
00565     if (_size==_space)
00566     {
00567         size_t oldsz = _size;
00568         Value_T * tmp = new Value_T [oldsz];
00569         for (size_t i=0; i<oldsz; ++i) tmp[i] = _values[i];
00570         Resize (oldsz+1);
00571         for (size_t i=0; i<oldsz; ++i) _values[i] = tmp[i];
00572         delete [] tmp;
00573     }
00574     else _size++;
00575     _values[_size-1] = Value;
00576 #endif
00577 }
00578 
00579 template<typename Value_T>
00580 inline void Array<Value_T>::XPush (Value_T const & Value)
00581 {
00582     if (!Has(Value)) Push (Value);
00583 }
00584 
00585 template<typename Value_T>
00586 inline void Array<Value_T>::PushN (Value_T const & Value, size_t Num)
00587 {
00588 #ifdef USE_STDVECTOR
00589     std::vector<Value_T>::reserve (Num);
00590     for (size_t i=0; i<Num; ++i) std::vector<Value_T>::push_back (Value);
00591 #else
00592     if (_size+Num>=_space)
00593     {
00594         size_t oldsz = _size;
00595         Value_T * tmp = new Value_T [oldsz];
00596         for (size_t i=0; i<oldsz; ++i) tmp[i] = _values[i];
00597         Resize (oldsz+Num);
00598         for (size_t i=0; i<oldsz; ++i) _values[i] = tmp[i];
00599         delete [] tmp;
00600     }
00601     else _size += Num;
00602     for (size_t i=0; i<Num; ++i) _values[_size-Num+i] = Value;
00603 #endif
00604 }
00605 
00606 template<typename Value_T>
00607 inline long Array<Value_T>::Find (Value_T const & Value) const
00608 {
00609 #ifdef USE_STDVECTOR
00610     typename std::vector<Value_T>::const_iterator it = std::find (std::vector<Value_T>::begin(), std::vector<Value_T>::end(), Value);
00611     if (it==std::vector<Value_T>::end()) return -1;
00612     else return it-std::vector<Value_T>::begin();
00613 #else
00614     Value_T * res = std::find(_values, _values+_size, Value);
00615     if (res==_values+_size) return -1;
00616     else return res-_values;
00617 #endif
00618 }
00619 
00620 template<typename Value_T>
00621 inline bool Array<Value_T>::Has (Value_T const & Value) const
00622 {
00623 #ifdef USE_STDVECTOR
00624     typename std::vector<Value_T>::const_iterator it = std::find (std::vector<Value_T>::begin(), std::vector<Value_T>::end(), Value);
00625     if (it==std::vector<Value_T>::end()) return false;
00626     else return true;
00627 #else
00628     Value_T * res = std::find(_values, _values+_size, Value);
00629     if (res==_values+_size) return false;
00630     else return true;
00631 #endif
00632 }
00633 
00634 template<typename Value_T>
00635 inline Value_T const & Array<Value_T>::TheMin () const
00636 {
00637 #ifdef USE_STDVECTOR
00638     typename std::vector<Value_T>::const_iterator it = std::min_element (std::vector<Value_T>::begin(), std::vector<Value_T>::end());
00639     return (*it);
00640 #else
00641     Value_T * res = std::min_element(_values, _values+_size);
00642     return (*res);
00643 #endif
00644 }
00645 
00646 template<typename Value_T>
00647 inline Value_T const & Array<Value_T>::TheMax () const
00648 {
00649 #ifdef USE_STDVECTOR
00650     typename std::vector<Value_T>::const_iterator it = std::max_element (std::vector<Value_T>::begin(), std::vector<Value_T>::end());
00651     return (*it);
00652 #else
00653     Value_T * res = std::max_element(_values, _values+_size);
00654     return (*res);
00655 #endif
00656 }
00657 
00658 /*
00659 template<typename Value_T>
00660 inline long Array<Value_T>::Min () const
00661 {
00662 #ifdef USE_STDVECTOR
00663     typename std::vector<Value_T>::const_iterator it = std::min_element (std::vector<Value_T>::begin(), std::vector<Value_T>::end());
00664     return it-std::vector<Value_T>::begin();
00665 #else
00666     Value_T * res = std::min_element(_values, _values+_size);
00667     return res-_values;
00668 #endif
00669 }
00670 
00671 template<typename Value_T>
00672 inline long Array<Value_T>::Max () const
00673 {
00674 #ifdef USE_STDVECTOR
00675     typename std::vector<Value_T>::const_iterator it = std::max_element (std::vector<Value_T>::begin(), std::vector<Value_T>::end());
00676     return it-std::vector<Value_T>::begin();
00677 #else
00678     Value_T * res = std::max_element(_values, _values+_size);
00679     return res-_values;
00680 #endif
00681 }
00682 */
00683 
00684 template<typename Value_T>
00685 inline Value_T Array<Value_T>::Mean () const
00686 {
00687     Value_T sum = 0.0;
00688     for (size_t i=0; i<Size(); ++i) sum += (*this)[i];
00689     return sum/Size();
00690 }
00691 
00692 template<typename Value_T>
00693 inline Value_T Array<Value_T>::Norm () const
00694 {
00695     Value_T sum = 0.0;
00696     for (size_t i=0; i<Size(); ++i) sum += (*this)[i]*(*this)[i];
00697     return sqrt(sum);
00698 }
00699 
00700 template<typename Value_T>
00701 inline void Array<Value_T>::SetValues (Value_T const & V)
00702 {
00703     for (size_t i=0; i<Size(); ++i) (*this)[i] = V;
00704 }
00705 
00706 template<typename Value_T>
00707 inline void Array<Value_T>::DelItem (size_t IdxToRemove)
00708 {
00709 #ifndef NDEBUG
00710     if (IdxToRemove<0 || IdxToRemove>=Size()) throw new Fatal("Array::DelItem: Subscript==%zd (size==%zd) is out of range.", IdxToRemove, Size());
00711 #endif
00712     Array<Value_T> tmp((*this));
00713     this->Resize (tmp.Size()-1);
00714     size_t k = 0;
00715     for (size_t i=0; i<tmp.Size(); ++i)
00716     {
00717         if (i!=IdxToRemove)
00718         {
00719             (*this)[k] = tmp[i];
00720             k++;
00721         }
00722     }
00723 }
00724 
00725 template<typename Value_T>
00726 inline void Array<Value_T>::DelItems (Array<int> const & Idxs)
00727 {
00728     if (Idxs.Size()>Size()) throw new Fatal("Array::DelItems: Array 'Idxs' with indices to be deleted must have size smaller than or equal to %zd. Idxs.Size()==%zd is invalid.",Size(),Idxs.Size());
00729     Array<Value_T> tmp((*this));
00730     this->Resize (tmp.Size()-Idxs.Size());
00731     size_t k = 0;
00732     for (size_t i=0; i<tmp.Size(); ++i)
00733     {
00734         if (!Idxs.Has(i))
00735         {
00736             (*this)[k] = tmp[i];
00737             k++;
00738         }
00739     }
00740 }
00741 
00742 template<typename Value_T>
00743 inline void Array<Value_T>::DelVal (Value_T const & V)
00744 {
00745     size_t idx_to_remove = Find(V);
00746     if (idx_to_remove<0) throw new Fatal("Array::DelVal: Value cannot be found for deletion");
00747     DelItem (idx_to_remove);
00748 }
00749 
00750 
00751 // Operators
00752 
00753 #ifndef USE_STDVECTOR
00754 
00755 template<typename Value_T>
00756 inline Value_T & Array<Value_T>::operator[] (size_t i)
00757 {
00758 #ifndef NDEBUG
00759     if (i<0 || i>=Size()) throw new Fatal("Array::operator[] (write) Subscript==%zd (size==%zd) is out of range.", i, Size());
00760 #endif
00761     return _values[i];
00762 }
00763 
00764 template<typename Value_T>
00765 inline Value_T const & Array<Value_T>::operator[] (size_t i) const
00766 {
00767 #ifndef NDEBUG
00768     if (i<0 || i>=Size()) throw new Fatal("Array::operator[] (read) Subscript==%zd (size==%zd) is out of range.", i, Size()); 
00769 #endif
00770     return _values[i];
00771 }
00772 
00773 template<typename Value_T>
00774 inline void Array<Value_T>::operator= (Array<Value_T> const & R)
00775 {
00776 #ifndef DNDEBUG
00777     if (&R==this) throw new Fatal("Array::operator= The right-hand-size of this operation (LHS = RHS) must not be equal to the LHS.");
00778 #endif
00779     // Reallocate if they are different (LHS != RHS)
00780     if (_size!=R.Size()) Resize(R.Size());
00781     
00782     // Copy values
00783     for (size_t i=0; i<_size; ++i) _values[i] = R[i];
00784 }
00785 
00786 #endif
00787 
00788 
00790 template<typename Value_T>
00791 std::ostream & operator<< (std::ostream & os, const Array<Value_T> & V)
00792 {
00793     for (size_t i=0; i<V.Size(); ++i)
00794     {
00795         os << V[i];
00796         if (i!=V.Size()-1) os << ", ";
00797     }
00798     return os;
00799 }
00800 
00801 #undef INIT_VALUES
00802 
00803 #endif // MECHSYS_ARRAY_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines