tc_loop_queue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _TC_LOOP_QUEUE_H_
  17. #define _TC_LOOP_QUEUE_H_
  18. #include <vector>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. using namespace std;
  22. namespace tars
  23. {
  24. /////////////////////////////////////////////////
  25. /**
  26. * @file tc_loop_queue.h
  27. * @brief 循环队列,大小固定 .
  28. *
  29. */
  30. /////////////////////////////////////////////////
  31. template<typename T>
  32. class TC_LoopQueue
  33. {
  34. public:
  35. typedef vector<T> queue_type;
  36. TC_LoopQueue(size_t iSize)
  37. {
  38. //做个保护 最多不能超过 1000000
  39. // assert(iSize<1000000);
  40. _iBegin = 0;
  41. _iEnd = 0;
  42. _iCapacitySub = iSize;
  43. _iCapacity = iSize + 1;
  44. _p=(T*)malloc(_iCapacity*sizeof(T));
  45. //_p= new T[_iCapacity];
  46. }
  47. ~TC_LoopQueue()
  48. {
  49. free(_p);
  50. //delete _p;
  51. }
  52. bool push_back(const T &t,bool & bEmpty, size_t & iBegin, size_t & iEnd)
  53. {
  54. bEmpty = false;
  55. //uint32_t iEnd = _iEnd;
  56. iEnd = _iEnd;
  57. iBegin = _iBegin;
  58. if((iEnd > _iBegin && iEnd - _iBegin < 2) ||
  59. ( _iBegin > iEnd && _iBegin - iEnd > (_iCapacity-2) ) )
  60. {
  61. return false;
  62. }
  63. else
  64. {
  65. memcpy(_p+_iBegin,&t,sizeof(T));
  66. //*(_p+_iBegin) = t;
  67. if(_iEnd == _iBegin)
  68. bEmpty = true;
  69. if(_iBegin == _iCapacitySub)
  70. _iBegin = 0;
  71. else
  72. _iBegin++;
  73. if(!bEmpty && 1 == size())
  74. bEmpty = true;
  75. return true;
  76. }
  77. }
  78. bool push_back(const T &t,bool & bEmpty)
  79. {
  80. bEmpty = false;
  81. size_t iEnd = _iEnd;
  82. if((iEnd > _iBegin && iEnd - _iBegin < 2) ||
  83. ( _iBegin > iEnd && _iBegin - iEnd > (_iCapacity-2) ) )
  84. {
  85. return false;
  86. }
  87. else
  88. {
  89. memcpy(_p+_iBegin,&t,sizeof(T));
  90. //*(_p+_iBegin) = t;
  91. if(_iEnd == _iBegin)
  92. bEmpty = true;
  93. if(_iBegin == _iCapacitySub)
  94. _iBegin = 0;
  95. else
  96. _iBegin++;
  97. if(!bEmpty && 1 == size())
  98. bEmpty = true;
  99. #if 0
  100. if(1 == size())
  101. bEmpty = true;
  102. #endif
  103. return true;
  104. }
  105. }
  106. bool push_back(const T &t)
  107. {
  108. bool bEmpty;
  109. return push_back(t,bEmpty);
  110. }
  111. bool push_back(const queue_type &vt)
  112. {
  113. size_t iEnd=_iEnd;
  114. if(vt.size()>(_iCapacity-1) ||
  115. (iEnd>_iBegin && (iEnd-_iBegin)<(vt.size()+1)) ||
  116. ( _iBegin>iEnd && (_iBegin-iEnd)>(_iCapacity-vt.size()-1) ) )
  117. {
  118. return false;
  119. }
  120. else
  121. {
  122. for(size_t i=0;i<vt.size();i++)
  123. {
  124. memcpy(_p+_iBegin,&vt[i],sizeof(T));
  125. //*(_p+_iBegin) = vt[i];
  126. if(_iBegin == _iCapacitySub)
  127. _iBegin = 0;
  128. else
  129. _iBegin++;
  130. }
  131. return true;
  132. }
  133. }
  134. bool pop_front(T &t)
  135. {
  136. if(_iEnd==_iBegin)
  137. {
  138. return false;
  139. }
  140. memcpy(&t,_p+_iEnd,sizeof(T));
  141. //t = *(_p+_iEnd);
  142. if(_iEnd == _iCapacitySub)
  143. _iEnd = 0;
  144. else
  145. _iEnd++;
  146. return true;
  147. }
  148. bool pop_front()
  149. {
  150. if(_iEnd==_iBegin)
  151. {
  152. return false;
  153. }
  154. if(_iEnd == _iCapacitySub)
  155. _iEnd = 0;
  156. else
  157. _iEnd++;
  158. return true;
  159. }
  160. bool get_front(T &t)
  161. {
  162. if(_iEnd==_iBegin)
  163. {
  164. return false;
  165. }
  166. memcpy(&t,_p+_iEnd,sizeof(T));
  167. //t = *(_p+_iEnd);
  168. return true;
  169. }
  170. bool empty()
  171. {
  172. if(_iEnd == _iBegin)
  173. {
  174. return true;
  175. }
  176. return false;
  177. }
  178. size_t size()
  179. {
  180. size_t iBegin=_iBegin;
  181. size_t iEnd=_iEnd;
  182. if(iBegin<iEnd)
  183. return iBegin+_iCapacity-iEnd;
  184. return iBegin-iEnd;
  185. }
  186. size_t getCapacity()
  187. {
  188. return _iCapacity;
  189. }
  190. private:
  191. T * _p;
  192. size_t _iCapacity;
  193. size_t _iCapacitySub;
  194. size_t _iBegin;
  195. size_t _iEnd;
  196. };
  197. }
  198. #endif /* ----- #ifndef _TC_LOOP_QUEUE_H_ ----- */