/* * Copyright (c) 2014, killerbee13 * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #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 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