Teuchos_Array.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_ARRAY_H
00030 #define TEUCHOS_ARRAY_H
00031 
00036 #include "Teuchos_ConfigDefs.hpp"
00037 #include "Teuchos_TestForException.hpp"
00038 #include "Teuchos_Utils.hpp"
00039 
00040 namespace Teuchos
00041 {
00042 
00047   template<class T>
00048   class Array : public std::vector<T>
00049   {
00050   public:
00052     Array();
00053 
00055     Array(int n);
00056 
00058     Array(int n, const T& t);
00059 
00061     inline Array<T>& append(const T& entry) {this->push_back(entry); return *this;}
00062 
00064     void remove(int i);
00065 
00069     int length() const {return this->size();}
00070 
00072     inline T& operator[](int i);
00073 
00075     inline const T& operator[](int i) const;
00076 
00078     std::string toString() const ;
00079 
00081     static bool hasBoundsChecking();
00082 
00083   private:
00084 
00087     void indexCheckCrash(int i) const;
00088   };
00089 
00093   template<class T> std::ostream& operator<<(std::ostream& os, 
00094                                              const Array<T>& array);
00095 
00097   template<class T> int hashCode(const Array<T>& array);
00098 
00100   template<class T> std::string toString(const Array<T>& array);
00101 
00102 
00103   template<class T> inline Array<T>::Array()
00104     : std::vector<T>()
00105   {}
00106 
00107   template<class T> inline Array<T>::Array(int n)
00108     : std::vector<T>(n)
00109   {}
00110 
00111   template<class T> inline Array<T>::Array(int n, const T& t)
00112     : std::vector<T>(n, t)
00113   {}
00114 
00115   template<class T>
00116   void Array<T>::remove(int i) {
00117 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00118     indexCheckCrash(i);
00119 #endif
00120     // Erase the i-th element of this array.
00121     this->erase( this->begin() + i );
00122   }
00123 
00124   template<class T> inline
00125   T& Array<T>::operator[](int i) {
00126 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00127     indexCheckCrash(i);
00128 #endif
00129     return std::vector<T>::operator[](i);
00130   }
00131 
00132   template<class T> inline
00133   const T& Array<T>::operator[](int i) const {
00134 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00135     indexCheckCrash(i);
00136 #endif
00137     return std::vector<T>::operator[](i);
00138   }
00139 
00140   template<class T> inline
00141   bool Array<T>::hasBoundsChecking()
00142   {
00143 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK  
00144     return true;
00145 #else
00146     return false;
00147 #endif
00148   }
00149 
00150   template<class T> inline
00151   void Array<T>::indexCheckCrash(int i) const
00152   {
00153     TEST_FOR_EXCEPTION(
00154       !( 0 <= i && i < length() ), std::range_error,
00155       "Array<"<<typeid(T).name()<<">::indexCheckCrash: "
00156       "index " << i << " out of range [0, "<< length() << ")"
00157       );
00158   }
00159 
00160   // print in form (), (1), or (1,2)
00161   template<class T> inline ostream& operator<<(ostream& os, const Array<T>& array)
00162   {
00163     return os << Teuchos::toString(array);
00164   }
00165 
00166   template<class T> inline int hashCode(const Array<T>& array)
00167   {
00168     int rtn = hashCode(array.length());
00169     for (int i=0; i<array.length(); i++)
00170       {
00171         rtn += hashCode(array[i]);
00172       }
00173     return rtn;
00174   }
00175 
00176   template<class T> inline std::string Array<T>::toString() const
00177   {
00178     ostringstream ss;
00179     ss << "{";
00180 
00181     for (int i=0; i<length(); i++)
00182       {
00183         ss << operator[](i);
00184         if (i<length()-1) ss << ", ";
00185       }
00186     ss << "}";
00187 
00188     return ss.str();
00189   }
00190 
00191   template<class T> inline std::string toString(const Array<T>& array)
00192   {
00193     return array.toString();
00194   }
00195 
00196 
00200   template<class T> inline
00201   Array<T> tuple(const T& a)
00202   {
00203     Array<T> rtn(1, a);
00204     return rtn;
00205   }
00206 
00210   template<class T> inline
00211   Array<T> tuple(const T& a, const T& b)
00212   {
00213     Array<T> rtn(2);
00214     rtn[0] = a;
00215     rtn[1] = b;
00216     return rtn;
00217   }
00218 
00222   template<class T> inline
00223   Array<T> tuple(const T& a, const T& b, const T& c)
00224   {
00225     Array<T> rtn(3);
00226     rtn[0] = a;
00227     rtn[1] = b;
00228     rtn[2] = c;
00229     return rtn;
00230   }
00231 
00235   template<class T> inline
00236   Array<T> tuple(const T& a, const T& b, const T& c, const T& d)
00237   {
00238     Array<T> rtn(4);
00239     rtn[0] = a;
00240     rtn[1] = b;
00241     rtn[2] = c;
00242     rtn[3] = d;
00243     return rtn;
00244   }
00245 
00249   template<class T> inline
00250   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e)
00251   {
00252     Array<T> rtn(5);
00253     rtn[0] = a;
00254     rtn[1] = b;
00255     rtn[2] = c;
00256     rtn[3] = d;
00257     rtn[4] = e;
00258     return rtn;
00259   }
00260 
00261 
00265   template<class T> inline
00266   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00267                  const T& f)
00268   {
00269     Array<T> rtn(6);
00270     rtn[0] = a;
00271     rtn[1] = b;
00272     rtn[2] = c;
00273     rtn[3] = d;
00274     rtn[4] = e;
00275     rtn[5] = f;
00276     return rtn;
00277   }
00278 
00282   template<class T> inline
00283   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00284                  const T& f, const T& g)
00285   {
00286     Array<T> rtn(7);
00287     rtn[0] = a;
00288     rtn[1] = b;
00289     rtn[2] = c;
00290     rtn[3] = d;
00291     rtn[4] = e;
00292     rtn[5] = f;
00293     rtn[6] = g;
00294     return rtn;
00295   }
00296 
00300   template<class T> inline
00301   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00302                  const T& f, const T& g, const T& h)
00303   {
00304     Array<T> rtn(8);
00305     rtn[0] = a;
00306     rtn[1] = b;
00307     rtn[2] = c;
00308     rtn[3] = d;
00309     rtn[4] = e;
00310     rtn[5] = f;
00311     rtn[6] = g;
00312     rtn[7] = h;
00313     return rtn;
00314   }
00315 
00319   template<class T> inline
00320   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00321                  const T& f, const T& g, const T& h, const T& i)
00322   {
00323     Array<T> rtn(9);
00324     rtn[0] = a;
00325     rtn[1] = b;
00326     rtn[2] = c;
00327     rtn[3] = d;
00328     rtn[4] = e;
00329     rtn[5] = f;
00330     rtn[6] = g;
00331     rtn[7] = h;
00332     rtn[8] = i;
00333     return rtn;
00334   }
00335 
00336 
00340   template<class T> inline
00341   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00342                  const T& f, const T& g, const T& h, const T& i, const T& j)
00343   {
00344     Array<T> rtn(10);
00345     rtn[0] = a;
00346     rtn[1] = b;
00347     rtn[2] = c;
00348     rtn[3] = d;
00349     rtn[4] = e;
00350     rtn[5] = f;
00351     rtn[6] = g;
00352     rtn[7] = h;
00353     rtn[8] = i;
00354     rtn[9] = j;
00355     return rtn;
00356   }
00357 }
00358 
00359 #endif // TEUCHOS_ARRAY_H
00360 

Generated on Thu Sep 18 12:30:29 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1