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 
00066     int length() const {return this->size();}
00067 
00069     inline T& operator[](int i);
00070 
00072     inline const T& operator[](int i) const;
00073 
00075     std::string toString() const ;
00076 
00078     static bool hasBoundsChecking();
00079 
00080   private:
00081 
00084     void indexCheckCrash(int i) const;
00085   };
00086 
00090   template<class T> std::ostream& operator<<(std::ostream& os, 
00091                                              const Array<T>& array);
00092 
00094   template<class T> int hashCode(const Array<T>& array);
00095 
00097   template<class T> std::string toString(const Array<T>& array);
00098 
00099 
00100   template<class T> inline Array<T>::Array()
00101     : std::vector<T>()
00102   {}
00103 
00104   template<class T> inline Array<T>::Array(int n)
00105     : std::vector<T>(n)
00106   {}
00107 
00108   template<class T> inline Array<T>::Array(int n, const T& t)
00109     : std::vector<T>(n, t)
00110   {}
00111 
00112   template<class T> inline
00113   T& Array<T>::operator[](int i) {
00114 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00115     indexCheckCrash(i);
00116 #endif
00117     return std::vector<T>::operator[](i);
00118   }
00119 
00120   template<class T> inline
00121   const T& Array<T>::operator[](int i) const {
00122 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00123     indexCheckCrash(i);
00124 #endif
00125     return std::vector<T>::operator[](i);
00126   }
00127 
00128   template<class T> inline
00129   bool Array<T>::hasBoundsChecking()
00130   {
00131 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK  
00132     return true;
00133 #else
00134     return false;
00135 #endif
00136   }
00137 
00138   template<class T> inline
00139   void Array<T>::indexCheckCrash(int i) const
00140   {
00141     TEST_FOR_EXCEPTION(i<0 || i>=length(), std::range_error,
00142                        "Array<T>::indexCheckCrash: "
00143                        "index " << i << "out of range [0, "<< length() << ")");
00144       
00145   }
00146 
00147   // print in form (), (1), or (1,2)
00148   template<class T> inline ostream& operator<<(ostream& os, const Array<T>& array)
00149   {
00150     return os << Teuchos::toString(array);
00151   }
00152 
00153   template<class T> inline int hashCode(const Array<T>& array)
00154   {
00155     int rtn = hashCode(array.length());
00156     for (int i=0; i<array.length(); i++)
00157       {
00158         rtn += hashCode(array[i]);
00159       }
00160     return rtn;
00161   }
00162 
00163   template<class T> inline std::string Array<T>::toString() const
00164   {
00165     ostringstream ss;
00166     ss << "{";
00167 
00168     for (int i=0; i<length(); i++)
00169       {
00170         ss << operator[](i);
00171         if (i<length()-1) ss << ", ";
00172       }
00173     ss << "}";
00174 
00175     return ss.str();
00176   }
00177 
00178   template<class T> inline std::string toString(const Array<T>& array)
00179   {
00180     return array.toString();
00181   }
00182 
00183 
00187   template<class T> inline
00188   Array<T> tuple(const T& a)
00189   {
00190     Array<T> rtn(1, a);
00191     return rtn;
00192   }
00193 
00197   template<class T> inline
00198   Array<T> tuple(const T& a, const T& b)
00199   {
00200     Array<T> rtn(2);
00201     rtn[0] = a;
00202     rtn[1] = b;
00203     return rtn;
00204   }
00205 
00209   template<class T> inline
00210   Array<T> tuple(const T& a, const T& b, const T& c)
00211   {
00212     Array<T> rtn(3);
00213     rtn[0] = a;
00214     rtn[1] = b;
00215     rtn[2] = c;
00216     return rtn;
00217   }
00218 
00222   template<class T> inline
00223   Array<T> tuple(const T& a, const T& b, const T& c, const T& d)
00224   {
00225     Array<T> rtn(4);
00226     rtn[0] = a;
00227     rtn[1] = b;
00228     rtn[2] = c;
00229     rtn[3] = d;
00230     return rtn;
00231   }
00232 
00236   template<class T> inline
00237   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e)
00238   {
00239     Array<T> rtn(5);
00240     rtn[0] = a;
00241     rtn[1] = b;
00242     rtn[2] = c;
00243     rtn[3] = d;
00244     rtn[4] = e;
00245     return rtn;
00246   }
00247 
00248 
00252   template<class T> inline
00253   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00254                  const T& f)
00255   {
00256     Array<T> rtn(6);
00257     rtn[0] = a;
00258     rtn[1] = b;
00259     rtn[2] = c;
00260     rtn[3] = d;
00261     rtn[4] = e;
00262     rtn[5] = f;
00263     return rtn;
00264   }
00265 
00269   template<class T> inline
00270   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00271                  const T& f, const T& g)
00272   {
00273     Array<T> rtn(7);
00274     rtn[0] = a;
00275     rtn[1] = b;
00276     rtn[2] = c;
00277     rtn[3] = d;
00278     rtn[4] = e;
00279     rtn[5] = f;
00280     rtn[6] = g;
00281     return rtn;
00282   }
00283 
00287   template<class T> inline
00288   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00289                  const T& f, const T& g, const T& h)
00290   {
00291     Array<T> rtn(8);
00292     rtn[0] = a;
00293     rtn[1] = b;
00294     rtn[2] = c;
00295     rtn[3] = d;
00296     rtn[4] = e;
00297     rtn[5] = f;
00298     rtn[6] = g;
00299     rtn[7] = h;
00300     return rtn;
00301   }
00302 
00306   template<class T> inline
00307   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00308                  const T& f, const T& g, const T& h, const T& i)
00309   {
00310     Array<T> rtn(9);
00311     rtn[0] = a;
00312     rtn[1] = b;
00313     rtn[2] = c;
00314     rtn[3] = d;
00315     rtn[4] = e;
00316     rtn[5] = f;
00317     rtn[6] = g;
00318     rtn[7] = h;
00319     rtn[8] = i;
00320     return rtn;
00321   }
00322 
00323 
00327   template<class T> inline
00328   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00329                  const T& f, const T& g, const T& h, const T& i, const T& j)
00330   {
00331     Array<T> rtn(10);
00332     rtn[0] = a;
00333     rtn[1] = b;
00334     rtn[2] = c;
00335     rtn[3] = d;
00336     rtn[4] = e;
00337     rtn[5] = f;
00338     rtn[6] = g;
00339     rtn[7] = h;
00340     rtn[8] = i;
00341     rtn[9] = j;
00342     return rtn;
00343   }
00344 }
00345 
00346 #endif // TEUCHOS_ARRAY_H
00347 

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