Project
Loading...
Searching...
No Matches
AdaptiveHistogram.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
17#ifndef INCLUDE_RANS_INTERNAL_CONTAINERS_ADAPTIVEHISTOGRAM_H_
18#define INCLUDE_RANS_INTERNAL_CONTAINERS_ADAPTIVEHISTOGRAM_H_
19
23
24namespace o2::rans
25{
26
27template <typename source_T>
28class AdaptiveHistogram : public internal::SparseVectorContainer<source_T, uint32_t>,
29 public internal::HistogramConcept<source_T,
30 typename internal::SparseVectorContainer<source_T, uint32_t>::value_type,
31 typename internal::SparseVectorContainer<source_T, uint32_t>::difference_type,
32 AdaptiveHistogram<source_T>>
33{
39
41
42 public:
53
54 AdaptiveHistogram() = default;
55
56 template <typename freq_IT>
58
59 // operations
61
63
64 protected:
65 template <typename source_IT>
66 AdaptiveHistogram& addSamplesImpl(source_IT begin, source_IT end);
67
68 inline AdaptiveHistogram& addSamplesImpl(gsl::span<const source_type> samples) { return addSamplesImpl(samples.data(), samples.data() + samples.size()); };
69
70 template <typename freq_IT>
72};
73
74template <typename source_T>
75template <typename source_IT>
77{
78
79 if constexpr (std::is_same_v<typename std::iterator_traits<source_IT>::iterator_category, std::random_access_iterator_tag>) {
80
81 const auto size = std::distance(begin, end);
82 constexpr size_t nUnroll = 2;
83
84 size_t pos{};
85 if (end - nUnroll > begin) {
86 for (pos = 0; pos < size - nUnroll; pos += nUnroll) {
87 ++this->mContainer[begin[pos + 0]];
88 ++this->mContainer[begin[pos + 1]];
89 this->mNSamples += nUnroll;
90 }
91 }
92
93 for (auto iter = begin + pos; iter != end; ++iter) {
94 ++this->mNSamples;
95 ++this->mContainer[*iter];
96 }
97 } else {
98 std::for_each(begin, end, [this](const source_type& symbol) {
99 ++this->mNSamples;
100 ++this->mContainer[symbol];
101 });
102 }
103 return *this;
104}
105
106template <typename source_T>
107template <typename freq_IT>
109{
110 source_type sourceSymbol = offset;
111 for (auto iter = begin; iter != end; ++iter) {
112 auto value = *iter;
113 if (value > 0) {
114 auto& currentValue = this->mContainer[sourceSymbol];
115 currentValue = internal::safeadd(currentValue, this->countSamples(value));
116 }
117 ++sourceSymbol;
118 }
119 return *this;
120}
121
122template <typename source_T>
124{
125 using iterator_value_type = typename AdaptiveHistogram<source_T>::const_iterator::value_type;
126 using value_type = typename AdaptiveHistogram<source_T>::value_type;
127
128 return std::count_if(histogram.begin(), histogram.end(), [](iterator_value_type v) { return v.second != value_type{}; });
129}
130
131} // namespace o2::rans
132
133#endif /* INCLUDE_RANS_INTERNAL_CONTAINERS_ADAPTIVEHISTOGRAM_H_ */
Abstract container class that defines and implements basic properties shared by histograms and lookup...
Operations that will be performed on a histogram.
uint16_t pos
Definition RawData.h:3
common helper classes and functions
typename containerBase_type::const_pointer const_pointer
typename containerBase_type::const_reference const_reference
typename containerBase_type::source_type source_type
typename containerBase_type::pointer pointer
AdaptiveHistogram(freq_IT begin, freq_IT end, source_type offset)
AdaptiveHistogram & addFrequenciesImpl(freq_IT begin, freq_IT end, source_type offset)
typename containerBase_type::const_iterator const_iterator
typename containerBase_type::reference reference
AdaptiveHistogram & addSamplesImpl(gsl::span< const source_type > samples)
typename containerBase_type::container_type container_type
typename containerBase_type::size_type size_type
typename containerBase_type::value_type value_type
AdaptiveHistogram & addSamplesImpl(source_IT begin, source_IT end)
typename containerBase_type::difference_type difference_type
const_iterator begin() const noexcept
Definition Container.h:53
const_iterator end() const noexcept
Definition Container.h:55
derived_T & addFrequencies(freq_IT begin, freq_IT end, difference_type offset)
derived_T & addSamples(source_IT begin, source_IT end)
typename base_type::difference_type difference_type
Definition Container.h:122
typename base_type::value_type value_type
Definition Container.h:119
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
const GLdouble * v
Definition glcorearb.h:832
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLintptr offset
Definition glcorearb.h:660
uint32_t safeadd(uint32_t a, uint32_t b)
Definition utils.h:144
size_t countNUsedAlphabetSymbols(const AdaptiveHistogram< source_T > &histogram)