From a35f1dbe324af658f46cbf664c73cb97cf17e033 Mon Sep 17 00:00:00 2001 From: drdkad Date: Sat, 26 Apr 2025 11:39:56 +0100 Subject: [PATCH] change m_outcomes from Array to std::vector and move them to GameRep --- src/games/game.cc | 10 ++++++++++ src/games/game.h | 4 +++- src/games/gameagg.h | 7 ------- src/games/gameexpl.cc | 14 -------------- src/games/gameexpl.h | 11 +---------- src/games/gametable.cc | 4 ++-- 6 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/games/game.cc b/src/games/game.cc index 001563503..7144e475c 100644 --- a/src/games/game.cc +++ b/src/games/game.cc @@ -221,6 +221,16 @@ Array GameRep::GetStrategies() const return ret; } +GameRep::~GameRep() +{ + for (auto player : m_players) { + player->Invalidate(); + } + for (auto outcome : m_outcomes) { + outcome->Invalidate(); + } +} + //------------------------------------------------------------------------ // GameRep: Writing data files //------------------------------------------------------------------------ diff --git a/src/games/game.h b/src/games/game.h index 920bf34c6..19d218957 100644 --- a/src/games/game.h +++ b/src/games/game.h @@ -420,6 +420,7 @@ class GameRep : public BaseGameRep { protected: std::vector m_players; + std::vector m_outcomes; std::string m_title, m_comment; unsigned int m_version{0}; @@ -435,7 +436,8 @@ class GameRep : public BaseGameRep { /// @name Lifecycle //@{ /// Clean up the game - ~GameRep() override = default; + ~GameRep() override; + /// Create a copy of the game, as a new game virtual Game Copy() const = 0; //@} diff --git a/src/games/gameagg.h b/src/games/gameagg.h index 78ae18ca9..64c59c079 100644 --- a/src/games/gameagg.h +++ b/src/games/gameagg.h @@ -39,13 +39,6 @@ class GameAGGRep : public GameRep { //@{ /// Constructor explicit GameAGGRep(std::shared_ptr); - /// Destructor - ~GameAGGRep() override - { - for (auto player : m_players) { - player->Invalidate(); - } - } /// Create a copy of the game, as a new game Game Copy() const override; //@} diff --git a/src/games/gameexpl.cc b/src/games/gameexpl.cc index 0830b00f9..5b11f78d2 100644 --- a/src/games/gameexpl.cc +++ b/src/games/gameexpl.cc @@ -32,20 +32,6 @@ namespace Gambit { // class GameExplicitRep //======================================================================== -//------------------------------------------------------------------------ -// GameExplicitRep: Lifecycle -//------------------------------------------------------------------------ - -GameExplicitRep::~GameExplicitRep() -{ - for (auto player : m_players) { - player->Invalidate(); - } - for (auto outcome : m_outcomes) { - outcome->Invalidate(); - } -} - //------------------------------------------------------------------------ // GameExplicitRep: General data access //------------------------------------------------------------------------ diff --git a/src/games/gameexpl.h b/src/games/gameexpl.h index 082207fcf..333c5d8b0 100644 --- a/src/games/gameexpl.h +++ b/src/games/gameexpl.h @@ -30,16 +30,7 @@ namespace Gambit { class GameExplicitRep : public GameRep { template friend class MixedStrategyProfile; -protected: - Array m_outcomes; - public: - /// @name Lifecycle - //@{ - /// Destructor - ~GameExplicitRep() override; - //@} - /// @name General data access //@{ /// Returns the smallest payoff to any player in any outcome of the game @@ -69,7 +60,7 @@ class GameExplicitRep : public GameRep { /// Returns the number of outcomes defined in the game size_t NumOutcomes() const override { return m_outcomes.size(); } /// Returns the index'th outcome defined in the game - GameOutcome GetOutcome(int index) const override { return m_outcomes[index]; } + GameOutcome GetOutcome(int index) const override { return m_outcomes.at(index - 1); } /// Creates a new outcome in the game GameOutcome NewOutcome() override; diff --git a/src/games/gametable.cc b/src/games/gametable.cc index 476467d5d..fc80f8e37 100644 --- a/src/games/gametable.cc +++ b/src/games/gametable.cc @@ -295,10 +295,10 @@ GameTableRep::GameTableRep(const Array &dim, bool p_sparseOutcomes /* = fal std::fill(m_results.begin(), m_results.end(), nullptr); } else { - m_outcomes = Array(m_results.size()); + m_outcomes = std::vector(m_results.size()); std::generate(m_outcomes.begin(), m_outcomes.end(), [this, outc = 1]() mutable { return new GameOutcomeRep(this, outc++); }); - m_results = m_outcomes; + std::copy(m_outcomes.begin(), m_outcomes.end(), m_results.begin()); } }