ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
compact_vector.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef UTILS_INCLUDE_UTILS_COMPACT_VECTOR_HPP
21#define UTILS_INCLUDE_UTILS_COMPACT_VECTOR_HPP
22
24
25#include <boost/container/vector.hpp>
26#include <boost/serialization/array_wrapper.hpp>
27#include <boost/serialization/split_member.hpp>
28
29#include <cstdint>
30
31namespace Utils {
32namespace detail {
33using container_size_type = std::uint16_t;
34using container_options = boost::container::vector_options<
35 boost::container::stored_size<detail::container_size_type>>::type;
36} // namespace detail
37
38/**
39 * Custom vector container optimized for size.
40 * Allocate only 16 bits for the number of elements, and take only
41 * <tt>16 + N * sizeof(T)</tt> bits during serialization in a binary archive.
42 */
43template <typename T>
44class compact_vector // NOLINT(bugprone-exception-escape)
45 : public boost::container::vector<T, boost::container::new_allocator<T>,
46 detail::container_options> {
47public:
48 using boost::container::vector<T, boost::container::new_allocator<T>,
49 detail::container_options>::vector;
50
51 template <class Archive> void save(Archive &oa, unsigned int const) const {
52 auto const size = static_cast<detail::container_size_type>(this->size());
53 oa << size << boost::serialization::make_array(this->data(), this->size());
54 }
55
56 template <class Archive> void load(Archive &ia, unsigned int const) {
57 auto size = static_cast<detail::container_size_type>(0u);
58 ia >> size;
59 this->resize(size);
60 ia >> boost::serialization::make_array(this->data(), size);
61 }
62
63 template <class Archive>
64 void serialize(Archive &ar, unsigned int const file_version) {
65 boost::serialization::split_member(ar, *this, file_version);
66 }
67};
68
69} // namespace Utils
70
72UTILS_ARRAY_BOOST_CLASS(Utils::compact_vector, 0, object_serializable)
73UTILS_ARRAY_BOOST_TRACK(Utils::compact_vector, 0, track_never)
74
75#endif
#define UTILS_ARRAY_BOOST_MPI_T(Container, N)
Mark array types as MPI data types.
Definition array.hpp:50
#define UTILS_ARRAY_BOOST_CLASS(Container, N, ImplementationLevel)
Redefinition of BOOST_CLASS_IMPLEMENTATION for array types.
Definition array.hpp:78
#define UTILS_ARRAY_BOOST_TRACK(Container, N, TrackingLevel)
Redefinition of BOOST_CLASS_TRACKING for array types.
Definition array.hpp:97
Custom vector container optimized for size.
void serialize(Archive &ar, unsigned int const file_version)
void load(Archive &ia, unsigned int const)
void save(Archive &oa, unsigned int const) const