ulfben

C++ algorithm utilities

Nov 1st, 2017
230
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <deque>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <iterator>
  8. #include <string>
  9. #include <vector>
  10. namespace Utils {
  11.     using namespace std::literals::string_literals;
  12.     template <typename E>
  13.     constexpr typename std::underlying_type<E>::type to_underlying(E e) noexcept {
  14.         return static_cast<typename std::underlying_type<E>::type>(e);
  15.     }
  16.  
  17.     template <typename T>
  18.     static constexpr T wrap(T val, T min, T max) {
  19.         return (val > max) ? min : (val < min) ? max : val;
  20.     }
  21.  
  22.     static std::vector<std::string> split(const std::string& s, const char delimiter = ' ') {
  23.         std::vector<std::string> tokens;
  24.         std::string token;
  25.         std::istringstream tokenStream(s);
  26.         while (std::getline(tokenStream, token, delimiter)) {
  27.             tokens.push_back(token);
  28.         }
  29.         return tokens;
  30.     }
  31.  
  32.     static constexpr std::string_view WHITEPACE{" \t\n\r\f\v"};
  33.     static inline void rtrim(std::string& s, std::string_view t = WHITEPACE) {
  34.         s.erase(s.find_last_not_of(t) + 1);
  35.     }
  36.     static inline void ltrim(std::string& s, std::string_view t = WHITEPACE) {
  37.         s.erase(0, s.find_first_not_of(t));
  38.     }
  39.     static inline void trim(std::string& s, std::string_view t = WHITEPACE) {
  40.         rtrim(s, t);
  41.         ltrim(s, t);
  42.     }
  43.  
  44.     template <typename T>
  45.     static constexpr T clamp(T val, T min, T max) noexcept {
  46.         return val < min ? min : val > max ? max : val;
  47.     }
  48.  
  49.     template <class Container>
  50.     static void sort(Container& c) {
  51.         std::sort(std::begin(c), std::end(c));
  52.     }
  53.     template <class Container>
  54.     static typename Container::iterator find(const Container& c, const typename Container::value_type& value) {
  55.         return std::find(std::begin(c), std::end(c), value);
  56.     }
  57.     template <class Container>
  58.     static typename Container::const_iterator find(const Container& c, const typename Container::value_type& value) {
  59.         return std::find(std::begin(c), std::end(c), value);
  60.     }
  61.  
  62.     template <class Container, typename Predicate>
  63.     static typename Container::iterator find_if(const Container& c, Predicate pred) {
  64.         return std::find_if(std::begin(c), std::end(c), pred);
  65.     }
  66.  
  67.     template <typename T>
  68.     static void quick_remove_at(std::vector<T>& v, std::size_t i) {
  69.         assert(i < v.size());
  70.         v[i] = std::move(v.back());
  71.         v.pop_back();
  72.     }
  73.     template <typename T>
  74.     static void quick_remove_at(std::vector<T>& v, typename std::vector<T>::iterator it) {
  75.         assert(it != std::end(v));
  76.         *it = std::move(v.back());
  77.         v.pop_back();
  78.     }
  79.     template <class Container>
  80.     static void insert_sorted(Container& v, const typename Container::value_type& item) {
  81.         assert(true == std::is_sorted(begin(v), end(v)));
  82.         const auto insert_pos(std::lower_bound(std::begin(v), std::end(v), item));
  83.         v.insert(insert_pos, item);
  84.     }
  85.     template <class Container>
  86.     static void print(const Container &v, const std::string& sep = ", "s) {
  87.         using T = typename Container::value_type;
  88.         std::copy(std::begin(v), std::end(v), std::ostream_iterator<T>{std::cout, sep});
  89.     }
  90.     template <class AssociativeContainer>
  91.     static void print_map(const AssociativeContainer &map, const std::string& sep = "\n"s) {
  92.         for (auto& elem : map) {
  93.             std::cout << elem.first << ": " << elem.second << sep;
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99.     //removing from sequence containers
  100.     template <class T>
  101.     static void remove(std::vector<T>& vector, T const& value) {
  102.         vector.erase(std::remove(std::begin(vector), std::end(vector), value), std::end(vector));
  103.     }
  104.     template <class T>
  105.     static void remove(std::deque<T>& deque, T const& value) {
  106.         deque.erase(std::remove(std::begin(deque), std::end(deque), value), std::end(deque));
  107.     }
  108.     static void remove(std::string& string, char letter) {
  109.         string.erase(std::remove(std::begin(string), std::end(string), letter), std::end(string));
  110.     }
  111.     template <class AssociativeContainer, class T>
  112.     static void remove(AssociativeContainer& c, const T& key) {
  113.         c.erase(key);
  114.     }
  115.  
  116.     //filtered remove from sequence containers
  117.     template <class T, typename Predicate>
  118.     static void remove_if(std::vector<T>& vector, Predicate pred) {
  119.         vector.erase(std::remove_if(std::begin(vector), std::end(vector), pred), std::end(vector));
  120.     }
  121.     template <class T, typename Predicate>
  122.     static void remove_if(std::deque<T>& deque, Predicate pred) {
  123.         deque.erase(std::remove_if(std::begin(deque), std::end(deque), pred), std::end(deque));
  124.     }
  125.     template <class Predicate>
  126.     static void remove_if(std::string& string, Predicate pred) {
  127.         string.erase(std::remove_if(std::begin(string), std::end(string), pred), std::end(string));
  128.     }
  129.     template <class AssociativeContainer, class Predicate>
  130.     static void remove_if(AssociativeContainer& c, Predicate pred) {
  131.         auto i = std::begin(c);
  132.         const auto last = std::end(c);
  133.         while ((i = std::find_if(i, last, pred)) != last) {
  134.             i = c.erase(i);
  135.         }
  136.     }
  137.  
  138.     //remove duplicates from sequence containers
  139.     template <class T>
  140.     static void unique(std::vector<T>& vector) {
  141.         vector.erase(std::unique(std::begin(vector), std::end(vector)), std::end(vector));
  142.     }
  143.     template <class T>
  144.     static void unique(std::deque<T>& deque) {
  145.         deque.erase(std::unique(std::begin(deque), std::end(deque)), std::end(deque));
  146.     }
  147.     static void unique(std::string& string) {
  148.         string.erase(std::unique(std::begin(string), std::end(string)), std::end(string));
  149.     }
  150. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×