Newer
Older
/*
* Copyright (c) 2015-2016, Luca Fulchir<luca@fulchir.it>, All rights reserved.
*
* This file is part of "libRaptorQ".
*
* libRaptorQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libRaptorQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and a copy of the GNU Lesser General Public License
* along with libRaptorQ. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RaptorQ/v1/common.hpp"
#include <cmath>
// NOTE: this file is included from RFC.hpp and CPP_RFC_API.hpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Yes, another template parameter would keep us away from macros.
// But I prefer to kep the parameters the same as for the classes.
// So we keep the macros. (NOTE: this header requires C++98 compatibility)
#ifdef RQ_HEADER_ONLY
#include "RaptorQ/v1/RFC.hpp"
namespace RFC6330__v1 {
namespace Impl {
template<typename Rnd_It, typename Fwd_It>
class Encoder;
template<typename In_It, typename Fwd_It>
class Decoder;
}
}
#define RQ_ENC_T Impl::Encoder<Rnd_It, Fwd_It>
#define RQ_DEC_T Impl::Decoder<In_It, Fwd_It>
#else
#include "RaptorQ/v1/wrapper/CPP_RFC_API_void.hpp"
#define RQ_ENC_T Impl::Encoder_void
#define RQ_DEC_T Impl::Decoder_void
#endif
///////////////////
//// Iterators
///////////////////
namespace RFC6330__v1 {
namespace It {
namespace Encoder {
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_API Symbol
{
public:
Symbol (RQ_ENC_T *const enc, const uint32_t id)
: _enc (enc), _id (id) {}
uint64_t operator() (Fwd_It &start, const Fwd_It end)
{
if (_enc == nullptr)
return 0;
return _enc->encode (start, end, _id);
}
uint32_t id() const
{ return _id; }
uint8_t block() const
{ return static_cast<uint8_t> (_id >> 24); }
uint32_t esi() const
{
uint32_t rev_mask = static_cast<uint32_t> (0xFF) << 24;
return static_cast<uint8_t> (_id & ~rev_mask);
}
private:
RQ_ENC_T *const _enc;
const uint32_t _id;
};
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_API Symbol_Iterator :
public std::iterator<std::input_iterator_tag, Symbol<Rnd_It, Fwd_It>>
{
public:
Symbol_Iterator (RQ_ENC_T *const enc, const uint32_t id)
: _enc (enc), _id (id) {}
Symbol<Rnd_It, Fwd_It> operator*()
{ return Symbol<Rnd_It, Fwd_It> (_enc, _id); }
Symbol_Iterator<Rnd_It, Fwd_It>& operator++()
{
++_id;
return *this;
}
Symbol_Iterator<Rnd_It, Fwd_It> operator++ (const int i) const
{
return Symbol_Iterator<Rnd_It, Fwd_It> (_enc, _id +
static_cast<uint32_t> (i));
}
bool operator== (const Symbol_Iterator<Rnd_It, Fwd_It> &it) const
{ return it._id == _id && it._enc == _enc; }
bool operator!= (const Symbol_Iterator<Rnd_It, Fwd_It> &it) const
{ return it._id != _id && it._enc == _enc; }
private:
RQ_ENC_T *const _enc;
uint32_t _id;
};
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_API Block
{
public:
Block (RQ_ENC_T *const enc, const uint8_t block)
: _enc (enc), _block (block) {}
Symbol_Iterator<Rnd_It, Fwd_It> begin_source() const
{
if (_enc == nullptr)
return Symbol_Iterator<Rnd_It, Fwd_It> (nullptr, 0);
const uint32_t id = static_cast<uint32_t> (_block) << 24;
return Symbol_Iterator<Rnd_It, Fwd_It> (_enc, id);
}
Symbol_Iterator<Rnd_It, Fwd_It> end_source() const
{
if (_enc == nullptr)
return Symbol_Iterator<Rnd_It, Fwd_It> (nullptr, 0);
const uint32_t id = static_cast<uint32_t> (_block) << 24;
return Symbol_Iterator<Rnd_It, Fwd_It> (_enc,
id + _enc->symbols (_block));
}
Symbol_Iterator<Rnd_It, Fwd_It> begin_repair() const
{ return end_source(); }
Symbol_Iterator<Rnd_It, Fwd_It> end_repair (uint32_t repairs) const
{
if (repairs > std::pow (2, 20))
repairs = static_cast<uint32_t> (std::pow (2, 20));
if (_enc == nullptr)
return Symbol_Iterator<Rnd_It, Fwd_It> (nullptr, 0);
const uint32_t id = static_cast<uint32_t> (_block) << 24;
return Symbol_Iterator<Rnd_It, Fwd_It> (_enc,
id + _enc->symbols (_block) + repairs);
}
uint8_t id() const
{ return _block; }
uint32_t max_repair() const
{
if (_enc == nullptr)
return 0;
return _enc->max_repair (_block);
}
uint16_t symbols() const
{
if (_enc == nullptr)
return 0;
return _enc->symbols (_block);
}
uint32_t block_size() const
{
if (_enc == nullptr)
return 0;
return _enc->block_size (_block);
}
private:
RQ_ENC_T *const _enc;
const uint8_t _block;
};
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_API Block_Iterator :
public std::iterator<std::input_iterator_tag, Block<Rnd_It, Fwd_It>>
{
public:
Block_Iterator (RQ_ENC_T *const enc, const uint8_t block)
: _enc (enc), _block (block) {}
Block<Rnd_It, Fwd_It> operator*()
{ return Block<Rnd_It, Fwd_It> (_enc, _block); }
Block_Iterator<Rnd_It, Fwd_It>& operator++()
{
++_block;
return *this;
}
Block_Iterator<Rnd_It, Fwd_It> operator++ (const int i) const
{
return Block_Iterator<Rnd_It, Fwd_It> (_enc, _block +
static_cast<uint8_t> (i));
}
bool operator== (const Block_Iterator<Rnd_It, Fwd_It> &it) const
{ return it._block == _block && it._enc == _enc; }
bool operator!= (const Block_Iterator<Rnd_It, Fwd_It> &it) const
{ return it._block != _block && it._enc == _enc; }
private:
RQ_ENC_T *const _enc;
uint8_t _block;
};
} // namespace Encoder
/////////////////
//// Decoder
/////////////////
namespace Decoder {
template <typename In_It, typename Fwd_It>
class RAPTORQ_API Symbol
{
public:
Symbol (RQ_DEC_T *const dec, const uint32_t id)
: _dec (dec), _id (id) {}
uint64_t operator() (In_It &start, const In_It end)
{
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
return 0;
// FIXME: we can't decode wach symbol separately? :(
//return _dec->decode (start, end, _id);
}
uint32_t id() const
{ return _id; }
uint8_t block() const
{ return static_cast<uint8_t> (_id >> 24); }
uint32_t esi() const
{
uint32_t rev_mask = static_cast<uint32_t> (0xFF) << 24;
return static_cast<uint8_t> (_id & ~rev_mask);
}
private:
RQ_DEC_T *const _dec;
const uint32_t _id;
};
template <typename In_It, typename Fwd_It>
class RAPTORQ_API Symbol_Iterator :
public std::iterator<std::input_iterator_tag, Symbol<In_It, Fwd_It>>
{
public:
Symbol_Iterator (RQ_DEC_T *const dec, const uint32_t id)
: _dec (dec), _id (id) {}
Symbol<In_It, Fwd_It> operator*()
{ return Symbol<In_It, Fwd_It> (_dec, _id); }
Symbol_Iterator<In_It, Fwd_It>& operator++()
{
++_id;
return *this;
}
Symbol_Iterator<In_It, Fwd_It> operator++ (const int i) const
{
return Symbol_Iterator<In_It, Fwd_It> (_dec, _id +
static_cast<uint32_t> (i));
}
bool operator== (const Symbol_Iterator<In_It, Fwd_It> &it) const
{ return it._id == _id && it._dec == _dec; }
bool operator!= (const Symbol_Iterator<In_It, Fwd_It> &it) const
{ return it._id != _id && it._dec == _dec; }
private:
RQ_DEC_T *const _dec;
uint32_t _id;
};
template <typename In_It, typename Fwd_It>
class RAPTORQ_API Block
{
public:
Block (RQ_DEC_T *const dec, const uint8_t block)
: _dec (dec), _block (block) {}
Symbol_Iterator<In_It, Fwd_It> begin() const
{
if (_dec == nullptr)
return Symbol_Iterator<In_It, Fwd_It> (nullptr, 0);
const uint32_t id = static_cast<uint32_t> (_block) << 24;
return Symbol_Iterator<In_It, Fwd_It> (_dec, id);
}
Symbol_Iterator<In_It, Fwd_It> end() const
{
if (_dec == nullptr)
return Symbol_Iterator<In_It, Fwd_It> (nullptr, 0);
const uint32_t id = static_cast<uint32_t> (_block) << 24;
return Symbol_Iterator<In_It, Fwd_It> (_dec, id +
_dec->symbols (_block));
}
uint8_t id() const
{ return _block; }
uint16_t symbols() const
{
if (_dec == nullptr)
return 0;
return _dec->symbols (_block);
}
uint32_t block_size() const
{
if (_dec == nullptr)
return 0;
return _dec->block_size (_block);
}
private:
RQ_DEC_T *const _dec;
const uint8_t _block;
};
template <typename In_It, typename Fwd_It>
class RAPTORQ_API Block_Iterator :
public std::iterator<std::input_iterator_tag, Block<In_It, Fwd_It>>
{
public:
Block_Iterator (RQ_DEC_T *const dec, const uint8_t block)
: _dec (dec), _block (block) {}
Block<In_It, Fwd_It> operator*()
{ return Block<In_It, Fwd_It> (_dec, _block); }
Block_Iterator<In_It, Fwd_It>& operator++()
{
++_block;
return *this;
}
Block_Iterator<In_It, Fwd_It> operator++ (const int i) const
{
return Block_Iterator<In_It, Fwd_It> (_dec, _block +
static_cast<uint8_t> (i));
}
bool operator== (const Block_Iterator<In_It, Fwd_It> &it) const
{ return it._block == _block && it._dec == _dec; }
bool operator!= (const Block_Iterator<In_It, Fwd_It> &it) const
{ return it._block != _block && it._dec == _dec; }
private:
RQ_DEC_T *const _dec;
uint8_t _block;
};
} // namespace Decoder
} // namespace It
} // namespace Rfc6330__v1