simox_alg.hpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::RobotStatePredictionClientExample
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2022
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <map>
26#include <vector>
27
28namespace simox::alg
29{
30 template <class... Args>
31 std::vector<Args...>
32 concatenate(const std::vector<Args...>& lhs, const std::vector<Args...>& rhs)
33 {
34 std::vector<Args...> conc = lhs;
35 std::copy(rhs.begin(), rhs.end(), std::back_inserter(conc));
36 return conc;
37 }
38
39 template <class KeyT, class ValueT>
40 std::map<KeyT, ValueT>
41 map_from_key_value_pairs(const std::vector<KeyT>& lhs, const std::vector<ValueT>& rhs)
42 {
43 const size_t size = std::min(lhs.size(), rhs.size());
44
45 std::map<KeyT, ValueT> map;
46 for (size_t i = 0; i < size; ++i)
47 {
48 map.emplace(lhs[i], rhs[i]);
49 }
50 return map;
51 }
52
53 template <class KeyT, class ValueT>
54 std::vector<ValueT>
55 multi_at(const std::map<KeyT, ValueT>& map,
56 const std::vector<KeyT>& keys,
57 bool skipMissing = false)
58 {
59 std::vector<ValueT> values;
60 values.reserve(keys.size());
61
62 for (const KeyT& key : keys)
63 {
64 if (skipMissing)
65 {
66 if (auto it = map.find(key); it != map.end())
67 {
68 values.push_back(it->second);
69 }
70 }
71 else
72 {
73 // Throw an exception if missing.
74 values.push_back(map.at(key));
75 }
76 }
77
78 return values;
79 }
80
81 template <class... Args>
82 std::vector<Args...>
83 slice(const std::vector<Args...>& vector,
84 size_t start = 0,
85 std::optional<size_t> end = std::nullopt)
86 {
87 std::vector<Args...> result;
88 auto beginIt = vector.begin() + start;
89 auto endIt = end ? vector.begin() + *end : vector.end();
90 std::copy(beginIt, endIt, std::back_inserter(result));
91 return result;
92 }
93
94} // namespace simox::alg
std::vector< ValueT > multi_at(const std::map< KeyT, ValueT > &map, const std::vector< KeyT > &keys, bool skipMissing=false)
Definition Impl.cpp:67
std::vector< Args... > concatenate(const std::vector< Args... > &lhs, const std::vector< Args... > &rhs)
Definition Impl.cpp:44
std::vector< Args... > slice(const std::vector< Args... > &vector, size_t start=0, std::optional< size_t > end=std::nullopt)
Definition Impl.cpp:95
std::map< KeyT, ValueT > map_from_key_value_pairs(const std::vector< KeyT > &lhs, const std::vector< ValueT > &rhs)
Definition Impl.cpp:53