forked from mcoquet642/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.h
More file actions
122 lines (96 loc) · 4.1 KB
/
Encoder.h
File metadata and controls
122 lines (96 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file Encoder.h
/// @author Michael Lettrich
/// @since 2020-04-06
/// @brief Encoder - code symbol into a rANS encoded state
#ifndef RANS_ENCODER_H
#define RANS_ENCODER_H
#include <memory>
#include <algorithm>
#include <iomanip>
#include <fairlogger/Logger.h>
#include <stdexcept>
#include "rANS/internal/EncoderBase.h"
#include "rANS/internal/Encoder.h"
#include "rANS/internal/EncoderSymbol.h"
#include "rANS/internal/helper.h"
#include "rANS/internal/SymbolTable.h"
#include "rANS/FrequencyTable.h"
namespace o2
{
namespace rans
{
template <typename coder_T, typename stream_T, typename source_T>
class Encoder : public internal::EncoderBase<coder_T, stream_T, source_T>
{
public:
//inherit constructors;
using internal::EncoderBase<coder_T, stream_T, source_T>::EncoderBase;
template <typename stream_IT, typename source_IT, std::enable_if_t<internal::isCompatibleIter_v<source_T, source_IT>, bool> = true>
const stream_IT process(source_IT inputBegin, source_IT inputEnd, stream_IT outputBegin) const;
private:
using ransCoder_t = typename internal::EncoderBase<coder_T, stream_T, source_T>::ransCoder_t;
};
template <typename coder_T, typename stream_T, typename source_T>
template <typename stream_IT, typename source_IT, std::enable_if_t<internal::isCompatibleIter_v<source_T, source_IT>, bool>>
const stream_IT Encoder<coder_T, stream_T, source_T>::process(source_IT inputBegin, source_IT inputEnd, stream_IT outputBegin) const
{
using namespace internal;
LOG(trace) << "start encoding";
RANSTimer t;
t.start();
if (inputBegin == inputEnd) {
LOG(warning) << "passed empty message to encoder, skip encoding";
return outputBegin;
}
ransCoder_t rans0{this->mSymbolTablePrecission};
ransCoder_t rans1{this->mSymbolTablePrecission};
stream_IT outputIter = outputBegin;
source_IT inputIT = inputEnd;
const auto inputBufferSize = std::distance(inputBegin, inputEnd);
auto encode = [this](source_IT symbolIter, stream_IT outputIter, ransCoder_t& coder) {
const source_T symbol = *symbolIter;
const auto& encoderSymbol = (this->mSymbolTable)[symbol];
return coder.putSymbol(outputIter, encoderSymbol);
};
// odd number of bytes?
if (inputBufferSize & 1) {
outputIter = encode(--inputIT, outputIter, rans0);
}
while (inputIT != inputBegin) { // NB: working in reverse!
outputIter = encode(--inputIT, outputIter, rans1);
outputIter = encode(--inputIT, outputIter, rans0);
}
outputIter = rans1.flush(outputIter);
outputIter = rans0.flush(outputIter);
// first iterator past the range so that sizes, distances and iterators work correctly.
++outputIter;
t.stop();
LOG(debug1) << "Encoder::" << __func__ << " {ProcessedBytes: " << inputBufferSize * sizeof(source_T) << ","
<< " inclusiveTimeMS: " << t.getDurationMS() << ","
<< " BandwidthMiBPS: " << std::fixed << std::setprecision(2) << (inputBufferSize * sizeof(source_T) * 1.0) / (t.getDurationS() * 1.0 * (1 << 20)) << "}";
// advanced diagnostics for debug builds
#if !defined(NDEBUG)
const auto inputBufferSizeB = inputBufferSize * sizeof(source_T);
LOG(debug2) << "EncoderProperties: {"
<< "sourceTypeB: " << sizeof(source_T) << ", "
<< "streamTypeB: " << sizeof(stream_T) << ", "
<< "coderTypeB: " << sizeof(coder_T) << ", "
<< "probabilityBits: " << this->mSymbolTablePrecission << ", "
<< "inputBufferSizeB: " << inputBufferSizeB << "}";
#endif
LOG(trace) << "done encoding";
return outputIter;
};
} // namespace rans
} // namespace o2
#endif /* RANS_ENCODER_H */