Skip to content

Commit 737dcc6

Browse files
committed
Store results in GameTableRep in a std::vector instead of Array
1 parent ca9256b commit 737dcc6

2 files changed

Lines changed: 16 additions & 31 deletions

File tree

src/games/gametable.cc

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Gambit {
3434

3535
class TablePureStrategyProfileRep : public PureStrategyProfileRep {
3636
protected:
37-
long m_index{1L};
37+
long m_index{0L};
3838

3939
std::shared_ptr<PureStrategyProfileRep> Copy() const override;
4040

@@ -166,13 +166,13 @@ T TableMixedStrategyProfileRep<T>::GetPayoff(int pl, int index, int current) con
166166
return outcome->GetPayoff<T>(this->m_support.GetGame()->GetPlayer(pl));
167167
}
168168
else {
169-
return T(0);
169+
return static_cast<T>(0);
170170
}
171171
}
172172

173173
T sum = static_cast<T>(0);
174174
for (auto s : this->m_support.GetStrategies(this->m_support.GetGame()->GetPlayer(current))) {
175-
if ((*this)[s] != T(0)) {
175+
if ((*this)[s] != static_cast<T>(0)) {
176176
sum += ((*this)[s] * GetPayoff(pl, index + s->m_offset, current + 1));
177177
}
178178
}
@@ -181,7 +181,7 @@ T TableMixedStrategyProfileRep<T>::GetPayoff(int pl, int index, int current) con
181181

182182
template <class T> T TableMixedStrategyProfileRep<T>::GetPayoff(int pl) const
183183
{
184-
return GetPayoff(pl, 1, 1);
184+
return GetPayoff(pl, 0, 1);
185185
}
186186

187187
template <class T>
@@ -211,8 +211,9 @@ void TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, int const_pl, int c
211211
template <class T>
212212
T TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &strategy) const
213213
{
214-
T value = T(0);
215-
GetPayoffDeriv(pl, strategy->GetPlayer()->GetNumber(), 1, strategy->m_offset + 1, T(1), value);
214+
T value = static_cast<T>(0);
215+
GetPayoffDeriv(pl, strategy->GetPlayer()->GetNumber(), 1, strategy->m_offset, static_cast<T>(1),
216+
value);
216217
return value;
217218
}
218219

@@ -249,12 +250,12 @@ T TableMixedStrategyProfileRep<T>::GetPayoffDeriv(int pl, const GameStrategy &st
249250
GamePlayerRep *player1 = strategy1->GetPlayer();
250251
GamePlayerRep *player2 = strategy2->GetPlayer();
251252
if (player1 == player2) {
252-
return T(0);
253+
return static_cast<T>(0);
253254
}
254255

255-
T value = T(0);
256+
T value = static_cast<T>(0);
256257
GetPayoffDeriv(pl, player1->GetNumber(), player2->GetNumber(), 1,
257-
strategy1->m_offset + strategy2->m_offset + 1, T(1), value);
258+
strategy1->m_offset + strategy2->m_offset, static_cast<T>(1), value);
258259
return value;
259260
}
260261

@@ -265,23 +266,9 @@ template class TableMixedStrategyProfileRep<Rational>;
265266
// GameTableRep: Lifecycle
266267
//------------------------------------------------------------------------
267268

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-
282269
GameTableRep::GameTableRep(const Array<int> &dim, bool p_sparseOutcomes /* = false */)
270+
: m_results(std::accumulate(dim.begin(), dim.end(), 1, std::multiplies<>()))
283271
{
284-
m_results = Array<GameOutcomeRep *>(Product(dim));
285272
for (size_t pl = 1; pl <= dim.size(); pl++) {
286273
m_players.push_back(new GamePlayerRep(this, pl, dim[pl]));
287274
m_players.back()->m_label = lexical_cast<std::string>(pl);
@@ -488,12 +475,12 @@ void GameTableRep::RebuildTable()
488475
size *= player->m_strategies.size();
489476
}
490477

491-
Array<GameOutcomeRep *> newResults(size);
478+
std::vector<GameOutcomeRep *> newResults(size);
492479
std::fill(newResults.begin(), newResults.end(), nullptr);
493480

494481
for (auto iter :
495482
StrategyContingencies(StrategySupportProfile(const_cast<GameTableRep *>(this)))) {
496-
long newindex = 1L;
483+
long newindex = 0L;
497484
for (const auto &player : m_players) {
498485
if (iter->GetStrategy(player)->m_offset < 0) {
499486
// This is a contingency involving a new strategy... skip
@@ -505,13 +492,11 @@ void GameTableRep::RebuildTable()
505492
}
506493
}
507494

508-
if (newindex >= 1) {
495+
if (newindex >= 0L) {
509496
newResults[newindex] = iter->GetOutcome();
510497
}
511498
}
512-
513-
m_results = newResults;
514-
499+
std::copy(newResults.begin(), newResults.end(), m_results.begin());
515500
IndexStrategies();
516501
}
517502

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)