Debian Codegraph @

src/internal/containers.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 <functional>
27 #include <vector>
28
29 /**
30 * @file
31 * @brief Helper functions for containers.
32 */
33
34 namespace injeqt { namespace internal {
35
36 /**
37 * @brief Functional interface to std::transform.
38 * @tparam S type of source items
39 * @tparam T type of result items
40 * @param source source data
41 * @param f transforming function
42 */
43 template<typename S, typename T>
44 inline std::vector<T> transform(const std::vector<S> &source, std::function<T(S)> f)
45 {
46 auto result = std::vector<T>{};
47 std::transform(std::begin(source), std::end(source), std::back_inserter(result), f);
48 return result;
49 }
50
51 /**
52 * @brief Extract data from multiple vectors into one vector.
53 * @tparam S type of source items
54 * @tparam T type of result items
55 * @param f function returning vector for each item in source
56 */
57 template<typename S, typename T>
58 inline std::vector<T> extract(const std::vector<S> &sources, std::function<std::vector<T>(const S &)> f)
59 {
60 auto result = std::vector<T>{};
61 for (auto &&source : sources)
62 {
63 auto v = f(source);
64 std::copy(std::begin(v), std::end(v), std::back_inserter(result));
65 }
66 return result;
67 }
68
69 }}