Project
Loading...
Searching...
No Matches
AdaptiveSymbolTable.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
16
17#ifndef RANS_INTERNAL_CONTAINERS_ADAPTIVESYMBOLTABLE_H_
18#define RANS_INTERNAL_CONTAINERS_ADAPTIVESYMBOLTABLE_H_
19
20#include <vector>
21#include <cstdint>
22#include <cmath>
23#include <fairlogger/Logger.h>
24
28
29namespace o2::rans
30{
31
32template <class source_T, class symbol_T>
33class AdaptiveSymbolTable : public internal::SparseVectorContainer<source_T, symbol_T>
34{
36 friend base_type;
37
38 public:
46 using pointer = typename base_type::pointer;
49
51
52 template <typename container_T>
53 explicit AdaptiveSymbolTable(const RenormedHistogramConcept<container_T>& renormedHistogram);
54
55 [[nodiscard]] inline const_reference operator[](source_type sourceSymbol) const noexcept
56 {
57 return this->mContainer.at(sourceSymbol);
58 };
59
60 [[nodiscard]] inline const_pointer lookupSafe(source_type sourceSymbol) const
61 {
62 return &this->mContainer.at(sourceSymbol);
63 };
64
65 [[nodiscard]] inline const_pointer lookupUnsafe(source_type sourceSymbol) const
66 {
67 return &this->mContainer[sourceSymbol];
68 };
69
70 [[nodiscard]] inline size_type size() const noexcept { return mSize; };
71
72 [[nodiscard]] inline bool hasEscapeSymbol() const noexcept { return mEscapeSymbol.getFrequency() > 0; };
73
74 [[nodiscard]] inline const_reference getEscapeSymbol() const noexcept { return mEscapeSymbol; };
75
76 [[nodiscard]] inline bool isEscapeSymbol(const_reference symbol) const noexcept { return symbol == mEscapeSymbol; };
77
78 [[nodiscard]] inline bool isEscapeSymbol(source_type sourceSymbol) const noexcept { return this->operator[](sourceSymbol) == mEscapeSymbol; };
79
80 [[nodiscard]] inline size_type getPrecision() const noexcept { return mSymbolTablePrecision; };
81
82 protected:
83 [[nodiscard]] inline bool isValidSymbol(const symbol_type& value) const noexcept
84 {
85 return !this->isEscapeSymbol(value);
86 };
87
91};
92
93template <class source_T, class value_T>
94template <typename container_T>
96{
97 using namespace utils;
98 using namespace internal;
99 using count_type = typename value_T::value_type;
100
101 this->mSymbolTablePrecision = renormedHistogram.getRenormingBits();
102 this->mEscapeSymbol = [&]() -> value_T {
103 const count_type symbolFrequency = renormedHistogram.getIncompressibleSymbolFrequency();
104 const count_type cumulatedFrequency = renormedHistogram.getNumSamples() - symbolFrequency;
105 return {symbolFrequency, cumulatedFrequency, this->getPrecision()};
106 }();
107
108 this->mContainer = container_type{mEscapeSymbol};
109
110 count_type cumulatedFrequency = 0;
111 forEachIndexValue(renormedHistogram, [&, this](const source_type& sourceSymbol, const count_type& symbolFrequency) {
112 if (symbolFrequency) {
113 this->mContainer[sourceSymbol] = symbol_type{symbolFrequency, cumulatedFrequency, this->getPrecision()};
114 cumulatedFrequency += symbolFrequency;
115 }
116 });
117 mSize = this->mContainer.size();
118};
119
120template <typename source_T, typename symbol_T>
121std::pair<source_T, source_T> getMinMax(const AdaptiveSymbolTable<source_T, symbol_T>& symbolTable)
122{
123 return internal::getMinMax(symbolTable, symbolTable.getEscapeSymbol());
124};
125
126template <typename source_T, typename symbol_T>
128{
129 using container_type = typename AdaptiveSymbolTable<source_T, symbol_T>::container_type;
130
131 return std::count_if(histogram.begin(), histogram.end(),
132 [&](const auto& value) {
133 return !histogram.isEscapeSymbol(internal::getValue(value));
134 });
135}
136
137} // namespace o2::rans
138
139#endif /* RANS_INTERNAL_CONTAINERS_ADAPTIVESYMBOLTABLE_H_ */
Abstract container class that defines and implements basic properties shared by histograms and lookup...
Histogram renormed to sum of frequencies being 2^P for use in fast rans coding.
helper functionalities useful for packing operations
typename base_type::const_reference const_reference
typename base_type::value_type symbol_type
size_type getPrecision() const noexcept
typename base_type::size_type size_type
typename base_type::source_type source_type
typename base_type::pointer pointer
const_pointer lookupUnsafe(source_type sourceSymbol) const
typename base_type::container_type container_type
const_reference getEscapeSymbol() const noexcept
typename base_type::const_iterator const_iterator
typename base_type::reference reference
bool isEscapeSymbol(const_reference symbol) const noexcept
const_pointer lookupSafe(source_type sourceSymbol) const
bool isEscapeSymbol(source_type sourceSymbol) const noexcept
size_type size() const noexcept
bool isValidSymbol(const symbol_type &value) const noexcept
bool hasEscapeSymbol() const noexcept
typename base_type::difference_type difference_type
const_reference operator[](source_type sourceSymbol) const noexcept
typename base_type::const_pointer const_pointer
value_type getIncompressibleSymbolFrequency() const noexcept
size_t getRenormingBits() const noexcept
const_iterator begin() const noexcept
Definition Container.h:53
const_iterator end() const noexcept
Definition Container.h:55
GLsizei const GLfloat * value
Definition glcorearb.h:819
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
size_t countNUsedAlphabetSymbols(const AdaptiveHistogram< source_T > &histogram)
std::pair< source_T, source_T > getMinMax(const AdaptiveSymbolTable< source_T, symbol_T > &symbolTable)
Common utility functions.