cvclutil.h

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

Generated on Thu Apr 13 16:57:30 2006 for CVC Lite by  doxygen 1.4.4