make PriorityQueue template class, and extend index field in EVENT struct

This commit is contained in:
nukeykt 2019-07-14 02:50:23 +09:00 committed by Christoph Oelckers
parent e4174d4f2d
commit 1561ffae9e
4 changed files with 138 additions and 180 deletions

View file

@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class EventQueue
{
public:
PriorityQueue* PQueue;
PriorityQueue<EVENT>* PQueue;
EventQueue()
{
PQueue = NULL;
@ -48,8 +48,7 @@ public:
}
EVENT ERemove(void)
{
unsigned int node = PQueue->Remove();
return *(EVENT*)&node;
return PQueue->Remove();
}
void Kill(int, int);
void Kill(int, int, CALLBACK_ID);
@ -58,19 +57,13 @@ public:
EventQueue eventQ;
void EventQueue::Kill(int a1, int a2)
{
EVENT evn = { (unsigned int)a1, (unsigned int)a2, 0, 0 };
//evn.at0_0 = a1;
//evn.at1_5 = a2;
short vs = *(short*)&evn;
PQueue->Kill([=](unsigned int nItem)->bool {return !memcmp(&nItem, &vs, 2); });
PQueue->Kill([=](EVENT nItem)->bool {return nItem.index == a1 && nItem.type == a2; });
}
void EventQueue::Kill(int a1, int a2, CALLBACK_ID a3)
{
EVENT evn = { (unsigned int)a1, (unsigned int)a2, kCommandCallback, (unsigned int)a3 };
unsigned int vc = *(unsigned int*)&evn;
PQueue->Kill([=](unsigned int nItem)->bool {return nItem == vc; });
PQueue->Kill([=](EVENT nItem)->bool {return !memcmp(&nItem, &evn, sizeof(EVENT)); });
}
//struct RXBUCKET
@ -276,9 +269,9 @@ void evInit(void)
if (eventQ.PQueue)
delete eventQ.PQueue;
if (VanillaMode())
eventQ.PQueue = new VanillaPriorityQueue();
eventQ.PQueue = new VanillaPriorityQueue<EVENT>();
else
eventQ.PQueue = new StdPriorityQueue();
eventQ.PQueue = new StdPriorityQueue<EVENT>();
eventQ.PQueue->Clear();
int nCount = 0;
for (int i = 0; i < numsectors; i++)
@ -481,22 +474,22 @@ void evPost(int nIndex, int nType, unsigned int nDelta, COMMAND_ID command)
command = evGetSourceState(nType, nIndex) ? COMMAND_ID_1 : COMMAND_ID_0;
else if (command == COMMAND_ID_4)
command = evGetSourceState(nType, nIndex) ? COMMAND_ID_0 : COMMAND_ID_1;
EVENT evn;
EVENT evn = {};
evn.index = nIndex;
evn.type = nType;
evn.cmd = command;
// Inlined?
eventQ.PQueue->Insert(gFrameClock+nDelta, *(unsigned int*)&evn);
eventQ.PQueue->Insert(gFrameClock+nDelta, evn);
}
void evPost(int nIndex, int nType, unsigned int nDelta, CALLBACK_ID a4)
{
EVENT evn;
EVENT evn = {};
evn.index = nIndex;
evn.type = nType;
evn.cmd = kCommandCallback;
evn.funcID = a4;
eventQ.PQueue->Insert(gFrameClock+nDelta, *(unsigned int*)&evn);
eventQ.PQueue->Insert(gFrameClock+nDelta, evn);
}
void evProcess(unsigned int nTime)
@ -563,18 +556,18 @@ void EventQLoadSave::Load()
delete eventQ.PQueue;
Read(&eventQ, sizeof(eventQ));
if (VanillaMode())
eventQ.PQueue = new VanillaPriorityQueue();
eventQ.PQueue = new VanillaPriorityQueue<EVENT>();
else
eventQ.PQueue = new StdPriorityQueue();
eventQ.PQueue = new StdPriorityQueue<EVENT>();
int nEvents;
Read(&nEvents, sizeof(nEvents));
for (int i = 0; i < nEvents; i++)
{
EVENT event;
EVENT event = {};
unsigned int eventtime;
Read(&eventtime, sizeof(eventtime));
Read(&event, sizeof(event));
eventQ.PQueue->Insert(eventtime, *(unsigned int*)&event);
eventQ.PQueue->Insert(eventtime, event);
}
Read(rxBucket, sizeof(rxBucket));
Read(bucketHead, sizeof(bucketHead));
@ -597,7 +590,7 @@ void EventQLoadSave::Save()
dassert(eventQ.PQueue->Size() == 0);
for (int i = 0; i < nEvents; i++)
{
eventQ.PQueue->Insert(eventstime[i], *(unsigned int*)&events[i]);
eventQ.PQueue->Insert(eventstime[i], events[i]);
}
Write(rxBucket, sizeof(rxBucket));
Write(bucketHead, sizeof(bucketHead));

View file

@ -52,11 +52,11 @@ enum COMMAND_ID {
};
struct EVENT {
unsigned int index : 13; // index
unsigned int index : 14; // index
unsigned int type : 3; // type
unsigned int cmd : 8; // cmd
unsigned int funcID : 8; // callback
}; // <= 4 bytes
};
void evInit(void);
char evGetSourceState(int nType, int nIndex);

View file

@ -20,128 +20,5 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//-------------------------------------------------------------------------
#include "common_game.h"
#include "pqueue.h"
PriorityQueue::~PriorityQueue()
{
}
VanillaPriorityQueue::~VanillaPriorityQueue()
{
}
void VanillaPriorityQueue::Clear(void)
{
fNodeCount = 0;
memset(queueItems, 0, sizeof(queueItems));
}
void VanillaPriorityQueue::Upheap(void)
{
queueItem item = queueItems[fNodeCount];
queueItems[0].at0 = 0;
unsigned int x = fNodeCount;
while (queueItems[x>>1] > item)
{
queueItems[x] = queueItems[x>>1];
x >>= 1;
}
queueItems[x] = item;
}
void VanillaPriorityQueue::Downheap(unsigned int n)
{
queueItem item = queueItems[n];
while (fNodeCount/2 >= n)
{
unsigned int t = n*2;
if (t < fNodeCount && queueItems[t] > queueItems[t+1])
t++;
if (item <= queueItems[t])
break;
queueItems[n] = queueItems[t];
n = t;
}
queueItems[n] = item;
}
void VanillaPriorityQueue::Delete(unsigned int k)
{
dassert(k <= fNodeCount);
queueItems[k] = queueItems[fNodeCount--];
Downheap(k);
}
void VanillaPriorityQueue::Insert(unsigned int a1, unsigned int a2)
{
dassert(fNodeCount < kPQueueSize);
fNodeCount++;
queueItems[fNodeCount].at0 = a1;
queueItems[fNodeCount].at4 = a2;
Upheap();
}
unsigned int VanillaPriorityQueue::Remove(void)
{
unsigned int data = queueItems[1].at4;
queueItems[1] = queueItems[fNodeCount--];
Downheap(1);
return data;
}
unsigned int VanillaPriorityQueue::LowestPriority(void)
{
dassert(fNodeCount > 0);
return queueItems[1].at0;
}
void VanillaPriorityQueue::Kill(std::function<bool(unsigned int)> pMatch)
{
for (unsigned int i = 1; i <= fNodeCount;)
{
if (pMatch(queueItems[i].at4))
Delete(i);
else
i++;
}
}
StdPriorityQueue::~StdPriorityQueue()
{
stdQueue.clear();
}
void StdPriorityQueue::Clear(void)
{
stdQueue.clear();
}
void StdPriorityQueue::Insert(unsigned int nPriority, unsigned int nData)
{
stdQueue.insert({ nPriority, nData });
}
unsigned int StdPriorityQueue::Remove(void)
{
dassert(stdQueue.size() > 0);
int nData = stdQueue.begin()->at4;
stdQueue.erase(stdQueue.begin());
return nData;
}
unsigned int StdPriorityQueue::LowestPriority(void)
{
return stdQueue.begin()->at0;
}
void StdPriorityQueue::Kill(std::function<bool(unsigned int)> pMatch)
{
for (auto i = stdQueue.begin(); i != stdQueue.end();)
{
if (pMatch(i->at4))
i = stdQueue.erase(i);
else
i++;
}
}
// TODO: delete this

View file

@ -23,12 +23,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#pragma once
#include <set>
#include <functional>
#include "common_game.h"
#define kPQueueSize 1024
struct queueItem
template <typename T> struct queueItem
{
unsigned int at0; // priority
unsigned int at4; // data
uint32_t at0; // priority
T at4; // data
bool operator>(const queueItem& other) const
{
return at0 > other.at0;
@ -55,44 +56,131 @@ struct queueItem
}
};
class PriorityQueue
template<typename T> class PriorityQueue
{
public:
virtual ~PriorityQueue() = 0;
virtual unsigned int Size(void) = 0;
virtual ~PriorityQueue() {}
virtual uint32_t Size(void) = 0;
virtual void Clear(void) = 0;
virtual void Insert(unsigned int, unsigned int) = 0;
virtual unsigned int Remove(void) = 0;
virtual unsigned int LowestPriority(void) = 0;
virtual void Kill(std::function<bool(unsigned int)> pMatch) = 0;
virtual void Insert(uint32_t, T) = 0;
virtual T Remove(void) = 0;
virtual uint32_t LowestPriority(void) = 0;
virtual void Kill(std::function<bool(T)> pMatch) = 0;
};
class VanillaPriorityQueue : public PriorityQueue
template<typename T> class VanillaPriorityQueue : public PriorityQueue<T>
{
public:
queueItem queueItems[kPQueueSize + 1];
unsigned int fNodeCount; // at2008
~VanillaPriorityQueue();
unsigned int Size(void) { return fNodeCount; };
void Clear(void);
void Upheap(void);
void Downheap(unsigned int);
void Delete(unsigned int);
void Insert(unsigned int, unsigned int);
unsigned int Remove(void);
unsigned int LowestPriority(void);
void Kill(std::function<bool(unsigned int)> pMatch);
queueItem<T> queueItems[kPQueueSize + 1];
uint32_t fNodeCount; // at2008
~VanillaPriorityQueue() {}
uint32_t Size(void) { return fNodeCount; };
void Clear(void)
{
fNodeCount = 0;
memset(queueItems, 0, sizeof(queueItems));
}
void Upheap(void)
{
queueItem<T> item = queueItems[fNodeCount];
queueItems[0].at0 = 0;
uint32_t x = fNodeCount;
while (queueItems[x>>1] > item)
{
queueItems[x] = queueItems[x>>1];
x >>= 1;
}
queueItems[x] = item;
}
void Downheap(uint32_t n)
{
queueItem<T> item = queueItems[n];
while (fNodeCount/2 >= n)
{
uint32_t t = n*2;
if (t < fNodeCount && queueItems[t] > queueItems[t+1])
t++;
if (item <= queueItems[t])
break;
queueItems[n] = queueItems[t];
n = t;
}
queueItems[n] = item;
}
void Delete(uint32_t k)
{
dassert(k <= fNodeCount);
queueItems[k] = queueItems[fNodeCount--];
Downheap(k);
}
void Insert(uint32_t a1, T a2)
{
dassert(fNodeCount < kPQueueSize);
fNodeCount++;
queueItems[fNodeCount].at0 = a1;
queueItems[fNodeCount].at4 = a2;
Upheap();
}
T Remove(void)
{
T data = queueItems[1].at4;
queueItems[1] = queueItems[fNodeCount--];
Downheap(1);
return data;
}
uint32_t LowestPriority(void)
{
dassert(fNodeCount > 0);
return queueItems[1].at0;
}
void Kill(std::function<bool(T)> pMatch)
{
for (unsigned int i = 1; i <= fNodeCount;)
{
if (pMatch(queueItems[i].at4))
Delete(i);
else
i++;
}
}
};
class StdPriorityQueue : public PriorityQueue
template<typename T> class StdPriorityQueue : public PriorityQueue<T>
{
public:
std::multiset<queueItem> stdQueue;
~StdPriorityQueue();
unsigned int Size(void) { return stdQueue.size(); };
void Clear(void);
void Insert(unsigned int, unsigned int);
unsigned int Remove(void);
unsigned int LowestPriority(void);
void Kill(std::function<bool(unsigned int)> pMatch);
std::multiset<queueItem<T>> stdQueue;
~StdPriorityQueue()
{
stdQueue.clear();
}
uint32_t Size(void) { return stdQueue.size(); };
void Clear(void)
{
stdQueue.clear();
}
void Insert(uint32_t nPriority, T data)
{
stdQueue.insert({ nPriority, data });
}
T Remove(void)
{
dassert(stdQueue.size() > 0);
T data = stdQueue.begin()->at4;
stdQueue.erase(stdQueue.begin());
return data;
}
uint32_t LowestPriority(void)
{
return stdQueue.begin()->at0;
}
void Kill(std::function<bool(T)> pMatch)
{
for (auto i = stdQueue.begin(); i != stdQueue.end();)
{
if (pMatch(i->at4))
i = stdQueue.erase(i);
else
i++;
}
}
};