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,42 @@
|
||||
#include "Cevo/CityProcessing.h"
|
||||
#include "Cevo/Database.h"
|
||||
|
||||
namespace Cevo {
|
||||
namespace CityProcessing {
|
||||
|
||||
void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo)
|
||||
{
|
||||
// Original also has `{$IFOPT O-}Assert(1 shl P and InvalidTreatyMap = 0)`
|
||||
// here - a debug-only overflow-checking assertion with no effect on
|
||||
// release behavior, not ported.
|
||||
Database::Vicinity21Loc radius;
|
||||
Database::v21ToLoc(loc, radius);
|
||||
for (int v21 = 0; v21 <= 26; ++v21) {
|
||||
const int loc1 = radius[v21];
|
||||
if (loc1 < 0 || loc1 >= Database::mapSize) {
|
||||
cityAreaInfo.Available[v21] = faInvalid;
|
||||
} else {
|
||||
const int p1 = static_cast<int>(Database::realMap[loc1] >> 27);
|
||||
if (p1 < nPl && p1 != p && Database::rw[p].Treaty[p1] >= trPeace)
|
||||
cityAreaInfo.Available[v21] = faTreaty;
|
||||
else if (Database::zocMap[loc1] > 0 && Database::occupant[loc1] != p &&
|
||||
Database::rw[p].Treaty[Database::occupant[loc1]] < trAlliance)
|
||||
cityAreaInfo.Available[v21] = faSiege;
|
||||
else if (Database::usedByCity[loc1] != -1 && Database::usedByCity[loc1] != loc)
|
||||
cityAreaInfo.Available[v21] = faNotAvailable;
|
||||
else
|
||||
cityAreaInfo.Available[v21] = faAvailable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool canCityGrow(int p, int cix)
|
||||
{
|
||||
const TCity &city = (*Database::rw[p].City)[cix];
|
||||
return city.Size < MaxCitySize &&
|
||||
(city.Size < NeedAqueductSize || (city.Built[imAqueduct] == 1 && city.Size < NeedSewerSize) ||
|
||||
city.Built[imSewer] == 1);
|
||||
}
|
||||
|
||||
} // namespace CityProcessing
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,295 @@
|
||||
#include "Cevo/CmdList.h"
|
||||
#include "Cevo/Protocol.h"
|
||||
|
||||
#include <QIODevice>
|
||||
#include <QString>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
namespace {
|
||||
// Unaligned 32-bit little-endian access, replacing the original's direct
|
||||
// `Cardinal((@LogData[Pos])^)` pointer-cast reads/writes - this format is
|
||||
// little-endian-only (same assumption the original already made, since a
|
||||
// raw pointer cast only behaves this way on the little-endian hosts C-evo
|
||||
// actually ships for).
|
||||
std::uint32_t readU32(const QByteArray &buffer, int pos)
|
||||
{
|
||||
std::uint32_t value;
|
||||
std::memcpy(&value, buffer.constData() + pos, sizeof(value));
|
||||
return value;
|
||||
}
|
||||
|
||||
void writeU32(QByteArray &buffer, int pos, std::uint32_t value)
|
||||
{
|
||||
std::memcpy(buffer.data() + pos, &value, sizeof(value));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int commandWithData(int command, int dataSize)
|
||||
{
|
||||
if (dataSize > CommandDataMaxSize) {
|
||||
throw std::out_of_range(
|
||||
QString("Command data size %1 out of range (0-%2).").arg(dataSize).arg(CommandDataMaxSize).toStdString());
|
||||
}
|
||||
const int dataElementCount = (dataSize + CommandDataElementSize - 1) / CommandDataElementSize; // Ceil
|
||||
return command | (dataElementCount & static_cast<int>(CommandDataElementCountMask));
|
||||
}
|
||||
|
||||
void CmdList::ensurePadded(int requiredSize)
|
||||
{
|
||||
// Get() always reads a full 32-bit word even for record formats that use
|
||||
// only 1-3 of its bytes near the end of the log, so the backing buffer
|
||||
// must always stay at least 4 bytes larger than any position it writes
|
||||
// to or reads from (the original relied on ReallocMem's large, chunk-
|
||||
// rounded over-allocation for this same safety margin).
|
||||
const int padded = requiredSize + 4;
|
||||
if (padded > m_log.size()) {
|
||||
const int oldSize = m_log.size();
|
||||
m_log.resize(padded);
|
||||
std::memset(m_log.data() + oldSize, 0, padded - oldSize);
|
||||
}
|
||||
}
|
||||
|
||||
void CmdList::get(int &command, int &player, int &subject, const void *&data)
|
||||
{
|
||||
if (m_state.loadMoveCode > 0) {
|
||||
player = -1;
|
||||
std::uint32_t dirCode;
|
||||
if (m_state.loadMoveCode & 1) { // FM
|
||||
dirCode = (m_state.loadMoveCode >> 1) & 7;
|
||||
subject = m_state.lastMovingUnit;
|
||||
m_state.loadMoveCode >>= 4;
|
||||
} else { // M
|
||||
dirCode = (m_state.loadMoveCode >> 3) & 7;
|
||||
subject = static_cast<int>((m_state.loadMoveCode >> 6) & 0xFFF);
|
||||
m_state.loadMoveCode >>= 18;
|
||||
m_state.lastMovingUnit = subject;
|
||||
}
|
||||
switch (dirCode) {
|
||||
case 0: command = sMoveUnit + 0x090; break;
|
||||
case 1: command = sMoveUnit + 0x0F0; break;
|
||||
case 2: command = sMoveUnit + 0x390; break;
|
||||
case 3: command = sMoveUnit + 0x3F0; break;
|
||||
case 4: command = sMoveUnit + 0x020; break;
|
||||
case 5: command = sMoveUnit + 0x060; break;
|
||||
case 6: command = sMoveUnit + 0x100; break;
|
||||
case 7: command = sMoveUnit + 0x300; break;
|
||||
}
|
||||
data = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
ensurePadded(m_state.loadPos);
|
||||
const std::uint32_t code = readU32(m_log, m_state.loadPos);
|
||||
if ((code & 3) == 0) { // non-clientex command
|
||||
command = static_cast<int>(((code >> 2) & 0x3FFF) + sExecute);
|
||||
player = static_cast<int>((code >> 16) & 0xF);
|
||||
subject = static_cast<int>((code >> 20) & 0xFFF);
|
||||
m_state.loadPos += 4;
|
||||
} else if ((code & 7) == 2) { // clientex command
|
||||
command = static_cast<int>((code >> 3) & 0xFFFF);
|
||||
player = static_cast<int>((code >> 19) & 0xF);
|
||||
subject = 0;
|
||||
m_state.loadPos += 3;
|
||||
} else { // move command shortcut
|
||||
if ((code & 1) && (code & (7u << 4)) != (6u << 4)) {
|
||||
m_state.loadMoveCode = code & 0xFF;
|
||||
m_state.loadPos += 1;
|
||||
} else {
|
||||
m_state.loadMoveCode = code & 0xFFFFFF;
|
||||
m_state.loadPos += 3;
|
||||
}
|
||||
get(command, player, subject, data);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((command & static_cast<int>(CommandDataElementCountMask)) == 0) {
|
||||
data = nullptr;
|
||||
} else {
|
||||
ensurePadded(m_state.loadPos);
|
||||
data = m_log.constData() + m_state.loadPos;
|
||||
m_state.loadPos += (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize;
|
||||
}
|
||||
}
|
||||
|
||||
void CmdList::getDataChanges(std::uint32_t *data, int dataSize)
|
||||
{
|
||||
ensurePadded(m_state.loadPos);
|
||||
std::uint32_t map0 = readU32(m_log, m_state.loadPos);
|
||||
m_state.loadPos += 4;
|
||||
int b0 = 0;
|
||||
while (map0 > 0) {
|
||||
if (map0 & 1) {
|
||||
ensurePadded(m_state.loadPos);
|
||||
std::uint32_t map1 = readU32(m_log, m_state.loadPos);
|
||||
m_state.loadPos += 4;
|
||||
for (int b1 = 0; b1 < 32; ++b1) {
|
||||
if ((1u << b1) & map1) {
|
||||
ensurePadded(m_state.loadPos);
|
||||
if (b0 * 32 + b1 < dataSize)
|
||||
data[b0 * 32 + b1] = readU32(m_log, m_state.loadPos);
|
||||
m_state.loadPos += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
++b0;
|
||||
map0 >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void CmdList::put(int command, int player, int subject, const void *data)
|
||||
{
|
||||
if ((command & 0xFC00) == sMoveUnit) { // move command shortcut
|
||||
std::uint32_t dirCode = 0;
|
||||
switch (command) {
|
||||
case sMoveUnit + 0x090: dirCode = 0; break;
|
||||
case sMoveUnit + 0x0F0: dirCode = 1; break;
|
||||
case sMoveUnit + 0x390: dirCode = 2; break;
|
||||
case sMoveUnit + 0x3F0: dirCode = 3; break;
|
||||
case sMoveUnit + 0x020: dirCode = 4; break;
|
||||
case sMoveUnit + 0x060: dirCode = 5; break;
|
||||
case sMoveUnit + 0x100: dirCode = 6; break;
|
||||
case sMoveUnit + 0x300: dirCode = 7; break;
|
||||
}
|
||||
std::uint32_t code;
|
||||
if (subject == m_state.lastMovingUnit)
|
||||
code = 1 + (dirCode << 1);
|
||||
else
|
||||
code = 6 + (dirCode << 3) + (static_cast<std::uint32_t>(subject) << 6);
|
||||
|
||||
if (m_state.moveCode == 0) {
|
||||
m_state.moveCode = code;
|
||||
} else if (m_state.moveCode & 1) { // FM + this
|
||||
m_state.moveCode += code << 4;
|
||||
putData(&m_state.moveCode, (code & 1) ? 1 : 3); // FM+FM : FM+M
|
||||
m_state.moveCode = 0;
|
||||
} else if (code & 1) { // M + FM
|
||||
m_state.moveCode += code << 18;
|
||||
putData(&m_state.moveCode, 3);
|
||||
m_state.moveCode = 0;
|
||||
} else { // M + M
|
||||
putData(&m_state.moveCode, 3);
|
||||
m_state.moveCode = code;
|
||||
}
|
||||
m_state.lastMovingUnit = subject;
|
||||
} else {
|
||||
completeMoveCode();
|
||||
std::uint32_t code;
|
||||
if (command >= cClientEx) {
|
||||
code = 2 + (static_cast<std::uint32_t>(command) << 3) + (static_cast<std::uint32_t>(player) << 19);
|
||||
putData(&code, 3);
|
||||
} else {
|
||||
code = (static_cast<std::uint32_t>(command - sExecute) << 2) +
|
||||
(static_cast<std::uint32_t>(player) << 16) + (static_cast<std::uint32_t>(subject) << 20);
|
||||
putData(&code, 4);
|
||||
}
|
||||
}
|
||||
if (command & static_cast<int>(CommandDataElementCountMask))
|
||||
putData(data, (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize);
|
||||
}
|
||||
|
||||
void CmdList::putDataChanges(int command, int player, const std::uint32_t *oldData, const std::uint32_t *newData,
|
||||
int dataSize)
|
||||
{
|
||||
if (dataSize <= 0)
|
||||
return;
|
||||
if (dataSize > MaxDataSize)
|
||||
dataSize = MaxDataSize;
|
||||
completeMoveCode();
|
||||
|
||||
int mapPos = m_state.nLog + 8;
|
||||
int logPos = mapPos + 4;
|
||||
std::uint32_t map0 = 0;
|
||||
for (int b0 = 0; b0 <= (dataSize - 1) / 32; ++b0) {
|
||||
ensurePadded(logPos + 4 * 32);
|
||||
map0 >>= 1;
|
||||
std::uint32_t map1 = 0;
|
||||
int rowEnd = dataSize - 1;
|
||||
if (rowEnd > b0 * 32 + 31)
|
||||
rowEnd = b0 * 32 + 31;
|
||||
for (int b1 = b0 * 32; b1 <= rowEnd; ++b1) {
|
||||
map1 >>= 1;
|
||||
if (newData[b1] != oldData[b1]) {
|
||||
writeU32(m_log, logPos, newData[b1]);
|
||||
logPos += 4;
|
||||
map1 += 0x80000000u;
|
||||
}
|
||||
}
|
||||
if (map1 > 0) {
|
||||
map1 >>= (b0 * 32 + 31 - rowEnd);
|
||||
writeU32(m_log, mapPos, map1);
|
||||
mapPos = logPos;
|
||||
logPos += 4;
|
||||
map0 += 0x80000000u;
|
||||
}
|
||||
}
|
||||
if (map0 == 0)
|
||||
return; // no changes
|
||||
|
||||
map0 >>= (31 - (dataSize - 1) / 32);
|
||||
writeU32(m_log, m_state.nLog + 4, map0);
|
||||
const std::uint32_t code =
|
||||
(static_cast<std::uint32_t>(command - sExecute) << 2) + (static_cast<std::uint32_t>(player) << 16);
|
||||
writeU32(m_log, m_state.nLog, code);
|
||||
m_state.nLog = mapPos;
|
||||
}
|
||||
|
||||
void CmdList::putData(const void *data, int length)
|
||||
{
|
||||
ensurePadded(m_state.nLog + length);
|
||||
std::memcpy(m_log.data() + m_state.nLog, data, length);
|
||||
m_state.nLog += length;
|
||||
}
|
||||
|
||||
void CmdList::completeMoveCode()
|
||||
{
|
||||
if (m_state.moveCode > 0) {
|
||||
putData(&m_state.moveCode, (m_state.moveCode & 1) ? 1 : 3); // Single FM : Single M
|
||||
m_state.moveCode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CmdList::loadFromFile(QIODevice &file)
|
||||
{
|
||||
std::int32_t nLog = 0;
|
||||
file.read(reinterpret_cast<char *>(&nLog), 4);
|
||||
m_state.nLog = nLog;
|
||||
m_log = QByteArray(nLog + 4, '\0'); // zero-filled trailing slack, see ensurePadded()
|
||||
file.read(m_log.data(), nLog);
|
||||
m_state.loadPos = 0;
|
||||
}
|
||||
|
||||
void CmdList::saveToFile(QIODevice &file)
|
||||
{
|
||||
completeMoveCode();
|
||||
const std::int32_t nLog = m_state.nLog;
|
||||
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
||||
file.write(m_log.constData(), nLog);
|
||||
}
|
||||
|
||||
void CmdList::appendToFile(QIODevice &file, const CmdListState &oldState)
|
||||
{
|
||||
completeMoveCode();
|
||||
const std::int32_t nLog = m_state.nLog;
|
||||
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
||||
file.seek(file.pos() + oldState.nLog);
|
||||
file.write(m_log.constData() + oldState.nLog, m_state.nLog - oldState.nLog);
|
||||
}
|
||||
|
||||
void CmdList::cut()
|
||||
{
|
||||
m_state.nLog = m_state.loadPos;
|
||||
}
|
||||
|
||||
int CmdList::progress() const
|
||||
{
|
||||
if (m_state.loadPos == m_state.nLog && m_state.loadMoveCode == 0)
|
||||
return 1000; // loading complete
|
||||
if (m_state.nLog > (1 << 20))
|
||||
return (m_state.loadPos >> 8) * 999 / (m_state.nLog >> 8);
|
||||
return m_state.loadPos * 999 / m_state.nLog;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,160 @@
|
||||
#include "Cevo/Database.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
namespace Database {
|
||||
|
||||
int lx = 0;
|
||||
int ly = 0;
|
||||
int mapSize = 0;
|
||||
|
||||
std::array<TPlayerContext, nPl> rw{};
|
||||
|
||||
std::array<std::uint32_t, lxmax * lymax> realMap{};
|
||||
std::array<std::int8_t, lxmax * lymax> occupant{};
|
||||
std::array<std::int8_t, lxmax * lymax> zocMap{};
|
||||
std::array<std::int32_t, lxmax * lymax> usedByCity{};
|
||||
|
||||
namespace {
|
||||
// Equivalent of Database.pas's private (implementation-section) `Primitive`
|
||||
// global - not exposed in the header, matching the original's own
|
||||
// interface/implementation visibility split.
|
||||
int primitive = 0;
|
||||
} // namespace
|
||||
|
||||
void maskD(std::uint32_t *x, int count, std::uint32_t mask)
|
||||
{
|
||||
for (int i = 0; i < count; ++i)
|
||||
x[i] &= mask;
|
||||
}
|
||||
|
||||
int dLoc(int loc, int dx, int dy)
|
||||
{
|
||||
// Faithful port of a likely upstream operator-precedence slip: Pascal's
|
||||
// `not` binds tighter than `and`, so this only negates the first clause
|
||||
// instead of the whole precondition. The very next line's assert
|
||||
// re-checks the FULL intended condition correctly, and this isn't a
|
||||
// memory-safety issue, so it's preserved as-is rather than silently
|
||||
// "fixed" (see established policy in repo memory).
|
||||
if (!(loc >= 0) && (loc < mapSize) && (dx + lx >= 0))
|
||||
throw std::runtime_error("Relative location error");
|
||||
assert((loc >= 0) && (loc < mapSize) && (dx + lx >= 0));
|
||||
|
||||
const int y0 = loc / lx;
|
||||
int result = (loc + ((dx + (y0 & 1) + lx + lx) >> 1)) % lx + lx * (y0 + dy);
|
||||
if (result < 0 || result >= mapSize)
|
||||
result = -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
void dxdy(int loc0, int loc1, int &dx, int &dy)
|
||||
{
|
||||
dx = (((loc1 % lx) * 2 + ((loc1 / lx) & 1)) - ((loc0 % lx) * 2 + ((loc0 / lx) & 1)) + 3 * lx) % (2 * lx) - lx;
|
||||
dy = loc1 / lx - loc0 / lx;
|
||||
}
|
||||
|
||||
int distance(int loc0, int loc1)
|
||||
{
|
||||
int dx, dy;
|
||||
dxdy(loc0, loc1, dx, dy);
|
||||
dx = std::abs(dx);
|
||||
dy = std::abs(dy);
|
||||
return dx + dy + (std::abs(dx - dy) >> 1);
|
||||
}
|
||||
|
||||
void v8ToLoc(int loc0, Vicinity8Loc &vicinityLoc)
|
||||
{
|
||||
const int lx0 = lx;
|
||||
int y0 = loc0 / lx0;
|
||||
const int x0 = loc0 - y0 * lx0;
|
||||
y0 &= 1;
|
||||
vicinityLoc[1] = loc0 + lx0 * 2;
|
||||
vicinityLoc[3] = loc0 - 1;
|
||||
vicinityLoc[5] = loc0 - lx0 * 2;
|
||||
vicinityLoc[7] = loc0 + 1;
|
||||
loc0 += y0;
|
||||
vicinityLoc[0] = loc0 + lx0;
|
||||
vicinityLoc[2] = loc0 + lx0 - 1;
|
||||
vicinityLoc[4] = loc0 - lx0 - 1;
|
||||
vicinityLoc[6] = loc0 - lx0;
|
||||
|
||||
// world is round!
|
||||
if (x0 < lx0 - 1) {
|
||||
if (x0 == 0) {
|
||||
vicinityLoc[3] += lx0;
|
||||
if (y0 == 0) {
|
||||
vicinityLoc[2] += lx0;
|
||||
vicinityLoc[4] += lx0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vicinityLoc[7] -= lx0;
|
||||
if (y0 == 1) {
|
||||
vicinityLoc[0] -= lx0;
|
||||
vicinityLoc[6] -= lx0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc)
|
||||
{
|
||||
const int y0 = loc0 / lx;
|
||||
int xComp0 = loc0 - y0 * lx - 1; // loc0 % lx - 1
|
||||
int xCompSwitch = xComp0 - 1 + (y0 & 1);
|
||||
if (xComp0 < 0)
|
||||
xComp0 += lx;
|
||||
if (xCompSwitch < 0)
|
||||
xCompSwitch += lx;
|
||||
xCompSwitch ^= xComp0;
|
||||
int yComp = lx * (y0 - 3);
|
||||
int *dst = vicinityLoc.data();
|
||||
int bit = 1;
|
||||
for (int dy = 0; dy < 7; ++dy) {
|
||||
xComp0 ^= xCompSwitch;
|
||||
int xComp = xComp0;
|
||||
for (int dx = 0; dx < 4; ++dx) {
|
||||
*dst = (bit & 0x67F7F76) != 0 ? xComp + yComp : -1;
|
||||
++xComp;
|
||||
if (xComp >= lx)
|
||||
xComp -= lx;
|
||||
++dst;
|
||||
bit <<= 1;
|
||||
}
|
||||
yComp += lx;
|
||||
}
|
||||
}
|
||||
|
||||
void calculatePrimitive()
|
||||
{
|
||||
primitive = 1;
|
||||
int i = 2;
|
||||
while (i * i <= mapSize + 1) { // test whether prime
|
||||
if ((mapSize + 1) % i == 0)
|
||||
primitive = 0;
|
||||
++i;
|
||||
}
|
||||
|
||||
if (primitive > 0) {
|
||||
int j = 0;
|
||||
do {
|
||||
++primitive;
|
||||
i = 1;
|
||||
j = 0;
|
||||
do {
|
||||
++j;
|
||||
i = i * primitive % (mapSize + 1);
|
||||
} while (!(i == 1 || j == mapSize + 1));
|
||||
} while (!(j == mapSize));
|
||||
}
|
||||
}
|
||||
|
||||
bool mapGeneratorAvailable()
|
||||
{
|
||||
return primitive > 0 && lx >= 20 && ly >= 40;
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
} // namespace Cevo
|
||||
@@ -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