storage_adaptor.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright 2018-2019 Hans Dembinski
  2. //
  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)
  6. #ifndef BOOST_HISTOGRAM_STORAGE_ADAPTOR_HPP
  7. #define BOOST_HISTOGRAM_STORAGE_ADAPTOR_HPP
  8. #include <algorithm>
  9. #include <boost/core/nvp.hpp>
  10. #include <boost/histogram/detail/array_wrapper.hpp>
  11. #include <boost/histogram/detail/detect.hpp>
  12. #include <boost/histogram/detail/iterator_adaptor.hpp>
  13. #include <boost/histogram/detail/safe_comparison.hpp>
  14. #include <boost/histogram/fwd.hpp>
  15. #include <boost/mp11/utility.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <stdexcept>
  18. #include <type_traits>
  19. namespace boost {
  20. namespace histogram {
  21. namespace detail {
  22. template <class T>
  23. struct vector_impl : T {
  24. using allocator_type = typename T::allocator_type;
  25. static constexpr bool has_threading_support =
  26. accumulators::is_thread_safe<typename T::value_type>::value;
  27. vector_impl(const allocator_type& a = {}) : T(a) {}
  28. vector_impl(const vector_impl&) = default;
  29. vector_impl& operator=(const vector_impl&) = default;
  30. vector_impl(vector_impl&&) = default;
  31. vector_impl& operator=(vector_impl&&) = default;
  32. explicit vector_impl(T&& t) : T(std::move(t)) {}
  33. explicit vector_impl(const T& t) : T(t) {}
  34. template <class U, class = requires_iterable<U>>
  35. explicit vector_impl(const U& u, const allocator_type& a = {})
  36. : T(std::begin(u), std::end(u), a) {}
  37. template <class U, class = requires_iterable<U>>
  38. vector_impl& operator=(const U& u) {
  39. T::resize(u.size());
  40. auto it = T::begin();
  41. for (auto&& x : u) *it++ = x;
  42. return *this;
  43. }
  44. void reset(std::size_t n) {
  45. using value_type = typename T::value_type;
  46. const auto old_size = T::size();
  47. T::resize(n, value_type());
  48. std::fill_n(T::begin(), (std::min)(n, old_size), value_type());
  49. }
  50. template <class Archive>
  51. void serialize(Archive& ar, unsigned /* version */) {
  52. ar& make_nvp("vector", static_cast<T&>(*this));
  53. }
  54. };
  55. template <class T>
  56. struct array_impl : T {
  57. static constexpr bool has_threading_support =
  58. accumulators::is_thread_safe<typename T::value_type>::value;
  59. array_impl() = default;
  60. array_impl(const array_impl& t) : T(t), size_(t.size_) {}
  61. array_impl& operator=(const array_impl& t) {
  62. T::operator=(t);
  63. size_ = t.size_;
  64. return *this;
  65. }
  66. explicit array_impl(T&& t) : T(std::move(t)) {}
  67. explicit array_impl(const T& t) : T(t) {}
  68. template <class U, class = requires_iterable<U>>
  69. explicit array_impl(const U& u) : size_(u.size()) {
  70. using std::begin;
  71. using std::end;
  72. std::copy(begin(u), end(u), this->begin());
  73. }
  74. template <class U, class = requires_iterable<U>>
  75. array_impl& operator=(const U& u) {
  76. if (u.size() > T::max_size()) // for std::arra
  77. BOOST_THROW_EXCEPTION(std::length_error("argument size exceeds maximum capacity"));
  78. size_ = u.size();
  79. using std::begin;
  80. using std::end;
  81. std::copy(begin(u), end(u), T::begin());
  82. return *this;
  83. }
  84. void reset(std::size_t n) {
  85. using value_type = typename T::value_type;
  86. if (n > T::max_size()) // for std::array
  87. BOOST_THROW_EXCEPTION(std::length_error("argument size exceeds maximum capacity"));
  88. std::fill_n(T::begin(), n, value_type());
  89. size_ = n;
  90. }
  91. typename T::iterator end() noexcept { return T::begin() + size_; }
  92. typename T::const_iterator end() const noexcept { return T::begin() + size_; }
  93. std::size_t size() const noexcept { return size_; }
  94. template <class Archive>
  95. void serialize(Archive& ar, unsigned /* version */) {
  96. ar& make_nvp("size", size_);
  97. auto w = detail::make_array_wrapper(T::data(), size_);
  98. ar& make_nvp("array", w);
  99. }
  100. std::size_t size_ = 0;
  101. };
  102. template <class T>
  103. struct map_impl : T {
  104. static_assert(std::is_same<typename T::key_type, std::size_t>::value,
  105. "requires std::size_t as key_type");
  106. using value_type = typename T::mapped_type;
  107. using const_reference = const value_type&;
  108. static constexpr bool has_threading_support = false;
  109. static_assert(
  110. !accumulators::is_thread_safe<value_type>::value,
  111. "std::map and std::unordered_map do not support thread-safe element access. "
  112. "If you have a map with thread-safe element access, please file an issue and"
  113. "support will be added.");
  114. struct reference {
  115. reference(map_impl* m, std::size_t i) noexcept : map(m), idx(i) {}
  116. reference(const reference&) noexcept = default;
  117. reference& operator=(const reference& o) {
  118. if (this != &o) operator=(static_cast<const_reference>(o));
  119. return *this;
  120. }
  121. operator const_reference() const noexcept {
  122. return static_cast<const map_impl*>(map)->operator[](idx);
  123. }
  124. reference& operator=(const_reference u) {
  125. auto it = map->find(idx);
  126. if (u == value_type{}) {
  127. if (it != static_cast<T*>(map)->end()) { map->erase(it); }
  128. } else {
  129. if (it != static_cast<T*>(map)->end()) {
  130. it->second = u;
  131. } else {
  132. map->emplace(idx, u);
  133. }
  134. }
  135. return *this;
  136. }
  137. template <class U, class V = value_type,
  138. class = std::enable_if_t<has_operator_radd<V, U>::value>>
  139. reference& operator+=(const U& u) {
  140. auto it = map->find(idx);
  141. if (it != static_cast<T*>(map)->end()) {
  142. it->second += u;
  143. } else {
  144. map->emplace(idx, u);
  145. }
  146. return *this;
  147. }
  148. template <class U, class V = value_type,
  149. class = std::enable_if_t<has_operator_rsub<V, U>::value>>
  150. reference& operator-=(const U& u) {
  151. auto it = map->find(idx);
  152. if (it != static_cast<T*>(map)->end()) {
  153. it->second -= u;
  154. } else {
  155. map->emplace(idx, -u);
  156. }
  157. return *this;
  158. }
  159. template <class U, class V = value_type,
  160. class = std::enable_if_t<has_operator_rmul<V, U>::value>>
  161. reference& operator*=(const U& u) {
  162. auto it = map->find(idx);
  163. if (it != static_cast<T*>(map)->end()) it->second *= u;
  164. return *this;
  165. }
  166. template <class U, class V = value_type,
  167. class = std::enable_if_t<has_operator_rdiv<V, U>::value>>
  168. reference& operator/=(const U& u) {
  169. auto it = map->find(idx);
  170. if (it != static_cast<T*>(map)->end()) {
  171. it->second /= u;
  172. } else if (!(value_type{} / u == value_type{})) {
  173. map->emplace(idx, value_type{} / u);
  174. }
  175. return *this;
  176. }
  177. template <class V = value_type,
  178. class = std::enable_if_t<has_operator_preincrement<V>::value>>
  179. reference operator++() {
  180. auto it = map->find(idx);
  181. if (it != static_cast<T*>(map)->end()) {
  182. ++it->second;
  183. } else {
  184. value_type tmp{};
  185. ++tmp;
  186. map->emplace(idx, tmp);
  187. }
  188. return *this;
  189. }
  190. template <class V = value_type,
  191. class = std::enable_if_t<has_operator_preincrement<V>::value>>
  192. value_type operator++(int) {
  193. const value_type tmp = *this;
  194. operator++();
  195. return tmp;
  196. }
  197. template <class U, class = std::enable_if_t<has_operator_equal<value_type, U>::value>>
  198. bool operator==(const U& rhs) const {
  199. return operator const_reference() == rhs;
  200. }
  201. template <class U, class = std::enable_if_t<has_operator_equal<value_type, U>::value>>
  202. bool operator!=(const U& rhs) const {
  203. return !operator==(rhs);
  204. }
  205. template <class CharT, class Traits>
  206. friend std::basic_ostream<CharT, Traits>& operator<<(
  207. std::basic_ostream<CharT, Traits>& os, reference x) {
  208. os << static_cast<const_reference>(x);
  209. return os;
  210. }
  211. template <class... Ts>
  212. auto operator()(const Ts&... args) -> decltype(std::declval<value_type>()(args...)) {
  213. return (*map)[idx](args...);
  214. }
  215. map_impl* map;
  216. std::size_t idx;
  217. };
  218. template <class Value, class Reference, class MapPtr>
  219. struct iterator_t
  220. : iterator_adaptor<iterator_t<Value, Reference, MapPtr>, std::size_t, Reference> {
  221. iterator_t() = default;
  222. template <class V, class R, class M,
  223. class = std::enable_if_t<std::is_convertible<M, MapPtr>::value>>
  224. iterator_t(const iterator_t<V, R, M>& it) noexcept : iterator_t(it.map_, it.base()) {}
  225. iterator_t(MapPtr m, std::size_t i) noexcept
  226. : iterator_t::iterator_adaptor_(i), map_(m) {}
  227. template <class V, class R, class M>
  228. bool equal(const iterator_t<V, R, M>& rhs) const noexcept {
  229. return map_ == rhs.map_ && iterator_t::base() == rhs.base();
  230. }
  231. Reference operator*() const { return (*map_)[iterator_t::base()]; }
  232. MapPtr map_ = nullptr;
  233. };
  234. using iterator = iterator_t<value_type, reference, map_impl*>;
  235. using const_iterator = iterator_t<const value_type, const_reference, const map_impl*>;
  236. using allocator_type = typename T::allocator_type;
  237. map_impl(const allocator_type& a = {}) : T(a) {}
  238. map_impl(const map_impl&) = default;
  239. map_impl& operator=(const map_impl&) = default;
  240. map_impl(map_impl&&) = default;
  241. map_impl& operator=(map_impl&&) = default;
  242. map_impl(const T& t) : T(t), size_(t.size()) {}
  243. map_impl(T&& t) : T(std::move(t)), size_(t.size()) {}
  244. template <class U, class = requires_iterable<U>>
  245. explicit map_impl(const U& u, const allocator_type& a = {}) : T(a), size_(u.size()) {
  246. using std::begin;
  247. using std::end;
  248. std::copy(begin(u), end(u), this->begin());
  249. }
  250. template <class U, class = requires_iterable<U>>
  251. map_impl& operator=(const U& u) {
  252. if (u.size() < size_)
  253. reset(u.size());
  254. else
  255. size_ = u.size();
  256. using std::begin;
  257. using std::end;
  258. std::copy(begin(u), end(u), this->begin());
  259. return *this;
  260. }
  261. void reset(std::size_t n) {
  262. T::clear();
  263. size_ = n;
  264. }
  265. reference operator[](std::size_t i) noexcept { return {this, i}; }
  266. const_reference operator[](std::size_t i) const noexcept {
  267. auto it = T::find(i);
  268. static const value_type null = value_type{};
  269. if (it == T::end()) return null;
  270. return it->second;
  271. }
  272. iterator begin() noexcept { return {this, 0}; }
  273. iterator end() noexcept { return {this, size_}; }
  274. const_iterator begin() const noexcept { return {this, 0}; }
  275. const_iterator end() const noexcept { return {this, size_}; }
  276. std::size_t size() const noexcept { return size_; }
  277. template <class Archive>
  278. void serialize(Archive& ar, unsigned /* version */) {
  279. ar& make_nvp("size", size_);
  280. ar& make_nvp("map", static_cast<T&>(*this));
  281. }
  282. std::size_t size_ = 0;
  283. };
  284. template <class T>
  285. struct ERROR_type_passed_to_storage_adaptor_not_recognized;
  286. // clang-format off
  287. template <class T>
  288. using storage_adaptor_impl =
  289. mp11::mp_cond<
  290. is_vector_like<T>, vector_impl<T>,
  291. is_array_like<T>, array_impl<T>,
  292. is_map_like<T>, map_impl<T>,
  293. std::true_type, ERROR_type_passed_to_storage_adaptor_not_recognized<T>
  294. >;
  295. // clang-format on
  296. } // namespace detail
  297. /// Turns any vector-like, array-like, and map-like container into a storage type.
  298. template <class T>
  299. class storage_adaptor : public detail::storage_adaptor_impl<T> {
  300. using impl_type = detail::storage_adaptor_impl<T>;
  301. public:
  302. // standard copy, move, assign
  303. storage_adaptor(storage_adaptor&&) = default;
  304. storage_adaptor(const storage_adaptor&) = default;
  305. storage_adaptor& operator=(storage_adaptor&&) = default;
  306. storage_adaptor& operator=(const storage_adaptor&) = default;
  307. // forwarding constructor
  308. template <class... Ts>
  309. storage_adaptor(Ts&&... ts) : impl_type(std::forward<Ts>(ts)...) {}
  310. // forwarding assign
  311. template <class U>
  312. storage_adaptor& operator=(U&& u) {
  313. impl_type::operator=(std::forward<U>(u));
  314. return *this;
  315. }
  316. template <class U, class = detail::requires_iterable<U>>
  317. bool operator==(const U& u) const {
  318. using std::begin;
  319. using std::end;
  320. return std::equal(this->begin(), this->end(), begin(u), end(u), detail::safe_equal{});
  321. }
  322. template <class Archive>
  323. void serialize(Archive& ar, unsigned /* version */) {
  324. ar& make_nvp("impl", static_cast<impl_type&>(*this));
  325. }
  326. private:
  327. friend struct unsafe_access;
  328. };
  329. } // namespace histogram
  330. } // namespace boost
  331. #endif