You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __BASIC_TEMPLATES_HEADER__
  2. #define __BASIC_TEMPLATES_HEADER__
  3. template <typename T> struct _Primitive_Ring_Element
  4. {
  5. T * data;
  6. U32 id;
  7. _Primitive_Ring_Element<T> * prev;
  8. _Primitive_Ring_Element<T> * next;
  9. };
  10. template <class T> class _Primitive_Ring
  11. {
  12. private:
  13. _Primitive_Ring_Element<T> * cursor;
  14. _Primitive_Ring_Element<T> * first;
  15. _Primitive_Ring_Element<T> * last;
  16. public:
  17. U32 count;
  18. _Primitive_Ring(); // create open ring
  19. _Primitive_Ring(U32 size); // create closed ring
  20. ~_Primitive_Ring();
  21. void add(T * str);
  22. int remove(void);
  23. int removeNode(void);
  24. void setAtBegin(void)
  25. {
  26. cursor = first;
  27. }
  28. void setAtEnd(void)
  29. {
  30. cursor = last;
  31. }
  32. T * fetch(U32 id);
  33. T * getAtCursor(void);
  34. void getSequentalUp(void);
  35. void getSequentalDown(void);
  36. };
  37. extern U32 _Primitive_Ring_List_INTERATOR;
  38. template <class T> _Primitive_Ring<T>::_Primitive_Ring()
  39. {
  40. count = 0;
  41. cursor = NULL;
  42. first = NULL;
  43. last = NULL;
  44. }
  45. template <class T> void _Primitive_Ring<T>::add(T * str)
  46. {
  47. if (last == NULL)
  48. {
  49. last = (_Primitive_Ring_Element<T> *) malloc(sizeof(_Primitive_Ring_Element<T>));
  50. last->prev = NULL;
  51. last->next = NULL;
  52. last->data = str;
  53. last->id = _Primitive_Ring_List_INTERATOR++;
  54. cursor = last;
  55. first = last;
  56. }
  57. else
  58. {
  59. last->next = (_Primitive_Ring_Element<T> *) malloc(sizeof(_Primitive_Ring_Element<T>));
  60. last->next->prev = last;
  61. last->next->data = str;
  62. last->next->next = NULL;
  63. last = last->next;
  64. };
  65. count++;
  66. }
  67. template <class T> int _Primitive_Ring<T>::remove(void)
  68. {
  69. _Primitive_Ring_Element<T> * temp;
  70. if (cursor != NULL)
  71. {
  72. delete cursor->data;
  73. if (cursor->prev != NULL && cursor->next != NULL)
  74. {
  75. cursor->next->prev = cursor->prev;
  76. cursor->prev->next = cursor->next;
  77. temp = cursor->next;
  78. delete cursor;
  79. cursor = temp;
  80. }
  81. else
  82. {
  83. if (cursor->next == NULL)
  84. {
  85. temp = cursor->prev;
  86. delete cursor;
  87. cursor = temp;
  88. last = temp;
  89. if (last != NULL)
  90. {
  91. last->next = NULL;
  92. }
  93. }
  94. else if (cursor->prev == NULL)
  95. {
  96. temp = cursor->next;
  97. delete cursor;
  98. cursor = temp;
  99. first = temp;
  100. if (first != NULL)
  101. {
  102. first->prev = NULL;
  103. }
  104. }
  105. else
  106. {
  107. delete cursor;
  108. last = NULL;
  109. first = NULL;
  110. cursor = NULL;
  111. };
  112. };
  113. count--;
  114. }
  115. else
  116. {
  117. count = 0;
  118. }
  119. return count;
  120. }
  121. template <class T> int _Primitive_Ring<T>::removeNode(void)
  122. {
  123. _Primitive_Ring_Element<T> * temp;
  124. if (cursor != NULL)
  125. {
  126. // delete cursor->data;
  127. if (cursor->prev != NULL && cursor->next != NULL)
  128. {
  129. cursor->next->prev = cursor->prev;
  130. cursor->prev->next = cursor->next;
  131. temp = cursor->next;
  132. delete cursor;
  133. cursor = temp;
  134. }
  135. else
  136. {
  137. if (cursor->next == NULL)
  138. {
  139. temp = cursor->prev;
  140. delete cursor;
  141. cursor = temp;
  142. last = temp;
  143. if (last != NULL)
  144. {
  145. last->next = NULL;
  146. }
  147. }
  148. else if (cursor->prev == NULL)
  149. {
  150. temp = cursor->next;
  151. delete cursor;
  152. cursor = temp;
  153. first = temp;
  154. if (first != NULL)
  155. {
  156. first->prev = NULL;
  157. }
  158. }
  159. else
  160. {
  161. delete cursor;
  162. last = NULL;
  163. first = NULL;
  164. cursor = NULL;
  165. };
  166. };
  167. count--;
  168. }
  169. else
  170. {
  171. count = 0;
  172. }
  173. return count;
  174. }
  175. template <class T> T * _Primitive_Ring<T>::fetch(U32 id)
  176. {
  177. setAtBegin();
  178. while (cursor != NULL)
  179. {
  180. if (cursor->id == id)
  181. {
  182. return cursor->data;
  183. }
  184. getSequentalUp();
  185. };
  186. return NULL;
  187. }
  188. template <class T> T * _Primitive_Ring<T>::getAtCursor(void)
  189. {
  190. if (cursor != NULL)
  191. {
  192. return cursor->data;
  193. }
  194. return NULL;
  195. }
  196. template <class T> void _Primitive_Ring<T>::getSequentalUp(void)
  197. {
  198. if (cursor != NULL)
  199. {
  200. cursor = cursor->next;
  201. };
  202. }
  203. template <class T> void _Primitive_Ring<T>::getSequentalDown(void)
  204. {
  205. if (cursor != NULL)
  206. {
  207. cursor = cursor->prev;
  208. };
  209. }
  210. template <class T> _Primitive_Ring<T>::~_Primitive_Ring()
  211. {
  212. setAtBegin();
  213. while (remove())
  214. ;
  215. }
  216. #endif