#pragma once #include #include #include #include #include // Used to silence warnings, will assert in debug build inline unsigned int make_u(int x){ assert(x >= 0); return static_cast(x); } inline uint8_t to_uint8_t(double x){ if(x >= 1) return 255; if(x <= 0) return 0; return static_cast(255*x); } // Makes numbers human-readable with one decimal // eg 2350000 becomes 2.3M template inline std::string human_string(Int n, std::string suffix = ""){ static const std::string names [] = {"", "K", "M", "G"}; unsigned int i = 0; Int m = 10*n; while(n > 1000 && i < sizeof(names)){ n /= 1000; m /= 1000; ++i; } // cast is to make the old gcc 4.4 happy (it doesn't have all overloads of to_string) return std::to_string(n) + "." + std::to_string(m % 10) + names[i] + suffix; } inline std::string field(std::string const & str){ const int length = 12; if(str.size() > length) return str + ":\t"; auto add = length - str.size(); return str + ":" + std::string(add, ' ') + "\t"; } inline std::string to_lower(std::string in){ std::transform(in.begin(), in.end(), in.begin(), ::tolower); return in; } // RAII struct for timing struct timer{ typedef std::chrono::high_resolution_clock clock; typedef std::chrono::time_point time; typedef std::chrono::duration seconds; std::string name; time begin; timer(std::string name_) : name(name_) , begin(clock::now()) {} ~timer(){ time end = clock::now(); std::cout << name << "\t" << from_dur(end - begin) << std::endl; } static double from_dur(seconds s){ return s.count(); } }; namespace colors { inline std::string red(std::string s){ return "\x1b[31m" + s + "\x1b[39m"; } inline std::string green(std::string s){ return "\x1b[32m" + s + "\x1b[39m"; } }