Project
Loading...
Searching...
No Matches
SparseHistogram.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
15
16#ifndef INCLUDE_RANS_INTERNAL_CONTAINERS_SPARSEHISTOGRAM_H_
17#define INCLUDE_RANS_INTERNAL_CONTAINERS_SPARSEHISTOGRAM_H_
18
22
23namespace o2::rans
24{
25
26template <typename source_T>
27class SparseHistogram : public internal::SetContainer<source_T, uint32_t>,
28 public internal::HistogramConcept<source_T,
29 typename internal::SetContainer<source_T, uint32_t>::value_type,
30 typename internal::SetContainer<source_T, uint32_t>::difference_type,
31 SparseHistogram<source_T>>
32{
38
40
41 public:
52
53 SparseHistogram() = default;
54
55 template <typename freq_IT>
57
58 // operations
60
62
63 protected:
64 template <typename source_IT>
65 SparseHistogram& addSamplesImpl(source_IT begin, source_IT end);
66
67 inline SparseHistogram& addSamplesImpl(gsl::span<const source_type> samples) { return addSamplesImpl(samples.data(), samples.data() + samples.size()); };
68
69 template <typename freq_IT>
71};
72
73template <typename source_T>
74template <typename source_IT>
76{
77
78 absl::flat_hash_map<source_type, value_type> map;
79
80 // first build a hash map of existing samples
81 std::for_each(this->mContainer.begin(), this->mContainer.end(), [this, &map](const auto& keyValuePair) {
82 map.emplace(keyValuePair.first, keyValuePair.second);
83 });
84
85 // then add new samples to the same map
86 std::for_each(begin, end, [this, &map](const source_type& symbol) {
87 ++this->mNSamples;
88 ++map[symbol];
89 });
90
91 //
92 typename container_type::container_type mergedSymbols;
93 mergedSymbols.reserve(map.size());
94 std::for_each(map.begin(), map.end(), [&](const auto& keyValuePair) { mergedSymbols.emplace_back(keyValuePair.first, keyValuePair.second); });
95
96 // and build a OrderedSet from it
97 this->mContainer = container_type(std::move(mergedSymbols), 0, internal::OrderedSetState::unordered);
98
99 return *this;
100}
101
102template <typename source_T>
103template <typename freq_IT>
105{
106 if (begin != end) {
107 // first build a map of the current list items
108 absl::flat_hash_map<source_type, value_type> map;
109
110 auto container = std::move(this->mContainer).release();
111 for (const auto& [key, value] : container) {
112 map.emplace(key, value);
113 }
114
115 // then add all new values to the map
116 source_type sourceSymbol = offset;
117 for (auto iter = begin; iter != end; ++iter) {
118 auto value = *iter;
119 if (value > 0) {
120 auto& currentValue = map[sourceSymbol];
121 currentValue = internal::safeadd(currentValue, this->countSamples(value));
122 }
123 ++sourceSymbol;
124 }
125
126 // then extract key/value pairs
127 container.clear();
128 container.reserve(map.size());
129 std::for_each(map.begin(), map.end(), [&](const auto& pair) { container.emplace_back(pair.first, pair.second); });
130
131 // and build a OrderedSet from it
132 this->mContainer = container_type{container, 0, internal::OrderedSetState::unordered};
133 }
134 return *this;
135};
136
137template <typename source_T>
139{
140 using value_type = typename SparseHistogram<source_T>::value_type;
141
142 return std::count_if(histogram.begin(), histogram.end(), [](const auto& v) { return v.second != value_type{}; });
143}
144
145} // namespace o2::rans
146
147#endif /* INCLUDE_RANS_INTERNAL_CONTAINERS_SPARSEHISTOGRAM_H_ */
Abstract container class that defines and implements basic properties shared by histograms and lookup...
Operations that will be performed on a histogram.
common helper classes and functions
StringRef key
typename containerBase_type::const_iterator const_iterator
SparseHistogram & addFrequenciesImpl(freq_IT begin, freq_IT end, source_type offset)
SparseHistogram & addSamplesImpl(source_IT begin, source_IT end)
typename containerBase_type::const_pointer const_pointer
typename containerBase_type::container_type container_type
typename containerBase_type::reference reference
typename containerBase_type::value_type value_type
typename containerBase_type::const_reference const_reference
typename containerBase_type::size_type size_type
SparseHistogram & addSamplesImpl(gsl::span< const source_type > samples)
typename containerBase_type::source_type source_type
typename containerBase_type::difference_type difference_type
typename containerBase_type::pointer pointer
SparseHistogram(freq_IT begin, freq_IT end, source_type offset)
const_iterator begin() const noexcept
Definition Container.h:53
container_type release() &&noexcept
Definition Container.h:65
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:182
typename base_type::const_reference const_reference
Definition Container.h:184
typename base_type::const_pointer const_pointer
Definition Container.h:186
typename base_type::container_type container_type
Definition Container.h:180
typename base_type::value_type value_type
Definition Container.h:179
typename base_type::const_iterator const_iterator
Definition Container.h:187
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
return * this
size_t countNUsedAlphabetSymbols(const AdaptiveHistogram< source_T > &histogram)