Project
Loading...
Searching...
No Matches
SourceProxy.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_SOURCEPROXY_H_
17#define RANS_INTERNAL_TRANSFORM_SOURCEPROXY_H_
18
19#include <array>
20#include <cstdint>
21#include <cstring>
22#include <type_traits>
23#include <optional>
24#include <variant>
25
26#include <gsl/span>
27
29
30namespace o2::rans
31{
32
33template <typename source_T>
35{
36 public:
39 using const_pointer = const source_type*;
42
43 SourceCache() = default;
44
45 public:
46 template <typename IT>
48 {
49 const size_t size = std::distance(begin, end);
50 mBuffer.reserve(size);
51
52 for (size_t i = 0; i < size; ++i) {
53 auto value = begin[i];
54 mMin = std::min(mMin, value);
55 mMax = std::max(mMax, value);
56 mBuffer.push_back(value);
57 };
58 }
59
60 inline const_iterator begin() const noexcept { return mBuffer.data(); };
61 inline const_iterator end() const noexcept { return begin() + mBuffer.size(); };
62 inline size_t size() const noexcept { return mBuffer.size(); };
63 inline bool empty() const noexcept { return mBuffer.empty(); };
64 inline source_type getMin() const noexcept { return mMin; };
65 inline source_type getMax() const noexcept { return mMax; };
66 inline size_t getAlphabetRangeBits() const noexcept { return utils::getRangeBits(getMin(), getMax()); };
67
68 private:
69 std::vector<source_type> mBuffer;
70 source_type mMin{std::numeric_limits<source_type>::min()};
71 source_type mMax{std::numeric_limits<source_type>::max()};
72};
73
74template <typename IT>
76{
77 using iterator = IT;
78
79 public:
80 RangeWrapper() = default;
81 RangeWrapper(IT begin, IT end) : mBegin{begin}, mEnd{end} {};
82
83 iterator begin() const { return mBegin; };
84 iterator end() const { return mEnd; };
85
86 private:
87 iterator mBegin{};
88 iterator mEnd{};
89};
90
91template <typename IT>
93{
94 using iterator = IT;
95 using source_type = typename std::iterator_traits<iterator>::value_type;
96 using pointer = source_type*;
97 using const_pointer = const source_type*;
100
101 public:
102 template <typename F>
103 SourceProxy(IT begin, IT end, F functor)
104 {
105 if (functor(begin, end)) {
106 mProxy.template emplace<0>(begin, end);
107 mIsCached = true;
108 LOGP(debug, "Caching enabled");
109 } else {
110 mProxy.template emplace<1>(begin, end);
111 mIsCached = false;
112 LOGP(debug, "Caching disabled");
113 }
114 }
115
116 inline const_pointer beginCache() const { return std::get<0>(mProxy).begin(); };
117 inline const_pointer endCache() const { return std::get<0>(mProxy).end(); };
118 inline iterator beginIter() const { return std::get<1>(mProxy).begin(); };
119 inline iterator endIter() const { return std::get<1>(mProxy).end(); };
120 inline source_type getMin() const { return std::get<0>(mProxy).getMin(); };
121 inline source_type getMax() const { return std::get<0>(mProxy).getMax(); };
122 inline size_t getAlphabetRangeBits() const { return std::get<0>(mProxy).getAlphabetRangeBits(); };
123
124 bool isCached() const { return mIsCached; };
125
126 inline size_t size() const noexcept
127 {
128 if (std::holds_alternative<cache_type>(mProxy)) {
129 return std::get<0>(mProxy).size();
130 } else {
131 return std::get<1>(mProxy).size();
132 }
133 };
134
135 inline bool empty() const noexcept
136 {
137 if (std::holds_alternative<cache_type>(mProxy)) {
138 return std::get<0>(mProxy).empty();
139 } else {
140 return std::get<1>(mProxy).empty();
141 }
142 };
143
144 private:
145 std::variant<cache_type, range_type> mProxy{};
146 bool mIsCached{true};
147};
148
149} // namespace o2::rans
150
151#endif /* RANS_INTERNAL_TRANSFORM_SOURCEPROXY_H_ */
int32_t i
common helper classes and functions
std::ostringstream debug
uint32_t source_type
iterator begin() const
Definition SourceProxy.h:83
iterator end() const
Definition SourceProxy.h:84
RangeWrapper(IT begin, IT end)
Definition SourceProxy.h:81
const source_type * const_pointer
Definition SourceProxy.h:39
source_type * pointer
Definition SourceProxy.h:38
source_type getMax() const noexcept
Definition SourceProxy.h:65
SourceCache(IT begin, IT end)
Definition SourceProxy.h:47
size_t getAlphabetRangeBits() const noexcept
Definition SourceProxy.h:66
const_iterator begin() const noexcept
Definition SourceProxy.h:60
size_t size() const noexcept
Definition SourceProxy.h:62
source_type getMin() const noexcept
Definition SourceProxy.h:64
bool empty() const noexcept
Definition SourceProxy.h:63
const_iterator end() const noexcept
Definition SourceProxy.h:61
source_type getMax() const
iterator endIter() const
iterator beginIter() const
size_t getAlphabetRangeBits() const
SourceProxy(IT begin, IT end, F functor)
bool empty() const noexcept
source_type getMin() const
bool isCached() const
size_t size() const noexcept
const_pointer beginCache() const
const_pointer endCache() const
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLsizei const GLfloat * value
Definition glcorearb.h:819
constexpr uint32_t getRangeBits(T min, T max) noexcept
Definition utils.h:200