From 8fcd5630fb1a0944829f760eabb0a5160d55df52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Leclaire-Fournier?= Date: Mon, 22 Jan 2024 19:05:17 -0500 Subject: [PATCH] =?UTF-8?q?Puisque=20demand=C3=A9,=20switch=20=C3=A0=20un?= =?UTF-8?q?=20tableau=20manuel.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GPA675Lab1GOL/GOLTeamH.cpp | 13 ++++++--- GPA675Lab1GOL/GOLTeamH.h | 1 + GPA675Lab1GOL/GridTeamH.cpp | 55 ++++++++++++++++++++++--------------- GPA675Lab1GOL/GridTeamH.h | 5 ++-- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/GPA675Lab1GOL/GOLTeamH.cpp b/GPA675Lab1GOL/GOLTeamH.cpp index 0906ed0..0d90db7 100644 --- a/GPA675Lab1GOL/GOLTeamH.cpp +++ b/GPA675Lab1GOL/GOLTeamH.cpp @@ -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(&(mData.intData()[0])) + (offset + 1) }; + auto* ptrGridInt{ reinterpret_cast(mData.intData()) + (offset + 1) }; // Pointeur qui se promène en mémoire. - auto* ptrGrid{ reinterpret_cast(&(mData.data()[0])) }; + auto* ptrGrid{ reinterpret_cast(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(&mData.data()[0]) }; + auto* ptrGrid{ reinterpret_cast(mData.data()) }; // On itère sur chaque éléments du tableau et on associe la couleur. while (s_ptr < e_ptr) { diff --git a/GPA675Lab1GOL/GOLTeamH.h b/GPA675Lab1GOL/GOLTeamH.h index 62857fe..23faa11 100644 --- a/GPA675Lab1GOL/GOLTeamH.h +++ b/GPA675Lab1GOL/GOLTeamH.h @@ -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; diff --git a/GPA675Lab1GOL/GridTeamH.cpp b/GPA675Lab1GOL/GridTeamH.cpp index 297aa4f..0d1b29e 100644 --- a/GPA675Lab1GOL/GridTeamH.cpp +++ b/GPA675Lab1GOL/GridTeamH.cpp @@ -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; + 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) { + 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; + 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) { + 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(mDistribution(mEngine) < percentAlive); - } + for (size_t i{}; i < mWidth; i++) + for (size_t j{}; j < mHeight; ++j) + mData[i + (j * mWidth)] = static_cast(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; } diff --git a/GPA675Lab1GOL/GridTeamH.h b/GPA675Lab1GOL/GridTeamH.h index c94736e..b51d579 100644 --- a/GPA675Lab1GOL/GridTeamH.h +++ b/GPA675Lab1GOL/GridTeamH.h @@ -15,7 +15,7 @@ class GridTeamH public: // Définition des types using CellType = GOL::State; - using DataType = std::vector; + 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; };