Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Vector.H
Go to the documentation of this file.
1#ifndef AMREX_VECTOR_H_
2#define AMREX_VECTOR_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_BLassert.H>
6#include <AMReX_Concepts.H>
7#include <AMReX_Extension.H>
8#include <AMReX_INT.H>
9#ifdef AMREX_SPACEDIM
10#include <AMReX_Array.H>
11#include <AMReX_TypeTraits.H>
12#endif
13
14#include <algorithm>
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace amrex {
25template <class T, class Allocator=std::allocator<T> >
26class Vector
27 :
28 public std::vector<T, Allocator>
29{
30public:
31
32 using std::vector<T, Allocator>::vector;
33 using typename std::vector<T, Allocator>::size_type;
34
35 [[nodiscard]] T& operator[] (size_type i)
36 {
37 AMREX_ASSERT_WITH_MESSAGE(i < (this->std::vector<T, Allocator>::size()),
38 "Out of bound error, index: " + std::to_string(i) + " size: " + std::to_string(size()));
39 return this->std::vector<T, Allocator>::operator[](i);
40 }
41
42 [[nodiscard]] const T& operator[] (size_type i) const
43 {
44 AMREX_ASSERT_WITH_MESSAGE(i < (this->std::vector<T, Allocator>::size()),
45 "Out of bound error, index: " + std::to_string(i) + " size: " + std::to_string(size()));
46 return this->std::vector<T, Allocator>::operator[](i);
47 }
48
50 [[nodiscard]] T* dataPtr () noexcept { return this->data(); }
52 [[nodiscard]] const T* dataPtr () const noexcept { return this->data(); }
53
54 [[nodiscard]] Long size () const noexcept {return static_cast<Long>(std::vector<T, Allocator>::size());}
55
56};
57
58}
59
60namespace amrex
61{
63
64 template <class T, typename = typename T::FABType>
66 {
67 Vector<T*> r;
68 r.reserve(a.size());
69 for (auto& x : a) { r.push_back(&x); }
70 return r;
71 }
72
73 template <class T, std::size_t N, typename = typename T::FABType>
75 {
77 r.reserve(a.size());
78 for (auto& x : a) { r.push_back(&x); }
79 return r;
80 }
81
82 template <class T>
83 [[nodiscard]] Vector<T*> GetVecOfPtrs (const Vector<std::unique_ptr<T> >& a)
84 {
85 Vector<T*> r;
86 r.reserve(a.size());
87 for (const auto& x : a) { r.push_back(x.get()); }
88 return r;
89 }
90
92
93 template <class T, typename = typename T::FABType>
95 {
97 r.reserve(a.size());
98 for (const auto& x : a) { r.push_back(&x); }
99 return r;
100 }
101
102 template <class T, std::size_t N, typename = typename T::FABType>
103 [[nodiscard]] Vector<Array<T,N> const*> GetVecOfConstPtrs (Vector<Array<T,N>> const& a)
104 {
105 Vector<Array<T,N> const*> r;
106 r.reserve(a.size());
107 for (auto& x : a) { r.push_back(&x); }
108 return r;
109 }
110
111 template <class T>
112 [[nodiscard]] Vector<const T*> GetVecOfConstPtrs (const Vector<std::unique_ptr<T> >& a)
113 {
115 r.reserve(a.size());
116 for (const auto& x : a) { r.push_back(x.get()); }
117 return r;
118 }
119
120 template <class T, typename = typename T::FABType>
122 {
123 return {a.begin(), a.end()};
124 }
125
127
128 template <class T>
129 [[nodiscard]] Vector<Vector<T*> > GetVecOfVecOfPtrs (const Vector<Vector<std::unique_ptr<T> > >& a)
130 {
132 r.reserve(a.size());
133 for (const auto& x : a) { r.push_back(GetVecOfPtrs(x)); }
134 return r;
135 }
136
138
139#ifdef AMREX_SPACEDIM
140 template <class T>
141 [[nodiscard]] Vector<std::array<T*,AMREX_SPACEDIM> >
142 GetVecOfArrOfPtrs (const Vector<std::array<std::unique_ptr<T>,AMREX_SPACEDIM> >& a)
143 {
145 r.reserve(a.size());
146 for (const auto& x : a) { r.push_back(GetArrOfPtrs(x)); }
147 return r;
148 }
149
150 template <class T>
151 [[nodiscard]] Vector<std::array<T const*,AMREX_SPACEDIM> >
152 GetVecOfArrOfPtrsConst (const Vector<std::array<std::unique_ptr<T>,AMREX_SPACEDIM> >& a)
153 {
155 r.reserve(a.size());
156 for (const auto& x : a) { r.push_back(GetArrOfConstPtrs(x)); }
157 return r;
158 }
159
160 template <class T>
161 [[nodiscard]] Vector<std::array<T const*,AMREX_SPACEDIM> >
162 GetVecOfArrOfConstPtrs (const Vector<std::array<std::unique_ptr<T>,AMREX_SPACEDIM> >& a)
163 {
165 r.reserve(a.size());
166 for (const auto& x : a) { r.push_back(GetArrOfConstPtrs(x)); }
167 return r;
168 }
169
170 template <class T>
171 requires (FabArrayType<T> || BaseFabType<T>)
172 [[nodiscard]] Vector<std::array<T const*,AMREX_SPACEDIM> >
173 GetVecOfArrOfConstPtrs (const Vector<std::array<T,AMREX_SPACEDIM> >& a)
174 {
176 r.reserve(a.size());
177 for (const auto& x : a) { r.push_back(GetArrOfConstPtrs(x)); }
178 return r;
179 }
180
181 template <class T>
182 requires (FabArrayType<T> || BaseFabType<T>)
183 [[nodiscard]] Vector<std::array<T*, AMREX_SPACEDIM> >
184 GetVecOfArrOfPtrs(Vector<std::array<T, AMREX_SPACEDIM> >& a)
185 {
187 r.reserve(a.size());
188 for (auto &x: a) { r.push_back(GetArrOfPtrs(x)); }
189 return r;
190 }
191#endif
192
194
195 template <class T>
197 {
198 std::for_each(a.begin(), a.end(), [](T*& p) { p = nullptr; });
199 }
200
201 template <class T>
202 void FillNull (Vector<std::unique_ptr<T> >& a)
203 {
204 std::for_each(a.begin(), a.end(), [](std::unique_ptr<T>& p) { p.reset(); });
205 }
206
208
209 template <class T>
211 std::sort(vec.begin(), vec.end());
212 auto it = std::unique(vec.begin(), vec.end());
213 vec.erase(it, vec.end());
214 }
215
217 namespace detail {
218 template <class T, class H>
219 std::size_t removeDupDoit (Vector<T>& vec, std::size_t start, std::size_t stop)
220 {
221 std::size_t N = stop-start;
222 if (N < 2) { return stop; }
223
224 T* const data = vec.data() + start;
225 T const sentinel = data[0]; // duplicates will be set to sentinel and removed later
226 H const hasher;
227 for (std::size_t i = 1; i < N; ) {
228 if (data[i] == sentinel) {
229 ++i;
230 continue;
231 }
232
233 std::size_t const hash = hasher(data[i]) % N;
234 if (i == hash) { // data[i] in correct hash position
235 ++i;
236 continue;
237 }
238
239 if (data[i] == data[hash]) {
240 data[i] = sentinel; // because it's a duplicate
241 ++i;
242 continue;
243 }
244
245 if (data[hash] == sentinel) {
246 std::swap(data[hash], data[i]);
247 // after swap, new data[i] holds sentinel
248 // newdata[hash] in correct hash poitiion
249 ++i;
250 continue;
251 }
252
253 std::size_t const hashhash = hasher(data[hash]) % N;
254 if (hashhash != hash) { // data[hash] not in correct has poision, thus will yield it's position
255 std::swap(data[i], data[hash]);
256 // after swap, new data[hash] in correct hash position
257 // new data[i] not sure
258 if (hash < i) { // we have seen new data[i]
259 ++i;
260 } // else next iteration we will work on data[i]
261 } else { // data[hash] in correct hash position, but data[i] is not because of hash collision
262 ++i;
263 }
264 }
265
266 // Now there are three types for data[i]
267 // (1) sentinel
268 // (2) data[i] != sentinel and hash(data[i]) == i
269 // (3) data[i] != sentinel and hash(data[i]) != i because of hash collision
270 // All type 2s are unique, and all sentinels except one can be removed.
271 // We will move all type 2s to the beginning, all sentinels to the end.
272 // This will leave all type 3s in the middle. Then we will work on the middle
273 // part plus one sentinel.
274
275 std::size_t swapPos = 0;
276 for (std::size_t i = 0; i < N; ++i) {
277 // move type 2 to the beginning pointed to by swapPos
278 if (data[i] != sentinel && i == hasher(data[i]) % N) {
279 std::swap(data[i], data[swapPos++]);
280 }
281 }
282
283 // Now we have moved all type 2 elements to the beginning, [0,swapPos)
284
285 std::size_t sentinelPos = N;
286 for (std::size_t i = swapPos; i < sentinelPos; ) {
287 // move type 1 to the end
288 if(data[i] == sentinel) {
289 std::swap(data[i], data[--sentinelPos]);
290 } else {
291 ++i;
292 }
293 }
294
295 // recursively work on the middle part
296 return detail::removeDupDoit<T,H>(vec, start+swapPos, start+sentinelPos+1);
297 }
298 }
300
301 template <class T, class H>
303 // https://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array
304 std::size_t pos = detail::removeDupDoit<T,H>(vec, 0, vec.size());
305 vec.erase(vec.begin()+pos, vec.end());
306 }
307}
308
309#endif
#define AMREX_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:37
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
T * dataPtr() noexcept
get access to the underlying data pointer
Definition AMReX_Vector.H:50
Long size() const noexcept
Definition AMReX_Vector.H:54
const T * dataPtr() const noexcept
get access to the underlying data pointer
Definition AMReX_Vector.H:52
T & operator[](size_type i)
Definition AMReX_Vector.H:35
amrex_long Long
Definition AMReX_INT.H:30
std::array< T, N > Array
Definition AMReX_Array.H:26
Definition AMReX_Amr.cpp:50
std::array< T const *, 3 > GetArrOfConstPtrs(const std::array< T, 3 > &a) noexcept
Create an array of const-qualified pointers from an array of objects.
Definition AMReX_Array.H:1047
Vector< std::array< T const *, 3 > > GetVecOfArrOfPtrsConst(const Vector< std::array< std::unique_ptr< T >, 3 > > &a)
Definition AMReX_Vector.H:152
Vector< const T * > GetVecOfConstPtrs(const Vector< T > &a)
Definition AMReX_Vector.H:94
std::array< T *, 3 > GetArrOfPtrs(std::array< T, 3 > &a) noexcept
Create an array of pointers from an array of objects.
Definition AMReX_Array.H:1001
Vector< T * > GetVecOfPtrs(Vector< T > &a)
Definition AMReX_Vector.H:65
void FillNull(Vector< T * > &a)
Definition AMReX_Vector.H:196
Vector< Vector< T * > > GetVecOfVecOfPtrs(const Vector< Vector< std::unique_ptr< T > > > &a)
Definition AMReX_Vector.H:129
Vector< std::array< T const *, 3 > > GetVecOfArrOfConstPtrs(const Vector< std::array< std::unique_ptr< T >, 3 > > &a)
Definition AMReX_Vector.H:162
Vector< std::array< T *, 3 > > GetVecOfArrOfPtrs(const Vector< std::array< std::unique_ptr< T >, 3 > > &a)
Definition AMReX_Vector.H:142
void RemoveDuplicates(Vector< T > &vec)
Definition AMReX_Vector.H:210