Util.h
Go to the documentation of this file.
00001 #ifndef UTIL_T1FR2WSR
00002 #define UTIL_T1FR2WSR
00003 
00004 /*
00005 File: Util.h
00006 Author: Samuel Barrett
00007 Description: a set of utility functions
00008 Created:  2011-08-23
00009 Modified: 2011-12-02
00010 */
00011 
00012 #include <bwi_tools/json/json.h>
00013 #include <boost/function.hpp>
00014 #include <string>
00015 #include <vector>
00016 #include <ostream>
00017 #include <map>
00018 #include <sys/time.h>
00019 
00020 #ifndef NULL
00021 #define NULL 0
00022 #endif
00023 
00024 inline double getTime() {
00025   struct timeval time;
00026 
00027   gettimeofday(&time,NULL);
00028   return time.tv_sec + time.tv_usec / 1000000.0;
00029 }
00030 void tic(int id=0);
00031 double toc(int id=0);
00032 void toc(double &counter, int id=0);
00033 
00034 class Timer {
00035 public:
00036   Timer():
00037     last(0),
00038     counter(0)
00039   {}
00040   inline void tic() {last = getTime(); }
00041   inline void toc() {counter += getTime()-last;}
00042   inline double get() {return counter;}
00043 private:
00044   double last;
00045   double counter;
00046 };
00047 
00048 template <class T>
00049 inline int sgn(const T &x) {
00050   return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
00051 }
00052 
00053 template <class T>
00054 inline T min(const T &x1, const T &x2) {
00055   return x1 < x2 ? x1 : x2;
00056 }
00057 
00058 template <class T>
00059 inline T max(const T &x1, const T &x2) {
00060   return x1 > x2 ? x1 : x2;
00061 }
00062 
00063 unsigned int vectorMaxInd(const std::vector<float> &arr);
00064 void vectorMax(const std::vector<float> &arr, float &maxVal, unsigned int &maxInd);
00065 
00066 float softmax(float x1, float x2, float factor); // returns the probability of x1 using a softmax with the given factor
00067 void softmax(const std::vector<unsigned int> &vals, float factor, std::vector<float> &probs); // fills out probs with the probabilities of the vals using a softmax with the given factor
00068 
00069 bool readJson(const std::string &filename, Json::Value &value);
00070 
00071 void jsonReplaceStrings(Json::Value &value, const std::map<std::string,std::string> &replacementMap);
00072 void jsonReplaceStrings(Json::Value &value, boost::function<void (Json::Value &)> replace);
00073 
00074 std::string indent(unsigned int indentation);
00075 
00076 template <class T>
00077 std::ostream &operator<<(std::ostream &out, const std::vector<T> &vect) {
00078   out << "[";
00079   for (unsigned int i = 0; i < vect.size(); i++) {
00080     out << vect[i];
00081     if (i != vect.size() - 1)
00082       out << ",";
00083   }
00084   out << "]";
00085   return out;
00086 }
00087 
00088 std::string tempFilename();
00089 
00090 #endif /* end of include guard: UTIL_T1FR2WSR */


bwi_tools
Author(s): Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:30