Project
Loading...
Searching...
No Matches
CodingModelDispatcher.h
Go to the documentation of this file.
1// Copyright 2019-2020 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
12/* Local Variables: */
13/* mode: c++ */
14/* End: */
15
16#ifndef CODINGMODELDISPATCHER_H
17#define CODINGMODELDISPATCHER_H
18
23
24#include "mpl_tools.h"
25#include "runtime_container.h"
26#include <iostream>
27#include <fstream>
28#include <boost/type.hpp>
29#include <boost/mpl/for_each.hpp>
30
31using namespace gNeric;
32
33namespace o2
34{
35namespace data_compression
36{
37
50template <typename ModelDefinition>
52{
53 public:
54 CodingModelDispatcher() : mPosition(0), mContainer() {}
56
58
59 // make_mpl_vector traits makes sure that an mpl sequence is used further on
60 // if the original type is not a sequence it is wrapped into an mpl vector with
61 // the original type as the only element
63
64 // the runtime container type is the heart of the dispatcher to runtime objects
65 // of the sequence of data types which define the probability model
67
68 using code_type = typename container_type::wrapped_type::code_type;
69
71 static int getNumberOfModels() { return boost::mpl::size<definition_type>::value; }
72
74 container_type& operator*() { return mContainer; }
75
77 template <typename ValueType, typename WeightType>
79 {
80 public:
81 addWeightFctr(ValueType _v, WeightType _w) : value(_v), weight(_w) {}
83
84 using return_type = bool;
85
86 template <typename T>
88 {
89 // the addWeight function belongs to the probability model as base
90 // of the specific model; funcions of the base can be accessed by
91 // static casting. This avoids an extra level of function calls.
92 return static_cast<typename T::wrapped_type::base_type&>(*stage).addWeight(value, weight);
93 }
94
95 private:
96 ValueType value;
97 WeightType weight;
98 };
99
106 template <typename ValueType, typename WeightType>
107 bool addWeight(ValueType v, WeightType w, bool switchToNextModel = true)
108 {
109 bool result = mContainer.apply(mPosition, addWeightFctr<ValueType, WeightType>(v, w));
110 if (switchToNextModel && ++mPosition >= getNumberOfModels()) {
111 mPosition = 0;
112 }
113 return result;
114 }
115
120 {
121 public:
122 initFctr(container_type& container) : mContainer(container) {}
124
126
127 template <typename T>
128 return_type operator()(boost::type<T>)
129 {
130 T& stage = static_cast<T&>(mContainer);
131 return (*stage).init();
132 }
133
134 private:
135 container_type& mContainer;
136 };
137
141 int init()
142 {
143 mPosition = 0;
144 boost::mpl::for_each<typename container_type::types, boost::type<boost::mpl::_>>(initFctr(mContainer));
145 return 0;
146 }
147
153 {
154 public:
155 generateFctr(container_type& container) : mContainer(container) {}
157
159
160 template <typename T>
161 return_type operator()(boost::type<T>)
162 {
163 T& stage = static_cast<T&>(mContainer);
164 return (*stage).GenerateHuffmanTree();
165 }
166
167 private:
168 container_type& mContainer;
169 };
170
175 {
176 boost::mpl::for_each<typename container_type::types, boost::type<boost::mpl::_>>(generateFctr(mContainer));
177 return 0;
178 }
179
181 template <typename CodeType, typename ValueType>
183 {
184 public:
185 encodeFctr(ValueType _v, CodeType& _code, uint16_t& _codeLength) : code(_code), value(_v), codeLength(_codeLength)
186 {
187 }
189
190 using return_type = bool;
191
192 template <typename T>
194 {
195 code = (*stage).Encode(value, codeLength);
196 return true;
197 }
198
199 private:
200 CodeType& code;
201 ValueType value;
202 uint16_t& codeLength;
203 };
204
211 template <typename CodeType, typename ValueType>
212 bool encode(ValueType v, CodeType& code, uint16_t& codeLength, bool switchToNextModel = true)
213 {
214 bool result = mContainer.apply(mPosition, encodeFctr<CodeType, ValueType>(v, code, codeLength));
215 if (switchToNextModel && ++mPosition >= getNumberOfModels()) {
216 mPosition = 0;
217 }
218 return result;
219 }
220
222 template <typename CodeType, typename ValueType>
224 {
225 public:
226 decodeFctr(ValueType& _v, CodeType _code, uint16_t& _codeLength) : code(_code), value(_v), codeLength(_codeLength)
227 {
228 }
230
231 using return_type = bool;
232
233 template <typename T>
235 {
236 value = (*stage).Decode(code, codeLength);
237 return true;
238 }
239
240 private:
241 CodeType code;
242 ValueType& value;
243 uint16_t& codeLength;
244 };
245
254 template <typename ValueType, typename CodeType>
255 bool decode(ValueType& v, CodeType code, uint16_t& codeLength, bool switchToNextModel = true)
256 {
257 bool result = mContainer.apply(mPosition, decodeFctr<CodeType, ValueType>(v, code, codeLength));
258 if (switchToNextModel && ++mPosition >= getNumberOfModels()) {
259 mPosition = 0;
260 }
261 return result;
262 }
263
265 {
266 public:
267 using return_type = bool;
268 template <typename T>
270 {
271 return T::wrapped_type::orderMSB;
272 }
273 };
274
278 bool getCodingDirection() { return mContainer.apply(mPosition, getCodingDirectionFctr()); }
279
282 {
283 public:
284 writeFctr(std::ostream& out, container_type& container) : mOut(out), mContainer(container) {}
286
287 using return_type = std::ostream&;
288
289 template <typename T>
290 return_type operator()(boost::type<T>)
291 {
292 T& stage = static_cast<T&>(mContainer);
293 if (T::level::value > 0) {
294 mOut << std::endl; // blank line between dumps
295 }
296 mOut << T::level::value << " " << (*stage).getName() << std::endl;
297 (*stage).write(mOut);
298 return mOut;
299 }
300
301 private:
302 std::ostream& mOut;
303 container_type& mContainer;
304 };
305
311 int write(const char* filename = nullptr)
312 {
313 std::ofstream ofile(filename);
314 boost::mpl::for_each<typename container_type::types, boost::type<boost::mpl::_>>(
315 writeFctr(ofile.good() ? ofile : std::cout, mContainer));
316 ofile.close();
317 return 0;
318 }
319
322 {
323 public:
324 readFctr(std::istream& in, container_type& container) : mIn(in), mContainer(container) {}
326
327 using return_type = bool;
328
329 template <typename T>
330 return_type operator()(boost::type<T>)
331 {
332 T& stage = static_cast<T&>(mContainer);
333 std::string level, name, remaining;
334 mIn >> level;
335 mIn >> name;
336 if (!mIn) {
337 return false;
338 }
339 if (std::stoi(level) != T::level::value || name.compare((*stage).getName())) {
340 std::cerr << "Format error: expecting level '" << T::level::value << "' and name '" << (*stage).getName()
341 << "', got: " << level << " " << name << std::endl;
342 }
343 std::cout << "reading configuration for model " << name << std::endl;
344 std::getline(mIn, remaining); // flush the current line
345 (*stage).read(mIn);
346 return true;
347 }
348
349 private:
350 std::istream& mIn;
351 container_type& mContainer;
352 };
353
359 int read(const char* filename)
360 {
361 std::ifstream input(filename);
362 if (!input.good()) {
363 return -1;
364 }
365 // TODO: probably need mpl fold here to propagate the return value
366 boost::mpl::for_each<typename container_type::types, boost::type<boost::mpl::_>>(readFctr(input, mContainer));
367 return 0;
368 }
369
370 private:
372 int mPosition;
374 container_type mContainer;
375};
376
377} // namespace data_compression
378} // namespace o2
379
380#endif
functor to add weight to probability model at runtime container level
Functor to execute decoding on runtime container level.
decodeFctr(ValueType &_v, CodeType _code, uint16_t &_codeLength)
functor to execute encoding on runtime container level
encodeFctr(ValueType _v, CodeType &_code, uint16_t &_codeLength)
readFctr(std::istream &in, container_type &container)
writeFctr(std::ostream &out, container_type &container)
Runtime dispatcher interface for probability model definitions.
container_type & operator*()
return highest stage of runtime container
typename mpl::make_mpl_vector< ModelDefinition >::type definition_type
bool addWeight(ValueType v, WeightType w, bool switchToNextModel=true)
static int getNumberOfModels()
get the number of models in the definition
typename create_rtc< definition_type, RuntimeContainer<> >::type container_type
typename container_type::wrapped_type::code_type code_type
bool encode(ValueType v, CodeType &code, uint16_t &codeLength, bool switchToNextModel=true)
bool decode(ValueType &v, CodeType code, uint16_t &codeLength, bool switchToNextModel=true)
typename VectorTraits< T, typename boost::mpl::begin< T >::type >::type type
the tarits type, always a sequence
Definition mpl_tools.h:84
GLuint64EXT * result
Definition glcorearb.h:5662
const GLdouble * v
Definition glcorearb.h:832
GLuint const GLchar * name
Definition glcorearb.h:781
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLint level
Definition glcorearb.h:275
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string filename()
A general runtime container for a compile time sequence This file is part of https://github....
create the runtime container type The runtime container type is build from a list of data types,...