From d1d9013d7e958e107e7e7cfb0fd9222d76b98f11 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Sat, 27 Oct 2018 21:17:14 +0200 Subject: [PATCH] refactor --- CMakeLists.txt | 4 - histogram.sublime-project | 1 - include/boost/histogram/detail/axes.hpp | 150 +++++++++--------- include/boost/histogram/histogram.hpp | 77 ++++----- .../histogram/storage/adaptive_storage.hpp | 57 +++++-- .../boost/histogram/storage/array_storage.hpp | 10 +- include/boost/histogram/storage/operators.hpp | 33 ---- test/Jamfile | 4 - test/adaptive_storage_test.cpp | 5 +- test/array_storage_test.cpp | 1 - ..._dynamic_at_tuple_wrong_dimension_fail.cpp | 17 -- ...dynamic_at_vector_wrong_dimension_fail.cpp | 16 -- ...togram_dynamic_at_wrong_dimension_fail.cpp | 16 -- test/histogram_dynamic_test.cpp | 13 ++ test/histogram_mixed_test.cpp | 4 +- ..._static_at_vector_wrong_dimension_fail.cpp | 15 -- test/histogram_test.cpp | 61 +++---- 17 files changed, 196 insertions(+), 288 deletions(-) delete mode 100644 include/boost/histogram/storage/operators.hpp delete mode 100644 test/histogram_dynamic_at_tuple_wrong_dimension_fail.cpp delete mode 100644 test/histogram_dynamic_at_vector_wrong_dimension_fail.cpp delete mode 100644 test/histogram_dynamic_at_wrong_dimension_fail.cpp delete mode 100644 test/histogram_static_at_vector_wrong_dimension_fail.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 11cd910b..526ac127 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,12 +90,8 @@ compiled_test(test/axis_variant_test.cpp) compiled_test(test/detail_test.cpp) compiled_test(test/histogram_dynamic_fill_one_dimensional_vector_fail.cpp) compiled_test(test/histogram_dynamic_fill_one_dimensional_tuple_fail.cpp) -compiled_test(test/histogram_dynamic_at_tuple_wrong_dimension_fail.cpp) -compiled_test(test/histogram_dynamic_at_vector_wrong_dimension_fail.cpp) -compiled_test(test/histogram_dynamic_at_wrong_dimension_fail.cpp) compiled_test(test/histogram_dynamic_reduce_wrong_order_fail.cpp) # test fail to compile (test/histogram_static_fill_one_dimensional_tuple_fail.cpp) -compiled_test(test/histogram_static_at_vector_wrong_dimension_fail.cpp) compiled_test(test/histogram_dynamic_test.cpp) compiled_test(test/histogram_mixed_test.cpp) compiled_test(test/histogram_test.cpp) diff --git a/histogram.sublime-project b/histogram.sublime-project index d35d0ac4..ac03ec8f 100644 --- a/histogram.sublime-project +++ b/histogram.sublime-project @@ -12,7 +12,6 @@ "style": "File", "format_on_save": true }, - "trim_trailing_white_space_on_save": false, "ensure_newline_at_eof_on_save": false, }, diff --git a/include/boost/histogram/detail/axes.hpp b/include/boost/histogram/detail/axes.hpp index 215bc868..84ab18b6 100644 --- a/include/boost/histogram/detail/axes.hpp +++ b/include/boost/histogram/detail/axes.hpp @@ -10,8 +10,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -81,23 +81,23 @@ bool axes_equal(const std::tuple& t, const std::tuple& u) { mp11::mp_same, mp11::mp_list>(), t, u); } -template -bool axes_equal(const std::tuple& t, const std::vector& u) { +template +bool axes_equal(const std::tuple& t, const std::vector& u) { if (sizeof...(Ts) != u.size()) return false; bool equal = true; - auto fn = axes_equal_static_dynamic_impl, std::vector>(equal, - t, u); + auto fn = + axes_equal_static_dynamic_impl, std::vector>(equal, t, u); mp11::mp_for_each>(fn); return equal; } -template -bool axes_equal(const std::vector& t, const std::tuple& u) { +template +bool axes_equal(const std::vector& t, const std::tuple& u) { return axes_equal(u, t); } -template -bool axes_equal(const std::vector& t, const std::vector& u) { +template +bool axes_equal(const std::vector& t, const std::vector& u) { if (t.size() != u.size()) return false; return std::equal(t.begin(), t.end(), u.begin()); } @@ -109,23 +109,21 @@ void axes_assign(std::tuple& t, const std::tuple& u) { t = u; } -template -void axes_assign(std::tuple& t, const std::vector& u) { - auto fn = - axes_assign_static_dynamic_impl, std::vector>(t, u); +template +void axes_assign(std::tuple& t, const std::vector& u) { + auto fn = axes_assign_static_dynamic_impl, std::vector>(t, u); mp11::mp_for_each>(fn); } -template -void axes_assign(std::vector& t, const std::tuple& u) { +template +void axes_assign(std::vector& t, const std::tuple& u) { t.resize(sizeof...(Us)); - auto fn = - axes_assign_dynamic_static_impl, std::tuple>(t, u); + auto fn = axes_assign_dynamic_static_impl, std::tuple>(t, u); mp11::mp_for_each>(fn); } -template -void axes_assign(std::vector& t, const std::vector& u) { +template +void axes_assign(std::vector& t, const std::vector& u) { t.assign(u.begin(), u.end()); } @@ -134,13 +132,13 @@ constexpr std::size_t axes_size(const std::tuple&) { return sizeof...(Ts); } -template -std::size_t axes_size(const std::vector& axes) { +template +std::size_t axes_size(const std::vector& axes) { return axes.size(); } -template -void range_check(const std::vector& axes) { +template +void range_check(const std::vector& axes) { BOOST_ASSERT_MSG(N < axes.size(), "index out of range"); } @@ -152,14 +150,12 @@ void range_check(const std::tuple&) { template using axis_at = mp_at_c::value * N>; -template > +template > auto axis_get(T&& axes) -> decltype(std::get(std::forward(axes))) { return std::get(std::forward(axes)); } -template > +template > auto axis_get(T&& axes) -> decltype(std::forward(axes)[N]) { return std::forward(axes)[N]; } @@ -169,11 +165,9 @@ void for_each_axis(const std::tuple& axes, F&& f) { mp11::tuple_for_each(axes, std::forward(f)); } -template -void for_each_axis(const std::vector& axes, F&& f) { - for (const auto& x : axes) { - axis::visit(std::forward(f), x); - } +template +void for_each_axis(const std::vector& axes, F&& f) { + for (const auto& x : axes) { axis::visit(std::forward(f), x); } } namespace { @@ -184,7 +178,7 @@ struct field_counter { value *= axis::traits::extend(t); } }; -} +} // namespace template std::size_t bincount(const T& axes) { @@ -200,20 +194,18 @@ void dimension_check(const std::tuple&, mp11::mp_size_t) { template void dimension_check(const std::tuple&, std::size_t n) { - BOOST_ASSERT_MSG(sizeof...(Ts) == n, "number of arguments does not match"); + if (sizeof...(Ts) != n) + throw std::invalid_argument("number of arguments does not match"); } -template -void dimension_check(const std::vector& axes, mp11::mp_size_t) { - BOOST_ASSERT_MSG(axes.size() == N, "number of arguments does not match"); - boost::ignore_unused(axes); +template +void dimension_check(const std::vector& axes, mp11::mp_size_t) { + if (axes.size() != N) throw std::invalid_argument("number of arguments does not match"); } -template -void dimension_check(const std::vector& axes, std::size_t n) { - BOOST_ASSERT_MSG(axes.size() == n, "number of arguments does not match"); - boost::ignore_unused(axes); - boost::ignore_unused(n); +template +void dimension_check(const std::vector& axes, std::size_t n) { + if (axes.size() != n) throw std::invalid_argument("number of arguments does not match"); } struct shape_collector { @@ -241,13 +233,13 @@ struct sub_axes_impl> { using type = mp11::mp_transform; }; -template -struct sub_axes_impl> { +template +struct sub_axes_impl> { static_assert(mp11::mp_is_set::value, "integer arguments must be strictly ascending"); - using type = std::vector; + using type = std::vector; }; -} +} // namespace template using sub_axes = typename sub_axes_impl, T>::type; @@ -262,7 +254,7 @@ struct sub_static_assign_impl { std::get(dst) = std::get(src); } }; -} +} // namespace template sub_axes, Ns...> make_sub_axes(const std::tuple& t, Ns...) { @@ -286,11 +278,11 @@ struct sub_dynamic_assign_impl { dst.emplace_back(src[I::value]); } }; -} +} // namespace -template -sub_axes, Ns...> make_sub_axes(const std::vector& t, Ns...) { - using T = std::vector; +template +sub_axes, Ns...> make_sub_axes(const std::vector& t, Ns...) { + using T = std::vector; T u(t.get_allocator()); u.reserve(sizeof...(Ns)); using N = mp11::mp_list; @@ -347,8 +339,8 @@ void indices_to_index_iter(mp11::mp_size_t, optional_index& idx, indices_to_index_iter(mp11::mp_size_t<(N - 1)>(), idx, axes, ++iter); } -template -void indices_to_index_iter(optional_index& idx, const std::vector& axes, +template +void indices_to_index_iter(optional_index& idx, const std::vector& axes, Iterator iter) { for (const auto& a : axes) { const auto a_size = static_cast(a.size()); @@ -396,7 +388,7 @@ void args_to_index_iter(mp11::mp_size_t<0>, optional_index&, const std::tuple void args_to_index_iter(mp11::mp_size_t, optional_index& idx, const std::tuple& axes, Iterator iter) { - constexpr std::size_t D = sizeof...(Ts)-N; + constexpr std::size_t D = sizeof...(Ts) - N; const auto& a = axis_get(axes); const auto a_size = a.size(); const auto a_shape = axis::traits::extend(a); @@ -445,27 +437,27 @@ struct args_to_index_visitor { template void impl(std::false_type, const U&) const { using arg_type = mp11::mp_first>; - throw std::invalid_argument(detail::cat( - boost::core::demangled_name( BOOST_CORE_TYPEID(U) ), - ": cannot convert argument of type ", - boost::core::demangled_name( BOOST_CORE_TYPEID(T) ), " to ", - boost::core::demangled_name( BOOST_CORE_TYPEID(arg_type) ))); + throw std::invalid_argument( + detail::cat(boost::core::demangled_name(BOOST_CORE_TYPEID(U)), + ": cannot convert argument of type ", + boost::core::demangled_name(BOOST_CORE_TYPEID(T)), " to ", + boost::core::demangled_name(BOOST_CORE_TYPEID(arg_type)))); } }; -} +} // namespace -template -void args_to_index(optional_index&, const std::vector&) {} +template +void args_to_index(optional_index&, const std::vector&) {} -template -void args_to_index(optional_index& idx, const std::vector& axes, const U& u, +template +void args_to_index(optional_index& idx, const std::vector& axes, const U& u, const Us&... us) { axis::visit(args_to_index_visitor(idx, u), axes[D]); args_to_index<(D + 1)>(idx, axes, us...); } -template -void args_to_index_iter(optional_index& idx, const std::vector& axes, +template +void args_to_index_iter(optional_index& idx, const std::vector& axes, Iterator iter) { for (const auto& a : axes) { // iter could be a plain pointer, so we cannot use nested value_type here @@ -473,13 +465,13 @@ void args_to_index_iter(optional_index& idx, const std::vector& axes, } } -template -void args_to_index_get(mp11::mp_size_t<0>, optional_index&, const std::vector&, +template +void args_to_index_get(mp11::mp_size_t<0>, optional_index&, const std::vector&, const T&) {} -template +template void args_to_index_get(mp11::mp_size_t, optional_index& idx, - const std::vector& axes, const T& t) { + const std::vector& axes, const T& t) { constexpr std::size_t D = mp_size::value - N; using U = decltype(std::get(t)); axis::visit(args_to_index_visitor(idx, std::get(t)), axes[D]); @@ -522,8 +514,8 @@ optional_index call_impl(iterable_container_tag, const std::tuple return i; } -template -optional_index call_impl(no_container_tag, const std::vector& axes, +template +optional_index call_impl(no_container_tag, const std::vector& axes, const Us&... us) { dimension_check(axes, mp11::mp_size_t()); optional_index i; @@ -531,8 +523,8 @@ optional_index call_impl(no_container_tag, const std::vector& axes, return i; } -template -optional_index call_impl(static_container_tag, const std::vector& axes, +template +optional_index call_impl(static_container_tag, const std::vector& axes, const U& u) { if (axes.size() == 1) // do not unpack for 1d histograms, it is ambiguous return call_impl(no_container_tag(), axes, u); @@ -542,8 +534,8 @@ optional_index call_impl(static_container_tag, const std::vector& axes, return i; } -template -optional_index call_impl(iterable_container_tag, const std::vector& axes, +template +optional_index call_impl(iterable_container_tag, const std::vector& axes, const U& u) { if (axes.size() == 1) // do not unpack for 1d histograms, it is ambiguous return call_impl(no_container_tag(), axes, u); @@ -588,8 +580,8 @@ std::size_t at_impl(detail::iterable_container_tag, const std::tuple& axe return *index; } -template -std::size_t at_impl(detail::iterable_container_tag, const std::vector& axes, +template +std::size_t at_impl(detail::iterable_container_tag, const std::vector& axes, const U& u) { dimension_check(axes, std::distance(std::begin(u), std::end(u))); auto index = detail::optional_index(); diff --git a/include/boost/histogram/histogram.hpp b/include/boost/histogram/histogram.hpp index a6b496d1..91e2ef79 100644 --- a/include/boost/histogram/histogram.hpp +++ b/include/boost/histogram/histogram.hpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -124,16 +123,14 @@ public: decltype(auto) axis() { return axis(mp11::mp_size_t<0>()); } /// Get N-th axis with runtime index (const version) - template > + template > decltype(auto) axis(std::size_t i) const { BOOST_ASSERT_MSG(i < axes_.size(), "index out of range"); return axes_[i]; } /// Get N-th axis with runtime index - template > + template > decltype(auto) axis(std::size_t i) { BOOST_ASSERT_MSG(i < axes_.size(), "index out of range"); return axes_[i]; @@ -153,6 +150,13 @@ public: if (index) storage_.increase(*index); } + template + void operator()(const T& t) { + // check whether we need to unpack argument + const auto index = detail::call_impl(detail::classify_container(), axes_, t); + if (index) storage_.increase(*index); + } + /// Fill histogram with a weight and a value tuple template void operator()(weight_type&& w, const Ts&... ts) { @@ -161,6 +165,13 @@ public: if (index) storage_.add(*index, w); } + template + void operator()(weight_type&& w, const T& t) { + // check whether we need to unpack argument + const auto index = detail::call_impl(detail::classify_container(), axes_, t); + if (index) storage_.add(*index, w); + } + /// Access bin counter at indices template const_reference at(const Ts&... ts) const { @@ -170,20 +181,6 @@ public: return storage_[index]; } - template - void operator()(const T& t) { - // check whether we need to unpack argument - const auto index = detail::call_impl(detail::classify_container(), axes_, t); - if (index) storage_.increase(*index); - } - - template - void operator()(weight_type&& w, const T& t) { - // check whether we need to unpack argument - const auto index = detail::call_impl(detail::classify_container(), axes_, t); - if (index) storage_.add(*index, w); - } - template const_reference at(const T& t) const { // check whether we need to unpack argument; @@ -260,53 +257,40 @@ private: /// static type factory with custom storage type template > -histogram< - std::tuple, detail::rm_cvref...>, - detail::rm_cvref -> +histogram, detail::rm_cvref...>, detail::rm_cvref> make_histogram_with(S&& s, T&& axis0, Ts&&... axis) { auto axes = std::make_tuple(std::forward(axis0), std::forward(axis)...); - return histogram>( - std::move(axes), std::forward(s) - ); + return histogram>(std::move(axes), + std::forward(s)); } /// static type factory with standard storage type template > auto make_histogram(T&& axis0, Ts&&... axis) - -> decltype(make_histogram_with(default_storage(), - std::forward(axis0), - std::forward(axis)...)) -{ - return make_histogram_with(default_storage(), - std::forward(axis0), + -> decltype(make_histogram_with(default_storage(), std::forward(axis0), + std::forward(axis)...)) { + return make_histogram_with(default_storage(), std::forward(axis0), std::forward(axis)...); } /// dynamic type factory from vector-like with custom storage type -template > -histogram, detail::rm_cvref> -make_histogram_with(S&& s, T&& t) { - return histogram, detail::rm_cvref>( - std::forward(t), std::forward(s) - ); +template > +histogram, detail::rm_cvref> make_histogram_with(S&& s, T&& t) { + return histogram, detail::rm_cvref>(std::forward(t), + std::forward(s)); } /// dynamic type factory from vector-like with standard storage type -template > +template > auto make_histogram(T&& t) - -> decltype(make_histogram_with(default_storage(), std::forward(t))) -{ + -> decltype(make_histogram_with(default_storage(), std::forward(t))) { return make_histogram_with(default_storage(), std::forward(t)); } /// dynamic type factory from iterator range with custom storage type template > -histogram>, - detail::rm_cvref> +histogram>, detail::rm_cvref> make_histogram_with(Storage&& s, Iterator begin, Iterator end) { using T = detail::iterator_value_type; auto axes = std::vector(begin, end); @@ -316,8 +300,7 @@ make_histogram_with(Storage&& s, Iterator begin, Iterator end) { /// dynamic type factory from iterator range with standard storage type template > auto make_histogram(Iterator begin, Iterator end) - -> decltype(make_histogram_with(default_storage(), begin, end)) -{ + -> decltype(make_histogram_with(default_storage(), begin, end)) { return make_histogram_with(default_storage(), begin, end); } } // namespace histogram diff --git a/include/boost/histogram/storage/adaptive_storage.hpp b/include/boost/histogram/storage/adaptive_storage.hpp index 95539268..e6510c6c 100644 --- a/include/boost/histogram/storage/adaptive_storage.hpp +++ b/include/boost/histogram/storage/adaptive_storage.hpp @@ -266,6 +266,12 @@ struct adaptive_storage { return apply(comparer(), buffer, o.buffer); } + template + bool operator==(const T& o) const { + if (size() != o.size()) return false; + return apply(comparer_extern(), buffer, o); + } + // precondition: storages have same size adaptive_storage& operator+=(const adaptive_storage& o) { BOOST_ASSERT(o.size() == size()); @@ -442,22 +448,29 @@ struct adaptive_storage { } }; + struct cmp { + template + bool operator()(const T& t, const U& u) { + return t == u; + } + bool operator()(const mp_int& t, const double& u) { + return static_cast(t) == u; + } + bool operator()(const double& t, const mp_int& u) { + return t == static_cast(u); + } + + bool operator()(const mp_int& t, const float& u) { + return static_cast(t) == u; + } + bool operator()(const float& t, const mp_int& u) { + return t == static_cast(u); + } + }; + // precondition: buffers already have same size struct comparer { struct inner { - struct cmp { - template - bool operator()(const T& t, const U& u) { - return t == u; - } - bool operator()(const mp_int& t, const double& u) { - return static_cast(t) == u; - } - bool operator()(const double& t, const mp_int& u) { - return t == static_cast(u); - } - }; - template bool operator()(const U* optr, const OBuffer& ob, const T* tp) { return std::equal(optr, optr + ob.size, tp, cmp()); @@ -486,6 +499,24 @@ struct adaptive_storage { } }; + // precondition: buffers already have same size + struct comparer_extern { + template + bool operator()(const T* ptr, const Buffer& b, const U& u) { + auto c = cmp(); + for (std::size_t i = 0; i < b.size; ++i) + if (!c(ptr[i], u[i])) return false; + return true; + } + + template + bool operator()(const void*, const Buffer& b, const U& u) { + for (std::size_t i = 0; i < b.size; ++i) + if (!(0 == u[i])) return false; + return true; + } + }; + struct multiplier { template void operator()(T* tp, Buffer& b, const double x) { diff --git a/include/boost/histogram/storage/array_storage.hpp b/include/boost/histogram/storage/array_storage.hpp index 77a6ecb5..9f745b18 100644 --- a/include/boost/histogram/storage/array_storage.hpp +++ b/include/boost/histogram/storage/array_storage.hpp @@ -8,9 +8,9 @@ #define BOOST_HISTOGRAM_STORAGE_ARRAY_HPP #include +#include #include #include -#include #include #include #include @@ -83,6 +83,14 @@ struct array_storage { return std::equal(buffer.begin(), buffer.end(), rhs.buffer.begin()); } + template + bool operator==(const U& rhs) const noexcept { + if (size() != rhs.size()) return false; + for (std::size_t i = 0; i < size(); ++i) + if (!(buffer[i] == rhs[i])) return false; + return true; + } + template array_storage& operator+=(const S& rhs) noexcept { for (std::size_t i = 0; i < size(); ++i) add(i, rhs[i]); diff --git a/include/boost/histogram/storage/operators.hpp b/include/boost/histogram/storage/operators.hpp deleted file mode 100644 index 5772ad4d..00000000 --- a/include/boost/histogram/storage/operators.hpp +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015-2017 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_STORAGE_OPERATORS_HPP -#define BOOST_HISTOGRAM_STORAGE_OPERATORS_HPP - -#include - -namespace boost { -namespace histogram { - -template , - typename = detail::requires_storage> -bool operator==(const S1& s1, const S2& s2) noexcept { - if (s1.size() != s2.size()) return false; - for (std::size_t i = 0, n = s1.size(); i < n; ++i) - if (!(s1[i] == s2[i])) return false; - return true; -} - -template , - typename = detail::requires_storage> -bool operator!=(const S1& s1, const S2& s2) noexcept { - return !operator==(s1, s2); -} - -} // namespace histogram -} // namespace boost - -#endif diff --git a/test/Jamfile b/test/Jamfile index 9b105292..991f97fd 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -35,11 +35,7 @@ alias run-tests : ; alias run-fail-tests : - [ run-fail histogram_dynamic_at_tuple_wrong_dimension_fail.cpp ] - [ run-fail histogram_dynamic_at_vector_wrong_dimension_fail.cpp ] - [ run-fail histogram_dynamic_at_wrong_dimension_fail.cpp ] [ run-fail histogram_dynamic_reduce_wrong_order_fail.cpp ] - [ run-fail histogram_static_at_vector_wrong_dimension_fail.cpp ] ; alias run-speed-tests : diff --git a/test/adaptive_storage_test.cpp b/test/adaptive_storage_test.cpp index b5af774f..c78f2eb2 100644 --- a/test/adaptive_storage_test.cpp +++ b/test/adaptive_storage_test.cpp @@ -7,15 +7,14 @@ #include #include #include -#include #include #include #include -#include namespace bh = boost::histogram; using adaptive_storage_type = bh::adaptive_storage>; -template using array_storage = bh::array_storage>; +template +using array_storage = bh::array_storage>; using bh::weight; template diff --git a/test/array_storage_test.cpp b/test/array_storage_test.cpp index ff6d83e8..cf53bdf7 100644 --- a/test/array_storage_test.cpp +++ b/test/array_storage_test.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/test/histogram_dynamic_at_tuple_wrong_dimension_fail.cpp b/test/histogram_dynamic_at_tuple_wrong_dimension_fail.cpp deleted file mode 100644 index c917276b..00000000 --- a/test/histogram_dynamic_at_tuple_wrong_dimension_fail.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2018 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) - -#include -#include -#include - -using namespace boost::histogram; -int main() { - auto v = std::vector>>(); - v.push_back(axis::integer<>(0, 2)); - auto h = make_histogram(v); - h.at(std::make_pair(0, 0)); -} diff --git a/test/histogram_dynamic_at_vector_wrong_dimension_fail.cpp b/test/histogram_dynamic_at_vector_wrong_dimension_fail.cpp deleted file mode 100644 index 6e0779ad..00000000 --- a/test/histogram_dynamic_at_vector_wrong_dimension_fail.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2018 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) - -#include -#include - -using namespace boost::histogram; -int main() { - auto v = std::vector>>(); - v.push_back(axis::integer<>(0, 2)); - auto h = make_histogram(v); - h.at(std::vector({0, 0})); -} diff --git a/test/histogram_dynamic_at_wrong_dimension_fail.cpp b/test/histogram_dynamic_at_wrong_dimension_fail.cpp deleted file mode 100644 index 24f3d546..00000000 --- a/test/histogram_dynamic_at_wrong_dimension_fail.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2018 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) - -#include -#include - -using namespace boost::histogram; -int main() { - auto v = std::vector>>(); - v.push_back(axis::integer<>(0, 2)); - auto h = make_histogram(v); - h.at(0, 0); -} diff --git a/test/histogram_dynamic_test.cpp b/test/histogram_dynamic_test.cpp index e75fd9b6..1949983f 100644 --- a/test/histogram_dynamic_test.cpp +++ b/test/histogram_dynamic_test.cpp @@ -120,5 +120,18 @@ int main() { BOOST_TEST(it == h.end()); } + // wrong dimension + { + auto h1 = make(dynamic_tag(), axis::integer<>(0, 2)); + h1(1); + BOOST_TEST_THROWS(h1.at(0, 0), std::invalid_argument); + BOOST_TEST_THROWS(h1.at(std::make_pair(0, 0)), std::invalid_argument); + } + + // { + // auto v = std::vector>(1, axis::integer<>(0, 3)); + // auto h = make_histogram(v); + // } + return boost::report_errors(); } diff --git a/test/histogram_mixed_test.cpp b/test/histogram_mixed_test.cpp index 7f807bb9..bc3ff27f 100644 --- a/test/histogram_mixed_test.cpp +++ b/test/histogram_mixed_test.cpp @@ -19,7 +19,7 @@ void run_tests() { // compare { auto a = make(T1{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); - auto b = make_s(T2{}, array_storage(), axis::regular<>{3, 0, 3}, + auto b = make_s(T2{}, array_storage(), axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); BOOST_TEST_EQ(a, b); auto b2 = make(T2{}, axis::integer<>{0, 3}, axis::integer<>(0, 2)); @@ -46,7 +46,7 @@ void run_tests() { // copy_assign { auto a = make(T1{}, axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); - auto b = make_s(T2{}, array_storage(), axis::regular<>{3, 0, 3}, + auto b = make_s(T2{}, array_storage(), axis::regular<>{3, 0, 3}, axis::integer<>(0, 2)); a(1, 1); BOOST_TEST_NE(a, b); diff --git a/test/histogram_static_at_vector_wrong_dimension_fail.cpp b/test/histogram_static_at_vector_wrong_dimension_fail.cpp deleted file mode 100644 index 9cdb7396..00000000 --- a/test/histogram_static_at_vector_wrong_dimension_fail.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2018 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) - -#include -#include - -using namespace boost::histogram; - -int main() { - auto h = make_histogram(axis::integer<>(0, 2)); - h.at(std::vector({0, 0})); -} diff --git a/test/histogram_test.cpp b/test/histogram_test.cpp index 7d961b0f..efaa45b6 100644 --- a/test/histogram_test.cpp +++ b/test/histogram_test.cpp @@ -5,13 +5,13 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) #include +#include #include #include #include #include #include #include -#include #include #include #include @@ -154,8 +154,7 @@ void run_tests() { a.axis().metadata() = "bar"; BOOST_TEST_EQ(a.axis().metadata(), "bar"); - auto b = make(Tag(), axis::regular<>(1, 1, 2, "foo"), - axis::integer<>(1, 3)); + auto b = make(Tag(), axis::regular<>(1, 1, 2, "foo"), axis::integer<>(1, 3)); BOOST_TEST_EQ(b.axis(0_c).size(), 1); BOOST_TEST_EQ(b.axis(0_c)[0].lower(), 1); BOOST_TEST_EQ(b.axis(0_c)[0].upper(), 2); @@ -254,8 +253,7 @@ void run_tests() { // d1w { - auto h = make_s(Tag(), array_storage>(), - axis::integer<>(0, 2)); + auto h = make_s(Tag(), array_storage>(), axis::integer<>(0, 2)); h(-1); h(0); h(weight(0.5), 0); @@ -309,9 +307,7 @@ void run_tests() { // d2w { - auto h = make_s(Tag(), - array_storage>(), - axis::regular<>(2, -1, 1), + auto h = make_s(Tag(), array_storage>(), axis::regular<>(2, -1, 1), axis::integer<>(-1, 2, {}, axis::option_type::none)); h(-1, 0); // -> 0, 1 h(weight(10), -1, -1); // -> 0, 0 @@ -356,11 +352,8 @@ void run_tests() { // d3w { - auto h = make_s(Tag(), - array_storage>(), - axis::integer<>(0, 3), - axis::integer<>(0, 4), - axis::integer<>(0, 5)); + auto h = make_s(Tag(), array_storage>(), axis::integer<>(0, 3), + axis::integer<>(0, 4), axis::integer<>(0, 5)); for (auto i = 0u; i < h.axis(0_c).size(); ++i) { for (auto j = 0u; j < h.axis(1_c).size(); ++j) { for (auto k = 0u; k < h.axis(2_c).size(); ++k) { h(weight(i + j + k), i, j, k); } @@ -402,10 +395,8 @@ void run_tests() { // add_2 { - auto a = make_s(Tag(), array_storage>(), - axis::integer<>(0, 2)); - auto b = make_s(Tag(), array_storage>(), - axis::integer<>(0, 2)); + auto a = make_s(Tag(), array_storage>(), axis::integer<>(0, 2)); + auto b = make_s(Tag(), array_storage>(), axis::integer<>(0, 2)); a(0); BOOST_TEST_EQ(a.at(0).variance(), 1); @@ -519,13 +510,13 @@ void run_tests() { auto a = make(Tag(), axis::regular<>(3, -1, 1, "r"), axis::integer<>(0, 2, "i")); std::ostringstream os; os << a; - BOOST_TEST_EQ(os.str(), - std::string( - "histogram(\n" - " regular(3, -1, 1, metadata=\"r\", options=underflow_and_overflow),\n" - " integer(0, 2, metadata=\"i\", options=underflow_and_overflow),\n" - ")" - )); + BOOST_TEST_EQ( + os.str(), + std::string( + "histogram(\n" + " regular(3, -1, 1, metadata=\"r\", options=underflow_and_overflow),\n" + " integer(0, 2, metadata=\"i\", options=underflow_and_overflow),\n" + ")")); } // histogram_reset @@ -643,9 +634,7 @@ void run_tests() { using integer::integer; // inherit ctors of base // customization point: convert argument and call base class - int operator()(const char* s) const { - return integer::operator()(std::atoi(s)); - } + int operator()(const char* s) const { return integer::operator()(std::atoi(s)); } }; auto h = make(Tag(), custom_axis(0, 3)); @@ -663,8 +652,7 @@ void run_tests() { // histogram iterator 1D { - auto h = make_s(Tag(), array_storage>(), - axis::integer<>(0, 3)); + auto h = make_s(Tag(), array_storage>(), axis::integer<>(0, 3)); const auto& a = h.axis(); h(weight(2), 0); h(1); @@ -698,9 +686,7 @@ void run_tests() { // histogram iterator 2D { - auto h = make_s(Tag(), - array_storage>(), - axis::integer<>(0, 1), + auto h = make_s(Tag(), array_storage>(), axis::integer<>(0, 1), axis::integer<>(2, 4, "", axis::option_type::none)); const auto& a0 = h.axis(0_c); const auto& a1 = h.axis(1_c); @@ -762,9 +748,8 @@ void run_tests() { // using STL containers { - auto h = make_s(Tag(), - array_storage>(), - axis::integer<>(0, 2), axis::regular<>(2, 2, 4)); + auto h = make_s(Tag(), array_storage>(), axis::integer<>(0, 2), + axis::regular<>(2, 2, 4)); // vector in h(std::vector({0, 2})); // pair in @@ -796,6 +781,9 @@ void run_tests() { h1(1); BOOST_TEST_EQ(h1.at(std::make_tuple(0)), 0); BOOST_TEST_EQ(h1.at(std::vector(1, 1)), 1); + + // wrong dimension + BOOST_TEST_THROWS(h1.at(std::vector({0, 2})), std::invalid_argument); } // bad bin access @@ -830,7 +818,8 @@ void run_tests() { if (Tag()) { // axis::variant allocation, only for dynamic histogram using T = axis::variant>; BOOST_TEST_EQ(db[typeid(T)].first, db[typeid(T)].second); - BOOST_TEST_LE(db[typeid(T)].first, 1u); // zero if vector uses small-vector-optimisation + BOOST_TEST_LE(db[typeid(T)].first, + 1u); // zero if vector uses small-vector-optimisation } } }