#ifndef SIMPLE_ALGORITHMS_H_INCLUDED #define SIMPLE_ALGORITHMS_H_INCLUDED //Header containing frequently reused code that is not as easily done without helper functions #include #include #include #include #include #include #include #include #include #include #include //This is the only one that can be expected to throw an exception template inline T fromStr(std::string val) { std::stringstream ss(val); T ret; if (!(ss>>ret).fail()) return ret; else throw std::runtime_error("\""+val+"\" is not a "+typeid(T).name()); } //Remember std::to_string() in for builtin types template inline std::string toStr(T val) { std::stringstream ss; ss< inline T arraycat(T A, T B) { A.insert(A.end(), B.begin(), B.end()); return A; } template inline std::vector listToVec(std::string list) { std::vector ret; std::stringstream ss(list); std::string tmp; while(!(ss>>tmp).fail()) { ret.push_back(fromStr(tmp)); } /* while(list.find_first_of(" ,") != std::string::npos) { std::string entry = list.substr(0, list.find_first_of(" ,")); ret.push_back(fromStr(entry)); list = list.substr(entry.size()); if (list.find_first_of(" ,") == std::string::npos) { entry = list.substr(0, list.find_first_of(" ,")); ret.push_back(fromStr(entry)); list = list.substr(entry.size()); } }*/ return ret; } inline std::string repeat(std::string val, int count) { std::string tmp; for (int i = 0; i < count; ++i) { tmp += val; } return tmp; } inline std::string repeat(char val, int count) { std::string tmp; for (int i = 0; i < count; ++i) { tmp += val; } return tmp; } template constexpr inline int digitsOf(Number val) { return std::floor(std::log10(val))+1; } template constexpr inline int digitsOf(Number val, int base) { return std::floor(std::log(val)/std::log(base))+1; } template inline int digitsList(ForwardIterator first, ForwardIterator last) { return digitsOf(*std::max_element(first, last)); } template inline int digitsList(ForwardIterator first, ForwardIterator last, int base) { return digitsOf(*std::max_element(first, last), base); } //Not working template inline ostream_type& padList(ostream_type& os, ForwardIterator first, ForwardIterator last, char fill=' ', int base=10) { //os< class ordered_pair { using pair=std::pair; public: ordered_pair(T a, T b) { if (a < b) {first = a; second = b;} else {first = b; second = a;} } T first, second; operator pair() {return std::pair(first, second);} bool operator < (const ordered_pair& rhs) const { if (first < rhs.first) return 1; if (second < rhs.second) return 1; } }; template ordered_pair mkop(T a, T b) { return ordered_pair(a, b); } //Memnonic aids template using Ptr = T*; template using PtrToConst = T const*; template using ConstPtr = T* const; template using ConstPtrToConst = T const* const; #endif //SIMPLE_ALGORITHMS_H_INCLUDED