SHOGUN  3.2.1
 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/lib/config.h>
14 
15 #include <shogun/lib/common.h>
16 #include <shogun/base/SGObject.h>
17 #include <shogun/lib/SGVector.h>
18 
19 namespace shogun
20 {
21 class CTokenizer;
22 
32 class CCircularBuffer : public CSGObject
33 {
34 public:
37 
42  CCircularBuffer(int32_t buffer_size);
43 
46 
51  void set_tokenizer(CTokenizer* tokenizer);
52 
58  int32_t push(SGVector<char> source);
59 
65  int32_t push(FILE* source, int32_t source_size);
66 
72  SGVector<char> pop(int32_t num_chars);
73 
79  bool has_next();
80 
88 
94  void skip_characters(int32_t num_chars);
95 
97  int32_t available() const
98  {
99  return m_bytes_available;
100  }
101 
103  int32_t num_bytes_contained() const
104  {
105  return m_bytes_count;
106  }
107 
109  void clear();
110 
112  virtual const char* get_name() const { return "CircularBuffer"; }
113 
114 private:
116  void init();
117 
119  int32_t append_chunk(const char* source, int32_t source_size,
120  bool from_buffer_begin);
121 
123  int32_t append_chunk(FILE* source, int32_t source_size,
124  bool from_buffer_begin);
125 
127  void detach_chunk(char** dest, int32_t* dest_size, int32_t dest_offset, int32_t num_bytes,
128  bool from_buffer_begin);
129 
133  bool has_next_locally(char* begin, char* end);
134 
138  index_t next_token_idx_locally(index_t &start, char* begin, char* end);
139 
141  void move_pointer(char** pointer, char* new_position);
142 
143 private:
145  SGVector<char> m_buffer;
146 
148  char* m_buffer_end;
149 
151  char* m_begin_pos;
152 
154  char* m_end_pos;
155 
157  CTokenizer* m_tokenizer;
158 
160  index_t m_last_idx;
161 
163  int32_t m_bytes_available;
164 
166  int32_t m_bytes_count;
167 };
168 
169 }
170 
171 #endif /* _CIRCULARBUFFER_H_ */

SHOGUN Machine Learning Toolbox - Documentation