1 // Copyright 2018-2019 Hans Dembinski
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_HISTOGRAM_STORAGE_ADAPTOR_HPP
8 #define BOOST_HISTOGRAM_STORAGE_ADAPTOR_HPP
11 #include <boost/core/nvp.hpp>
12 #include <boost/histogram/accumulators/is_thread_safe.hpp>
13 #include <boost/histogram/detail/array_wrapper.hpp>
14 #include <boost/histogram/detail/detect.hpp>
15 #include <boost/histogram/detail/iterator_adaptor.hpp>
16 #include <boost/histogram/detail/safe_comparison.hpp>
17 #include <boost/histogram/fwd.hpp>
18 #include <boost/mp11/utility.hpp>
19 #include <boost/throw_exception.hpp>
21 #include <type_traits>
28 struct vector_impl : T {
29 using allocator_type = typename T::allocator_type;
31 static constexpr bool has_threading_support =
32 accumulators::is_thread_safe<typename T::value_type>::value;
34 vector_impl(const allocator_type& a = {}) : T(a) {}
35 vector_impl(const vector_impl&) = default;
36 vector_impl& operator=(const vector_impl&) = default;
37 vector_impl(vector_impl&&) = default;
38 vector_impl& operator=(vector_impl&&) = default;
40 explicit vector_impl(T&& t) : T(std::move(t)) {}
41 explicit vector_impl(const T& t) : T(t) {}
43 template <class U, class = requires_iterable<U>>
44 explicit vector_impl(const U& u, const allocator_type& a = {})
45 : T(std::begin(u), std::end(u), a) {}
47 template <class U, class = requires_iterable<U>>
48 vector_impl& operator=(const U& u) {
51 for (auto&& x : u) *it++ = x;
55 void reset(std::size_t n) {
56 using value_type = typename T::value_type;
57 const auto old_size = T::size();
58 T::resize(n, value_type());
59 std::fill_n(T::begin(), (std::min)(n, old_size), value_type());
62 template <class Archive>
63 void serialize(Archive& ar, unsigned /* version */) {
64 ar& make_nvp("vector", static_cast<T&>(*this));
69 struct array_impl : T {
70 static constexpr bool has_threading_support =
71 accumulators::is_thread_safe<typename T::value_type>::value;
73 array_impl() = default;
74 array_impl(const array_impl& t) : T(t), size_(t.size_) {}
75 array_impl& operator=(const array_impl& t) {
81 explicit array_impl(T&& t) : T(std::move(t)) {}
82 explicit array_impl(const T& t) : T(t) {}
84 template <class U, class = requires_iterable<U>>
85 explicit array_impl(const U& u) : size_(u.size()) {
88 std::copy(begin(u), end(u), this->begin());
91 template <class U, class = requires_iterable<U>>
92 array_impl& operator=(const U& u) {
93 if (u.size() > T::max_size()) // for std::arra
94 BOOST_THROW_EXCEPTION(std::length_error("argument size exceeds maximum capacity"));
98 std::copy(begin(u), end(u), T::begin());
102 void reset(std::size_t n) {
103 using value_type = typename T::value_type;
104 if (n > T::max_size()) // for std::array
105 BOOST_THROW_EXCEPTION(std::length_error("argument size exceeds maximum capacity"));
106 std::fill_n(T::begin(), n, value_type());
110 typename T::iterator end() noexcept { return T::begin() + size_; }
111 typename T::const_iterator end() const noexcept { return T::begin() + size_; }
113 std::size_t size() const noexcept { return size_; }
115 template <class Archive>
116 void serialize(Archive& ar, unsigned /* version */) {
117 ar& make_nvp("size", size_);
118 auto w = detail::make_array_wrapper(T::data(), size_);
119 ar& make_nvp("array", w);
122 std::size_t size_ = 0;
126 struct map_impl : T {
127 static_assert(std::is_same<typename T::key_type, std::size_t>::value,
128 "requires std::size_t as key_type");
130 using value_type = typename T::mapped_type;
131 using const_reference = const value_type&;
133 static constexpr bool has_threading_support = false;
135 !accumulators::is_thread_safe<value_type>::value,
136 "std::map and std::unordered_map do not support thread-safe element access. "
137 "If you have a map with thread-safe element access, please file an issue and"
138 "support will be added.");
141 reference(map_impl* m, std::size_t i) noexcept : map(m), idx(i) {}
143 reference(const reference&) noexcept = default;
144 reference& operator=(const reference& o) {
145 if (this != &o) operator=(static_cast<const_reference>(o));
149 operator const_reference() const noexcept {
150 return static_cast<const map_impl*>(map)->operator[](idx);
153 reference& operator=(const_reference u) {
154 auto it = map->find(idx);
155 if (u == value_type{}) {
156 if (it != static_cast<T*>(map)->end()) { map->erase(it); }
158 if (it != static_cast<T*>(map)->end()) {
161 map->emplace(idx, u);
167 template <class U, class V = value_type,
168 class = std::enable_if_t<has_operator_radd<V, U>::value>>
169 reference& operator+=(const U& u) {
170 auto it = map->find(idx);
171 if (it != static_cast<T*>(map)->end()) {
174 map->emplace(idx, u);
179 template <class U, class V = value_type,
180 class = std::enable_if_t<has_operator_rsub<V, U>::value>>
181 reference& operator-=(const U& u) {
182 auto it = map->find(idx);
183 if (it != static_cast<T*>(map)->end()) {
186 map->emplace(idx, -u);
191 template <class U, class V = value_type,
192 class = std::enable_if_t<has_operator_rmul<V, U>::value>>
193 reference& operator*=(const U& u) {
194 auto it = map->find(idx);
195 if (it != static_cast<T*>(map)->end()) it->second *= u;
199 template <class U, class V = value_type,
200 class = std::enable_if_t<has_operator_rdiv<V, U>::value>>
201 reference& operator/=(const U& u) {
202 auto it = map->find(idx);
203 if (it != static_cast<T*>(map)->end()) {
205 } else if (!(value_type{} / u == value_type{})) {
206 map->emplace(idx, value_type{} / u);
211 template <class V = value_type,
212 class = std::enable_if_t<has_operator_preincrement<V>::value>>
213 reference operator++() {
214 auto it = map->find(idx);
215 if (it != static_cast<T*>(map)->end()) {
220 map->emplace(idx, tmp);
225 template <class V = value_type,
226 class = std::enable_if_t<has_operator_preincrement<V>::value>>
227 value_type operator++(int) {
228 const value_type tmp = *this;
233 template <class U, class = std::enable_if_t<has_operator_equal<value_type, U>::value>>
234 bool operator==(const U& rhs) const {
235 return operator const_reference() == rhs;
238 template <class U, class = std::enable_if_t<has_operator_equal<value_type, U>::value>>
239 bool operator!=(const U& rhs) const {
240 return !operator==(rhs);
243 template <class CharT, class Traits>
244 friend std::basic_ostream<CharT, Traits>& operator<<(
245 std::basic_ostream<CharT, Traits>& os, reference x) {
246 os << static_cast<const_reference>(x);
250 template <class... Ts>
251 auto operator()(const Ts&... args) -> decltype(std::declval<value_type>()(args...)) {
252 return (*map)[idx](args...);
259 template <class Value, class Reference, class MapPtr>
261 : iterator_adaptor<iterator_t<Value, Reference, MapPtr>, std::size_t, Reference> {
262 iterator_t() = default;
263 template <class V, class R, class M,
264 class = std::enable_if_t<std::is_convertible<M, MapPtr>::value>>
265 iterator_t(const iterator_t<V, R, M>& it) noexcept : iterator_t(it.map_, it.base()) {}
266 iterator_t(MapPtr m, std::size_t i) noexcept
267 : iterator_t::iterator_adaptor_(i), map_(m) {}
268 template <class V, class R, class M>
269 bool equal(const iterator_t<V, R, M>& rhs) const noexcept {
270 return map_ == rhs.map_ && iterator_t::base() == rhs.base();
272 Reference operator*() const { return (*map_)[iterator_t::base()]; }
273 MapPtr map_ = nullptr;
276 using iterator = iterator_t<value_type, reference, map_impl*>;
277 using const_iterator = iterator_t<const value_type, const_reference, const map_impl*>;
279 using allocator_type = typename T::allocator_type;
281 map_impl(const allocator_type& a = {}) : T(a) {}
283 map_impl(const map_impl&) = default;
284 map_impl& operator=(const map_impl&) = default;
285 map_impl(map_impl&&) = default;
286 map_impl& operator=(map_impl&&) = default;
288 map_impl(const T& t) : T(t), size_(t.size()) {}
289 map_impl(T&& t) : T(std::move(t)), size_(t.size()) {}
291 template <class U, class = requires_iterable<U>>
292 explicit map_impl(const U& u, const allocator_type& a = {}) : T(a), size_(u.size()) {
295 std::copy(begin(u), end(u), this->begin());
298 template <class U, class = requires_iterable<U>>
299 map_impl& operator=(const U& u) {
300 if (u.size() < size_)
306 std::copy(begin(u), end(u), this->begin());
310 void reset(std::size_t n) {
315 reference operator[](std::size_t i) noexcept { return {this, i}; }
316 const_reference operator[](std::size_t i) const noexcept {
317 auto it = T::find(i);
318 static const value_type null = value_type{};
319 if (it == T::end()) return null;
323 iterator begin() noexcept { return {this, 0}; }
324 iterator end() noexcept { return {this, size_}; }
326 const_iterator begin() const noexcept { return {this, 0}; }
327 const_iterator end() const noexcept { return {this, size_}; }
329 std::size_t size() const noexcept { return size_; }
331 template <class Archive>
332 void serialize(Archive& ar, unsigned /* version */) {
333 ar& make_nvp("size", size_);
334 ar& make_nvp("map", static_cast<T&>(*this));
337 std::size_t size_ = 0;
341 struct ERROR_type_passed_to_storage_adaptor_not_recognized;
345 using storage_adaptor_impl =
347 is_vector_like<T>, vector_impl<T>,
348 is_array_like<T>, array_impl<T>,
349 is_map_like<T>, map_impl<T>,
350 std::true_type, ERROR_type_passed_to_storage_adaptor_not_recognized<T>
353 } // namespace detail
355 /// Turns any vector-like, array-like, and map-like container into a storage type.
357 class storage_adaptor : public detail::storage_adaptor_impl<T> {
358 using impl_type = detail::storage_adaptor_impl<T>;
361 // standard copy, move, assign
362 storage_adaptor(storage_adaptor&&) = default;
363 storage_adaptor(const storage_adaptor&) = default;
364 storage_adaptor& operator=(storage_adaptor&&) = default;
365 storage_adaptor& operator=(const storage_adaptor&) = default;
367 // forwarding constructor
368 template <class... Ts>
369 storage_adaptor(Ts&&... ts) : impl_type(std::forward<Ts>(ts)...) {}
373 storage_adaptor& operator=(U&& u) {
374 impl_type::operator=(std::forward<U>(u));
378 template <class U, class = detail::requires_iterable<U>>
379 bool operator==(const U& u) const {
382 return std::equal(this->begin(), this->end(), begin(u), end(u), detail::safe_equal{});
385 template <class Archive>
386 void serialize(Archive& ar, unsigned /* version */) {
387 ar& make_nvp("impl", static_cast<impl_type&>(*this));
391 friend struct unsafe_access;
394 } // namespace histogram