-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathContainer.hpp
More file actions
347 lines (295 loc) · 11.7 KB
/
Container.hpp
File metadata and controls
347 lines (295 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright 2017-2025 Fabian Koller and Franz Poeschel
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "openPMD/Error.hpp"
#include "openPMD/IO/Access.hpp"
#include "openPMD/backend/Attributable.hpp"
#include <initializer_list>
#include <map>
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
// expose private and protected members for invasive testing
#ifndef OPENPMD_protected
#define OPENPMD_protected protected:
#endif
namespace openPMD
{
namespace traits
{
/** Container Element Creation Policy
*
* The operator() of this policy is called after the container
* insert() of a new element. The passed parameter is an iterator to the
* newly added element.
*/
template <typename U>
struct GenerationPolicy
{
constexpr static bool is_noop = true;
template <typename... Args>
void operator()(Args &&...)
{}
};
} // namespace traits
namespace internal
{
class SeriesData;
template <typename>
class EraseStaleEntries;
template <
typename T,
typename T_key = std::string,
typename T_container = std::map<T_key, T>>
class ContainerData : virtual public AttributableData
{
public:
using InternalContainer = T_container;
/**
* The wrapped container holding all the actual data, e.g. std::map.
*/
InternalContainer m_container;
ContainerData() = default;
ContainerData(ContainerData const &) = delete;
ContainerData(ContainerData &&) = delete;
ContainerData &operator=(ContainerData const &) = delete;
ContainerData &operator=(ContainerData &&) = delete;
};
} // namespace internal
/** @brief Map-like container that enforces openPMD requirements and handles IO.
*
* @see http://en.cppreference.com/w/cpp/container/map
*
* @tparam T Type of objects stored
* @tparam T_key Key type to look elements up by
* @tparam T_container Type of container used for internal storage (must supply
* the same type traits and interface as std::map)
*/
template <
typename T,
typename T_key = std::string,
typename T_container = std::map<T_key, T>>
class Container : virtual public Attributable
{
static_assert(
std::is_base_of<Attributable, T>::value,
"Type of container element must be derived from Writable");
friend class Iteration;
friend class ParticleSpecies;
friend class ParticlePatches;
friend class internal::SeriesData;
friend class Series;
template <typename>
friend class internal::EraseStaleEntries;
friend class StatefulIterator;
protected:
using ContainerData = internal::ContainerData<T, T_key, T_container>;
using InternalContainer = T_container;
std::shared_ptr<ContainerData> m_containerData;
inline void setData(std::shared_ptr<ContainerData> containerData)
{
m_containerData = std::move(containerData);
Attributable::setData(m_containerData);
}
inline InternalContainer const &container() const
{
return m_containerData->m_container;
}
inline InternalContainer &container()
{
return m_containerData->m_container;
}
public:
using key_type = typename InternalContainer::key_type;
using mapped_type = typename InternalContainer::mapped_type;
using value_type = typename InternalContainer::value_type;
using size_type = typename InternalContainer::size_type;
using difference_type = typename InternalContainer::difference_type;
using allocator_type = typename InternalContainer::allocator_type;
using reference = typename InternalContainer::reference;
using const_reference = typename InternalContainer::const_reference;
using pointer = typename InternalContainer::pointer;
using const_pointer = typename InternalContainer::const_pointer;
using iterator = typename InternalContainer::iterator;
using const_iterator = typename InternalContainer::const_iterator;
using reverse_iterator = typename InternalContainer::reverse_iterator;
using const_reverse_iterator =
typename InternalContainer::const_reverse_iterator;
iterator begin() noexcept;
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
bool empty() const noexcept;
size_type size() const noexcept;
/** Remove all objects from the container and (if written) from disk.
*
* @note Calling this operation on any container in a Series with
* <code>Access::READ_ONLY</code> will throw an exception.
* @throws std::runtime_error
*/
void clear();
std::pair<iterator, bool> insert(value_type const &value);
std::pair<iterator, bool> insert(value_type &&value);
iterator insert(const_iterator hint, value_type const &value);
iterator insert(const_iterator hint, value_type &&value);
template <class InputIt>
void insert(InputIt first, InputIt last)
{
container().insert(first, last);
}
void insert(std::initializer_list<value_type> ilist);
void swap(Container &other);
mapped_type &at(key_type const &key);
mapped_type const &at(key_type const &key) const;
/** Access the value that is mapped to a key equivalent to key, creating it
* if such key does not exist already.
*
* @param key Key of the element to find (lvalue).
* @return Reference to the mapped value of the new element if no element
* with key key existed. Otherwise a reference to the mapped value of the
* existing element whose key is equivalent to key.
* @throws std::out_of_range error if in READ_ONLY mode and key does not
* exist, otherwise key will be created
*/
mapped_type &operator[](key_type const &key);
/** Access the value that is mapped to a key equivalent to key, creating it
* if such key does not exist already.
*
* @param key Key of the element to find (rvalue).
* @return Reference to the mapped value of the new element if no element
* with key key existed. Otherwise a reference to the mapped value of the
* existing element whose key is equivalent to key.
* @throws std::out_of_range error if in READ_ONLY mode and key does not
* exist, otherwise key will be created
*/
mapped_type &operator[](key_type &&key);
iterator find(key_type const &key);
const_iterator find(key_type const &key) const;
/** This returns either 1 if the key is found in the container of 0 if not.
*
* @param key key value of the element to count
* @return since keys are unique in this container, returns 0 or 1
*/
size_type count(key_type const &key) const;
/** Checks if there is an element with a key equivalent to an exiting key in
* the container.
*
* @param key key value of the element to search for
* @return true of key is found, else false
*/
bool contains(key_type const &key) const;
/** Remove a single element from the container and (if written) from disk.
*
* @note Calling this operation on any container in a Series with
* <code>Access::READ_ONLY</code> will throw an exception.
* @throws std::runtime_error
* @param key Key of the element to remove.
* @return Number of elements removed (either 0 or 1).
*/
size_type erase(key_type const &key);
//! @todo why does const_iterator not work compile with pybind11?
iterator erase(iterator res);
//! @todo add also:
// virtual iterator erase(const_iterator first, const_iterator last)
template <class... Args>
auto emplace(Args &&...args)
-> decltype(InternalContainer().emplace(std::forward<Args>(args)...))
{
return container().emplace(std::forward<Args>(args)...);
}
// clang-format off
OPENPMD_protected
// clang-format on
void clear_unchecked();
virtual void
flush(std::string const &path, internal::FlushParams const &flushParams);
Container();
Container(NoInit);
public:
/*
* Need to define these manually due to the virtual inheritance from
* Attributable.
* Otherwise, they would only run from the most derived class
* if explicitly called.
* If not defining these, a user could destroy copy/move constructors/
* assignment operators by deriving from any class that has a virtual
* Attributable somewhere.
* Care must be taken in move constructors/assignment operators to not move
* multiple times (which could happen in diamond inheritance situations).
*/
Container(Container const &other);
Container(Container &&other) noexcept;
Container &operator=(Container const &other);
Container &operator=(Container &&other) noexcept;
};
namespace internal
{
/**
* This class wraps a Container and forwards operator[]() and at() to it.
* It remembers the keys used for accessing. Upon going out of scope, all
* keys not yet accessed are removed from the Container.
* Note that the container is stored by non-owning reference, thus
* requiring that the original Container stay in scope while using this
* class.
* Container_t can be instantiated either by a reference or value type.
*/
template <typename Container_t>
class EraseStaleEntries
{
static_assert(
std::is_same_v<Container_t, std::remove_reference_t<Container_t>>);
using key_type = typename Container_t::key_type;
using mapped_type = typename Container_t::mapped_type;
std::set<key_type> m_accessedKeys;
/*
* Note: Putting a copy here leads to weird bugs due to destructors
* being called too eagerly upon destruction.
* Should be avoidable by extending the frontend redesign to the
* Container class template
* (https://github.com/openPMD/openPMD-api/pull/886)
*/
Container_t &m_originalContainer;
public:
explicit EraseStaleEntries(Container_t &container_in);
EraseStaleEntries(EraseStaleEntries &&) = delete;
EraseStaleEntries &operator=(EraseStaleEntries &&) = delete;
mapped_type &operator[](typename Container_t::key_type const &k);
mapped_type &at(typename Container_t::key_type const &k);
/**
* Remove key from the list of accessed keys.
* If the key is not accessed after this again, it will be deleted along
* with all other unaccessed keys upon destruction.
*/
void forget(typename Container_t::key_type const &k);
~EraseStaleEntries();
};
} // namespace internal
} // namespace openPMD