-
Notifications
You must be signed in to change notification settings - Fork 8
/
RingQueue.h
198 lines (189 loc) · 3.9 KB
/
RingQueue.h
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
// RingQueue.h
// Copyright 2018/11/2 Robin.Rowe@CinePaint.org
// License open source MIT
#ifndef RingQueue_h
#define RingQueue_h
#include <iostream>
#include <iterator>
#include <algorithm>
#include "MemoryPool.h"
typedef int ring_dif;
class RingQueueBase
{protected:
// Useful capacity is limited to one less than capacity.
// If allowed head == tail to happen on full, that would look the same as being empty.
const int usefulCapacity;
ring_dif head;
ring_dif tail;
RingQueueBase(int usefulCapacity)
: usefulCapacity(usefulCapacity)
, head(0)
, tail(0)
{}
public:
bool IsEmpty() const
{ return head == tail;
}
int Count() const
{ const int spread = tail-head;
if(spread>=0)
{ return spread;
}
const int capacity = usefulCapacity+1;
return capacity+spread;
}
int Capacity() const
{ return usefulCapacity;
}
bool IsFull() const
{ return Count() == usefulCapacity;
}
};
template <typename T>
class RingQueueIterator
{protected:
const RingQueueBase& q;
const T* buffer;
int i;
bool OpEqEq(const RingQueueIterator& rhs) const
{ if(rhs.i == i)
{ return true;
}
return false;
}
void OpPlusPlus()
{ i++;
if(i > q.Capacity())
{ i = 0;
} }
public:
RingQueueIterator<T>(const RingQueueBase& q,T* buffer,int i)
: q(q)
, buffer(buffer)
, i(i)
{}
bool operator==(const RingQueueIterator<T>& rhs) const
{ return OpEqEq(rhs);
}
bool operator!=(const RingQueueIterator<T>& rhs) const
{ return !OpEqEq(rhs);
}
RingQueueIterator<T>& operator++()
{ OpPlusPlus();
return *this;
}
operator T*()
{ return buffer+i;
}
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
template <typename T>
class ConstRingQueueIterator
: public RingQueueIterator<T>
{public:
ConstRingQueueIterator(const RingQueueBase& q,const T* buffer,int i)
: RingQueueIterator<T>(q,(T*)buffer,i)
{}
bool operator==(const ConstRingQueueIterator<T>& rhs) const
{ return RingQueueIterator<T>::OpEqEq(rhs);
}
bool operator!=(const ConstRingQueueIterator<T>& rhs) const
{ return !RingQueueIterator<T>::OpEqEq(rhs);
}
ConstRingQueueIterator<T>& operator++()
{ RingQueueIterator<T>::OpPlusPlus();
return *this;
}
operator T*() const
{ return (T*) RingQueueIterator<T>::buffer+RingQueueIterator<T>::i;
}
};
template <typename T,int capacity>
class RingQueue
: public RingQueueBase
{ T q[capacity+1];
public:
RingQueue()
: RingQueueBase(capacity)
{}
RingQueueIterator<T> begin()
{ return RingQueueIterator<T>(*this,q,head);
}
RingQueueIterator<T> end()
{ return RingQueueIterator<T>(*this,q,tail);
}
ConstRingQueueIterator<T> begin() const
{ return ConstRingQueueIterator<T>(*this,q,head);
}
ConstRingQueueIterator<T> end() const
{ return ConstRingQueueIterator<T>(*this,q,tail);
}
void* operator new(size_t size,void* pool) throw()
{ if(!pool)
{ return 0;
}
IPC::MemoryPool* p = static_cast<IPC::MemoryPool*>(pool);
return p->Allocate(size);
}
void operator delete(void* pool,void* ptr) throw()
{ if(!pool)
{ return;
}
IPC::MemoryPool* p = static_cast<IPC::MemoryPool*>(pool);
p->Deallocate(ptr);
}
const T* Fence() const
{ return q+capacity;
}
void Clear(int init = 0)
{ for(int i = 0;i<capacity;i++)
{ q[i] = init;
} }
T& Front()
{ return q[head];
}
const T& Front() const
{ return q[head];
}
bool Push(const T& v)
{ if(IsFull())
{ puts("is full can't push");
return false;
}
q[tail] = v;
tail++;
if(tail > capacity)
{ tail = 0;
}
return true;
}
bool Pop()
{ if(IsEmpty())
{ return false;
}
head++;
if(head > capacity)
{ head = 0;
}
return true;
}
const int Head() const
{ return head;
}
const int Tail() const
{ return tail;
}
};
template <typename T,int size>
std::ostream& operator<<(std::ostream& os,const RingQueue<T,size>& q)
{ auto print = [&](const T& n)
{ os << n << " ";
};
std::for_each(q.begin(),q.end(),print);
return os;
}
#endif