Skip to content

Commit 9d654ed

Browse files
committed
Move InsertAction
1 parent 0e11628 commit 9d654ed

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

src/games/game.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ class GameInfosetRep : public GameObject {
215215
void SetLabel(const std::string &p_label) { m_label = p_label; }
216216
const std::string &GetLabel() const { return m_label; }
217217

218-
GameAction InsertAction(GameAction p_where = nullptr);
219-
220218
/// @name Actions
221219
//@{
222220
/// Returns the number of actions available at the information set
@@ -321,7 +319,7 @@ class GamePlayerRep : public GameObject {
321319
GameRep *m_game;
322320
int m_number;
323321
std::string m_label;
324-
std::vector<class GameInfosetRep *> m_infosets;
322+
std::vector<GameInfosetRep *> m_infosets;
325323
Array<GameStrategyRep *> m_strategies;
326324

327325
GamePlayerRep(GameRep *p_game, int p_id) : m_game(p_game), m_number(p_id) {}
@@ -546,6 +544,10 @@ class GameRep : public BaseGameRep {
546544
virtual void Reveal(GameInfoset, GamePlayer) { throw UndefinedException(); }
547545
virtual void SetInfoset(GameNode, GameInfoset) { throw UndefinedException(); }
548546
virtual GameInfoset LeaveInfoset(GameNode) { throw UndefinedException(); }
547+
virtual GameAction InsertAction(GameInfoset, GameAction p_where = nullptr)
548+
{
549+
throw UndefinedException();
550+
}
549551
virtual void DeleteAction(GameAction) { throw UndefinedException(); }
550552

551553
/// @name Dimensions of the game

src/games/gametree.cc

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,32 +213,35 @@ bool GameInfosetRep::Precedes(GameNode p_node) const
213213
return false;
214214
}
215215

216-
GameAction GameInfosetRep::InsertAction(GameAction p_action /* =0 */)
216+
GameAction GameTreeRep::InsertAction(GameInfoset p_infoset, GameAction p_action /* =nullptr */)
217217
{
218-
if (p_action && p_action->GetInfoset() != this) {
218+
if (p_action && p_action->GetInfoset() != p_infoset) {
219+
throw MismatchException();
220+
}
221+
if (p_infoset->m_efg != this) {
219222
throw MismatchException();
220223
}
221224

222-
m_efg->IncrementVersion();
223-
int where = m_actions.size() + 1;
225+
IncrementVersion();
226+
int where = p_infoset->m_actions.size() + 1;
224227
if (p_action) {
225-
for (where = 1; m_actions[where] != p_action; where++)
228+
for (where = 1; p_infoset->m_actions[where] != p_action; where++)
226229
;
227230
}
228231

229-
auto *action = new GameActionRep(where, "", this);
230-
m_actions.insert(std::next(m_actions.cbegin(), where - 1), action);
231-
if (m_player->IsChance()) {
232-
m_probs.insert(std::next(m_probs.cbegin(), where - 1), Number());
232+
auto *action = new GameActionRep(where, "", p_infoset);
233+
p_infoset->m_actions.insert(std::next(p_infoset->m_actions.cbegin(), where - 1), action);
234+
if (p_infoset->m_player->IsChance()) {
235+
p_infoset->m_probs.insert(std::next(p_infoset->m_probs.cbegin(), where - 1), Number());
233236
}
234-
RenumberActions();
235-
for (const auto &member : m_members) {
237+
p_infoset->RenumberActions();
238+
for (const auto &member : p_infoset->m_members) {
236239
member->m_children.insert(std::next(member->m_children.cbegin(), where - 1),
237-
new GameNodeRep(m_efg, member));
240+
new GameNodeRep(this, member));
238241
}
239242

240-
dynamic_cast<GameTreeRep *>(m_efg)->ClearComputedValues();
241-
dynamic_cast<GameTreeRep *>(m_efg)->Canonicalize();
243+
ClearComputedValues();
244+
Canonicalize();
242245
return action;
243246
}
244247

src/games/gametree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class GameTreeRep : public GameExplicitRep {
136136
void SetInfoset(GameNode, GameInfoset) override;
137137
GameInfoset LeaveInfoset(GameNode) override;
138138
Game SetChanceProbs(const GameInfoset &, const Array<Number> &) override;
139+
GameAction InsertAction(GameInfoset, GameAction p_where = nullptr) override;
139140
void DeleteAction(GameAction) override;
140141

141142
Game CopySubgame(GameNode) const override;

src/gui/gamedoc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ void gbtGameDocument::DoInsertAction(GameNode p_node)
671671
if (!p_node || !p_node->GetInfoset()) {
672672
return;
673673
}
674-
const GameAction action = p_node->GetInfoset()->InsertAction();
674+
const GameAction action = m_game->InsertAction(p_node->GetInfoset());
675675
action->SetLabel(std::to_string(action->GetNumber()));
676676
UpdateViews(GBT_DOC_MODIFIED_GAME);
677677
}

src/pygambit/gambit.pxd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ cdef extern from "games/game.h":
114114

115115
int NumActions() except +
116116
c_GameAction GetAction(int) except +IndexError
117-
c_GameAction InsertAction(c_GameAction) except +ValueError
118-
119117
c_Number GetActionProb(c_GameAction) except +IndexError
120118

121119
int NumMembers() except +
@@ -222,6 +220,7 @@ cdef extern from "games/game.h":
222220
void Reveal(c_GameInfoset, c_GamePlayer) except +
223221
void SetInfoset(c_GameNode, c_GameInfoset) except +ValueError
224222
c_GameInfoset LeaveInfoset(c_GameNode) except +
223+
c_GameAction InsertAction(c_GameInfoset, c_GameAction) except +ValueError
225224
void DeleteAction(c_GameAction) except +ValueError
226225
c_Game SetChanceProbs(c_GameInfoset, Array[c_Number]) except +
227226

src/pygambit/game.pxi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,14 +1679,15 @@ class Game:
16791679
"""
16801680
resolved_infoset = cython.cast(Infoset, self._resolve_infoset(infoset, "add_action"))
16811681
if before is None:
1682-
resolved_infoset.infoset.deref().InsertAction(cython.cast(c_GameAction, NULL))
1682+
self.game.deref().InsertAction(resolved_infoset.infoset,
1683+
cython.cast(c_GameAction, NULL))
16831684
else:
16841685
resolved_action = cython.cast(
16851686
Action, self._resolve_action(before, "add_action", "before")
16861687
)
16871688
if resolved_infoset != resolved_action.infoset:
16881689
raise MismatchError("add_action(): must specify an action from the same infoset")
1689-
resolved_infoset.infoset.deref().InsertAction(resolved_action.action)
1690+
self.game.deref().InsertAction(resolved_infoset.infoset, resolved_action.action)
16901691

16911692
def delete_action(self, action: typing.Union[Action, str]) -> None:
16921693
"""Deletes `action` from its information set. The subtrees which

0 commit comments

Comments
 (0)