00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef TPETRA_UTIL_HPP
00030 #define TPETRA_UTIL_HPP
00031
00032 #include "Tpetra_ConfigDefs.hpp"
00033 #include <Teuchos_Utils.hpp>
00034 #include <Teuchos_TestForException.hpp>
00035 #include <Teuchos_ArrayView.hpp>
00036
00037 namespace Tpetra {
00038
00039
00040
00041
00042 #define SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00043 { \
00044 const Ordinal lcl_throw_exception = (throw_exception_test) ? Teuchos::rank(comm)+1 : 0; \
00045 Ordinal gbl_throw; \
00046 Teuchos::reduceAll(comm,Teuchos::REDUCE_MAX,lcl_throw_exception,&gbl_throw); \
00047 TEST_FOR_EXCEPTION(gbl_throw,Exception, \
00048 msg << " Failure on node " << gbl_throw-1 << "." << std::endl); \
00049 }
00050
00051
00052
00053 #ifdef TEUCHOS_DEBUG
00054 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00055 { \
00056 SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm); \
00057 }
00058 #else
00059 #define SWITCHED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
00060 { \
00061 TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg); \
00062 }
00063 #endif
00064
00065
00088
00089
00090
00091 template<typename MapType, typename KeyArgType, typename ValueArgType>
00092 typename MapType::iterator efficientAddOrUpdate(MapType& m,
00093 const KeyArgType & k,
00094 const ValueArgType & v)
00095 {
00096 typename MapType::iterator lb = m.lower_bound(k);
00097 if(lb != m.end() && !(m.key_comp()(k, lb->first))) {
00098 lb->second = v;
00099 return(lb);
00100 }
00101 else {
00102 typedef typename MapType::value_type MVT;
00103 return(m.insert(lb, MVT(k, v)));
00104 }
00105 }
00106
00107
00108
00109
00110
00111 template <typename T>
00112 std::string toString(const std::vector<T> & x) {
00113 std::ostringstream os;
00114 os << "{";
00115 typename std::vector<T>::const_iterator i = x.begin();
00116 if (i != x.end()) {
00117 os << *i;
00118 i++;
00119 for(; i != x.end(); i++) {
00120 os << "," << *i;
00121 }
00122 }
00123 os << "}";
00124 return(os.str());
00125 }
00126
00127 template <typename T>
00128 std::string toString(const std::complex<T> & x) {
00129 return("(" + Teuchos::toString(x.real()) + "," + Teuchos::toString(x.imag()) + ")");
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 template<typename T1, typename T2>
00139 void sortArrays(const Teuchos::ArrayView<T1> &sortVals, const Teuchos::ArrayView<T2> &otherVals) {
00140
00141 TEST_FOR_EXCEPTION(sortVals.size() != otherVals.size(), std::runtime_error,
00142 "Error in Tpetra_Util::sortArrays(sortVals,otherVals): sortVals and otherVals are not equally sized");
00143
00144
00145
00146 std::multimap<T1,T2> tempMap;
00147 typename Teuchos::ArrayView<T1>::iterator keyIter = sortVals.begin();
00148 typename Teuchos::ArrayView<T2>::iterator valueIter = otherVals.begin();
00149 for(; keyIter != sortVals.end(); ++keyIter, ++valueIter) {
00150 tempMap.insert(std::pair<T1,T2>(*keyIter, *valueIter));
00151 }
00152
00153
00154
00155 keyIter = sortVals.begin();
00156 valueIter = otherVals.begin();
00157 for(typename std::multimap<T1,T2>::iterator i = tempMap.begin();
00158 i != tempMap.end(); ++i, ++keyIter, ++valueIter)
00159 {
00160 *keyIter = i->first;
00161 *valueIter = i->second;
00162 }
00163 }
00164
00165
00166 template<typename T1, typename T2, typename T3>
00167 void sortArrays(const Teuchos::ArrayView<T1> &sortVals,
00168 const Teuchos::ArrayView<T2> &otherVals1,
00169 const Teuchos::ArrayView<T2> &otherVals2)
00170 {
00171
00172 TEST_FOR_EXCEPTION((sortVals.size() != otherVals1.size()) || (sortVals.size() != otherVals2.size()), std::runtime_error,
00173 "Error in Tpetra_Util::sortArrays(sortVals,otherVals1,otherVals2): sortVals and otherVals are not equally sized");
00174
00175
00176
00177 typedef typename std::pair<T2, T3> ValuePair;
00178 std::multimap<T1, ValuePair> tempMap;
00179 typename Teuchos::ArrayView<T1>::iterator keyIter = sortVals.begin();
00180 typename Teuchos::ArrayView<T2>::iterator valueIter1 = otherVals1.begin();
00181 typename Teuchos::ArrayView<T3>::iterator valueIter2 = otherVals2.begin();
00182 for(; keyIter != sortVals.end(); ++keyIter, ++valueIter1, ++valueIter2) {
00183 tempMap.insert(std::pair<T1, ValuePair>(*keyIter, ValuePair(*valueIter1, *valueIter2)));
00184 }
00185
00186
00187
00188 keyIter = sortVals.begin();
00189 valueIter1 = otherVals1.begin();
00190 valueIter2 = otherVals2.begin();
00191 for(typename std::multimap<T1,ValuePair>::iterator i = tempMap.begin();
00192 i != tempMap.end(); i++, keyIter++, valueIter1++, valueIter2++)
00193 {
00194 *keyIter = i->first;
00195 *valueIter1 = i->second.first;
00196 *valueIter2 = i->second.second;
00197 }
00198 }
00199
00200 }
00201
00202 #endif // TPETRA_UTIL_HPP