| 1 |
/* |
| 2 |
* %injeqt copyright begin% |
| 3 |
* Copyright 2014 Rafał Malinowski ([email protected]) |
| 4 |
* %injeqt copyright end% |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include "internal.h" |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
/** |
| 29 |
* @file |
| 30 |
* @brief Helper functions for containers. |
| 31 |
*/ |
| 32 |
|
| 33 |
namespace injeqt { namespace internal { |
| 34 |
|
| 35 |
/** |
| 36 |
* @brief Functional interface to std::transform. |
| 37 |
* @tparam S type of source items |
| 38 |
* @tparam T type of result items |
| 39 |
* @param source source data |
| 40 |
* @param f transforming function |
| 41 |
*/ |
| 42 |
template<typename S, typename T> |
| 43 |
inline std::vector<T> transform(const std::vector<S> &source, std::function<T(S)> f) |
| 44 |
{ |
| 45 |
auto result = std::vector<T>{}; |
| 46 |
std::transform(std::begin(source), std::end(source), std::back_inserter(result), f); |
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @brief Extract data from multiple vectors into one vector. |
| 52 |
* @tparam S type of source items |
| 53 |
* @tparam T type of result items |
| 54 |
* @param f function returning vector for each item in source |
| 55 |
*/ |
| 56 |
template<typename S, typename T> |
| 57 |
inline std::vector<T> extract(const std::vector<S> &sources, std::function<std::vector<T>(const S &)> f) |
| 58 |
{ |
| 59 |
auto result = std::vector<T>{}; |
| 60 |
for (auto &&source : sources) |
| 61 |
{ |
| 62 |
auto v = f(source); |
| 63 |
std::copy(std::begin(v), std::end(v), std::back_inserter(result)); |
| 64 |
} |
| 65 |
return result; |
| 66 |
} |
| 67 |
|
| 68 |
}} |