File indexing completed on 2026-08-17 08:46:50
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_COLLECTOR_HPP
0008 #define BOOST_HISTOGRAM_ACCUMULATORS_COLLECTOR_HPP
0009
0010 #include <algorithm> // for std::equal
0011 #include <boost/core/nvp.hpp>
0012 #include <boost/histogram/detail/detect.hpp>
0013 #include <boost/histogram/fwd.hpp> // for collector<>
0014 #include <initializer_list>
0015 #include <type_traits>
0016
0017 namespace boost {
0018 namespace histogram {
0019 namespace accumulators {
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 template <class ContainerType>
0030 class collector {
0031 public:
0032 using container_type = ContainerType;
0033 using value_type = typename container_type::value_type;
0034 using allocator_type = typename container_type::allocator_type;
0035 using const_reference = typename container_type::const_reference;
0036 using iterator = typename container_type::iterator;
0037 using const_iterator = typename container_type::const_iterator;
0038 using size_type = typename container_type::size_type;
0039 using const_pointer = typename container_type::const_pointer;
0040
0041
0042 template <typename... Args, class = decltype(container_type(std::declval<Args>()...))>
0043 explicit collector(Args&&... args) : container_(std::forward<Args>(args)...) {}
0044
0045
0046 template <class T, typename... Args, class = decltype(container_type(std::initializer_list<T>(),std::declval<Args>()...))>
0047 explicit collector(std::initializer_list<T> list, Args&&... args)
0048 : container_(list, std::forward<Args>(args)...) {}
0049
0050
0051 void operator()(const_reference x) { container_.push_back(x); }
0052
0053
0054 template <class C>
0055 collector& operator+=(const collector<C>& rhs) {
0056 container_.reserve(size() + rhs.size());
0057 container_.insert(end(), rhs.begin(), rhs.end());
0058 return *this;
0059 }
0060
0061
0062
0063
0064
0065 template <class Iterable, class = detail::is_iterable<Iterable>>
0066 bool operator==(const Iterable& rhs) const noexcept {
0067 return std::equal(begin(), end(), rhs.begin(), rhs.end());
0068 }
0069
0070
0071 template <class Iterable, class = detail::is_iterable<Iterable>>
0072 bool operator!=(const Iterable& rhs) const noexcept {
0073 return !operator==(rhs);
0074 }
0075
0076
0077 size_type size() const noexcept { return container_.size(); }
0078
0079
0080 size_type count() const noexcept { return container_.size(); }
0081
0082
0083 const const_iterator begin() const noexcept { return container_.begin(); }
0084
0085
0086 const const_iterator end() const noexcept { return container_.end(); }
0087
0088
0089 const_reference operator[](size_type idx) const noexcept { return container_[idx]; }
0090
0091
0092 const_pointer data() const noexcept { return container_.data(); }
0093
0094 allocator_type get_allocator() const { return container_.get_allocator(); }
0095
0096 template <class Archive>
0097 void serialize(Archive& ar, unsigned version) {
0098 (void)version;
0099 ar& make_nvp("container", container_);
0100 }
0101
0102 private:
0103 container_type container_;
0104 };
0105
0106 }
0107 }
0108 }
0109
0110 #endif