QMCPACK
template_types.hpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2019 QMCPACK developers
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
10 //
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 #ifndef QMCPLUSPLUS_TEMPLATE_TYPES_HPP
14 #define QMCPLUSPLUS_TEMPLATE_TYPES_HPP
15 
16 #include <vector>
17 #include <functional>
18 #include <memory>
19 #include <cassert>
20 #include "RefVectorWithLeader.h"
21 
22 namespace qmcplusplus
23 {
24 /** @name Convenience templated type aliases
25  *
26  * when feasible do not use previous aliases in following
27  * that way one line can be scanned to understand a single alias
28  * see: UPtrVector
29  * @{
30  */
31 template<typename T>
32 using RefVector = std::vector<std::reference_wrapper<T>>;
33 
34 template<typename T>
35 using UPtr = std::unique_ptr<T>;
36 
37 template<typename T>
38 using UPtrVector = std::vector<std::unique_ptr<T>>;
39 /** }@ */
40 
41 /** helper function to take vector of class A to refvector of any valid reference type for A
42  *
43  * intended usage looks like this
44  * std::vector<DerivedType> vdt
45  * auto refvecbase = makeRefVector<BaseType>(vdt)
46  * or if you just want a refvector of type vdt
47  * auto refvec = makeRefVector<decltype(vdt)::value_type>(vdt)
48  *
49  * godbolt.org indicates at least with clang 11&12 we get RVO here.
50  * auto ref_whatevers = makeRefVector<ValidTypeForReference>(whatevers);
51  * makes no extra copy.
52  */
53 template<class TR, class T>
54 static RefVector<TR> makeRefVector(std::vector<T>& vec_list)
55 {
56  RefVector<TR> ref_list;
57  ref_list.reserve(vec_list.size());
58  for (int i = 0; i < vec_list.size(); ++i)
59  ref_list.push_back(vec_list[i]);
60  return ref_list;
61 }
62 
63 /** convert a vector of std::unique_ptrs<T> to a refvector<T>
64  */
65 template<class T>
67 {
68  RefVector<T> ref_list;
69  ref_list.reserve(ptr_list.size());
70  for (const UPtr<T>& ptr : ptr_list)
71  ref_list.push_back(*ptr);
72  return ref_list;
73 }
74 
75 /** convert a vector of std::unique_ptrs<T> to a refvector<T2>
76  * the static assert prevents ambiguity between this function and the above
77  * when T is a derived type of T2.
78  */
79 template<class T2, class T>
81 {
82  static_assert(!std::is_same_v<T2, T> && std::is_base_of_v<T2, T>);
83  RefVector<T2> ref_list;
84  ref_list.reserve(ptr_list.size());
85  for (const UPtr<T>& ptr : ptr_list)
86  ref_list.push_back(*ptr);
87  return ref_list;
88 }
89 
90 // temporary helper function
91 template<class T>
92 static RefVector<T> convertPtrToRefVector(const std::vector<T*>& ptr_list)
93 {
94  RefVector<T> ref_list;
95  ref_list.reserve(ptr_list.size());
96  for (auto ptr : ptr_list)
97  ref_list.push_back(*ptr);
98  return ref_list;
99 }
100 
101 // temporary helper function
102 template<class T>
103 static std::vector<T*> convert_ref_to_ptr_list(const std::vector<std::reference_wrapper<T>>& ref_list)
104 {
105  std::vector<T*> ptr_list;
106  ptr_list.reserve(ref_list.size());
107  for (auto& ref : ref_list)
108  ptr_list.push_back(&ref.get());
109  return ptr_list;
110 }
111 
112 // temporary helper function
113 template<class T>
114 static std::vector<T*> convertUPtrToPtrVector(const UPtrVector<T>& uptr_list)
115 {
116  std::vector<T*> ptr_list;
117  ptr_list.reserve(uptr_list.size());
118  for (auto& uptr : uptr_list)
119  ptr_list.push_back(uptr.get());
120  return ptr_list;
121 }
122 
123 // handling subset
124 template<class T>
125 static RefVector<T> convertUPtrToRefVectorSubset(const UPtrVector<T>& ptr_list, int offset, int len)
126 {
127  // check lower and upper bounds
128  assert(offset >= 0);
129  assert(offset + len <= ptr_list.size());
130 
131  RefVector<T> ref_list;
132  ref_list.reserve(len);
133  for (int i = offset; i < offset + len; i++)
134  ref_list.push_back(*ptr_list[i]);
135 
136  return ref_list;
137 }
138 
139 template<class T>
140 static RefVector<T> convertPtrToRefVectorSubset(const std::vector<T*>& ptr_list, int offset, int len)
141 {
142  // check lower and upper bounds
143  assert(offset >= 0);
144  assert(offset + len <= ptr_list.size());
145 
146  RefVector<T> ref_list;
147  ref_list.reserve(len);
148  for (int i = offset; i < offset + len; i++)
149  ref_list.push_back(*ptr_list[i]);
150 
151  return ref_list;
152 }
153 
154 template<class T>
155 static RefVector<T> convertRefVectorToRefVectorSubset(const RefVector<T>& ref_list, int offset, int len)
156 {
157  // check lower and upper bounds
158  assert(offset >= 0);
159  assert(offset + len <= ref_list.size());
160 
161  RefVector<T> sub_ref_list;
162  sub_ref_list.reserve(len);
163  for (int i = offset; i < offset + len; i++)
164  sub_ref_list.push_back(ref_list[i]);
165 
166  return sub_ref_list;
167 }
168 
169 } // namespace qmcplusplus
170 #endif
static RefVector< T > convertPtrToRefVector(const std::vector< T *> &ptr_list)
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
static RefVector< T > convertRefVectorToRefVectorSubset(const RefVector< T > &ref_list, int offset, int len)
static RefVector< T > convertPtrToRefVectorSubset(const std::vector< T *> &ptr_list, int offset, int len)
std::vector< std::unique_ptr< T > > UPtrVector
static RefVector< T > convertUPtrToRefVector(const UPtrVector< T > &ptr_list)
convert a vector of std::unique_ptrs<T> to a refvector<T>
static std::vector< T * > convert_ref_to_ptr_list(const std::vector< std::reference_wrapper< T >> &ref_list)
static RefVector< TR > makeRefVector(std::vector< T > &vec_list)
}@
static RefVector< T > convertUPtrToRefVectorSubset(const UPtrVector< T > &ptr_list, int offset, int len)
std::unique_ptr< T > UPtr
std::vector< std::reference_wrapper< T > > RefVector
static std::vector< T * > convertUPtrToPtrVector(const UPtrVector< T > &uptr_list)