Created
September 7, 2017 08:24
-
-
Save bramblex/e3b2ffbddd173ce8ec1541881008df82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 一些方便用的函数库 | |
| #ifndef GAME_ENGINE_UTILS_HPP | |
| #define GAME_ENGINE_UTILS_HPP | |
| #include <iostream> | |
| #include <vector> | |
| #include <functional> | |
| #include <map> | |
| namespace Utils { | |
| template <class Key, class T> | |
| void travelMap(const std::map<Key, T> & m, const std::function<void(const std::pair<Key, T>&)> f) { | |
| for (auto it = m.begin(); it != m.end(); ++it) { | |
| f(*it); | |
| } | |
| }; | |
| template<class T> | |
| void forEach(const std::vector<T> &arr, const std::function<void(const T&)> f) { | |
| for (auto it = arr.begin(); it != arr.end(); ++it) { | |
| f(*it); | |
| } | |
| }; | |
| template <class T> | |
| void forEachRight(const std::vector<T> &arr, const std::function<void(const T&)> f) { | |
| for (auto it = arr.end(); it != arr.begin(); --it) { | |
| f(*it); | |
| } | |
| }; | |
| template <class T> | |
| void showArray(const std::vector<T> &arr) { | |
| forEach<T>(arr, [](T item){ | |
| std::cout << item << " ,"; | |
| }); | |
| std::cout << std::endl; | |
| }; | |
| template <class T> | |
| std::vector<T> concat(const std::vector<T> &arr1, const std::vector<T> &arr2){ | |
| std::vector<T> arr; | |
| auto const f = [&arr](T item){ arr.push_back(item); }; | |
| forEach<T>(arr1, f); | |
| forEach<T>(arr2, f); | |
| return arr; | |
| }; | |
| template <class T> | |
| std::vector<T> filter(const std::vector<T> &arr, std::function<bool(const T&)> f){ | |
| std::vector<T> new_arr; | |
| forEach<T>(arr, [&new_arr, &f](const T& item){ | |
| if (f(item)) new_arr.push_back(item); | |
| }); | |
| return new_arr; | |
| }; | |
| template <class T0, class T1> | |
| std::vector<T1> map(const std::vector<T0> &arr, std::function<T1(const T0&)> f){ | |
| std::vector<T1> result; | |
| for (auto it = arr.begin(); it != arr.end(); ++it) { | |
| result.push_back(f(*it)); | |
| } | |
| return result; | |
| }; | |
| template <class T0, class T1> | |
| T1 fold(const std::vector<T0> &arr, T1 _left, std::function<T1(const T1&, const T0&)> f){ | |
| T1 left = _left; | |
| forEach<T0>(arr, [&left, &f](T0 right) { | |
| left = f(left, right); | |
| }); | |
| return left; | |
| }; | |
| template <class T0, class T1> | |
| T1 foldr(const std::vector<T0> &arr, T1 _left, std::function<T1(const T1&, const T0&)> f){ | |
| T1 left = _left; | |
| forEachRight<T0>(arr, [&left, &f](T0 right) { | |
| left = f(left, right); | |
| }); | |
| return left; | |
| }; | |
| template<class Key, class T> | |
| std::map<Key, std::vector<T>> group(const std::vector<T> &arr, std::function<Key(const T&)> f){ | |
| std::map<Key, std::vector<T>> grouped; | |
| forEach<T>(arr, [&grouped, &f](const T &item){ | |
| auto key = f(item); | |
| auto search = grouped.find(key); | |
| if (search == grouped.end()){ | |
| std::vector<T> content; | |
| grouped.insert(std::pair<Key, std::vector<T>>(key, content)); | |
| } else { | |
| search->second.push_back(item); | |
| } | |
| }); | |
| return grouped; | |
| }; | |
| // template<class Key0, class T0, class Key1 = Key0, class T1 = T0> | |
| // @TODO: map pick merge | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment