Project
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1// Copyright 2019-2023 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 RANS_INTERNAL_TRANSFORM_ALGORITHM_H_
17#define RANS_INTERNAL_TRANSFORM_ALGORITHM_H_
18
19#include <array>
20#include <cstdint>
21#include <cstring>
22#include <type_traits>
23
27
28namespace o2::rans::internal
29{
30
31template <class container_T>
32class SparseVectorIterator;
33
34template <class IT, std::enable_if_t<isPair_v<typename std::iterator_traits<IT>::value_type>, bool> = true>
35inline auto getValue(IT iter) -> typename std::iterator_traits<IT>::value_type::second_type
36{
37 return iter->second;
38}
39
40template <typename source_T, typename value_T>
41inline auto getValue(const std::pair<source_T, value_T>& pair) -> value_T
42{
43 return pair.second;
44}
45
46template <class IT, std::enable_if_t<std::is_pointer_v<std::remove_reference_t<IT>>, bool> = true>
47inline auto getValue(IT iter) -> typename std::iterator_traits<IT>::value_type
48{
49 return *iter;
50}
51
52template <class IT, std::enable_if_t<isPair_v<typename std::iterator_traits<IT>::value_type>, bool> = true>
53inline void setValue(IT iter, const typename std::iterator_traits<IT>::value_type::second_type& value)
54{
55 return iter->second = value;
56}
57
58template <class IT, std::enable_if_t<std::is_pointer_v<std::remove_reference_t<IT>>, bool> = true>
59inline void setValue(IT iter, std::add_lvalue_reference_t<std::add_const_t<typename std::iterator_traits<IT>::value_type>> value)
60{
61 *iter = value;
62}
63
64template <typename container_T, std::enable_if_t<isDenseContainer_v<container_T>, bool> = true>
65inline constexpr auto getIndex(const container_T& container, typename container_T::const_iterator iter) -> typename container_T::source_type
66{
67 return container.getOffset() + std::distance(container.begin(), iter);
68};
69
70template <typename container_T, std::enable_if_t<isAdaptiveContainer_v<container_T> ||
71 isHashContainer_v<container_T> ||
72 isSetContainer_v<container_T>,
73 bool> = true>
74inline constexpr auto getIndex(const container_T& container, typename container_T::const_iterator iter) -> typename container_T::source_type
75{
76 return iter->first;
77};
78
79template <typename container_T, class F>
80inline void forEachIndexValue(const container_T& container, typename container_T::const_iterator begin, typename container_T::const_iterator end, F functor)
81{
82 algorithmImpl::forEachIndexValue(container, begin, end, functor);
83};
84
85template <typename container_T, class F, std::enable_if_t<isStorageContainer_v<container_T>, bool> = true>
86inline void forEachIndexValue(container_T& container, typename container_T::iterator begin, typename container_T::iterator end, F functor)
87{
88 algorithmImpl::forEachIndexValue(container, begin, end, functor);
89};
90
91template <typename container_T, class F>
92inline void forEachIndexValue(const container_T& container, F functor)
93{
94 forEachIndexValue(container, container.begin(), container.end(), functor);
95};
96
97template <typename container_T, class F>
98inline void forEachIndexValue(container_T& container, F functor)
99{
100 forEachIndexValue(container, container.begin(), container.end(), functor);
101};
102
103template <typename container_T>
104inline auto trim(typename container_T::iterator begin, typename container_T::iterator end, typename container_T::const_reference zeroElem = {})
105 -> std::pair<typename container_T::iterator, typename container_T::iterator>
106{
107 return algorithmImpl::trim<container_T, typename container_T::iterator>(begin, end, zeroElem);
108};
109
110template <typename container_T>
111inline auto trim(typename container_T::const_iterator begin, typename container_T::const_iterator end, typename container_T::const_reference zeroElem = {})
112 -> std::pair<typename container_T::const_iterator, typename container_T::const_iterator>
113{
114 return algorithmImpl::trim<container_T, typename container_T::const_iterator>(begin, end, zeroElem);
115}
116
117template <typename container_T, std::enable_if_t<isStorageContainer_v<container_T>, bool> = true>
118inline decltype(auto) trim(container_T& container, const typename container_T::value_type& zeroElem = {})
119{
120 return algorithmImpl::trim<container_T, typename container_T::iterator>(container.begin(), container.end(), zeroElem);
121};
122
123template <typename container_T, std::enable_if_t<isContainer_v<container_T>, bool> = true>
124inline decltype(auto) trim(const container_T& container, const typename container_T::value_type& zeroElem = {})
125{
126 return algorithmImpl::trim<container_T, typename container_T::const_iterator>(container.begin(), container.end(), zeroElem);
127};
128
129template <class container_T,
130 std::enable_if_t<isDenseContainer_v<container_T> ||
131 isAdaptiveContainer_v<container_T> ||
132 isSetContainer_v<container_T>,
133 bool> = true>
134auto getMinMax(const container_T& container,
135 typename container_T::const_iterator begin,
136 typename container_T::const_iterator end,
137 typename container_T::const_reference zeroElem = {})
138 -> std::pair<typename container_T::source_type, typename container_T::source_type>
139{
140 auto [trimmedBegin, trimmedEnd] = trim<container_T>(begin, end, zeroElem);
141
142 if (trimmedBegin != trimmedEnd) {
143 const auto min = getIndex(container, trimmedBegin);
144 const auto max = getIndex(container, --trimmedEnd);
145 assert(max >= min);
146 return {min, max};
147 }
148 return {container.getOffset(), container.getOffset()};
149};
150
151template <typename container_T, std::enable_if_t<isHashContainer_v<container_T>, bool> = true>
152auto getMinMax(const container_T& container,
153 typename container_T::const_iterator begin,
154 typename container_T::const_iterator end,
155 typename container_T::const_reference zeroElem = {})
156 -> std::pair<typename container_T::source_type, typename container_T::source_type>
157{
158 using iterator_type = typename container_T::const_iterator;
159 using value_type = typename std::iterator_traits<iterator_type>::value_type::second_type;
160 using return_type = std::pair<value_type, value_type>;
161
162 bool empty = container.empty();
163
164 if constexpr (isRenormedHistogram_v<container_T>) {
165 empty = container.getNumSamples() == container.getIncompressibleSymbolFrequency();
166 }
167
168 if (empty) {
169 return return_type{container.getOffset(), container.getOffset()};
170 };
171
172 const auto [minIter, maxIter] = std::minmax_element(begin, end, [](const auto& a, const auto& b) { return a.first < b.first; });
173 return return_type{minIter->first, maxIter->first};
174};
175
176template <typename container_T>
177auto getMinMax(const container_T& container, typename container_T::const_reference zeroElem = {})
178 -> std::pair<typename container_T::source_type, typename container_T::source_type>
179{
180 return getMinMax(container, container.begin(), container.end(), zeroElem);
181};
182
183} // namespace o2::rans::internal
184
185#endif /* RANS_INTERNAL_TRANSFORM_ALGORITHM_H_ */
common helper classes and functions
GLuint GLuint end
Definition glcorearb.h:469
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
void forEachIndexValue(container_T &&container, IT begin, IT end, F functor)
auto trim(typename container_T::iterator begin, typename container_T::iterator end, typename container_T::const_reference zeroElem={}) -> std::pair< typename container_T::iterator, typename container_T::iterator >
Definition algorithm.h:104
auto getMinMax(const container_T &container, typename container_T::const_iterator begin, typename container_T::const_iterator end, typename container_T::const_reference zeroElem={}) -> std::pair< typename container_T::source_type, typename container_T::source_type >
Definition algorithm.h:134
void setValue(IT iter, const typename std::iterator_traits< IT >::value_type::second_type &value)
Definition algorithm.h:53
auto getValue(IT iter) -> typename std::iterator_traits< IT >::value_type::second_type
Definition algorithm.h:35
void forEachIndexValue(const container_T &container, typename container_T::const_iterator begin, typename container_T::const_iterator end, F functor)
Definition algorithm.h:80
constexpr auto getIndex(const container_T &container, typename container_T::const_iterator iter) -> typename container_T::source_type
Definition algorithm.h:65
void empty(int)
constexpr size_t min
constexpr size_t max