ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Bag.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef ESPRESSO_UTILS_BAG_HPP
23#define ESPRESSO_UTILS_BAG_HPP
24
25#include <boost/serialization/access.hpp>
26#include <boost/serialization/vector.hpp>
27
28#include <cstddef>
29#include <type_traits>
30#include <utility>
31#include <vector>
32
33namespace Utils {
34/**
35 * @brief Bag of elements.
36 *
37 * A bag is a container in which the elements do not have
38 * a fixed order. It can be considered an unordered variant
39 * of vector, and implements the Container named requirement
40 * specified in C++11.
41 *
42 * Elements in the container do not have a stable position and
43 * removing elements can change the order of the other elements.
44 * The looser contract (compared to a vector) allows removing any
45 * element in the container in constant time.
46 *
47 * @tparam T Element type, needs to be Swappable.
48 */
49template <class T> class Bag {
50 static_assert(std::is_swappable_v<T>);
51
52 /** Storage backend */
53 using storage_type = std::vector<T>;
54
55public:
56 using value_type = T;
57 using iterator = T *;
58 using const_iterator = const T *;
59 using pointer = T *;
60 using reference = T &;
61
62 /**
63 * @brief Construct an empty container.
64 */
65 Bag() = default;
66
67private:
68 /** Underlying storage of the container */
69 storage_type m_storage;
70
71 friend boost::serialization::access;
72 /**
73 * @brief Serialize the container.
74 *
75 * Serialization requires T to be serializable.
76 */
77 template <class Archive> void serialize(Archive &ar, long int /* version */) {
78 ar & m_storage;
79 }
80
81public:
82 iterator begin() { return m_storage.data(); }
83 iterator end() { return m_storage.data() + size(); }
84 const_iterator begin() const { return m_storage.data(); }
85 const_iterator end() const { return m_storage.data() + size(); }
86
87 /**
88 * @brief Number of elements in the container.
89 */
90 std::size_t size() const { return m_storage.size(); }
91
92 /**
93 * @brief Is the container empty?
94 * @return True if there are no elements.
95 */
96 bool empty() const { return m_storage.empty(); }
97
98 /**
99 * @brief Capacity of the container.
100 *
101 * Number of elements the container can at least hold
102 * without reallocating.
103 */
104 std::size_t capacity() const { return m_storage.capacity(); }
105
106 /**
107 * @brief Maximum number of elements the container can hold.
108 */
109 std::size_t max_size() const { return m_storage.max_size(); }
110
111 /**
112 * @brief Reserve storage.
113 *
114 * Increase capacity to at least the specified value.
115 *
116 * @param new_capacity New minimum capacity.
117 */
118 void reserve(std::size_t new_capacity) { m_storage.reserve(new_capacity); }
119
120 /**
121 * @brief Resize container.
122 *
123 * Newly added Ts are default-initialized.
124 * If the new size is larger than the capacity, all
125 * iterators into the container are invalidated.
126 *
127 * @param new_size Size to resize to.
128 */
129 void resize(std::size_t new_size) { m_storage.resize(new_size); }
130
131 /**
132 * @brief Remove all elements form container.
133 */
134 void clear() { m_storage.clear(); }
135
136 /**
137 * @brief Insert an element into the container.
138 *
139 * If before the call size() >= capacity(),
140 * this may reallocate, in which case all
141 * iterators into the container are invalidated.
142 * Otherwise only the end iterator is invalidated.
143 *
144 * @param v Element to add.
145 * @return Reference to the added element.
146 */
147 T &insert(T const &v) {
148 m_storage.push_back(v);
149
150 return m_storage.back();
151 }
152 /** @overload */
153 T &insert(T &&v) {
154 m_storage.push_back(std::move(v));
155
156 return m_storage.back();
157 }
158
159 /**
160 * @brief Remove element from the list.
161 *
162 * @param it Iterator pointing to the element to remove.
163 * @return An iterator past the element that was removed.
164 */
166 *it = std::move(m_storage.back());
167
168 m_storage.pop_back();
169
170 return it;
171 }
172
173 /**
174 * @brief Swap two Bags.
175 *
176 * Efficiently swap to bags by swapping
177 * their contained storage.
178 */
179 friend void swap(Bag &lhs, Bag &rhs) {
180 using std::swap;
181
182 swap(lhs.m_storage, rhs.m_storage);
183 }
184};
185} // namespace Utils
186#endif
Bag of elements.
Definition Bag.hpp:49
void reserve(std::size_t new_capacity)
Reserve storage.
Definition Bag.hpp:118
const_iterator begin() const
Definition Bag.hpp:84
T & reference
Definition Bag.hpp:60
std::size_t capacity() const
Capacity of the container.
Definition Bag.hpp:104
T * pointer
Definition Bag.hpp:59
iterator begin()
Definition Bag.hpp:82
T & insert(T &&v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Bag.hpp:153
T value_type
Definition Bag.hpp:56
friend void swap(Bag &lhs, Bag &rhs)
Swap two Bags.
Definition Bag.hpp:179
void clear()
Remove all elements form container.
Definition Bag.hpp:134
bool empty() const
Is the container empty?
Definition Bag.hpp:96
T & insert(T const &v)
Insert an element into the container.
Definition Bag.hpp:147
iterator erase(iterator it)
Remove element from the list.
Definition Bag.hpp:165
const T * const_iterator
Definition Bag.hpp:58
iterator end()
Definition Bag.hpp:83
T * iterator
Definition Bag.hpp:57
const_iterator end() const
Definition Bag.hpp:85
std::size_t size() const
Number of elements in the container.
Definition Bag.hpp:90
Bag()=default
Construct an empty container.
std::size_t max_size() const
Maximum number of elements the container can hold.
Definition Bag.hpp:109
void resize(std::size_t new_size)
Resize container.
Definition Bag.hpp:129