Fix linked list

This commit is contained in:
Silent 2016-11-20 15:58:39 +01:00
parent 4bcf3ba89a
commit 2b51f5f187
2 changed files with 48 additions and 8 deletions

View file

@ -13,6 +13,14 @@ public:
m_pNext = pAttach; m_pNext = pAttach;
} }
inline void InsertBefore(CLinkSA<T>* pAttach) {
pAttach->m_pPrev = m_pPrev;
m_pPrev->m_pNext = pAttach;
pAttach->m_pNext = this;
m_pPrev = pAttach;
}
inline void Remove(void) { inline void Remove(void) {
m_pNext->m_pPrev = m_pPrev; m_pNext->m_pPrev = m_pPrev;
m_pPrev->m_pNext = m_pNext; m_pPrev->m_pNext = m_pNext;
@ -38,7 +46,7 @@ public:
{ {
} }
void Init(int nNumLinks) { void Init(size_t nNumLinks) {
m_plnLinks = new CLinkSA<T>[nNumLinks]; m_plnLinks = new CLinkSA<T>[nNumLinks];
m_lnListHead.m_pNext = &m_lnListTail; m_lnListHead.m_pNext = &m_lnListTail;
@ -47,8 +55,8 @@ public:
m_lnFreeListHead.m_pNext = &m_lnFreeListTail; m_lnFreeListHead.m_pNext = &m_lnFreeListTail;
m_lnFreeListTail.m_pPrev = &m_lnFreeListHead; m_lnFreeListTail.m_pPrev = &m_lnFreeListHead;
for(int i = nNumLinks - 1; i >= 0; i--) { for(size_t i = nNumLinks; i > 0; --i) {
m_lnFreeListHead.Insert(&m_plnLinks[i]); m_lnFreeListHead.Insert(&m_plnLinks[i - 1]);
} }
} }
@ -95,9 +103,28 @@ public:
return pLink; return pLink;
} }
CLinkSA<T>* InsertFront(const T& pItem) {
return Insert(pItem);
}
CLinkSA<T>* InsertBack(const T& pItem) {
CLinkSA<T>* pLink = m_lnFreeListHead.m_pNext;
if(pLink == &m_lnFreeListTail) {
return nullptr;
}
pLink->m_pItem = pItem;
pLink->Remove();
m_lnListTail.InsertBefore(pLink);
return pLink;
}
void Clear(void) { void Clear(void) {
while(m_lnListHead.m_pNext != &m_lnListTail) { while(m_lnListHead.m_pNext != &m_lnListTail) {
m_lnListHead.m_pNext->Remove(); Remove( m_lnListHead.m_pNext );
} }
} }
@ -107,18 +134,31 @@ public:
} }
CLinkSA<T>* Next(CLinkSA<T>* pCurrent) { CLinkSA<T>* Next(CLinkSA<T>* pCurrent) {
if(pCurrent == 0) { if(pCurrent == nullptr) {
pCurrent = &m_lnListHead; pCurrent = &m_lnListHead;
} }
if(pCurrent->m_pNext == &m_lnListTail) { if(pCurrent->m_pNext == &m_lnListTail) {
return 0; return nullptr;
} }
else { else {
return pCurrent->m_pNext; return pCurrent->m_pNext;
} }
} }
CLinkSA<T>* Prev(CLinkSA<T>* pCurrent) {
if(pCurrent == nullptr) {
pCurrent = &m_lnListTail;
}
if(pCurrent->m_pPrev == &m_lnListHead) {
return nullptr;
}
else {
return pCurrent->m_pPrev;
}
}
CLinkSA<T> m_lnListHead; // 0-12 CLinkSA<T> m_lnListHead; // 0-12
//head of the list of active links //head of the list of active links
CLinkSA<T> m_lnListTail; // 12-24 CLinkSA<T> m_lnListTail; // 12-24

View file

@ -401,7 +401,7 @@ void RenderVehicleHiDetailAlphaCB_HunterDoor(RpAtomic* pAtomic)
NewObject.fCompareValue = -std::numeric_limits<float>::infinity(); NewObject.fCompareValue = -std::numeric_limits<float>::infinity();
NewObject.pAtomic = pAtomic; NewObject.pAtomic = pAtomic;
m_alphaList.InsertSorted(NewObject); m_alphaList.InsertFront(NewObject);
} }
template <RpAtomic* renderer(RpAtomic*)> template <RpAtomic* renderer(RpAtomic*)>
@ -431,7 +431,7 @@ void RenderWeaponPedsForPC()
RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, reinterpret_cast<void*>(TRUE)); RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, reinterpret_cast<void*>(TRUE));
RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, FALSE); RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, FALSE);
for ( auto it = ms_weaponPedsForPC.m_lnListHead.m_pNext; it != &ms_weaponPedsForPC.m_lnListTail; it = it->m_pNext ) for ( auto it = ms_weaponPedsForPC.Next( nullptr ); it != nullptr; it = ms_weaponPedsForPC.Next( it ) )
{ {
it->V()->SetupLighting(); it->V()->SetupLighting();
it->V()->RenderWeapon(true, false); it->V()->RenderWeapon(true, false);