| 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 |
|
| 40 |
template < typename T > |
| 41 |
inline bool CircularList<T>::Delete() |
| 42 |
{ |
| 43 |
if (m_size > 1) |
| 44 |
{ |
| 45 |
CircularListElement<T> * next = m_head->GetNext(); |
| 46 |
CircularListElement<T> * prev = m_head->GetPrev(); |
| 47 |
delete m_head; |
| 48 |
m_size--; |
| 49 |
m_head = next; |
| 50 |
next->GetPrev() = prev; |
| 51 |
prev->GetNext() = next; |
| 52 |
return true; |
| 53 |
} |
| 54 |
else if (m_size == 1) |
| 55 |
{ |
| 56 |
delete m_head; |
| 57 |
m_size--; |
| 58 |
m_head = 0; |
| 59 |
return true; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
template < typename T > |
| 67 |
inline CircularListElement<T> * CircularList<T>::Add(const T * data) |
| 68 |
{ |
| 69 |
if (m_size == 0) |
| 70 |
{ |
| 71 |
if (data) |
| 72 |
{ |
| 73 |
m_head = new CircularListElement<T>(*data); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
m_head = new CircularListElement<T>(); |
| 78 |
} |
| 79 |
m_head->GetNext() = m_head->GetPrev() = m_head; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
CircularListElement<T> * next = m_head->GetNext(); |
| 84 |
CircularListElement<T> * element = m_head; |
| 85 |
if (data) |
| 86 |
{ |
| 87 |
m_head = new CircularListElement<T>(*data); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
m_head = new CircularListElement<T>(); |
| 92 |
} |
| 93 |
m_head->GetNext() = next; |
| 94 |
m_head->GetPrev() = element; |
| 95 |
element->GetNext() = m_head; |
| 96 |
next->GetPrev() = m_head; |
| 97 |
} |
| 98 |
m_size++; |
| 99 |
return m_head; |
| 100 |
} |
| 101 |
template < typename T > |
| 102 |
inline CircularListElement<T> * CircularList<T>::Add(const T & data) |
| 103 |
{ |
| 104 |
const T * pData = &data; |
| 105 |
return Add(pData); |
| 106 |
} |
| 107 |
template < typename T > |
| 108 |
inline bool CircularList<T>::Next() |
| 109 |
{ |
| 110 |
if (m_size == 0) |
| 111 |
{ |
| 112 |
return false; |
| 113 |
} |
| 114 |
m_head = m_head->GetNext(); |
| 115 |
return true; |
| 116 |
} |
| 117 |
template < typename T > |
| 118 |
inline bool CircularList<T>::Prev() |
| 119 |
{ |
| 120 |
if (m_size == 0) |
| 121 |
{ |
| 122 |
return false; |
| 123 |
} |
| 124 |
m_head = m_head->GetPrev(); |
| 125 |
return true; |
| 126 |
} |
| 127 |
template < typename T > |
| 128 |
inline CircularList<T>::CircularList(const CircularList& rhs) |
| 129 |
{ |
| 130 |
if (rhs.m_size > 0) |
| 131 |
{ |
| 132 |
CircularListElement<T> * current = rhs.m_head; |
| 133 |
do |
| 134 |
{ |
| 135 |
current = current->GetNext(); |
| 136 |
Add(current->GetData()); |
| 137 |
} |
| 138 |
while ( current != rhs.m_head ); |
| 139 |
} |
| 140 |
} |
| 141 |
template < typename T > |
| 142 |
inline const CircularList<T>& CircularList<T>::operator=(const CircularList& rhs) |
| 143 |
{ |
| 144 |
if (&rhs != this) |
| 145 |
{ |
| 146 |
Clear(); |
| 147 |
if (rhs.m_size > 0) |
| 148 |
{ |
| 149 |
CircularListElement<T> * current = rhs.m_head; |
| 150 |
do |
| 151 |
{ |
| 152 |
current = current->GetNext(); |
| 153 |
Add(current->GetData()); |
| 154 |
} |
| 155 |
while ( current != rhs.m_head ); |
| 156 |
} |
| 157 |
} |
| 158 |
return (*this); |
| 159 |
} |
| 160 |
} |
| 161 |
#endif |