Project
Loading...
Searching...
No Matches
SparseSymbolTable.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_HASHSYMBOLTABLE_H_
18#define RANS_INTERNAL_CONTAINERS_HASHSYMBOLTABLE_H_
19
20#include <vector>
21#include <cstdint>
22#include <cmath>
23#include <fairlogger/Logger.h>
24
29
30namespace o2::rans
31{
32
33template <class source_T, class symbol_T>
34class SparseSymbolTable : public internal::HashContainer<source_T, symbol_T>
35{
37 friend base_type;
38
39 public:
47 using pointer = typename base_type::pointer;
50
51 SparseSymbolTable() = default;
52
53 template <typename container_T>
54 inline SparseSymbolTable(const RenormedHistogramConcept<container_T>& renormedHistogram);
55
56 [[nodiscard]] inline const_pointer lookupSafe(source_type sourceSymbol) const;
57
58 [[nodiscard]] inline const_pointer lookupUnsafe(source_type sourceSymbol) const { return &(this->mContainer.find(sourceSymbol)->second); };
59
60 [[nodiscard]] inline size_type size() const noexcept { return this->mContainer.size(); };
61
62 [[nodiscard]] inline bool hasEscapeSymbol() const noexcept { return this->getEscapeSymbol().getFrequency() > 0; };
63
64 [[nodiscard]] inline const_reference getEscapeSymbol() const noexcept { return this->mContainer.getNullElement(); };
65
66 [[nodiscard]] inline bool isEscapeSymbol(const_reference symbol) const noexcept { return symbol == this->getEscapeSymbol(); };
67
68 [[nodiscard]] inline bool isEscapeSymbol(source_type sourceSymbol) const noexcept { return this->mContainer.find(sourceSymbol) == this->mContainer.end(); };
69
70 [[nodiscard]] inline size_type getPrecision() const noexcept { return mSymbolTablePrecision; };
71
72 protected:
73 [[nodiscard]] inline bool isValidSymbol(const symbol_type& value) const noexcept { return !this->isEscapeSymbol(value); };
74
76};
77
78template <class source_T, class value_T>
79template <typename container_T>
81{
82 using namespace utils;
83 using namespace internal;
84 using count_type = typename value_T::value_type;
85
86 this->mSymbolTablePrecision = renormedHistogram.getRenormingBits();
87 auto nullElement = [&]() -> value_T {
88 const count_type symbolFrequency = renormedHistogram.getIncompressibleSymbolFrequency();
89 const count_type cumulatedFrequency = renormedHistogram.getNumSamples() - symbolFrequency;
90 return {symbolFrequency, cumulatedFrequency, this->getPrecision()};
91 }();
92
93 this->mContainer = container_type(nullElement);
94
95 const auto [trimmedBegin, trimmedEnd] = trim(renormedHistogram);
96
97 count_type cumulatedFrequency = 0;
98 forEachIndexValue(
99 renormedHistogram, trimmedBegin, trimmedEnd, [&, this](const source_type& sourceSymbol, const count_type& symbolFrequency) {
100 if (symbolFrequency) {
101 this->mContainer[sourceSymbol] = symbol_type{symbolFrequency, cumulatedFrequency, this->getPrecision()};
102 cumulatedFrequency += symbolFrequency;
103 }
104 });
105};
106
107template <class source_T, class value_T>
108[[nodiscard]] inline auto SparseSymbolTable<source_T, value_T>::lookupSafe(source_type sourceSymbol) const -> const_pointer
109{
110 auto iter = this->mContainer.find(sourceSymbol);
111 if (iter == this->mContainer.end()) {
112 return nullptr;
113 } else {
114 return &iter->second;
115 }
116};
117
118template <typename source_T, typename symbol_T>
119std::pair<source_T, source_T> getMinMax(const SparseSymbolTable<source_T, symbol_T>& symbolTable)
120{
121 return internal::getMinMax(symbolTable, symbolTable.getEscapeSymbol());
122};
123
124template <typename source_T, typename symbol_T>
126{
127 return std::count_if(symbolTable.begin(), symbolTable.end(), [&symbolTable](const auto& v) { return !symbolTable.isEscapeSymbol(v.second); });
128}
129
130} // namespace o2::rans
131
132#endif /* RANS_INTERNAL_CONTAINERS_HASHSYMBOLTABLE_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
value_type getIncompressibleSymbolFrequency() const noexcept
size_t getRenormingBits() const noexcept
bool isEscapeSymbol(source_type sourceSymbol) const noexcept
bool isValidSymbol(const symbol_type &value) const noexcept
const_pointer lookupUnsafe(source_type sourceSymbol) const
size_type getPrecision() const noexcept
typename base_type::source_type source_type
typename base_type::pointer pointer
typename base_type::value_type symbol_type
typename base_type::const_reference const_reference
bool isEscapeSymbol(const_reference symbol) const noexcept
typename base_type::const_pointer const_pointer
const_reference getEscapeSymbol() const noexcept
typename base_type::difference_type difference_type
size_type size() const noexcept
bool hasEscapeSymbol() const noexcept
typename base_type::container_type container_type
const_pointer lookupSafe(source_type sourceSymbol) const
typename base_type::const_iterator const_iterator
typename base_type::reference reference
typename base_type::size_type size_type
const_iterator begin() const noexcept
Definition Container.h:53
const_iterator end() const noexcept
Definition Container.h:55
typename base_type::const_pointer const_pointer
Definition Container.h:154
typename base_type::const_reference const_reference
Definition Container.h:152
typename base_type::difference_type difference_type
Definition Container.h:150
typename base_type::container_type container_type
Definition Container.h:148
typename base_type::const_iterator const_iterator
Definition Container.h:155
const GLdouble * v
Definition glcorearb.h:832
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
HistogramView< Hist_IT > trim(const HistogramView< Hist_IT > &buffer)
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.
manipulation of types at compile time