mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: Improve LinkedList iteration - simplify code generated for begin, end and operator++
This commit is contained in:
@@ -234,7 +234,7 @@ void ObjectGridUnloader::Visit(GridRefManager<T> &m)
|
||||
{
|
||||
while (!m.empty())
|
||||
{
|
||||
T *obj = m.getFirst()->GetSource();
|
||||
T* obj = m.front()->GetSource();
|
||||
//Some creatures may summon other temp summons in CleanupsBeforeDelete()
|
||||
//So we need this even after cleaner (maybe we can remove cleaner)
|
||||
//Example: Flame Leviathan Turret 33139 is summoned when a creature is deleted
|
||||
|
||||
@@ -3576,7 +3576,7 @@ AreaTrigger* Map::GetAreaTriggerBySpawnId(ObjectGuid::LowType spawnId) const
|
||||
void Map::UpdateIteratorBack(Player* player)
|
||||
{
|
||||
if (&*m_mapRefIter == &player->GetMapRef())
|
||||
m_mapRefIter = m_mapRefIter->nocheck_prev();
|
||||
--m_mapRefIter;
|
||||
}
|
||||
|
||||
void Map::SaveRespawnTime(SpawnObjectType type, ObjectGuid::LowType spawnId, uint32 entry, time_t respawnTime, uint32 gridId, CharacterDatabaseTransaction dbTrans, bool startup)
|
||||
|
||||
@@ -35,27 +35,19 @@ class LinkedListElement
|
||||
public:
|
||||
LinkedListElement() : iNext(nullptr), iPrev(nullptr) { }
|
||||
|
||||
bool hasNext() const { return (iNext && iNext->iNext != nullptr); }
|
||||
bool hasPrev() const { return (iPrev && iPrev->iPrev != nullptr); }
|
||||
bool isInList() const { return (iNext != nullptr && iPrev != nullptr); }
|
||||
|
||||
LinkedListElement * next() { return hasNext() ? iNext : nullptr; }
|
||||
LinkedListElement const* next() const { return hasNext() ? iNext : nullptr; }
|
||||
LinkedListElement * prev() { return hasPrev() ? iPrev : nullptr; }
|
||||
LinkedListElement const* prev() const { return hasPrev() ? iPrev : nullptr; }
|
||||
|
||||
LinkedListElement * nocheck_next() { return iNext; }
|
||||
LinkedListElement const* nocheck_next() const { return iNext; }
|
||||
LinkedListElement * nocheck_prev() { return iPrev; }
|
||||
LinkedListElement const* nocheck_prev() const { return iPrev; }
|
||||
LinkedListElement * next() { return iNext; }
|
||||
LinkedListElement const* next() const { return iNext; }
|
||||
LinkedListElement * prev() { return iPrev; }
|
||||
LinkedListElement const* prev() const { return iPrev; }
|
||||
|
||||
void delink()
|
||||
{
|
||||
if (!isInList())
|
||||
return;
|
||||
if (iNext)
|
||||
iNext->iPrev = iPrev;
|
||||
|
||||
if (iPrev)
|
||||
iPrev->iNext = iNext;
|
||||
|
||||
iNext->iPrev = iPrev;
|
||||
iPrev->iNext = iNext;
|
||||
iNext = nullptr;
|
||||
iPrev = nullptr;
|
||||
}
|
||||
@@ -109,12 +101,6 @@ class LinkedListHead
|
||||
|
||||
bool empty() const { return iFirst.iNext == &iLast; }
|
||||
|
||||
LinkedListElement * getFirst() { return (empty() ? nullptr : iFirst.iNext); }
|
||||
LinkedListElement const* getFirst() const { return (empty() ? nullptr : iFirst.iNext); }
|
||||
|
||||
LinkedListElement * getLast() { return (empty() ? nullptr : iLast.iPrev); }
|
||||
LinkedListElement const* getLast() const { return (empty() ? nullptr : iLast.iPrev); }
|
||||
|
||||
void push_front(LinkedListElement* pElem)
|
||||
{
|
||||
iFirst.insertAfter(pElem);
|
||||
@@ -130,12 +116,9 @@ class LinkedListHead
|
||||
if (!iSize)
|
||||
{
|
||||
uint32 result = 0;
|
||||
LinkedListElement const* e = getFirst();
|
||||
while (e)
|
||||
{
|
||||
for (auto itr = begin_impl<LinkedListElement>(); itr != end_impl<LinkedListElement>(); ++itr)
|
||||
++result;
|
||||
e = e->next();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
@@ -145,41 +128,33 @@ class LinkedListHead
|
||||
void incSize() { ++iSize; }
|
||||
void decSize() { --iSize; }
|
||||
|
||||
template<class _Ty>
|
||||
class Iterator
|
||||
template <typename _Ty>
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
typedef std::bidirectional_iterator_tag iterator_category;
|
||||
typedef _Ty value_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef ptrdiff_t distance_type;
|
||||
typedef _Ty* pointer;
|
||||
typedef _Ty const* const_pointer;
|
||||
typedef _Ty& reference;
|
||||
typedef _Ty const & const_reference;
|
||||
using iterator_category = std::bidirectional_iterator_tag;
|
||||
using value_type = _Ty;
|
||||
using difference_type = ptrdiff_t;
|
||||
using base_pointer = std::conditional_t<std::is_const_v<_Ty>, LinkedListElement const, LinkedListElement>*;
|
||||
using pointer = _Ty*;
|
||||
using reference = _Ty&;
|
||||
|
||||
Iterator() : _Ptr(nullptr)
|
||||
{ // construct with null node pointer
|
||||
}
|
||||
|
||||
explicit Iterator(pointer _Pnode) : _Ptr(_Pnode)
|
||||
explicit Iterator(base_pointer _Pnode) : _Ptr(_Pnode)
|
||||
{ // construct with node pointer _Pnode
|
||||
}
|
||||
|
||||
Iterator& operator=(const_pointer _Right)
|
||||
{
|
||||
_Ptr = const_cast<pointer>(_Right);
|
||||
return *this;
|
||||
}
|
||||
|
||||
reference operator*() const
|
||||
{ // return designated value
|
||||
return *_Ptr;
|
||||
return static_cast<reference>(*_Ptr);
|
||||
}
|
||||
|
||||
pointer operator->() const
|
||||
{ // return pointer to class object
|
||||
return _Ptr;
|
||||
return static_cast<pointer>(_Ptr);
|
||||
}
|
||||
|
||||
Iterator& operator++()
|
||||
@@ -190,7 +165,7 @@ class LinkedListHead
|
||||
|
||||
Iterator operator++(int)
|
||||
{ // postincrement
|
||||
iterator _Tmp = *this;
|
||||
Iterator _Tmp = *this;
|
||||
++*this;
|
||||
return (_Tmp);
|
||||
}
|
||||
@@ -203,7 +178,7 @@ class LinkedListHead
|
||||
|
||||
Iterator operator--(int)
|
||||
{ // postdecrement
|
||||
iterator _Tmp = *this;
|
||||
Iterator _Tmp = *this;
|
||||
--*this;
|
||||
return (_Tmp);
|
||||
}
|
||||
@@ -212,10 +187,33 @@ class LinkedListHead
|
||||
// test for iterator equality
|
||||
|
||||
protected:
|
||||
pointer _Ptr; // pointer to node
|
||||
base_pointer _Ptr; // pointer to node
|
||||
};
|
||||
|
||||
typedef Iterator<LinkedListElement> iterator;
|
||||
protected:
|
||||
template <typename T>
|
||||
T* front_impl() { return static_cast<T*>(iFirst.iNext); }
|
||||
|
||||
template <typename T>
|
||||
T const* front_impl() const { return static_cast<T const*>(iFirst.iNext); }
|
||||
|
||||
template <typename T>
|
||||
T* back_impl() { return static_cast<T*>(iLast.iPrev); }
|
||||
|
||||
template <typename T>
|
||||
T const* back_impl() const { return static_cast<T const*>(iLast.iPrev); }
|
||||
|
||||
template <typename T>
|
||||
Iterator<T> begin_impl() { return Iterator<T>(iFirst.iNext); }
|
||||
|
||||
template <typename T>
|
||||
Iterator<T const> begin_impl() const { return Iterator<T const>(iFirst.iNext); }
|
||||
|
||||
template <typename T>
|
||||
Iterator<T> end_impl() { return Iterator<T>(&iLast); }
|
||||
|
||||
template <typename T>
|
||||
Iterator<T const> end_impl() const { return Iterator<T const>(&iLast); }
|
||||
|
||||
private:
|
||||
LinkedListHead(LinkedListHead const&) = delete;
|
||||
|
||||
@@ -29,15 +29,14 @@ public:
|
||||
typedef LinkedListHead::Iterator<ReferenceType const> const_iterator;
|
||||
RefManager() { }
|
||||
|
||||
ReferenceType* getFirst() { return static_cast<ReferenceType*>(LinkedListHead::getFirst()); }
|
||||
ReferenceType* front() { return front_impl<ReferenceType>(); }
|
||||
ReferenceType const* front() const { return front_impl<ReferenceType>(); }
|
||||
|
||||
ReferenceType const* getFirst() const { return static_cast<ReferenceType const*>(LinkedListHead::getFirst()); }
|
||||
iterator begin() { return begin_impl<ReferenceType>(); }
|
||||
iterator end() { return end_impl<ReferenceType>(); }
|
||||
|
||||
iterator begin() { return iterator(getFirst()); }
|
||||
iterator end() { return iterator(nullptr); }
|
||||
|
||||
const_iterator begin() const { return const_iterator(getFirst()); }
|
||||
const_iterator end() const { return const_iterator(nullptr); }
|
||||
const_iterator begin() const { return begin_impl<ReferenceType>(); }
|
||||
const_iterator end() const { return end_impl<ReferenceType>(); }
|
||||
|
||||
virtual ~RefManager()
|
||||
{
|
||||
@@ -46,8 +45,8 @@ public:
|
||||
|
||||
void clearReferences()
|
||||
{
|
||||
while (ReferenceType* ref = getFirst())
|
||||
ref->invalidate();
|
||||
while (!empty())
|
||||
front()->invalidate();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -81,16 +81,6 @@ template <class TO, class FROM, class Derived> class Reference : public LinkedLi
|
||||
return iRefTo != nullptr;
|
||||
}
|
||||
|
||||
Derived * next() { return static_cast<Derived*>(LinkedListElement::next()); }
|
||||
Derived const* next() const { return static_cast<Derived const*>(LinkedListElement::next()); }
|
||||
Derived * prev() { return static_cast<Derived*>(LinkedListElement::prev()); }
|
||||
Derived const* prev() const { return static_cast<Derived const*>(LinkedListElement::prev()); }
|
||||
|
||||
Derived * nocheck_next() { return static_cast<Derived*>(LinkedListElement::nocheck_next()); }
|
||||
Derived const* nocheck_next() const { return static_cast<Derived const*>(LinkedListElement::nocheck_next()); }
|
||||
Derived * nocheck_prev() { return static_cast<Derived*>(LinkedListElement::nocheck_prev()); }
|
||||
Derived const* nocheck_prev() const { return static_cast<Derived const*>(LinkedListElement::nocheck_prev()); }
|
||||
|
||||
TO* operator->() const { return iRefTo; }
|
||||
TO* getTarget() const { return iRefTo; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user