The Sparta Modeling Framework
Loading...
Searching...
No Matches
VectorUtils.hpp
1// <VectorUtils.hpp> -*- C++ -*-
2
3#pragma once
4
5#include <memory>
6
7#include "sparta/utils/Utils.hpp"
10
11
12namespace sparta {
13namespace utils {
14
15template<typename T> struct is_vector : public std::false_type {};
16template<typename T, typename A>
17 struct is_vector<std::vector<T, A>> : public std::true_type {};
18
23template <template<class, class...> class C, typename T>
25{
26public:
27 typedef C<T> container_t;
28
29 const std::vector<container_t>& input_;
30
31 VectorCopier(const std::vector<container_t>& input)
32 : input_(input)
33 {;}
34
35 void deepCopy(std::vector<container_t>& output) const
36 {
37 for (const auto& el : input_){
38 output.push_back(el); // No copy of T because this unspecialized class does not know how
39 }
40 }
41
42 void extractRawCopy(std::vector<T*>& output) const
43 {
44 for (const container_t& el : input_){
45 output.push_back(el.get()); // Extract raw pointer
46 }
47 }
48};
49
53template <typename VT>
54class VectorCopier<std::unique_ptr, VT>
55{
56public:
57
58 typedef std::unique_ptr<VT> container_t;
59 const std::vector<container_t>& input_;
60
61 VectorCopier(const std::vector<container_t>& input)
62 : input_(input)
63 {;}
64
65 void deepCopy(std::vector<container_t>& output) const
66 {
67 for(const container_t& el : input_){
68 if(el.get() != nullptr){
69 output.emplace_back(new VT(*el.get())); // Copy T
70 }else{
71 output.emplace_back(nullptr);
72 }
73 }
74 }
75
76 void extractRawCopy(std::vector<VT*>& output) const
77 {
78 for (const container_t& el : input_){
79 output.push_back(el.get()); // Extract raw pointer
80 }
81 }
82};
83
87template <typename VT>
88class VectorCopier<std::shared_ptr, VT>
89{
90public:
91
92 typedef std::shared_ptr<VT> container_t;
93 const std::vector<container_t>& input_;
94
95 VectorCopier(const std::vector<container_t>& input)
96 : input_(input)
97 {;}
98
99 void deepCopy(std::vector<container_t>& output) const
100 {
101 for(const container_t& el : input_){
102 if(el.get() != nullptr){
103 output.emplace_back(new VT(*el.get())); // Copy T
104 }else{
105 output.emplace_back(nullptr);
106 }
107 }
108 }
109
110 void extractRawCopy(std::vector<VT*>& output) const
111 {
112 for (const container_t& el : input_){
113 output.push_back(el.get()); // Extract raw pointer
114 }
115 }
116};
117
124template <template<class, class...> class C, typename T>
125void copyVectorDeep(const std::vector<C<T>>& input,
126 std::vector<C<T>>& output)
127{
128 VectorCopier<C,T>(input).deepCopy(output);
129}
130
137template <template<class, class...> class C, typename T>
138void copyVectorExtractRawPointers(const std::vector<C<T>>& input,
139 std::vector<T*>& output)
140{
141 VectorCopier<C,T>(input).extractRawCopy(output);
142}
143
144} // namepace utils
145} // namespace sparta
146
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Utility for element-wise potentially-deep-copying a vector. Invoked by methods at the end of this fil...
Macros for handling exponential backoff.