SHARE
TWEET

C++ algorithm utilities

ulfben Nov 1st, 2017 (edited) 166 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.     static std::vector<std::string> split(const std::string& s, const char delimiter = ' '){
  12.         std::vector<std::string> tokens;
  13.         std::string token;
  14.         std::istringstream tokenStream(s);
  15.         while (std::getline(tokenStream, token, delimiter)){
  16.             tokens.push_back(token);
  17.         }
  18.         return tokens;
  19.     }
  20.  
  21.     const char* WHITEPACE = " \t\n\r\f\v";
  22.     static inline void rtrim(std::string& s, const char* t = WHITEPACE){
  23.         s.erase(s.find_last_not_of(t) + 1);
  24.     }
  25.     static inline void ltrim(std::string& s, const char* t = WHITEPACE){
  26.         s.erase(0, s.find_first_not_of(t));
  27.     }
  28.     static inline void trim(std::string& s, const char* t = WHITEPACE){
  29.         rtrim(s, t);
  30.         ltrim(s, t);
  31.     }
  32.  
  33.     template <class Container>
  34.     static void sort(Container& c) {
  35.         std::sort(std::begin(c), std::end(c));
  36.     }
  37.     template <class Container>
  38.     static typename Container::iterator find(const Container& c, const typename Container::value_type& value) {
  39.         return std::find(std::begin(c), std::end(c), value);
  40.     }
  41.     template <class Container>
  42.     static typename Container::const_iterator find(const Container& c, const typename Container::value_type& value) {
  43.         return std::find(std::begin(c), std::end(c), value);
  44.     }
  45.  
  46.     template <typename T>
  47.     static void quick_remove_at(std::vector<T>& v, std::size_t i) {
  48.         assert(i < v.size());
  49.         v[i] = std::move(v.back());
  50.         v.pop_back();
  51.     }
  52.     template <typename T>
  53.     static void quick_remove_at(std::vector<T>& v, typename std::vector<T>::iterator it) {
  54.         assert(it != std::end(v));
  55.         *it = std::move(v.back());
  56.         v.pop_back();
  57.     }
  58.     template <class Container>
  59.     static void insert_sorted(Container& v, const typename Container::value_type& item) {
  60.         assert(true == std::is_sorted(begin(v), end(v)));
  61.         const auto insert_pos(std::lower_bound(std::begin(v), std::end(v), item));
  62.         v.insert(insert_pos, item);
  63.     }
  64.     template <class Container>
  65.     static void print(const Container &v, const std::string& sep = ", "s) {
  66.         using T = typename Container::value_type;
  67.         std::copy(std::begin(v), std::end(v), std::ostream_iterator<T>{std::cout, sep});
  68.     }
  69.     template <class AssociativeContainer>
  70.     static void print_map(const AssociativeContainer &map, const std::string& sep = "\n"s) {
  71.         for (auto& elem : map)  {
  72.             std::cout << elem.first << ": " << elem.second << sep;
  73.         }
  74.     }
  75.    
  76.  
  77.  
  78.     //removing from sequence containers
  79.     template <class T>
  80.     static void remove(std::vector<T>& vector, T const& value) {
  81.         vector.erase(std::remove(std::begin(vector), std::end(vector), value), std::end(vector));
  82.     }
  83.     template <class T>
  84.     static void remove(std::deque<T>& deque, T const& value) {
  85.         deque.erase(std::remove(std::begin(deque), std::end(deque), value), std::end(deque));
  86.     }
  87.     static void remove(std::string& string, char letter) {
  88.         string.erase(std::remove(std::begin(string), std::end(string), letter), std::end(string));
  89.     }
  90.     template <class AssociativeContainer, class T>
  91.     static void remove(AssociativeContainer& c, const T& key) {
  92.         c.erase(key);
  93.     }
  94.  
  95.     //filtered remove from sequence containers
  96.     template <class T, typename Predicate>
  97.     static void remove_if(std::vector<T>& vector, Predicate pred) {
  98.         vector.erase(std::remove_if(std::begin(vector), std::end(vector), pred), std::end(vector));
  99.     }
  100.     template <class T, typename Predicate>
  101.     static void remove_if(std::deque<T>& deque, Predicate pred) {
  102.         deque.erase(std::remove_if(std::begin(deque), std::end(deque), pred), std::end(deque));
  103.     }
  104.     template <class Predicate>
  105.     static void remove_if(std::string& string, Predicate pred) {
  106.         string.erase(std::remove_if(std::begin(string), std::end(string), pred), std::end(string));
  107.     }
  108.     template <class AssociativeContainer, class Predicate>
  109.     static void remove_if(AssociativeContainer& c, Predicate pred) {
  110.         auto i = std::begin(c);
  111.         const auto last = std::end(c);
  112.         while ((i = std::find_if(i, last, pred)) != last) {
  113.             i = c.erase(i);
  114.         }
  115.     }
  116.  
  117.     //remove duplicates from sequence containers
  118.     template <class T>
  119.     static void unique(std::vector<T>& vector) {
  120.         vector.erase(std::unique(std::begin(vector), std::end(vector)), std::end(vector));
  121.     }
  122.     template <class T>
  123.     static void unique(std::deque<T>& deque) {
  124.         deque.erase(std::unique(std::begin(deque), std::end(deque)), std::end(deque));
  125.     }
  126.     static void unique(std::string& string) {
  127.         string.erase(std::unique(std::begin(string), std::end(string)), std::end(string));
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top