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.

165 lines
3.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __BASIC_TEMPLATES_HEADER__
  2. #define __BASIC_TEMPLATES_HEADER__
  3. template <typename T> struct _Primitive_Ring_Element{
  4. T *data;
  5. U32 id;
  6. _Primitive_Ring_Element<T> *prev;
  7. _Primitive_Ring_Element<T> *next;
  8. };
  9. template <class T> class _Primitive_Ring{
  10. private:
  11. _Primitive_Ring_Element<T> *cursor;
  12. _Primitive_Ring_Element<T> *first;
  13. _Primitive_Ring_Element<T> *last;
  14. public:
  15. U32 count;
  16. _Primitive_Ring(); // create open ring
  17. _Primitive_Ring( U32 size ); // create closed ring
  18. ~_Primitive_Ring();
  19. void add( T *str );
  20. int remove( void );
  21. int removeNode( void );
  22. void setAtBegin( void ){ cursor = first; };
  23. void setAtEnd( void ){ cursor = last; };
  24. T *fetch( U32 id );
  25. T *getAtCursor( void );
  26. void getSequentalUp( void );
  27. void getSequentalDown( void );
  28. };
  29. extern U32 _Primitive_Ring_List_INTERATOR;
  30. template <class T> _Primitive_Ring<T>::_Primitive_Ring(){
  31. count = 0;
  32. cursor = NULL;
  33. first = NULL;
  34. last = NULL;
  35. };
  36. template <class T> void _Primitive_Ring<T>::add( T *str ){
  37. if ( last == NULL ){
  38. last = (_Primitive_Ring_Element<T> *) malloc ( sizeof( _Primitive_Ring_Element<T> ) );
  39. last->prev = NULL;
  40. last->next = NULL;
  41. last->data = str;
  42. last->id = _Primitive_Ring_List_INTERATOR++;
  43. cursor = last;
  44. first = last;
  45. } else {
  46. last->next = (_Primitive_Ring_Element<T> *) malloc ( sizeof( _Primitive_Ring_Element<T> ) );
  47. last->next->prev = last;
  48. last->next->data = str;
  49. last->next->next = NULL;
  50. last = last->next;
  51. };
  52. count++;
  53. };
  54. template <class T> int _Primitive_Ring<T>::remove(void){
  55. _Primitive_Ring_Element<T> *temp;
  56. if( cursor != NULL ){
  57. delete cursor->data;
  58. if ( cursor->prev != NULL && cursor->next != NULL ){
  59. cursor->next->prev = cursor->prev;
  60. cursor->prev->next = cursor->next;
  61. temp = cursor->next;
  62. delete cursor;
  63. cursor = temp;
  64. } else {
  65. if ( cursor->next == NULL ){
  66. temp = cursor->prev;
  67. delete cursor;
  68. cursor = temp;
  69. last = temp;
  70. if( last != NULL ) last->next = NULL;
  71. } else if ( cursor->prev == NULL ) {
  72. temp = cursor->next;
  73. delete cursor;
  74. cursor = temp;
  75. first = temp;
  76. if( first != NULL ) first->prev = NULL;
  77. } else {
  78. delete cursor;
  79. last = NULL;
  80. first = NULL;
  81. cursor = NULL;
  82. };
  83. };
  84. count --;
  85. } else {
  86. count = 0;
  87. }
  88. return count;
  89. };
  90. template <class T> int _Primitive_Ring<T>::removeNode(void){
  91. _Primitive_Ring_Element<T> *temp;
  92. if( cursor != NULL ){
  93. //delete cursor->data;
  94. if ( cursor->prev != NULL && cursor->next != NULL ){
  95. cursor->next->prev = cursor->prev;
  96. cursor->prev->next = cursor->next;
  97. temp = cursor->next;
  98. delete cursor;
  99. cursor = temp;
  100. } else {
  101. if ( cursor->next == NULL ){
  102. temp = cursor->prev;
  103. delete cursor;
  104. cursor = temp;
  105. last = temp;
  106. if( last != NULL ) last->next = NULL;
  107. } else if ( cursor->prev == NULL ) {
  108. temp = cursor->next;
  109. delete cursor;
  110. cursor = temp;
  111. first = temp;
  112. if( first != NULL ) first->prev = NULL;
  113. } else {
  114. delete cursor;
  115. last = NULL;
  116. first = NULL;
  117. cursor = NULL;
  118. };
  119. };
  120. count --;
  121. } else {
  122. count = 0;
  123. }
  124. return count;
  125. };
  126. template <class T> T *_Primitive_Ring<T>::fetch( U32 id ){
  127. setAtBegin();
  128. while ( cursor != NULL ){
  129. if( cursor->id == id ) return cursor->data;
  130. getSequentalUp();
  131. };
  132. return NULL;
  133. };
  134. template <class T> T *_Primitive_Ring<T>::getAtCursor( void ){
  135. if ( cursor != NULL ) return cursor->data;
  136. return NULL;
  137. };
  138. template <class T> void _Primitive_Ring<T>::getSequentalUp( void ){
  139. if ( cursor != NULL ){
  140. cursor = cursor->next;
  141. };
  142. };
  143. template <class T> void _Primitive_Ring<T>::getSequentalDown( void ){
  144. if ( cursor != NULL ){
  145. cursor = cursor->prev;
  146. };
  147. };
  148. template <class T> _Primitive_Ring<T>::~_Primitive_Ring(){
  149. setAtBegin();
  150. while( remove() );
  151. };
  152. #endif