1 |
tim |
770 |
#ifndef INC_CircularQueue_hpp__ |
2 |
|
|
#define INC_CircularQueue_hpp__ |
3 |
|
|
|
4 |
|
|
/* ANTLR Translator Generator |
5 |
|
|
* Project led by Terence Parr at http://www.jGuru.com |
6 |
|
|
* Software rights: http://www.antlr.org/license.html |
7 |
|
|
* |
8 |
|
|
* $Id: CircularQueue.hpp,v 1.1 2005-12-02 15:38:02 tim Exp $ |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <antlr/config.hpp> |
12 |
|
|
#include <antlr/Token.hpp> |
13 |
|
|
#include <vector> |
14 |
|
|
#include <cassert> |
15 |
|
|
|
16 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
17 |
|
|
namespace antlr { |
18 |
|
|
#endif |
19 |
|
|
|
20 |
|
|
// Resize every 5000 items |
21 |
|
|
#define OFFSET_MAX_RESIZE 5000 |
22 |
|
|
|
23 |
|
|
template <class T> |
24 |
|
|
class ANTLR_API CircularQueue { |
25 |
|
|
public: |
26 |
|
|
CircularQueue() |
27 |
|
|
: storage() |
28 |
|
|
, m_offset(0) |
29 |
|
|
{ |
30 |
|
|
} |
31 |
|
|
~CircularQueue() |
32 |
|
|
{ |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/// Clear the queue |
36 |
|
|
inline void clear( void ) |
37 |
|
|
{ |
38 |
|
|
m_offset = 0; |
39 |
|
|
storage.clear(); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
/// @todo this should use at or should have a check |
43 |
|
|
inline T elementAt( size_t idx ) const |
44 |
|
|
{ |
45 |
|
|
return storage[idx+m_offset]; |
46 |
|
|
} |
47 |
|
|
void removeFirst() |
48 |
|
|
{ |
49 |
|
|
if (m_offset >= OFFSET_MAX_RESIZE) |
50 |
|
|
{ |
51 |
|
|
storage.erase( storage.begin(), storage.begin() + m_offset + 1 ); |
52 |
|
|
m_offset = 0; |
53 |
|
|
} |
54 |
|
|
else |
55 |
|
|
++m_offset; |
56 |
|
|
} |
57 |
|
|
inline void removeItems( size_t nb ) |
58 |
|
|
{ |
59 |
|
|
assert(nb <= entries()); |
60 |
|
|
if (m_offset >= OFFSET_MAX_RESIZE) |
61 |
|
|
{ |
62 |
|
|
storage.erase( storage.begin(), storage.begin() + m_offset + nb ); |
63 |
|
|
m_offset = 0; |
64 |
|
|
} |
65 |
|
|
else |
66 |
|
|
m_offset += nb; |
67 |
|
|
} |
68 |
|
|
inline void append(const T& t) |
69 |
|
|
{ |
70 |
|
|
storage.push_back(t); |
71 |
|
|
} |
72 |
|
|
inline size_t entries() const |
73 |
|
|
{ |
74 |
|
|
return storage.size() - m_offset; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
private: |
78 |
|
|
typename ANTLR_USE_NAMESPACE(std)vector<T> storage; |
79 |
|
|
size_t m_offset; |
80 |
|
|
|
81 |
|
|
CircularQueue(const CircularQueue&); |
82 |
|
|
const CircularQueue& operator=(const CircularQueue&); |
83 |
|
|
}; |
84 |
|
|
|
85 |
|
|
#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE |
86 |
|
|
} |
87 |
|
|
#endif |
88 |
|
|
|
89 |
|
|
#endif //INC_CircularQueue_hpp__ |