Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <memory>
7 : #include <unordered_map>
8 : #include <vector>
9 :
10 : /// \ingroup UtilitiesGroup
11 : /// \brief Given a map of `std::unique_ptr` returns a copy of the map by
12 : /// invoking `get_clone()` on each element of the input map.
13 : template <typename KeyType, typename T>
14 1 : std::unordered_map<KeyType, std::unique_ptr<T>> clone_unique_ptrs(
15 : const std::unordered_map<KeyType, std::unique_ptr<T>>& map) {
16 : std::unordered_map<KeyType, std::unique_ptr<T>> result{};
17 : for (const auto& kv : map) {
18 : result[kv.first] = kv.second->get_clone();
19 : }
20 : return result;
21 : }
22 :
23 : /// \ingroup UtilitiesGroup
24 : /// \brief Given a vector of `std::unique_ptr` returns a copy of the vector by
25 : /// invoking `get_clone()` on each element of the input vector.
26 : template <typename T>
27 1 : std::vector<std::unique_ptr<T>> clone_unique_ptrs(
28 : const std::vector<std::unique_ptr<T>>& vector) {
29 : std::vector<std::unique_ptr<T>> result{vector.size()};
30 : for (size_t i = 0; i < vector.size(); ++i) {
31 : result[i] = vector[i]->get_clone();
32 : }
33 : return result;
34 : }
|