Newer
Older
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
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
224
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
/*
* 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>
class RAPTORQ_LOCAL Encoder
{
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,
const uint16_t symbol_size);
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> begin ();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> end
(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>
class RAPTORQ_LOCAL Decoder
{
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() const;
std::future<std::pair<Error, uint16_t>> wait (bool async) const;
// 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>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
Encoder<Rnd_It, Fwd_It>::begin()
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
return encoder->begin();
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
Encoder<Rnd_It, Fwd_It>::end (const uint32_t repair)
{
if (encoder == nullptr)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (
nullptr, 0);
return encoder->end (repair);
}
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>
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>
std::pair<Error, uint16_t> Decoder<In_It, Fwd_It>::poll () const
{
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 (
const bool async) const
{
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