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/RaptorQ.hpp"
#include "RaptorQ/v1/RaptorQ_Iterators.hpp"
#include <future>
namespace RaptorQ__v1 {
template <typename Rnd_It, typename Fwd_It>
{
public:
// used for precomputation
Encoder (const uint16_t symbols, const uint16_t symbol_size);
// with data at the beginning. Less work.
Encoder (const Rnd_It data_from, const Rnd_It data_to,
uint16_t symbols() const;
uint16_t symbol_size() const;
uint32_t max_repair() const;
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> begin_source();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> end_source();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> begin_repair();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> end_repair
(const uint32_t repair);
uint64_t add_data (Rnd_It from, const Rnd_It to);
bool compute_sync();
std::future<Error> compute();
uint64_t encode (Fwd_It &output, const Fwd_It end, const uint32_t &id);
private:
std::unique_ptr<Impl::Encoder<Rnd_It, Fwd_It>> encoder;
};
template <typename In_It, typename Fwd_It>
{
public:
using Report = typename Impl::Decoder<In_It, Fwd_It>::Report;
Decoder (const uint64_t bytes, const uint16_t symbol_size,
const Report type);
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> begin ();
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> end ();
Error add_symbol (In_It from, const In_It to, const uint32_t esi);
using Decoder_Result =typename Impl::Decoder<In_It, Fwd_It>::Decoder_Result;
bool can_decode() const;
Decoder_Result decode();
void stop();
std::pair<Error, uint16_t> poll();
std::future<std::pair<Error, uint16_t>> wait (bool async);
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
// return number of symbols.
// simbol_size % sizeof(FWD) == 0 else assert!
// returns number of iterators written
uint64_t decode_symbol (Fwd_It &start, const Fwd_It end,const uint16_t esi);
std::pair<uint64_t, size_t> decode_bytes (Fwd_It &start, const Fwd_It end,
const size_t from_byte, const size_t skip);
private:
std::unique_ptr<Impl::Decoder<In_It, Fwd_It>> decoder;
};
///////////////////
//// Encoder
///////////////////
template <typename Rnd_It, typename Fwd_It>
Encoder<Rnd_It, Fwd_It>::Encoder (const uint16_t symbols,
const uint16_t symbol_size)
{
IS_RANDOM(Rnd_It, "RaptorQ__v1::Encoder");
IS_FORWARD(Fwd_It, "RaptorQ__v1::Encoder");
encoder = std::unique_ptr<Impl::Encoder<Rnd_It, Fwd_It>> (
new Impl::Encoder<Rnd_It, Fwd_It> (symbols, symbol_size));
}
template <typename Rnd_It, typename Fwd_It>
Encoder<Rnd_It, Fwd_It>::Encoder (const Rnd_It data_from, const Rnd_It data_to,
const uint16_t symbol_size)
{
IS_RANDOM(Rnd_It, "RaptorQ__v1::Encoder");
IS_FORWARD(Fwd_It, "RaptorQ__v1::Encoder");
encoder = std::unique_ptr<Impl::Encoder<Rnd_It, Fwd_It>> (
new Impl::Encoder<Rnd_It, Fwd_It> (data_from, data_to,
symbol_size));
}
template <typename Rnd_It, typename Fwd_It>
uint16_t Encoder<Rnd_It, Fwd_It>::symbols() const
{
if (encoder == nullptr)
return 0;
return encoder->symbols();
}
template <typename Rnd_It, typename Fwd_It>
uint16_t Encoder<Rnd_It, Fwd_It>::symbol_size() const
{
if (encoder == nullptr)
return 0;
return encoder->symbol_size();
}
template <typename Rnd_It, typename Fwd_It>
uint32_t Encoder<Rnd_It, Fwd_It>::max_repair() const
{
if (encoder == nullptr)
return 0;
return encoder->max_repair();
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
return encoder->end_source();
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
Encoder<Rnd_It, Fwd_It>::begin_repair()
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
return encoder->begin_repair();
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
Encoder<Rnd_It, Fwd_It>::end_repair (const uint32_t repair)
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
return encoder->end_repair (repair);
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
224
225
226
227
228
229
230
231
232
}
template <typename Rnd_It, typename Fwd_It>
uint64_t Encoder<Rnd_It, Fwd_It>::add_data (Rnd_It from, const Rnd_It to)
{
if (encoder == nullptr)
return 0;
return encoder->add_data (from, to);
}
template <typename Rnd_It, typename Fwd_It>
bool Encoder<Rnd_It, Fwd_It>::compute_sync()
{
if (encoder == nullptr)
return false;
return encoder->compute_sync();
}
template <typename Rnd_It, typename Fwd_It>
uint64_t Encoder<Rnd_It, Fwd_It>::encode (Fwd_It &output, const Fwd_It end,
const uint32_t &id)
{
if (encoder == nullptr)
return 0;
return encoder->encode (output, end, id);
}
///////////////////
//// Decoder
///////////////////
template <typename In_It, typename Fwd_It>
Decoder<In_It, Fwd_It>::Decoder (const uint64_t bytes,
const uint16_t symbol_size, const Report type)
{
IS_INPUT(In_It, "RaptorQ__v1::Decoder");
IS_FORWARD(Fwd_It, "RaptorQ__v1::Decoder");
decoder = std::unique_ptr<Impl::Decoder<In_It, Fwd_It>> (
new Impl::Decoder<In_It, Fwd_It> (bytes, symbol_size, type));
}
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::symbols() const
{
if (decoder == nullptr)
return 0;
return decoder->symbols();
}
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::symbol_size() const
{
if (decoder == nullptr)
return 0;
return decoder->symbol_size();
}
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
template <typename In_It, typename Fwd_It>
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It>
Decoder<In_It, Fwd_It>::begin()
{
if (decoder == nullptr)
return RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> (
nullptr, 0);
return decoder->begin();
}
template <typename In_It, typename Fwd_It>
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It>
Decoder<In_It, Fwd_It>::end()
{
if (decoder == nullptr)
return RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> (
nullptr, 0);
return decoder->end();
}
template <typename In_It, typename Fwd_It>
Error Decoder<In_It, Fwd_It>::add_symbol (In_It from, const In_It to,
const uint32_t esi)
{
if (decoder == nullptr)
return Error::INITIALIZATION;
return decoder->add_symbol (from, to, esi);
}
template <typename In_It, typename Fwd_It>
{
if (decoder == nullptr)
return {Error::INITIALIZATION, 0};
return decoder->poll();
}
template <typename In_It, typename Fwd_It>
std::future<std::pair<Error, uint16_t>> Decoder<In_It, Fwd_It>::wait (
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
{
if (decoder == nullptr) {
std::promise<std::pair<Error, uint16_t>> p;
p.set_value ({Error::INITIALIZATION, 0});
return p.get_future();
}
return decoder->wait (async);
}
template <typename In_It, typename Fwd_It>
bool Decoder<In_It, Fwd_It>::can_decode() const
{
if (decoder == nullptr)
return false;
return decoder->can_decode();
}
template <typename In_It, typename Fwd_It>
typename Decoder<In_It, Fwd_It>::Decoder_Result Decoder<In_It, Fwd_It>::decode()
{
if (decoder == nullptr)
return Decoder_Result::NEED_DATA;
return decoder->decode();
}
template <typename In_It, typename Fwd_It>
void Decoder<In_It, Fwd_It>::stop()
{
if (decoder != nullptr)
decoder->stop();
}
template <typename In_It, typename Fwd_It>
std::pair<uint64_t, size_t> Decoder<In_It, Fwd_It>::decode_bytes (Fwd_It &start,
const Fwd_It end,
const uint64_t from_byte,
const size_t skip)
{
if (decoder == nullptr)
return {0, skip};
return decoder->decode_bytes (start, end, from_byte, skip);
}
template <typename In_It, typename Fwd_It>
uint64_t Decoder<In_It, Fwd_It>::decode_symbol (Fwd_It &start, const Fwd_It end,
const uint16_t esi)
{
if (decoder == nullptr)
return 0;
return decoder->decode_symbol (start, end, esi);
}
} // namespace RaptorQ__v1