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 << 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     std::string rtn = "{";
00166 
00167     for (int i=0; i<length(); i++)
00168       {
00169         rtn += Teuchos::toString(operator[](i));
00170         if (i<length()-1) rtn += ", ";
00171       }
00172     rtn += "}";
00173 
00174     return rtn;
00175   }
00176 
00177   template<class T> inline std::string toString(const Array<T>& array)
00178   {
00179     return array.toString();
00180   }
00181 
00182 
00186   template<class T> inline
00187   Array<T> tuple(const T& a)
00188   {
00189     Array<T> rtn(1, a);
00190     return rtn;
00191   }
00192 
00196   template<class T> inline
00197   Array<T> tuple(const T& a, const T& b)
00198   {
00199     Array<T> rtn(2);
00200     rtn[0] = a;
00201     rtn[1] = b;
00202     return rtn;
00203   }
00204 
00208   template<class T> inline
00209   Array<T> tuple(const T& a, const T& b, const T& c)
00210   {
00211     Array<T> rtn(3);
00212     rtn[0] = a;
00213     rtn[1] = b;
00214     rtn[2] = c;
00215     return rtn;
00216   }
00217 
00221   template<class T> inline
00222   Array<T> tuple(const T& a, const T& b, const T& c, const T& d)
00223   {
00224     Array<T> rtn(4);
00225     rtn[0] = a;
00226     rtn[1] = b;
00227     rtn[2] = c;
00228     rtn[3] = d;
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, const T& e)
00237   {
00238     Array<T> rtn(5);
00239     rtn[0] = a;
00240     rtn[1] = b;
00241     rtn[2] = c;
00242     rtn[3] = d;
00243     rtn[4] = e;
00244     return rtn;
00245   }
00246 
00247 
00251   template<class T> inline
00252   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00253                  const T& f)
00254   {
00255     Array<T> rtn(6);
00256     rtn[0] = a;
00257     rtn[1] = b;
00258     rtn[2] = c;
00259     rtn[3] = d;
00260     rtn[4] = e;
00261     rtn[5] = f;
00262     return rtn;
00263   }
00264 
00268   template<class T> inline
00269   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00270                  const T& f, const T& g)
00271   {
00272     Array<T> rtn(7);
00273     rtn[0] = a;
00274     rtn[1] = b;
00275     rtn[2] = c;
00276     rtn[3] = d;
00277     rtn[4] = e;
00278     rtn[5] = f;
00279     rtn[6] = g;
00280     return rtn;
00281   }
00282 
00286   template<class T> inline
00287   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00288                  const T& f, const T& g, const T& h)
00289   {
00290     Array<T> rtn(8);
00291     rtn[0] = a;
00292     rtn[1] = b;
00293     rtn[2] = c;
00294     rtn[3] = d;
00295     rtn[4] = e;
00296     rtn[5] = f;
00297     rtn[6] = g;
00298     rtn[7] = h;
00299     return rtn;
00300   }
00301 
00305   template<class T> inline
00306   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00307                  const T& f, const T& g, const T& h, const T& i)
00308   {
00309     Array<T> rtn(9);
00310     rtn[0] = a;
00311     rtn[1] = b;
00312     rtn[2] = c;
00313     rtn[3] = d;
00314     rtn[4] = e;
00315     rtn[5] = f;
00316     rtn[6] = g;
00317     rtn[7] = h;
00318     rtn[8] = i;
00319     return rtn;
00320   }
00321 
00322 
00326   template<class T> inline
00327   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00328                  const T& f, const T& g, const T& h, const T& i, const T& j)
00329   {
00330     Array<T> rtn(10);
00331     rtn[0] = a;
00332     rtn[1] = b;
00333     rtn[2] = c;
00334     rtn[3] = d;
00335     rtn[4] = e;
00336     rtn[5] = f;
00337     rtn[6] = g;
00338     rtn[7] = h;
00339     rtn[8] = i;
00340     rtn[9] = j;
00341     return rtn;
00342   }
00343 }
00344 
00345 #endif // TEUCHOS_ARRAY_H
00346 

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