SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CircularBuffer.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Engeniy Andreev (gsomix)
8  */
9 
10 #ifndef __CIRCULARBUFFER_H_
11 #define __CIRCULARBUFFER_H_
12 
13 #include <shogun/base/SGObject.h>
14 #include <shogun/lib/SGVector.h>
15 #include <shogun/lib/Tokenizer.h>
16 
17 namespace shogun
18 {
19 
29 class CCircularBuffer : public CSGObject
30 {
31 public:
34 
39  CCircularBuffer(int32_t buffer_size);
40 
43 
48  void set_tokenizer(CTokenizer* tokenizer);
49 
55  int32_t push(SGVector<char> source);
56 
62  int32_t push(FILE* source, int32_t source_size);
63 
69  SGVector<char> pop(int32_t num_chars);
70 
76  bool has_next();
77 
85 
91  void skip_characters(int32_t num_chars);
92 
94  int32_t available() const
95  {
96  return m_bytes_available;
97  }
98 
100  int32_t num_bytes_contained() const
101  {
102  return m_bytes_count;
103  }
104 
106  void clear();
107 
109  virtual const char* get_name() const { return "CircularBuffer"; }
110 
111 private:
113  void init();
114 
116  int32_t append_chunk(const char* source, int32_t source_size,
117  bool from_buffer_begin);
118 
120  int32_t append_chunk(FILE* source, int32_t source_size,
121  bool from_buffer_begin);
122 
124  void detach_chunk(char** dest, int32_t* dest_size, int32_t dest_offset, int32_t num_bytes,
125  bool from_buffer_begin);
126 
130  bool has_next_locally(char* begin, char* end);
131 
135  index_t next_token_idx_locally(index_t &start, char* begin, char* end);
136 
138  void move_pointer(char** pointer, char* new_position);
139 
140 private:
142  SGVector<char> m_buffer;
143 
145  char* m_buffer_end;
146 
148  char* m_begin_pos;
149 
151  char* m_end_pos;
152 
154  CTokenizer* m_tokenizer;
155 
157  index_t m_last_idx;
158 
160  int32_t m_bytes_available;
161 
163  int32_t m_bytes_count;
164 };
165 
166 }
167 
168 #endif /* _CIRCULARBUFFER_H_ */

SHOGUN Machine Learning Toolbox - Documentation