initial port for Cevo game in C++ (libs)

This commit is contained in:
Geoffray Levasseur committed 2026-10-22 17:20:45 +02:00
commit 8436242032
375 files changed
+46282

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#include "Cevo/Pile.h"
#include <algorithm>
#include <cassert>
namespace Cevo {
void Pile::create(int size)
{
assert(size <= MaxSize);
if (n_ != 0 || size > currentSize_) {
std::fill_n(ix_, size, -1);
n_ = 0;
}
currentSize_ = size;
}
void Pile::empty()
{
if (n_ != 0) {
std::fill_n(ix_, currentSize_, -1);
n_ = 0;
}
}
// Parent(i) = (i-1)/2.
bool Pile::put(int item, int value) // O(lg(n))
{
assert(item < currentSize_);
int i = ix_[item];
if (i >= 0) {
if (bh_[i].value <= value)
return false;
} else {
i = n_;
++n_;
}
while (i > 0) {
const int j = (i - 1) >> 1; // Parent(i) = (i-1)/2
if (value >= bh_[j].value)
break;
bh_[i] = bh_[j];
ix_[bh_[i].item] = i;
i = j;
}
// Insert the new item at the insertion point found.
bh_[i].value = value;
bh_[i].item = item;
ix_[bh_[i].item] = i;
return true;
}
bool Pile::testPut(int item, int value) const
{
assert(item < currentSize_);
const int i = ix_[item];
return i < 0 || bh_[i].value > value;
}
// Left(i) = 2*i+1, Right(i) = Left(i)+1.
bool Pile::get(int &item, int &value) // O(lg(n))
{
if (n_ == 0)
return false;
item = bh_[0].item;
value = bh_[0].value;
ix_[item] = -1;
--n_;
if (n_ > 0) {
const HeapItem last = bh_[n_];
int i = 0;
int j = 1;
while (j < n_) {
if (j < n_ - 1 && bh_[j].value > bh_[j + 1].value)
++j; // Right(i) = Left(i)+1
if (last.value <= bh_[j].value)
break;
bh_[i] = bh_[j];
ix_[bh_[i].item] = i;
i = j;
j = (j << 1) + 1; // Left(j) = 2*j+1
}
// Insert the root in the correct place in the heap.
bh_[i] = last;
ix_[last.item] = i;
}
return true;
}
} // namespace Cevo