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 #include "Teuchos_TypeNameTraits.hpp"
00040 
00041 namespace Teuchos
00042 {
00043 
00047   class InvalidArrayStringRepresentation : public std::logic_error
00048   {public:InvalidArrayStringRepresentation(const std::string& what_arg) : std::logic_error(what_arg) {}};
00049 
00054   template<class T>
00055   class Array : public std::vector<T>
00056   {
00057   public:
00059     Array();
00060 
00062     Array(int n);
00063 
00065     Array(int n, const T& t);
00066 
00068     inline Array<T>& append(const T& entry) {this->push_back(entry); return *this;}
00069 
00071     void remove(int i);
00072 
00076     int length() const {return this->size();}
00077 
00079     inline T& operator[](int i);
00080 
00082     inline const T& operator[](int i) const;
00083 
00085     std::string toString() const ;
00086 
00088     static bool hasBoundsChecking();
00089 
00090   private:
00091 
00094     void indexCheckCrash(int i) const;
00095   };
00096 
00100   template<class T> std::ostream& operator<<(std::ostream& os, 
00101                                              const Array<T>& array);
00102 
00104   template<class T> int hashCode(const Array<T>& array);
00105 
00107   template<class T> std::string toString(const Array<T>& array);
00108 
00109   template<class T> inline Array<T>::Array()
00110     : std::vector<T>()
00111   {}
00112 
00113   template<class T> inline Array<T>::Array(int n)
00114     : std::vector<T>(n)
00115   {}
00116 
00117   template<class T> inline Array<T>::Array(int n, const T& t)
00118     : std::vector<T>(n, t)
00119   {}
00120 
00121   template<class T>
00122   void Array<T>::remove(int i) {
00123 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00124     indexCheckCrash(i);
00125 #endif
00126     // Erase the i-th element of this array.
00127     this->erase( this->begin() + i );
00128   }
00129 
00130   template<class T> inline
00131   T& Array<T>::operator[](int i) {
00132 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00133     indexCheckCrash(i);
00134 #endif
00135     return std::vector<T>::operator[](i);
00136   }
00137 
00138   template<class T> inline
00139   const T& Array<T>::operator[](int i) const {
00140 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00141     indexCheckCrash(i);
00142 #endif
00143     return std::vector<T>::operator[](i);
00144   }
00145 
00146   template<class T> inline
00147   bool Array<T>::hasBoundsChecking()
00148   {
00149 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK  
00150     return true;
00151 #else
00152     return false;
00153 #endif
00154   }
00155 
00156   template<class T> inline
00157   void Array<T>::indexCheckCrash(int i) const
00158   {
00159     TEST_FOR_EXCEPTION(
00160       !( 0 <= i && i < length() ), std::range_error,
00161       "Array<"<<TypeNameTraits<T>::name()<<">::indexCheckCrash: "
00162       "index " << i << " out of range [0, "<< length() << ")"
00163       );
00164   }
00165 
00166   // print in form (), (1), or (1,2)
00167   template<class T> inline std::ostream& operator<<(std::ostream& os, const Array<T>& array)
00168   {
00169     return os << Teuchos::toString(array);
00170   }
00171 
00172   template<class T> inline int hashCode(const Array<T>& array)
00173   {
00174     int rtn = hashCode(array.length());
00175     for (int i=0; i<array.length(); i++)
00176       {
00177         rtn += hashCode(array[i]);
00178       }
00179     return rtn;
00180   }
00181 
00182   template<class T> inline std::string Array<T>::toString() const
00183   {
00184     std::ostringstream ss;
00185     ss << "{";
00186 
00187     for (int i=0; i<length(); i++)
00188       {
00189         ss << operator[](i);
00190         if (i<length()-1) ss << ", ";
00191       }
00192     ss << "}";
00193 
00194     return ss.str();
00195   }
00196 
00198   template<class T> inline
00199   std::string toString(const Array<T>& array)
00200   {
00201     return array.toString();
00202   }
00203 
00255   template<class T>
00256   Array<T> fromStringToArray(const std::string& arrayStr)
00257   {
00258     const std::string str = Utils::trimWhiteSpace(arrayStr);
00259     std::istringstream iss(str);
00260     TEST_FOR_EXCEPTION(
00261       ( str[0]!='{' || str[str.length()-1] != '}' )
00262       ,InvalidArrayStringRepresentation
00263       ,"Error, the std::string:\n"
00264       "----------\n"
00265       <<str<<
00266       "\n----------\n"
00267       "is not a valid array represntation!"
00268       );
00269     char c;
00270     c = iss.get(); // Read initial '{'
00271     TEST_FOR_EXCEPT(c!='{'); // Should not throw!
00272     // Now we are ready to begin reading the entries of the array!
00273     Array<T> a;
00274     while( !iss.eof() ) {
00275       // Get the basic entry std::string
00276       std::string entryStr;
00277       std::getline(iss,entryStr,','); // Get next entry up to ,!
00278       // ToDo: Above, we might have to be careful to look for the opening and
00279       // closing of parentheses in order not to pick up an internal ',' in the
00280       // middle of an entry (for a std::complex number for instance).  The above
00281       // implementation assumes that there will be no commas in the middle of
00282       // the std::string representation of an entry.  This is certainly true for
00283       // the types bool, int, float, and double.
00284       //
00285       // Trim whitespace from beginning and end
00286       entryStr = Utils::trimWhiteSpace(entryStr);
00287       // Remove the final '}' if this is the last entry and we did not
00288       // actually terminate the above getline(...) on ','
00289       bool found_end = false;
00290       if(entryStr[entryStr.length()-1]=='}') {
00291         entryStr = entryStr.substr(0,entryStr.length()-1);
00292         found_end = true;
00293         if( entryStr.length()==0 && a.size()==0 )
00294           return a; // This is the empty array "{}" (with any spaces in it!)
00295       }
00296       TEST_FOR_EXCEPTION(
00297         0 == entryStr.length()
00298         ,InvalidArrayStringRepresentation
00299         ,"Error, the std::string:\n"
00300         "----------\n"
00301         <<str<<
00302         "\n----------\n"
00303         "is not a valid array represntation!"
00304         );
00305       // Finally we can convert the entry and add it to the array!
00306       std::istringstream entryiss(entryStr);
00307       T entry;
00308       entryiss >> entry; // Assumes type has operator>>(...) defined!
00309       // ToDo: We may need to define a traits class to allow us to specialized
00310       // how conversion from a std::string to a object is done!
00311       a.push_back(entry);
00312       // At the end of the loop body here, if we have reached the last '}'
00313       // then the input stream iss should be empty and iss.eof() should be
00314       // true, so the loop should terminate.  We put an std::exception test here
00315       // just in case something has gone wrong.
00316       TEST_FOR_EXCEPTION(
00317         found_end && !iss.eof()
00318         ,InvalidArrayStringRepresentation
00319         ,"Error, the std::string:\n"
00320         "----------\n"
00321         <<str<<
00322         "\n----------\n"
00323         "is not a valid array represntation!"
00324         );
00325     }
00326     return a;
00327   }
00328                       
00332   template<class T> inline
00333   Array<T> tuple(const T& a)
00334   {
00335     Array<T> rtn(1, a);
00336     return rtn;
00337   }
00338 
00342   template<class T> inline
00343   Array<T> tuple(const T& a, const T& b)
00344   {
00345     Array<T> rtn(2);
00346     rtn[0] = a;
00347     rtn[1] = b;
00348     return rtn;
00349   }
00350 
00354   template<class T> inline
00355   Array<T> tuple(const T& a, const T& b, const T& c)
00356   {
00357     Array<T> rtn(3);
00358     rtn[0] = a;
00359     rtn[1] = b;
00360     rtn[2] = c;
00361     return rtn;
00362   }
00363 
00367   template<class T> inline
00368   Array<T> tuple(const T& a, const T& b, const T& c, const T& d)
00369   {
00370     Array<T> rtn(4);
00371     rtn[0] = a;
00372     rtn[1] = b;
00373     rtn[2] = c;
00374     rtn[3] = d;
00375     return rtn;
00376   }
00377 
00381   template<class T> inline
00382   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e)
00383   {
00384     Array<T> rtn(5);
00385     rtn[0] = a;
00386     rtn[1] = b;
00387     rtn[2] = c;
00388     rtn[3] = d;
00389     rtn[4] = e;
00390     return rtn;
00391   }
00392 
00393 
00397   template<class T> inline
00398   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00399                  const T& f)
00400   {
00401     Array<T> rtn(6);
00402     rtn[0] = a;
00403     rtn[1] = b;
00404     rtn[2] = c;
00405     rtn[3] = d;
00406     rtn[4] = e;
00407     rtn[5] = f;
00408     return rtn;
00409   }
00410 
00414   template<class T> inline
00415   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00416                  const T& f, const T& g)
00417   {
00418     Array<T> rtn(7);
00419     rtn[0] = a;
00420     rtn[1] = b;
00421     rtn[2] = c;
00422     rtn[3] = d;
00423     rtn[4] = e;
00424     rtn[5] = f;
00425     rtn[6] = g;
00426     return rtn;
00427   }
00428 
00432   template<class T> inline
00433   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00434                  const T& f, const T& g, const T& h)
00435   {
00436     Array<T> rtn(8);
00437     rtn[0] = a;
00438     rtn[1] = b;
00439     rtn[2] = c;
00440     rtn[3] = d;
00441     rtn[4] = e;
00442     rtn[5] = f;
00443     rtn[6] = g;
00444     rtn[7] = h;
00445     return rtn;
00446   }
00447 
00451   template<class T> inline
00452   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00453                  const T& f, const T& g, const T& h, const T& i)
00454   {
00455     Array<T> rtn(9);
00456     rtn[0] = a;
00457     rtn[1] = b;
00458     rtn[2] = c;
00459     rtn[3] = d;
00460     rtn[4] = e;
00461     rtn[5] = f;
00462     rtn[6] = g;
00463     rtn[7] = h;
00464     rtn[8] = i;
00465     return rtn;
00466   }
00467 
00468 
00472   template<class T> inline
00473   Array<T> tuple(const T& a, const T& b, const T& c, const T& d, const T& e,
00474                  const T& f, const T& g, const T& h, const T& i, const T& j)
00475   {
00476     Array<T> rtn(10);
00477     rtn[0] = a;
00478     rtn[1] = b;
00479     rtn[2] = c;
00480     rtn[3] = d;
00481     rtn[4] = e;
00482     rtn[5] = f;
00483     rtn[6] = g;
00484     rtn[7] = h;
00485     rtn[8] = i;
00486     rtn[9] = j;
00487     return rtn;
00488   }
00489 }
00490 
00491 #endif // TEUCHOS_ARRAY_H

Generated on Tue Oct 20 12:45:25 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7