Skip to content

Commit aca7fef

Browse files
committed
Store results in a std::vector
1 parent ca9256b commit aca7fef

2 files changed

Lines changed: 12 additions & 28 deletions

File tree

src/games/gametable.cc

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ void TablePureStrategyProfileRep::SetStrategy(const GameStrategy &s)
8181

8282
GameOutcome TablePureStrategyProfileRep::GetOutcome() const
8383
{
84-
return dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index];
84+
return dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index - 1];
8585
}
8686

8787
void TablePureStrategyProfileRep::SetOutcome(GameOutcome p_outcome)
8888
{
89-
dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index] = p_outcome;
89+
dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index - 1] = p_outcome;
9090
}
9191

9292
Rational TablePureStrategyProfileRep::GetPayoff(const GamePlayer &p_player) const
9393
{
94-
GameOutcomeRep *outcome = dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index];
94+
GameOutcomeRep *outcome = dynamic_cast<GameTableRep &>(*m_nfg).m_results[m_index - 1];
9595
if (outcome) {
9696
return outcome->GetPayoff<Rational>(p_player);
9797
}
@@ -105,7 +105,7 @@ Rational TablePureStrategyProfileRep::GetStrategyValue(const GameStrategy &p_str
105105
const auto &player = p_strategy->GetPlayer();
106106
GameOutcomeRep *outcome =
107107
dynamic_cast<GameTableRep &>(*m_nfg)
108-
.m_results[m_index - m_profile.at(player)->m_offset + p_strategy->m_offset];
108+
.m_results[m_index - m_profile.at(player)->m_offset + p_strategy->m_offset - 1];
109109
if (outcome) {
110110
return outcome->GetPayoff<Rational>(player);
111111
}
@@ -161,7 +161,7 @@ T TableMixedStrategyProfileRep<T>::GetPayoff(int pl, int index, int current) con
161161
if (current > static_cast<int>(this->m_support.GetGame()->NumPlayers())) {
162162
const Game game = this->m_support.GetGame();
163163
auto &g = dynamic_cast<GameTableRep &>(*game);
164-
GameOutcomeRep *outcome = g.m_results[index];
164+
GameOutcomeRep *outcome = g.m_results[index - 1];
165165
if (outcome) {
166166
return outcome->GetPayoff<T>(this->m_support.GetGame()->GetPlayer(pl));
167167
}
@@ -194,7 +194,7 @@ void TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, int const_pl, int c
194194
if (cur_pl > static_cast<int>(this->m_support.GetGame()->NumPlayers())) {
195195
const Game game = this->m_support.GetGame();
196196
auto &g = dynamic_cast<GameTableRep &>(*game);
197-
GameOutcomeRep *outcome = g.m_results[index];
197+
GameOutcomeRep *outcome = g.m_results[index - 1];
198198
if (outcome) {
199199
value += prob * outcome->GetPayoff<T>(this->m_support.GetGame()->GetPlayer(pl));
200200
}
@@ -227,7 +227,7 @@ void TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, int const_pl1, int
227227
if (cur_pl > static_cast<int>(this->m_support.GetGame()->NumPlayers())) {
228228
const Game game = this->m_support.GetGame();
229229
auto &g = dynamic_cast<GameTableRep &>(*game);
230-
GameOutcomeRep *outcome = g.m_results[index];
230+
GameOutcomeRep *outcome = g.m_results[index - 1];
231231
if (outcome) {
232232
value += prob * outcome->GetPayoff<T>(this->m_support.GetGame()->GetPlayer(pl));
233233
}
@@ -265,23 +265,9 @@ template class TableMixedStrategyProfileRep<Rational>;
265265
// GameTableRep: Lifecycle
266266
//------------------------------------------------------------------------
267267

268-
namespace {
269-
/// This convenience function computes the Cartesian product of the
270-
/// elements in dim.
271-
int Product(const Array<int> &dim)
272-
{
273-
int accum = 1;
274-
for (auto d : dim) {
275-
accum *= d;
276-
}
277-
return accum;
278-
}
279-
280-
} // end anonymous namespace
281-
282268
GameTableRep::GameTableRep(const Array<int> &dim, bool p_sparseOutcomes /* = false */)
269+
: m_results(std::accumulate(dim.begin(), dim.end(), 1, std::multiplies<>()))
283270
{
284-
m_results = Array<GameOutcomeRep *>(Product(dim));
285271
for (size_t pl = 1; pl <= dim.size(); pl++) {
286272
m_players.push_back(new GamePlayerRep(this, pl, dim[pl]));
287273
m_players.back()->m_label = lexical_cast<std::string>(pl);
@@ -488,7 +474,7 @@ void GameTableRep::RebuildTable()
488474
size *= player->m_strategies.size();
489475
}
490476

491-
Array<GameOutcomeRep *> newResults(size);
477+
std::vector<GameOutcomeRep *> newResults(size);
492478
std::fill(newResults.begin(), newResults.end(), nullptr);
493479

494480
for (auto iter :
@@ -501,17 +487,15 @@ void GameTableRep::RebuildTable()
501487
break;
502488
}
503489
else {
504-
newindex += (iter->GetStrategy(player)->m_number - 1) * offsets[player->GetNumber()];
490+
newindex += (iter->GetStrategy(player)->m_number - 1) * offsets[player->m_number];
505491
}
506492
}
507493

508494
if (newindex >= 1) {
509-
newResults[newindex] = iter->GetOutcome();
495+
newResults[newindex - 1] = iter->GetOutcome();
510496
}
511497
}
512-
513498
m_results = newResults;
514-
515499
IndexStrategies();
516500
}
517501

src/games/gametable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GameTableRep : public GameExplicitRep {
3636
template <class T> friend class TableMixedStrategyProfileRep;
3737

3838
private:
39-
Array<GameOutcomeRep *> m_results;
39+
std::vector<GameOutcomeRep *> m_results;
4040

4141
/// @name Private auxiliary functions
4242
//@{

0 commit comments

Comments
 (0)