From 70b98302a2ea7f61de15a1b52274e2649dd2d50d Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Sun, 19 Feb 2023 17:42:01 +0100 Subject: [PATCH] Replace detail::span and detail::make_span with implementations in boost::core (#384) * fix cmake build of benchmarks * wip * u * u * rename span.hpp to make_span.hpp * missing file * also replace make_span --- .gitignore | 2 +- .../boost/histogram/detail/array_wrapper.hpp | 5 +- include/boost/histogram/detail/fill_n.hpp | 7 +- .../boost/histogram/detail/reduce_command.hpp | 2 +- include/boost/histogram/detail/span.hpp | 250 ------------------ include/boost/histogram/histogram.hpp | 11 +- ...etail_array_wrapper_serialization_test.cpp | 3 +- test/detail_misc_test.cpp | 18 +- test/detail_sub_array_and_span_test.cpp | 3 +- 9 files changed, 34 insertions(+), 267 deletions(-) delete mode 100644 include/boost/histogram/detail/span.hpp diff --git a/.gitignore b/.gitignore index 9f01f920..408de80c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ tools/lcov-* tools/codecov coverage-report .cache -venv \ No newline at end of file +venv diff --git a/include/boost/histogram/detail/array_wrapper.hpp b/include/boost/histogram/detail/array_wrapper.hpp index 752a7303..fb03922f 100644 --- a/include/boost/histogram/detail/array_wrapper.hpp +++ b/include/boost/histogram/detail/array_wrapper.hpp @@ -7,8 +7,8 @@ #ifndef BOOST_HISTOGRAM_DETAIL_ARRAY_WRAPPER_HPP #define BOOST_HISTOGRAM_DETAIL_ARRAY_WRAPPER_HPP +#include #include -#include #include #include #include @@ -48,8 +48,7 @@ struct array_wrapper { ar); }, [this](auto& ar) { - for (auto&& x : boost::histogram::detail::make_span(this->ptr, this->size)) - ar& make_nvp("item", x); + for (auto&& x : make_span(this->ptr, this->size)) ar& make_nvp("item", x); }, ar); } diff --git a/include/boost/histogram/detail/fill_n.hpp b/include/boost/histogram/detail/fill_n.hpp index d1b55a27..e5a540bc 100644 --- a/include/boost/histogram/detail/fill_n.hpp +++ b/include/boost/histogram/detail/fill_n.hpp @@ -8,6 +8,8 @@ #define BOOST_HISTOGRAM_DETAIL_FILL_N_HPP #include +#include +#include #include #include #include @@ -16,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -260,7 +261,7 @@ void fill_n_1(const std::size_t offset, S& storage, A& axes, const std::size_t v } template -std::size_t get_total_size(const A& axes, const dtl::span& values) { +std::size_t get_total_size(const A& axes, const span& values) { // supported cases (T = value type; CT = containter of T; V = variant): // - span: for any histogram, N == rank // - span, N>: for any histogram, N == rank @@ -311,7 +312,7 @@ void fill_n_check_extra_args(std::size_t size, weight_type&& w, Ts&&... ts) { template void fill_n(std::true_type, const std::size_t offset, S& storage, A& axes, - const dtl::span values, Us&&... us) { + const span values, Us&&... us) { // supported cases (T = value type; CT = containter of T; V = variant): // - span: only valid for 1D histogram, N > 1 allowed // - span: for any histogram, N == rank diff --git a/include/boost/histogram/detail/reduce_command.hpp b/include/boost/histogram/detail/reduce_command.hpp index eafce093..3fb67b50 100644 --- a/include/boost/histogram/detail/reduce_command.hpp +++ b/include/boost/histogram/detail/reduce_command.hpp @@ -7,7 +7,7 @@ #ifndef BOOST_HISTOGRAM_DETAIL_REDUCE_COMMAND_HPP #define BOOST_HISTOGRAM_DETAIL_REDUCE_COMMAND_HPP -#include +#include #include #include #include diff --git a/include/boost/histogram/detail/span.hpp b/include/boost/histogram/detail/span.hpp deleted file mode 100644 index f5bdb440..00000000 --- a/include/boost/histogram/detail/span.hpp +++ /dev/null @@ -1,250 +0,0 @@ -// Copyright 2019 Hans Dembinski -// -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt -// or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef BOOST_HISTOGRAM_DETAIL_SPAN_HPP -#define BOOST_HISTOGRAM_DETAIL_SPAN_HPP - -// to be replaced by boost::span - -#include -#include -#include -#include -#include -#include - -namespace boost { -namespace histogram { -namespace detail { - -namespace dtl = ::boost::histogram::detail; - -static constexpr std::size_t dynamic_extent = ~static_cast(0); - -template -class span_base { -public: - constexpr T* data() noexcept { return begin_; } - constexpr const T* data() const noexcept { return begin_; } - constexpr std::size_t size() const noexcept { return N; } - -protected: - constexpr span_base(T* b, std::size_t s) noexcept : begin_(b) { - (void)s; - assert(N == s); - } - constexpr void set(T* b, std::size_t s) noexcept { - (void)s; - begin_ = b; - assert(N == s); - } - -private: - T* begin_; -}; - -template -class span_base { -public: - constexpr span_base() noexcept : begin_(nullptr), size_(0) {} - - constexpr T* data() noexcept { return begin_; } - constexpr const T* data() const noexcept { return begin_; } - constexpr std::size_t size() const noexcept { return size_; } - -protected: - constexpr span_base(T* b, std::size_t s) noexcept : begin_(b), size_(s) {} - constexpr void set(T* b, std::size_t s) noexcept { - begin_ = b; - size_ = s; - } - -private: - T* begin_; - std::size_t size_; -}; - -template -class span : public span_base { - using base = span_base; - -public: - using element_type = T; - using value_type = std::remove_cv_t; - using index_type = std::size_t; - using difference_type = std::ptrdiff_t; - using pointer = T*; - using const_pointer = const T*; - using reference = T&; - using const_reference = const T&; - using iterator = pointer; - using const_iterator = const_pointer; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - - static constexpr std::size_t extent = Extent; - - using base::base; - - constexpr span(pointer first, pointer last) - : span(first, static_cast(last - first)) { - assert(extent == dynamic_extent || - static_cast(extent) == (last - first)); - } - - constexpr span(pointer ptr, index_type count) : base(ptr, count) {} - - template - constexpr span(element_type (&arr)[N]) noexcept : span(dtl::data(arr), N) { - static_assert(extent == dynamic_extent || extent == N, "static sizes do not match"); - } - - template > - constexpr span(std::array& arr) noexcept : span(dtl::data(arr), N) {} - - template > - constexpr span(const std::array& arr) noexcept - : span(dtl::data(arr), N) {} - - template ()), - dtl::data(std::declval())), - pointer>::value> > - constexpr span(const Container& cont) : span(dtl::data(cont), dtl::size(cont)) {} - - template ()), - dtl::data(std::declval())), - pointer>::value> > - constexpr span(Container& cont) : span(dtl::data(cont), dtl::size(cont)) {} - - template ::value)> > - constexpr span(const span& s) noexcept : span(s.data(), s.size()) {} - - template ::value)> > - constexpr span(span& s) noexcept : span(s.data(), s.size()) {} - - constexpr span(const span& other) noexcept = default; - - constexpr iterator begin() { return base::data(); } - constexpr const_iterator begin() const { return base::data(); } - constexpr const_iterator cbegin() const { return base::data(); } - - constexpr iterator end() { return base::data() + base::size(); } - constexpr const_iterator end() const { return base::data() + base::size(); } - constexpr const_iterator cend() const { return base::data() + base::size(); } - - reverse_iterator rbegin() { return reverse_iterator(end()); } - const_reverse_iterator rbegin() const { return reverse_iterator(end()); } - const_reverse_iterator crbegin() { return reverse_iterator(end()); } - - reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rend() const { return reverse_iterator(begin()); } - const_reverse_iterator crend() { return reverse_iterator(begin()); } - - constexpr reference front() { return *base::data(); } - constexpr reference back() { return *(base::data() + base::size() - 1); } - - constexpr reference operator[](index_type idx) const { return base::data()[idx]; } - - constexpr std::size_t size_bytes() const noexcept { - return base::size() * sizeof(element_type); - } - - constexpr bool empty() const noexcept { return base::size() == 0; } - - template - constexpr span first() const { - assert(Count <= base::size()); - return span(base::data(), Count); - } - - constexpr span first(std::size_t count) const { - assert(count <= base::size()); - return span(base::data(), count); - } - - template - constexpr span last() const { - assert(Count <= base::size()); - return span(base::data() + base::size() - Count, Count); - } - - constexpr span last(std::size_t count) const { - assert(count <= base::size()); - return span(base::data() + base::size() - count, count); - } - - template - constexpr span - subspan() const { - assert(Offset <= base::size()); - constexpr std::size_t E = - (Count != dynamic_extent - ? Count - : (extent != dynamic_extent ? extent - Offset : dynamic_extent)); - assert(E == dynamic_extent || E <= base::size()); - return span(base::data() + Offset, - Count == dynamic_extent ? base::size() - Offset : Count); - } - - constexpr span subspan( - std::size_t offset, std::size_t count = dynamic_extent) const { - assert(offset <= base::size()); - const std::size_t s = count == dynamic_extent ? base::size() - offset : count; - assert(s <= base::size()); - return span(base::data() + offset, s); - } -}; - -} // namespace detail -} // namespace histogram -} // namespace boost - -#include -#include - -namespace boost { -namespace histogram { -namespace detail { - -namespace dtl = ::boost::histogram::detail; - -template -auto make_span(T* begin, T* end) { - return dtl::span{begin, end}; -} - -template -auto make_span(T* begin, std::size_t size) { - return dtl::span{begin, size}; -} - -template ()), - dtl::data(std::declval()))> -auto make_span(const Container& cont) { - return make_span(dtl::data(cont), dtl::size(cont)); -} - -template -auto make_span(T (&arr)[N]) { - return dtl::span(arr, N); -} - -} // namespace detail -} // namespace histogram -} // namespace boost - -#endif diff --git a/include/boost/histogram/histogram.hpp b/include/boost/histogram/histogram.hpp index ae023855..e1c0750e 100644 --- a/include/boost/histogram/histogram.hpp +++ b/include/boost/histogram/histogram.hpp @@ -7,6 +7,7 @@ #ifndef BOOST_HISTOGRAM_HISTOGRAM_HPP #define BOOST_HISTOGRAM_HISTOGRAM_HPP +#include #include #include #include @@ -16,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -239,7 +239,7 @@ public: "sample argument is missing but required by accumulator"); std::lock_guard guard{mutex_base::get()}; detail::fill_n(mp11::mp_bool<(n_sample_args_expected == 0)>{}, offset_, storage_, - axes_, detail::make_span(args)); + axes_, make_span(args)); } /** Fill histogram with several values and weights at once. @@ -257,8 +257,7 @@ public: std::is_convertible, typename acc_traits::args>::value; std::lock_guard guard{mutex_base::get()}; detail::fill_n(mp11::mp_bool<(weight_valid && sample_valid)>{}, offset_, storage_, - axes_, detail::make_span(args), - weight(detail::to_ptr_size(weights.value))); + axes_, make_span(args), weight(detail::to_ptr_size(weights.value))); } /** Fill histogram with several values and weights at once. @@ -289,7 +288,7 @@ public: constexpr bool sample_valid = std::is_convertible::value; detail::fill_n(mp11::mp_bool<(sample_valid)>{}, offset_, storage_, axes_, - detail::make_span(args), detail::to_ptr_size(sargs)...); + make_span(args), detail::to_ptr_size(sargs)...); }, samples.value); } @@ -321,7 +320,7 @@ public: constexpr bool sample_valid = std::is_convertible::value; detail::fill_n(mp11::mp_bool<(weight_valid && sample_valid)>{}, offset_, - storage_, axes_, detail::make_span(args), + storage_, axes_, make_span(args), weight(detail::to_ptr_size(weights.value)), detail::to_ptr_size(sargs)...); }, diff --git a/test/detail_array_wrapper_serialization_test.cpp b/test/detail_array_wrapper_serialization_test.cpp index edf3a97f..ec336d56 100644 --- a/test/detail_array_wrapper_serialization_test.cpp +++ b/test/detail_array_wrapper_serialization_test.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -24,7 +25,7 @@ struct dummy_array_wrapper { std::size_t size; template void serialize(Archive& ar, unsigned /* version */) { - for (auto&& x : dtl::make_span(ptr, size)) ar& x; + for (auto&& x : boost::make_span(ptr, size)) ar& x; } }; diff --git a/test/detail_misc_test.cpp b/test/detail_misc_test.cpp index ae1dfefc..cf36e87f 100644 --- a/test/detail_misc_test.cpp +++ b/test/detail_misc_test.cpp @@ -7,13 +7,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include #include @@ -126,6 +126,22 @@ int main() { BOOST_TEST_EQ(count, 6); } + // sub_array and make_span + { + dtl::sub_array a(2, 1); + a[1] = 2; + auto sp = boost::make_span(a); + BOOST_TEST_EQ(sp.size(), 2); + BOOST_TEST_EQ(sp.front(), 1); + BOOST_TEST_EQ(sp.back(), 2); + + const auto& ca = a; + auto csp = boost::make_span(ca); + BOOST_TEST_EQ(csp.size(), 2); + BOOST_TEST_EQ(csp.front(), 1); + BOOST_TEST_EQ(csp.back(), 2); + } + // index_translator { using I = axis::integer<>; diff --git a/test/detail_sub_array_and_span_test.cpp b/test/detail_sub_array_and_span_test.cpp index 00fb916f..5dd1758b 100644 --- a/test/detail_sub_array_and_span_test.cpp +++ b/test/detail_sub_array_and_span_test.cpp @@ -1,7 +1,7 @@ #include +#include #include #include -#include #include #include #include @@ -23,6 +23,7 @@ std::ostream& operator<<(std::ostream& os, const reduce_command&) { return os; } using namespace boost::histogram::detail; int main() { + using boost::span; { sub_array a = {1, 2};