2015-07-26 00:13:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
template <typename StructType>
|
|
|
|
struct COsStructQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef COsStructManager<StructType> StructManager;
|
|
|
|
|
2015-08-09 04:03:42 +00:00
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
public:
|
2018-04-30 20:01:23 +00:00
|
|
|
iterator(const StructManager& container, uint32* idPtr)
|
|
|
|
: m_container(container)
|
|
|
|
, m_idPtr(idPtr)
|
|
|
|
{
|
|
|
|
}
|
2015-08-09 04:03:42 +00:00
|
|
|
|
2018-04-30 20:01:23 +00:00
|
|
|
iterator& operator++()
|
2015-08-09 04:03:42 +00:00
|
|
|
{
|
|
|
|
MoveNext();
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:01:23 +00:00
|
|
|
iterator operator++(int)
|
2015-08-09 04:03:42 +00:00
|
|
|
{
|
|
|
|
iterator copy(*this);
|
|
|
|
MoveNext();
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:01:23 +00:00
|
|
|
bool operator!=(const iterator& rhs) const
|
2015-08-09 04:03:42 +00:00
|
|
|
{
|
|
|
|
return m_idPtr != rhs.m_idPtr;
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:01:23 +00:00
|
|
|
std::pair<uint32, StructType*> operator*() const
|
2015-08-09 04:03:42 +00:00
|
|
|
{
|
|
|
|
assert(m_idPtr);
|
|
|
|
return std::make_pair(*m_idPtr, m_container[*m_idPtr]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void MoveNext()
|
|
|
|
{
|
|
|
|
assert(m_idPtr);
|
|
|
|
auto nextItem = m_container[*m_idPtr];
|
|
|
|
m_idPtr = &nextItem->nextId;
|
|
|
|
if(*m_idPtr == 0)
|
|
|
|
{
|
|
|
|
m_idPtr = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const StructManager& m_container;
|
|
|
|
uint32* m_idPtr = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-07-26 00:13:59 +00:00
|
|
|
COsStructQueue(StructManager& items, uint32* headIdPtr)
|
2018-04-30 20:01:23 +00:00
|
|
|
: m_items(items)
|
|
|
|
, m_headIdPtr(headIdPtr)
|
2015-07-26 00:13:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-08 22:14:56 +00:00
|
|
|
COsStructQueue(const COsStructQueue&) = delete;
|
2018-04-30 20:01:23 +00:00
|
|
|
COsStructQueue& operator=(const COsStructQueue) = delete;
|
2015-08-08 22:14:56 +00:00
|
|
|
|
2015-08-09 04:03:42 +00:00
|
|
|
bool IsEmpty() const
|
|
|
|
{
|
|
|
|
return (*m_headIdPtr == 0);
|
|
|
|
}
|
|
|
|
|
2015-07-26 00:13:59 +00:00
|
|
|
void PushFront(uint32 id)
|
|
|
|
{
|
|
|
|
uint32 nextId = (*m_headIdPtr);
|
|
|
|
(*m_headIdPtr) = id;
|
|
|
|
auto item = m_items[id];
|
|
|
|
item->nextId = nextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushBack(uint32 id)
|
|
|
|
{
|
|
|
|
auto nextId = m_headIdPtr;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(*nextId == 0) break;
|
|
|
|
auto nextItem = m_items[*nextId];
|
|
|
|
nextId = &nextItem->nextId;
|
|
|
|
}
|
|
|
|
*nextId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddBefore(uint32 beforeId, uint32 id)
|
|
|
|
{
|
|
|
|
auto newItem = m_items[id];
|
|
|
|
auto nextId = m_headIdPtr;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(*nextId == 0)
|
|
|
|
{
|
|
|
|
//Id not found
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto nextItem = m_items[*nextId];
|
|
|
|
if((*nextId) == beforeId)
|
|
|
|
{
|
|
|
|
(*nextId) = id;
|
|
|
|
newItem->nextId = beforeId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextId = &nextItem->nextId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlink(uint32 id)
|
|
|
|
{
|
|
|
|
auto nextId = m_headIdPtr;
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
if(*nextId == 0)
|
|
|
|
{
|
|
|
|
//Id not found
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto nextItem = m_items[*nextId];
|
|
|
|
if((*nextId) == id)
|
|
|
|
{
|
|
|
|
(*nextId) = nextItem->nextId;
|
|
|
|
nextItem->nextId = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextId = &nextItem->nextId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-09 04:03:42 +00:00
|
|
|
iterator begin() const
|
|
|
|
{
|
|
|
|
return iterator(m_items, IsEmpty() ? nullptr : m_headIdPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() const
|
|
|
|
{
|
|
|
|
return iterator(m_items, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-07-26 00:13:59 +00:00
|
|
|
private:
|
2018-04-30 20:01:23 +00:00
|
|
|
StructManager& m_items;
|
|
|
|
uint32* m_headIdPtr = nullptr;
|
2015-07-26 00:13:59 +00:00
|
|
|
};
|