Puisque demandé, switch à un tableau manuel.

This commit is contained in:
Timothée Leclaire-Fournier 2024-01-22 19:05:17 -05:00
parent 4095ffba07
commit 8fcd5630fb
4 changed files with 46 additions and 28 deletions

View File

@ -99,6 +99,7 @@ GOL::ImplementationInformation GOLTeamH::information() const
void GOLTeamH::resize(size_t width, size_t height, State defaultState)
{
mData.resize(width, height, defaultState);
drawBorder();
}
//! \brief Mutateur modifiant la règle de la simulation.
@ -215,8 +216,12 @@ void GOLTeamH::setBorderManagement(BorderManagement borderManagement)
{
mBorderManagement = borderManagement;
mIteration = 0;
drawBorder();
}
switch (borderManagement) {
void GOLTeamH::drawBorder()
{
switch (mBorderManagement.value_or(GOL::BorderManagement::foreverDead)) {
case GOL::BorderManagement::foreverDead:
mData.fillBorder(GridTeamH::CellType::dead);
break;
@ -389,10 +394,10 @@ void GOLTeamH::processOneStep()
// On commence à la première case qui n'est pas dans le border
// Pointeur du tableau intermédiaire.
auto* ptrGridInt{ reinterpret_cast<uint8_t*>(&(mData.intData()[0])) + (offset + 1) };
auto* ptrGridInt{ reinterpret_cast<uint8_t*>(mData.intData()) + (offset + 1) };
// Pointeur qui se promène en mémoire.
auto* ptrGrid{ reinterpret_cast<uint8_t*>(&(mData.data()[0])) };
auto* ptrGrid{ reinterpret_cast<uint8_t*>(mData.data()) };
for (size_t j{ 1 }; j < heightNoBorder + 1; ++j) {
for (size_t i{ 1 }; i < widthNoBorder + 1; ++i) {
@ -495,7 +500,7 @@ void GOLTeamH::updateImage(uint32_t* buffer, size_t buffer_size) const
auto* s_ptr{ buffer }, * e_ptr{ buffer + buffer_size };
// Pointeur qui se promène en mémoire.
auto* ptrGrid{ reinterpret_cast<const uint8_t*>(&mData.data()[0]) };
auto* ptrGrid{ reinterpret_cast<const uint8_t*>(mData.data()) };
// On itère sur chaque éléments du tableau et on associe la couleur.
while (s_ptr < e_ptr) {

View File

@ -27,6 +27,7 @@ public:
void resize(size_t width, size_t height, State defaultState) override;
bool setRule(std::string const& rule) override;
void setBorderManagement(BorderManagement borderManagement) override;
void drawBorder();
void setState(int x, int y, State state) override;
void fill(State state) override;
void fillAlternately(State firstCell) override;

View File

@ -11,6 +11,7 @@ GridTeamH::GridTeamH()
GridTeamH::GridTeamH(size_t width, size_t height, CellType initValue)
:mWidth{ width }, mHeight{ height }, mEngine(mRandomDevice()), mDistribution(0.0, 1.0), mAliveCount{}
, mData{}, mIntermediateData{}
{
resize(width, height, initValue);
}
@ -18,20 +19,30 @@ GridTeamH::GridTeamH(size_t width, size_t height, CellType initValue)
// Destructeur Grid
GridTeamH::~GridTeamH()
{
dealloc();
}
// Mutateur modifiant la taille de la grille et initialise le contenu par la valeur spécifiée.
void GridTeamH::resize(size_t width, size_t height, CellType initValue)
{
if (mData)
dealloc();
mWidth = width;
mHeight = height;
mData.resize(width * height);
mIntermediateData.resize(width * height);
mData = new CellType[width * height];
mIntermediateData = new CellType[width * height];
fill(initValue, true);
}
void GridTeamH::dealloc()
{
delete[] mData;
delete[] mIntermediateData;
}
// Accesseur retournant la valeur d'une cellule à une certaine coordonnée.
GridTeamH::CellType GridTeamH::value(int column, int row) const
{
@ -114,15 +125,14 @@ float GridTeamH::totalAliveRel() const
void GridTeamH::fill(CellType value, bool fillBorder)
{
if (fillBorder) {
for (auto& i : mData)
i = value;
}
else {
for (size_t i{ 1 }; i < mWidth - 1; i++) {
for (size_t j{ 1 }; j < mHeight - 1; ++j) {
for (size_t i{}; i < mWidth; i++)
for (size_t j{}; j < mHeight; ++j)
mData[i + (j * mWidth)] = value;
}
}
else {
for (size_t i{ 1 }; i < mWidth - 1; i++)
for (size_t j{ 1 }; j < mHeight - 1; ++j)
mData[i + (j * mWidth)] = value;
}
}
@ -133,24 +143,23 @@ void GridTeamH::fillAternately(CellType initValue, bool fillBorder)
auto otherValue = (initValue == CellType::alive) ? CellType::dead : CellType::alive;
if (fillBorder) {
for (size_t i{}; i < mData.size(); i++)
mData[i] = !(i % 2) ? initValue : otherValue;
}
else {
for (size_t i{ 1 }; i < mWidth - 1; i++) {
for (size_t j{ 1 }; j < mHeight - 1; ++j) {
for (size_t i{}; i < mWidth; i++)
for (size_t j{}; j < mHeight; ++j)
mData[i + (j * mWidth)] = !(i % 2) ? initValue : otherValue;
}
}
else {
for (size_t i{ 1 }; i < mWidth - 1; i++)
for (size_t j{ 1 }; j < mHeight - 1; ++j)
mData[i + (j * mWidth)] = !(i % 2) ? initValue : otherValue;
}
}
void GridTeamH::randomize(double percentAlive, bool fillBorder)
{
if (fillBorder) {
for (auto& i : mData) {
i = static_cast<GridTeamH::CellType>(mDistribution(mEngine) < percentAlive);
}
for (size_t i{}; i < mWidth; i++)
for (size_t j{}; j < mHeight; ++j)
mData[i + (j * mWidth)] = static_cast<GridTeamH::CellType>(mDistribution(mEngine) < percentAlive);
}
else {
for (size_t i{ 1 }; i < mWidth - 1; i++) {
@ -163,11 +172,11 @@ void GridTeamH::randomize(double percentAlive, bool fillBorder)
void GridTeamH::fillBorder(CellType value)
{
fillBorderManipulations(&mData.front(), value);
fillBorderManipulations(&mIntermediateData.front(), value);
fillBorderManipulations(mData, value);
fillBorderManipulations(mIntermediateData, value);
}
void GridTeamH::fillBorderManipulations(CellType* ptr, CellType value) const
void GridTeamH::fillBorderManipulations(DataType ptr, CellType value) const
{
auto* e_ptr = ptr + (mWidth - 1);
@ -214,7 +223,9 @@ void GridTeamH::fillBorderMirror()
void GridTeamH::switchToIntermediate()
{
// Swap pour la performance.
mData.swap(mIntermediateData);
auto* temp{ mData };
mData = mIntermediateData;
mIntermediateData = temp;
}

View File

@ -15,7 +15,7 @@ class GridTeamH
public:
// Définition des types
using CellType = GOL::State;
using DataType = std::vector<CellType>;
using DataType = CellType *;
// Définition des constructeurs / destructeur
GridTeamH();
@ -34,6 +34,7 @@ public:
size_t aliveCount() const { return mAliveCount; }
void resize(size_t width, size_t height, CellType initValue = CellType{});
void dealloc();
// Accesseurs et mutateurs des cellules
CellType value(int column, int row) const;
@ -76,5 +77,5 @@ private:
std::mt19937 mEngine;
std::uniform_real_distribution<> mDistribution;
void fillBorderManipulations(CellType* ptr, CellType value) const;
void fillBorderManipulations(DataType ptr, CellType value) const;
};