cvc_util.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  *\file cvc_util.h
00004  *\brief basic helper utilities
00005  *
00006  * Author: Clark Barrett
00007  *
00008  * Created: Thu Dec  1 16:35:52 2005
00009  *
00010  * <hr>
00011  *
00012  * License to use, copy, modify, sell and/or distribute this software
00013  * and its documentation for any purpose is hereby granted without
00014  * royalty, subject to the terms and conditions defined in the \ref
00015  * LICENSE file provided with this distribution.
00016  * 
00017  * <hr>
00018  */
00019 /*****************************************************************************/
00020 
00021 #ifndef _cvc3__debug_h
00022 #include "debug.h"
00023 #endif
00024 
00025 #ifndef _cvc3__cvc_util_h
00026 #define _cvc3__cvc_util_h
00027 
00028 namespace CVC3 {
00029 
00030 inline std::string int2string(int n) {
00031   std::ostringstream ss;
00032   ss << n;
00033   return ss.str();
00034 }
00035 
00036 template<class T>
00037 T abs(T t) { return t < 0 ? -t : t; }
00038 
00039 template<class T>
00040 T max(T a, T b) { return a > b ? a : b; }
00041 
00042 struct ltstr{
00043   bool operator()(const std::string& s1, const std::string& s2) const{
00044     return s1.compare(s2) < 0;
00045   }
00046 };
00047 
00048 template<class T>
00049 class StrPairLess {
00050 public:
00051   bool operator()(const std::pair<std::string,T>& p1,
00052       const std::pair<std::string,T>& p2) const {
00053     return p1.first < p2.first;
00054   }
00055 };
00056 
00057 template<class T>
00058 std::pair<std::string,T> strPair(const std::string& f, const T& t) {
00059   return std::pair<std::string,T>(f, t);
00060 }
00061 
00062 typedef std::pair<std::string,std::string> StrPair;
00063 
00064 //! Sort two vectors based on the first vector
00065 template<class T>
00066 void sort2(std::vector<std::string>& keys, std::vector<T>& vals) {
00067   DebugAssert(keys.size()==vals.size(), "sort2()");
00068   // Create std::vector of pairs
00069   std::vector<std::pair<std::string,T> > pairs;
00070   for(size_t i=0, iend=keys.size(); i<iend; ++i)
00071     pairs.push_back(strPair(keys[i], vals[i]));
00072   // Sort pairs
00073   StrPairLess<T> comp;
00074   sort(pairs.begin(), pairs.end(), comp);
00075   DebugAssert(pairs.size() == keys.size(), "sort2()");
00076   // Split the pairs back into the original vectors
00077   for(size_t i=0, iend=pairs.size(); i<iend; ++i) {
00078     keys[i] = pairs[i].first;
00079     vals[i] = pairs[i].second;
00080   }
00081 }
00082 
00083 }
00084 
00085 #endif

Generated on Tue Jul 3 14:33:36 2007 for CVC3 by  doxygen 1.5.1