Project
Loading...
Searching...
No Matches
Packer.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 ALICEO2_PACKER_H_
17#define ALICEO2_PACKER_H_
18
19#ifndef __CLING__
20#include "rANS/pack.h"
21#include "rANS/metrics.h"
22#endif
23
24namespace o2::ctf::internal
25{
26
27template <typename source_T>
28class Packer
29{
30 public:
32
33 Packer() = default;
34#ifndef __CLING__
35 explicit Packer(rans::Metrics<source_type>& metrics) : mOffset{metrics.getDatasetProperties().min},
36 mPackingWidth{metrics.getDatasetProperties().alphabetRangeBits} {};
37#endif
38
39 template <typename source_IT>
40 Packer(source_IT srcBegin, source_IT srcEnd);
41
42 [[nodiscard]] inline source_type getOffset() const noexcept { return mOffset; };
43
44 [[nodiscard]] inline size_t getPackingWidth() const noexcept { return mPackingWidth; };
45
46 template <typename buffer_T>
47 [[nodiscard]] size_t getPackingBufferSize(size_t messageLength) const noexcept;
48
49 template <typename source_IT, typename dst_T>
50 [[nodiscard]] dst_T* pack(source_IT srcBegin, source_IT srcEnd, dst_T* dstBegin, dst_T* dstEnd) const;
51
52 template <typename dst_T>
53 [[nodiscard]] dst_T* pack(const source_T* __restrict srcBegin, size_t extent, dst_T* dstBegin, dst_T* dstEnd) const;
54
55 private:
56 source_type mOffset{};
57 size_t mPackingWidth{};
58};
59
60template <typename source_T>
61template <typename source_IT>
62Packer<source_T>::Packer(source_IT srcBegin, source_IT srcEnd)
63{
64#ifndef __CLING__
65 static_assert(rans::utils::isCompatibleIter_v<source_type, source_IT>);
66 if (srcBegin != srcEnd) {
67
68 const auto [min, max] = [&]() {
69 if constexpr (std::is_pointer_v<source_IT>) {
70 return rans::utils::minmax(gsl::span<const source_type>(srcBegin, srcEnd));
71 } else {
72 const auto [minIter, maxIter] = std::minmax_element(srcBegin, srcEnd);
73 return std::make_pair<source_type>(*minIter, *maxIter);
74 }
75 }();
76
77 mOffset = min;
78 mPackingWidth = rans::utils::getRangeBits(min, max);
79 }
80#endif
81};
82
83template <typename source_T>
84template <typename buffer_T>
85[[nodiscard]] inline size_t Packer<source_T>::getPackingBufferSize(size_t messageLength) const noexcept
86{
87#ifndef __CLING__
88 return rans::computePackingBufferSize<buffer_T>(messageLength, mPackingWidth);
89#else
90 return 0;
91#endif
92};
93
94template <typename source_T>
95template <typename dst_T>
96[[nodiscard]] inline dst_T* Packer<source_T>::pack(const source_T* __restrict srcBegin, size_t extent, dst_T* dstBegin, dst_T* dstEnd) const
97{
98 return pack(srcBegin, srcBegin + extent, dstBegin, dstEnd);
99}
100
101template <typename source_T>
102template <typename source_IT, typename dst_T>
103[[nodiscard]] dst_T* Packer<source_T>::pack(source_IT srcBegin, source_IT srcEnd, dst_T* dstBegin, dst_T* dstEnd) const
104{
105 static_assert(std::is_same_v<source_T, typename std::iterator_traits<source_IT>::value_type>);
106 size_t extent = std::distance(srcBegin, srcEnd);
107
108 if (extent == 0) {
109 return dstBegin;
110 }
111#ifndef __CLING__
112 rans::BitPtr packEnd = rans::pack(srcBegin, extent, dstBegin, mPackingWidth, mOffset);
113 auto* end = packEnd.toPtr<dst_T>();
114 ++end; // one past end iterator;
116 return end;
117#else
118 return nullptr;
119#endif
120};
121
122} // namespace o2::ctf::internal
123
124#endif /* ALICEO2_PACKER_H_ */
source_type getOffset() const noexcept
Definition Packer.h:42
dst_T * pack(const source_T *__restrict srcBegin, size_t extent, dst_T *dstBegin, dst_T *dstEnd) const
Definition Packer.h:96
Packer(source_IT srcBegin, source_IT srcEnd)
Definition Packer.h:62
size_t getPackingWidth() const noexcept
Definition Packer.h:44
size_t getPackingBufferSize(size_t messageLength) const noexcept
Definition Packer.h:85
Packer(rans::Metrics< source_type > &metrics)
Definition Packer.h:35
dst_T * pack(source_IT srcBegin, source_IT srcEnd, dst_T *dstBegin, dst_T *dstEnd) const
Definition Packer.h:103
constexpr T * toPtr() const noexcept
Definition BitPtr.h:58
GLuint GLuint end
Definition glcorearb.h:469
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
Definition glcorearb.h:5500
constexpr uint32_t getRangeBits(T min, T max) noexcept
Definition utils.h:200
void checkBounds(IT iteratorPosition, IT upperBound)
Definition utils.h:244
std::pair< source_T, source_T > minmax(gsl::span< const source_T > range)
constexpr BitPtr pack(const input_T *__restrict inputBegin, size_t extent, output_T *__restrict outputBegin, size_t packingWidth, input_T offset=static_cast< input_T >(0))
Definition pack.h:196
constexpr size_t min
constexpr size_t max