basic game files port
This commit is contained in:
1 parent
8436242032
commit
9f24650296
10 files changed
+836
-1
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#include "Cevo/IPQ.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
IPQ::IPQ(int max) : m_heap(static_cast<std::size_t>(max)), m_index(static_cast<std::size_t>(max), -1)
|
||||
{
|
||||
}
|
||||
|
||||
void IPQ::clear()
|
||||
{
|
||||
if (m_size != 0) {
|
||||
std::fill(m_index.begin(), m_index.end(), -1);
|
||||
m_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Parent(i) = (i-1)/2.
|
||||
bool IPQ::put(int item, int value)
|
||||
{
|
||||
int i = m_index[item];
|
||||
if (i >= 0) {
|
||||
if (m_heap[i].value <= value)
|
||||
return false;
|
||||
} else {
|
||||
i = m_size;
|
||||
++m_size;
|
||||
}
|
||||
|
||||
while (i > 0) {
|
||||
const int j = (i - 1) >> 1; // Parent(i) = (i-1)/2
|
||||
if (value >= m_heap[j].value)
|
||||
break;
|
||||
m_heap[i] = m_heap[j];
|
||||
m_index[m_heap[i].item] = i;
|
||||
i = j;
|
||||
}
|
||||
// Insert the new item at the insertion point found.
|
||||
m_heap[i].value = value;
|
||||
m_heap[i].item = item;
|
||||
m_index[item] = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IPQ::testPut(int item, int value) const
|
||||
{
|
||||
const int i = m_index[item];
|
||||
return i < 0 || m_heap[i].value > value;
|
||||
}
|
||||
|
||||
// Left(i) = 2*i+1, Right(i) = Left(i)+1.
|
||||
bool IPQ::get(int &item, int &value)
|
||||
{
|
||||
if (m_size == 0)
|
||||
return false;
|
||||
|
||||
item = m_heap[0].item;
|
||||
value = m_heap[0].value;
|
||||
m_index[item] = -1;
|
||||
|
||||
--m_size;
|
||||
if (m_size > 0) {
|
||||
const HeapItem last = m_heap[m_size];
|
||||
int i = 0;
|
||||
int j = 1;
|
||||
while (j < m_size) {
|
||||
// Right(i) = Left(i)+1
|
||||
if (j < m_size - 1 && m_heap[j].value > m_heap[j + 1].value)
|
||||
++j;
|
||||
if (last.value <= m_heap[j].value)
|
||||
break;
|
||||
|
||||
m_heap[i] = m_heap[j];
|
||||
m_index[m_heap[i].item] = i;
|
||||
i = j;
|
||||
j = (j << 1) + 1; // Left(j) = 2*j+1
|
||||
}
|
||||
|
||||
// Insert the former last element in the correct place in the heap.
|
||||
m_heap[i] = last;
|
||||
m_index[last.item] = i;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user