Skip to content

Commit e870634

Browse files
committed
Experimental definition of Players collection as a member of GameRep
1 parent 7ab1937 commit e870634

6 files changed

Lines changed: 73 additions & 15 deletions

File tree

src/games/file.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ void ParsePayoffBody(GameFileLexer &p_parser, Game &p_nfg)
428428
{
429429
const StrategySupportProfile profile(p_nfg);
430430
for (auto iter : StrategyContingencies(profile)) {
431-
for (auto player : p_nfg->GetPlayers()) {
431+
for (const auto &player : p_nfg->GetPlayers()) {
432432
p_parser.ExpectCurrentToken(TOKEN_NUMBER, "numerical payoff");
433433
iter->GetOutcome()->SetPayoff(player, Number(p_parser.GetLastText()));
434434
p_parser.GetNextToken();

src/games/game.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace Gambit {
4141

4242
GameOutcomeRep::GameOutcomeRep(GameRep *p_game, int p_number) : m_game(p_game), m_number(p_number)
4343
{
44-
for (const auto &player : p_game->GetPlayers()) {
44+
for (const auto &player : m_game->m_players) {
4545
m_payoffs[player] = Number();
4646
}
4747
}
@@ -199,15 +199,6 @@ size_t GamePlayerRep::NumSequences() const
199199
// class GameRep
200200
//========================================================================
201201

202-
Array<GamePlayer> GameRep::GetPlayers() const
203-
{
204-
Array<GamePlayer> ret(NumPlayers());
205-
for (size_t pl = 1; pl <= NumPlayers(); pl++) {
206-
ret[pl] = GetPlayer(pl);
207-
}
208-
return ret;
209-
}
210-
211202
Array<GameStrategy> GameRep::GetStrategies() const
212203
{
213204
Array<GameStrategy> ret(MixedProfileLength());

src/games/game.h

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,63 @@ class GameRep : public BaseGameRep {
433433
//@}
434434

435435
public:
436+
class Players {
437+
Game m_game{nullptr};
438+
439+
public:
440+
class iterator {
441+
Game m_game{nullptr};
442+
size_t m_index{0};
443+
444+
public:
445+
using iterator_category = std::bidirectional_iterator_tag;
446+
using difference_type = std::ptrdiff_t;
447+
using value_type = GamePlayer;
448+
using pointer = value_type *;
449+
using reference = value_type &;
450+
451+
iterator() = default;
452+
iterator(const Game &p_game, size_t p_index = 0) : m_game(p_game), m_index(p_index) {}
453+
iterator(const iterator &) = default;
454+
~iterator() = default;
455+
iterator &operator=(const iterator &) = default;
456+
457+
bool operator==(const iterator &p_iter) const
458+
{
459+
return m_game == p_iter.m_game && m_index == p_iter.m_index;
460+
}
461+
bool operator!=(const iterator &p_iter) const
462+
{
463+
return m_game != p_iter.m_game || m_index != p_iter.m_index;
464+
}
465+
466+
iterator &operator++()
467+
{
468+
m_index++;
469+
return *this;
470+
}
471+
iterator &operator--()
472+
{
473+
m_index--;
474+
return *this;
475+
}
476+
GamePlayer operator*() const { return m_game->m_players.at(m_index); }
477+
};
478+
479+
Players() = default;
480+
explicit Players(const Game &p_game) : m_game(p_game) {}
481+
Players(const Players &) = default;
482+
~Players() = default;
483+
Players &operator=(const Players &) = default;
484+
485+
size_t size() const { return m_game->m_players.size(); }
486+
487+
iterator begin() const { return {m_game, 0}; }
488+
iterator end() const { return {m_game, (m_game) ? m_game->m_players.size() : 0}; }
489+
iterator cbegin() const { return {m_game, 0}; }
490+
iterator cend() const { return {m_game, (m_game) ? m_game->m_players.size() : 0}; }
491+
};
492+
436493
/// @name Lifecycle
437494
//@{
438495
/// Clean up the game
@@ -599,7 +656,7 @@ class GameRep : public BaseGameRep {
599656
/// Returns the pl'th player in the game
600657
GamePlayer GetPlayer(int pl) const { return m_players.at(pl - 1); }
601658
/// Returns the set of players in the game
602-
Array<GamePlayer> GetPlayers() const;
659+
Players GetPlayers() const { return Players(this); }
603660
/// Returns the chance (nature) player
604661
virtual GamePlayer GetChance() const = 0;
605662
/// Creates a new player in the game, with no moves

src/games/stratspt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class StrategySupportProfile {
102102
/// Returns the number of players in the game
103103
int NumPlayers() const { return m_nfg->NumPlayers(); }
104104
/// Returns the set of players in the game
105-
Array<GamePlayer> GetPlayers() const { return m_nfg->GetPlayers(); }
105+
GameRep::Players GetPlayers() const { return m_nfg->GetPlayers(); }
106106
/// Returns the set of strategies in the support for a player
107107
Support GetStrategies(const GamePlayer &p_player) const { return {this, p_player}; }
108108

src/pygambit/gambit.pxd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ cdef extern from "games/game.h":
167167
c_GameAction GetPriorAction() except +
168168

169169
cdef cppclass c_GameRep "GameRep":
170+
cppclass Players:
171+
cppclass iterator:
172+
c_GamePlayer operator *()
173+
iterator operator++()
174+
bint operator ==(iterator)
175+
bint operator !=(iterator)
176+
int size() except +
177+
iterator begin() except +
178+
iterator end() except +
179+
170180
int IsTree() except +
171181

172182
string GetTitle() except +
@@ -177,7 +187,7 @@ cdef extern from "games/game.h":
177187

178188
int NumPlayers() except +
179189
c_GamePlayer GetPlayer(int) except +IndexError
180-
Array[c_GamePlayer] GetPlayers() except +
190+
Players GetPlayers() except +
181191
c_GamePlayer GetChance() except +
182192
c_GamePlayer NewPlayer() except +
183193

src/solvers/enumpoly/gameseq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class GameSequenceForm {
269269

270270
Contingencies GetContingencies() const { return {this}; }
271271

272-
Array<GamePlayer> GetPlayers() const { return m_support.GetGame()->GetPlayers(); }
272+
GameRep::Players GetPlayers() const { return m_support.GetGame()->GetPlayers(); }
273273

274274
Infosets GetInfosets() const { return {this}; }
275275

0 commit comments

Comments
 (0)