-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CircularBuffer.cpp
412 lines (333 loc) · 9.29 KB
/
CircularBuffer.cpp
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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Evgeniy Andreev (gsomix)
*/
#include <shogun/lib/CircularBuffer.h>
#include <cstdio>
#include <cstring>
using namespace shogun;
CCircularBuffer::CCircularBuffer()
{
init();
}
CCircularBuffer::CCircularBuffer(int32_t buffer_size)
{
init();
m_buffer=SGVector<char>(buffer_size);
m_buffer_end=m_buffer.vector+m_buffer.vlen;
m_begin_pos=m_buffer.vector;
m_end_pos=m_begin_pos;
m_bytes_available=m_buffer.vlen;
}
CCircularBuffer::~CCircularBuffer()
{
m_buffer=SGVector<char>();
}
void CCircularBuffer::set_tokenizer(CTokenizer* tokenizer)
{
m_tokenizer=tokenizer;
}
int32_t CCircularBuffer::push(SGVector<char> source)
{
if (source.vector==NULL || source.vlen==0)
{
SG_ERROR("CCircularBuffer::push(SGVector<char>):: Invalid parameters! Source shouldn't be NULL or zero sized\n");
return -1;
}
int32_t bytes_to_write;
if (source.vlen>m_bytes_available)
bytes_to_write=m_bytes_available;
else
bytes_to_write=source.vlen;
if (bytes_to_write==0)
return 0;
// determine which part of the memory block is free to read
if (m_end_pos>=m_begin_pos)
{
int32_t bytes_to_memory_end=m_buffer.vlen-(m_end_pos-m_buffer.vector);
if (bytes_to_memory_end<bytes_to_write)
{
// we need write as at end of memory block and at begin
// because logical structure of buffer is ring
int32_t first_chunk_size=bytes_to_memory_end;
int32_t second_chunk_size=bytes_to_write-first_chunk_size;
bytes_to_write=append_chunk(source.vector, first_chunk_size, false);
bytes_to_write+=append_chunk(source.vector+first_chunk_size, second_chunk_size, true);
}
else
{
bytes_to_write=append_chunk(source.vector, bytes_to_write, false);
}
}
else
{
bytes_to_write=append_chunk(source.vector, bytes_to_write, false);
}
return bytes_to_write;
}
int32_t CCircularBuffer::push(FILE* source, int32_t source_size)
{
if (source==NULL || source_size==0)
{
SG_ERROR("CCircularBuffer::push(FILE*, int32_t):: Invalid parameters! Source shouldn't be NULL or zero sized\n");
return -1;
}
int32_t bytes_to_write;
if (source_size>m_bytes_available)
bytes_to_write=m_bytes_available;
else
bytes_to_write=source_size;
if (bytes_to_write==0)
return 0;
// determine which part of the memory block is free to read
if (m_end_pos>=m_begin_pos)
{
int32_t bytes_to_memory_end=m_buffer.vlen-(m_end_pos-m_buffer.vector);
if (bytes_to_memory_end<bytes_to_write)
{
// we need write as at end of memory block and at begin
// because logical structure of buffer is ring
int32_t first_chunk_size=bytes_to_memory_end;
int32_t second_chunk_size=bytes_to_write-first_chunk_size;
bytes_to_write=append_chunk(source, first_chunk_size, false);
bytes_to_write+=append_chunk(source, second_chunk_size, true);
}
else
{
bytes_to_write=append_chunk(source, bytes_to_write, false);
}
}
else
{
bytes_to_write=append_chunk(source, bytes_to_write, false);
}
return bytes_to_write;
}
SGVector<char> CCircularBuffer::pop(int32_t num_bytes)
{
SGVector<char> result;
int32_t bytes_to_read;
if (num_bytes>m_bytes_count)
bytes_to_read=m_bytes_count;
else
bytes_to_read=num_bytes;
if (bytes_to_read==0)
return 0;
// determine which part of the memory block will be read
if (m_begin_pos>=m_end_pos)
{
int32_t bytes_to_memory_end=m_buffer.vlen-(m_begin_pos-m_buffer.vector);
if (bytes_to_memory_end<bytes_to_read)
{
// read continious block from end of memory and from begin
int32_t first_chunk_size=bytes_to_memory_end;
int32_t second_chunk_size=bytes_to_read-first_chunk_size;
detach_chunk(&result.vector, &result.vlen, 0, first_chunk_size, false);
detach_chunk(&result.vector, &result.vlen, first_chunk_size, second_chunk_size, true);
}
else
{
detach_chunk(&result.vector, &result.vlen, 0, bytes_to_read, false);
}
}
else
{
detach_chunk(&result.vector, &result.vlen, 0, bytes_to_read, false);
}
return result;
}
bool CCircularBuffer::has_next()
{
if (m_tokenizer==NULL)
{
SG_ERROR("CCircularBuffer::has_next():: Tokenizer is not initialized\n");
return false;
}
if (m_bytes_count==0)
return false;
int32_t head_length=m_buffer_end-m_begin_pos;
// determine position of finder pointer in memory block
if (m_last_idx<head_length)
{
if (m_end_pos>=m_begin_pos && m_bytes_available!=0)
{
return has_next_locally(m_begin_pos+m_last_idx, m_end_pos);
}
else
{
bool temp=false;
temp=has_next_locally(m_begin_pos+m_last_idx, m_buffer_end);
if (temp)
return temp;
return has_next_locally(m_buffer.vector+m_last_idx-head_length, m_end_pos);
}
}
else
{
return has_next_locally(m_buffer.vector+m_last_idx-head_length, m_end_pos);
}
return false;
}
index_t CCircularBuffer::next_token_idx(index_t &start)
{
index_t end;
if (m_tokenizer==NULL)
{
SG_ERROR("CCircularBuffer::next_token_idx(index_t&):: Tokenizer is not initialized\n");
return 0;
}
if (m_bytes_count==0)
return m_bytes_count;
int32_t tail_length=m_end_pos-m_buffer.vector;
int32_t head_length=m_buffer_end-m_begin_pos;
// determine position of finder pointer in memory block
if (m_last_idx<head_length)
{
if (m_end_pos>=m_begin_pos && m_bytes_available!=0)
{
end=next_token_idx_locally(start, m_begin_pos+m_last_idx, m_end_pos);
if (end<=m_bytes_count)
return end;
}
else
{
index_t temp_start;
// in this case we should find first at end of memory block
end=next_token_idx_locally(start, m_begin_pos+m_last_idx, m_buffer_end);
if (end<head_length)
return end;
// and then at begin
end=next_token_idx_locally(temp_start, m_buffer.vector+m_last_idx-head_length, m_end_pos);
if (start>=head_length)
start=temp_start;
return end;
}
}
else
{
end=next_token_idx_locally(start, m_buffer.vector+m_last_idx-head_length, m_end_pos);
if (end-head_length<=tail_length)
return end;
}
start=0;
return start;
}
void CCircularBuffer::skip_characters(int32_t num_chars)
{
move_pointer(&m_begin_pos, m_begin_pos+num_chars);
m_last_idx-=num_chars;
if (m_last_idx<0)
m_last_idx=0;
m_bytes_available+=num_chars;
m_bytes_count-=num_chars;
}
void CCircularBuffer::clear()
{
m_begin_pos=m_buffer.vector;
m_end_pos=m_begin_pos;
m_last_idx=0;
m_bytes_available=m_buffer.vlen;
m_bytes_count=0;
}
void CCircularBuffer::init()
{
m_buffer_end=m_buffer.vector+m_buffer.vlen;
m_tokenizer=NULL;
m_begin_pos=NULL;
m_end_pos=NULL;
m_last_idx=0;
m_bytes_available=0;
m_bytes_count=0;
}
int32_t CCircularBuffer::append_chunk(const char* source, int32_t source_size,
bool from_buffer_begin)
{
if (source==NULL || source_size==0)
{
SG_ERROR("CCircularBuffer::append_chunk(const char*, int32_t, bool):: Invalid parameters!\
Source shouldn't be NULL or zero sized\n");
return -1;
}
if (from_buffer_begin)
m_end_pos=m_buffer.vector;
memcpy(m_end_pos, source, source_size);
move_pointer(&m_end_pos, m_end_pos+source_size);
m_bytes_available-=source_size;
m_bytes_count+=source_size;
return source_size;
}
int32_t CCircularBuffer::append_chunk(FILE* source, int32_t source_size,
bool from_buffer_begin)
{
int32_t actually_read=fread(m_end_pos, sizeof(char), source_size, source);
if (from_buffer_begin && actually_read==source_size)
m_end_pos=m_buffer.vector;
move_pointer(&m_end_pos, m_end_pos+actually_read);
m_bytes_available-=actually_read;
m_bytes_count+=actually_read;
return actually_read;
}
void CCircularBuffer::detach_chunk(char** dest, int32_t* dest_size, int32_t dest_offset, int32_t num_bytes,
bool from_buffer_begin)
{
if (dest==NULL || dest_size==NULL)
{
SG_ERROR("CCircularBuffer::detach_chunk(...):: Invalid parameters! Pointers are NULL\n");
return;
}
if (*dest==NULL)
{
*dest=SG_MALLOC(char, num_bytes+dest_offset);
*dest_size=num_bytes+dest_offset;
}
if (*dest_size<num_bytes+dest_offset)
{
*dest=SG_REALLOC(char, *dest, *dest_size, num_bytes+dest_offset);
*dest_size=num_bytes+dest_offset;
}
if (from_buffer_begin)
m_begin_pos=m_buffer.vector;
memcpy(*dest+dest_offset, m_begin_pos, num_bytes);
move_pointer(&m_begin_pos, m_begin_pos+num_bytes);
m_last_idx-=num_bytes;
if (m_last_idx<0)
m_last_idx=0;
m_bytes_available+=num_bytes;
m_bytes_count-=num_bytes;
}
bool CCircularBuffer::has_next_locally(char* part_begin, char* part_end)
{
int32_t num_bytes_to_search=part_end-part_begin;
SGVector<char> buffer_part(part_begin, num_bytes_to_search, false);
m_tokenizer->set_text(buffer_part);
return m_tokenizer->has_next();
}
index_t CCircularBuffer::next_token_idx_locally(index_t &start, char* part_begin, char* part_end)
{
index_t end=0;
int32_t num_bytes_to_search=part_end-part_begin;
if (num_bytes_to_search<=0)
{
start=0;
return m_last_idx;
}
SGVector<char> buffer_part(part_begin, num_bytes_to_search, false);
m_tokenizer->set_text(buffer_part);
end=m_tokenizer->next_token_idx(start);
start+=m_last_idx;
m_last_idx+=end;
if (end==num_bytes_to_search)
return m_last_idx;
else
return m_last_idx++;
}
void CCircularBuffer::move_pointer(char** pointer, char* new_position)
{
*pointer=new_position;
if (*pointer>=m_buffer.vector+m_buffer.vlen)
*pointer=m_buffer.vector;
}