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 
28 namespace 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 
40  template <class KeyT, class ValueT>
41  std::map<KeyT, ValueT>
42  map_from_key_value_pairs(const std::vector<KeyT>& lhs, const std::vector<ValueT>& rhs)
43  {
44  const size_t size = std::min(lhs.size(), rhs.size());
45 
46  std::map<KeyT, ValueT> map;
47  for (size_t i = 0; i < size; ++i)
48  {
49  map.emplace(lhs[i], rhs[i]);
50  }
51  return map;
52  }
53 
54 
55  template <class KeyT, class ValueT>
56  std::vector<ValueT>
57  multi_at(const std::map<KeyT, ValueT>& map,
58  const std::vector<KeyT>& keys,
59  bool skipMissing = false)
60  {
61  std::vector<ValueT> values;
62  values.reserve(keys.size());
63 
64  for (const KeyT& key : keys)
65  {
66  if (skipMissing)
67  {
68  if (auto it = map.find(key); it != map.end())
69  {
70  values.push_back(it->second);
71  }
72  }
73  else
74  {
75  // Throw an exception if missing.
76  values.push_back(map.at(key));
77  }
78  }
79 
80  return values;
81  }
82 
83  template <class... Args>
84  std::vector<Args...>
85  slice(const std::vector<Args...>& vector,
86  size_t start = 0,
87  std::optional<size_t> end = std::nullopt)
88  {
89  std::vector<Args...> result;
90  auto beginIt = vector.begin() + start;
91  auto endIt = end ? vector.begin() + *end : vector.end();
92  std::copy(beginIt, endIt, std::back_inserter(result));
93  return result;
94  }
95 
96 } // namespace simox::alg
simox::alg::slice
std::vector< Args... > slice(const std::vector< Args... > &vector, size_t start=0, std::optional< size_t > end=std::nullopt)
Definition: Impl.cpp:95
simox::alg::multi_at
std::vector< ValueT > multi_at(const std::map< KeyT, ValueT > &map, const std::vector< KeyT > &keys, bool skipMissing=false)
Definition: Impl.cpp:67
simox::alg
Definition: Impl.cpp:40
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
simox::alg::map_from_key_value_pairs
std::map< KeyT, ValueT > map_from_key_value_pairs(const std::vector< KeyT > &lhs, const std::vector< ValueT > &rhs)
Definition: Impl.cpp:53
simox::alg::concatenate
std::vector< Args... > concatenate(const std::vector< Args... > &lhs, const std::vector< Args... > &rhs)
Definition: Impl.cpp:44
min
T min(T t1, T t2)
Definition: gdiam.h:42