ZFFramework
 
Loading...
Searching...
No Matches
ZFCoreQueue.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreQueue_h_
7#define _ZFI_ZFCoreQueue_h_
8
9#include "ZFCoreArray.h"
10
12
18template<typename T_POD>
20private:
21 enum {
22 _bufSize = 16, // capacity = _bufSize - 1
23 };
24public:
29 : _bufBuiltin()
30 , _bufHead(_bufBuiltin)
31 , _bufTail(_bufBuiltin + _bufSize)
32 , _pHead(_bufHead)
33 , _pTail(_pHead)
34 {
35 }
36 virtual ~ZFCoreQueuePOD(void) {
37 if(_bufHead != _bufBuiltin) {
38 zffree(_bufHead);
39 }
40 }
41private:
42 // we don't allow copy
44 ZFCoreQueuePOD<T_POD> &operator = (ZF_IN const ZFCoreQueuePOD<T_POD> &ref);
45
46public:
49 this->objectInfoOfContentT(ret, 10);
50 }
51
52 zffinal inline zfstring objectInfo(void) const {
53 zfstring ret;
54 this->objectInfoT(ret);
55 return ret;
56 }
57
58public:
62 , ZF_IN_OPT zfindex maxCount = zfindexMax()
65 ) const {
66 zfindex count = 0;
67 ret += token.tokenLeft;
68 for(T_POD *p = _pHead; p != _pTail && count < maxCount; ++count, _loopNext(p)) {
69 if(count > 0) {
70 ret += token.tokenSeparator;
71 }
72 ret += token.tokenValueLeft;
73 if(infoGetter != zfnull) {
74 infoGetter(ret, *p);
75 }
76 else {
77 zftToStringT(ret, *p);
78 }
79 ret += token.tokenValueRight;
80 }
81 if(count < this->count()) {
82 if(count > 0) {
83 ret += token.tokenSeparator;
84 }
85 ret += token.tokenEtc;
86 }
87 ret += token.tokenRight;
88 }
89
93 ZF_IN_OPT zfindex maxCount = zfindexMax()
96 ) const {
97 zfstring ret;
98 this->objectInfoOfContentT(ret, maxCount, token, infoGetter);
99 return ret;
100 }
101
102private:
103 static inline void _capacityOptimize(ZF_IN_OUT zfindex &capacity) {
104 if(capacity == 0) {
105 }
106 else if(capacity < 64) {
107 capacity = ((capacity / 16) + 1) * 16 - 1;
108 }
109 else if(capacity < 256) {
110 capacity = ((capacity / 64) + 1) * 64 - 1;
111 }
112 else {
113 capacity = ((capacity / 256) + 1) * 256 - 1;
114 }
115 }
116public:
122 inline zfindex capacity(void) const {
123 return (_bufTail - _bufHead - 1);
124 }
125
129 if(capacity > this->capacity()) {
130 _capacityOptimize(capacity);
131 T_POD *bufHeadNew = (T_POD *)zfmalloc((capacity + 1) * sizeof(T_POD));
132 _bufChange(bufHeadNew, bufHeadNew + capacity + 1);
133 }
134 }
135
139 zfindex capacity = this->count();
140 if(capacity < _bufSize) {
141 if(_bufHead != _bufBuiltin) {
142 _bufChange(_bufBuiltin, _bufBuiltin + _bufSize);
143 }
144 }
145 else {
146 _capacityOptimize(capacity);
147 if(capacity != this->capacity()) {
148 T_POD *bufHeadNew = (T_POD *)zfmalloc((capacity + 1) * sizeof(T_POD));
149 _bufChange(bufHeadNew, bufHeadNew + capacity + 1);
150 }
151 }
152 }
153
154public:
159 inline T_POD &add(void) {
160 this->capacity(this->count() + 1);
161 T_POD *ret = _pTail;
162 _loopNext(_pTail);
163 return *ret;
164 }
165
169 inline void add(ZF_IN T_POD const &e) {
170 this->capacity(this->count() + 1);
171 *_pTail = e;
172 _loopNext(_pTail);
173 }
174
178 template<typename T_Type>
180 for(zfindex i = 0, iEnd = arr.count(); i < iEnd; ++i) {
181 this->add(arr[i]);
182 }
183 }
184
189 ZF_IN const T_POD *buf
191 ) {
192 if(buf == zfnull || count == 0) {
193 return;
194 }
195 this->capacity(this->count() + count);
196 if(_pHead <= _pTail) {
197 if(_pTail + count < _bufTail) {
198 zfmemcpy(_pTail, buf, count * sizeof(T_POD));
199 _pTail += count;
200 }
201 else {
202 zfindex tmp = _bufTail - _pTail;
203 zfmemcpy(_pTail, buf, tmp * sizeof(T_POD));
204 zfmemcpy(_bufHead, buf + tmp, (count - tmp) * sizeof(T_POD));
205 _pTail = _bufHead + (count - tmp);
206 }
207 }
208 else {
209 zfmemcpy(_pTail, buf, count * sizeof(T_POD));
210 _pTail += count;
211 }
212 }
213
216 inline T_POD &take(void) {
217 ZFCoreAssertWithMessage(_pHead != _pTail, "take from an empty queue");
218 T_POD *ret = _pHead;
219 _loopNext(_pHead);
220 return *ret;
221 }
222
226 inline T_POD &takeLast(void) {
227 ZFCoreAssertWithMessage(_pHead != _pTail, "take from an empty queue");
228 if(_pTail == _bufHead) {
229 _pTail = _bufTail - 1;
230 }
231 else {
232 --_pTail;
233 }
234 return *_pTail;
235 }
236
237public:
241 inline zfindex count(void) const {
242 return ((_pTail >= _pHead)
243 ? (_pTail - _pHead)
244 : ((_bufTail - _pHead) + (_pTail - _bufHead)));
245 }
246
249 inline zfbool isEmpty(void) const {
250 return (_pHead == _pTail);
251 }
252
253public:
257 void removeAll(void) {
258 _pHead = _bufHead;
259 _pTail = _bufHead;
260 }
261
262public:
266 template<typename T_Type>
268 array.capacity(array.capacity() + this->count());
269 T_POD *p = _pHead;
270 while(p != _pTail) {
271 array.add(*p);
272 _loopNext(p);
273 }
274 }
275
280 this->toArrayT(ret);
281 return ret;
282 }
283
284private:
285 T_POD _bufBuiltin[_bufSize];
286 T_POD *_bufHead;
287 T_POD *_bufTail;
288 T_POD *_pHead;
289 T_POD *_pTail;
290private:
291 inline void _loopNext(ZF_IN_OUT T_POD *&p) const {
292 ++p;
293 if(p >= _bufTail) {
294 p = _bufHead;
295 }
296 }
297 void _bufChange(
298 ZF_IN T_POD *bufHeadNew
299 , ZF_IN T_POD *bufTailNew
300 ) {
301 T_POD *pTailNew = bufHeadNew + this->count();
302
303 if(_pTail >= _pHead) {
304 zfmemcpy(bufHeadNew, _pHead, (_pTail - _pHead) * sizeof(T_POD));
305 }
306 else {
307 zfmemcpy(bufHeadNew, _pHead, (_bufTail - _pHead) * sizeof(T_POD));
308 zfmemcpy(bufHeadNew + (_bufTail - _pHead), _bufHead, (_pTail - _bufHead) * sizeof(T_POD));
309 }
310
311 if(_bufHead != _bufBuiltin) {
312 zffree(_bufHead);
313 }
314 _bufHead = bufHeadNew;
315 _bufTail = bufTailNew;
316 _pHead = _bufHead;
317 _pTail = pTailNew;
318 }
319};
320ZFOUTPUT_TYPE_TEMPLATE(typename T_POD, ZFCoreQueuePOD<T_POD>, {v.objectInfoT(s);})
321
323
324#endif // #ifndef _ZFI_ZFCoreQueue_h_
325
light weight array
#define ZFCoreAssertWithMessage(exp, fmt,...)
ZFCoreAssert with custom message
Definition ZFCoreLog_CommonLog.h:71
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:140
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:100
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
#define ZFOUTPUT_TYPE_TEMPLATE(T_typenameList, T_Type, outputAction)
see ZFOUTPUT_TYPE
Definition ZFCoreTypeDef_OtherType.h:262
void zftToStringT(zfstring &s, T_Type const &v)
util function to obtain object info
Definition ZFCoreTypeDef_OtherType.h:182
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define ZFTokenForContainerDefault()
see ZFTokenForContainer, modifyable, ZFTokenForContainerTrim by default
Definition ZFToken.h:107
light weight array
Definition ZFCoreArray.h:331
void(* InfoGetter)(zfstring &ret, T_Type const &v)
proto type for obtain object info, see zftToStringT
Definition ZFCoreTypeDef_OtherType.h:151
core queue type for performance and for private use only
Definition ZFCoreQueue.h:19
void capacity(zfindex capacity)
change the capacity
Definition ZFCoreQueue.h:128
T_POD & add(void)
push element at tail of the queue, auto increase capacity if necessary
Definition ZFCoreQueue.h:159
zfstring objectInfo(void) const
return object info
Definition ZFCoreQueue.h:52
T_POD & take(void)
take element at head of the queue, assert fail if empty
Definition ZFCoreQueue.h:216
void add(T_POD const &e)
push element at tail of the queue, auto increase capacity if necessary
Definition ZFCoreQueue.h:169
zfindex capacity(void) const
get current capacity
Definition ZFCoreQueue.h:122
void removeAll(void)
remove all contents
Definition ZFCoreQueue.h:257
void capacityTrim(void)
trim the buffer, call only if necessary
Definition ZFCoreQueue.h:138
zfindex count(void) const
element count of this array
Definition ZFCoreQueue.h:241
T_POD & takeLast(void)
take element at tail of the queue, assert fail if empty
Definition ZFCoreQueue.h:226
ZFCoreArray< T_POD > toArray(void) const
copy contents to array
Definition ZFCoreQueue.h:278
void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCoreQueue.h:48
void addFrom(const ZFCoreArray< T_Type > &arr)
push element at tail of the queue, auto increase capacity if necessary
Definition ZFCoreQueue.h:179
void objectInfoOfContentT(zfstring &ret, zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault, typename ZFCoreInfoGetter< T_POD >::InfoGetter infoGetter=zft_zfnull) const
see objectInfoOfContent
Definition ZFCoreQueue.h:60
zfstring objectInfoOfContent(zfindex maxCount=((zfindex) -1), const ZFTokenForContainer &token=_ZFP_ZFTokenForContainerDefault, typename ZFCoreInfoGetter< T_POD >::InfoGetter infoGetter=zft_zfnull) const
return contents info
Definition ZFCoreQueue.h:92
ZFCoreQueuePOD(void)
main constructor
Definition ZFCoreQueue.h:28
zfbool isEmpty(void) const
true if empty
Definition ZFCoreQueue.h:249
void addFrom(const T_POD *buf, zfindex count)
push element at tail of the queue, auto increase capacity if necessary
Definition ZFCoreQueue.h:188
void toArrayT(ZFCoreArray< T_Type > &array) const
copy contents to array
Definition ZFCoreQueue.h:267
util class to hold string tokens
Definition ZFToken.h:17