vhacdCircularList.inl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #ifndef HACD_CIRCULAR_LIST_INL
  3. #define HACD_CIRCULAR_LIST_INL
  4. namespace VHACD
  5. {
  6. template < typename T >
  7. inline bool CircularList<T>::Delete(CircularListElement<T> * element)
  8. {
  9. if (!element)
  10. {
  11. return false;
  12. }
  13. if (m_size > 1)
  14. {
  15. CircularListElement<T> * next = element->GetNext();
  16. CircularListElement<T> * prev = element->GetPrev();
  17. delete element;
  18. m_size--;
  19. if (element == m_head)
  20. {
  21. m_head = next;
  22. }
  23. next->GetPrev() = prev;
  24. prev->GetNext() = next;
  25. return true;
  26. }
  27. else if (m_size == 1)
  28. {
  29. delete m_head;
  30. m_size--;
  31. m_head = 0;
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. template < typename T >
  40. inline bool CircularList<T>::Delete()
  41. {
  42. if (m_size > 1)
  43. {
  44. CircularListElement<T> * next = m_head->GetNext();
  45. CircularListElement<T> * prev = m_head->GetPrev();
  46. delete m_head;
  47. m_size--;
  48. m_head = next;
  49. next->GetPrev() = prev;
  50. prev->GetNext() = next;
  51. return true;
  52. }
  53. else if (m_size == 1)
  54. {
  55. delete m_head;
  56. m_size--;
  57. m_head = 0;
  58. return true;
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. template < typename T >
  66. inline CircularListElement<T> * CircularList<T>::Add(const T * data)
  67. {
  68. if (m_size == 0)
  69. {
  70. if (data)
  71. {
  72. m_head = new CircularListElement<T>(*data);
  73. }
  74. else
  75. {
  76. m_head = new CircularListElement<T>();
  77. }
  78. m_head->GetNext() = m_head->GetPrev() = m_head;
  79. }
  80. else
  81. {
  82. CircularListElement<T> * next = m_head->GetNext();
  83. CircularListElement<T> * element = m_head;
  84. if (data)
  85. {
  86. m_head = new CircularListElement<T>(*data);
  87. }
  88. else
  89. {
  90. m_head = new CircularListElement<T>();
  91. }
  92. m_head->GetNext() = next;
  93. m_head->GetPrev() = element;
  94. element->GetNext() = m_head;
  95. next->GetPrev() = m_head;
  96. }
  97. m_size++;
  98. return m_head;
  99. }
  100. template < typename T >
  101. inline CircularListElement<T> * CircularList<T>::Add(const T & data)
  102. {
  103. const T * pData = &data;
  104. return Add(pData);
  105. }
  106. template < typename T >
  107. inline bool CircularList<T>::Next()
  108. {
  109. if (m_size == 0)
  110. {
  111. return false;
  112. }
  113. m_head = m_head->GetNext();
  114. return true;
  115. }
  116. template < typename T >
  117. inline bool CircularList<T>::Prev()
  118. {
  119. if (m_size == 0)
  120. {
  121. return false;
  122. }
  123. m_head = m_head->GetPrev();
  124. return true;
  125. }
  126. template < typename T >
  127. inline CircularList<T>::CircularList(const CircularList& rhs)
  128. {
  129. if (rhs.m_size > 0)
  130. {
  131. CircularListElement<T> * current = rhs.m_head;
  132. do
  133. {
  134. current = current->GetNext();
  135. Add(current->GetData());
  136. }
  137. while ( current != rhs.m_head );
  138. }
  139. }
  140. template < typename T >
  141. inline const CircularList<T>& CircularList<T>::operator=(const CircularList& rhs)
  142. {
  143. if (&rhs != this)
  144. {
  145. Clear();
  146. if (rhs.m_size > 0)
  147. {
  148. CircularListElement<T> * current = rhs.m_head;
  149. do
  150. {
  151. current = current->GetNext();
  152. Add(current->GetData());
  153. }
  154. while ( current != rhs.m_head );
  155. }
  156. }
  157. return (*this);
  158. }
  159. }
  160. #endif