Skip to content

Commit 2f17932

Browse files
committed
Replace strategy contingency calculation with generic extent_product
1 parent 1b0b736 commit 2f17932

3 files changed

Lines changed: 29 additions & 25 deletions

File tree

src/games/game.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -892,14 +892,6 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
892892
}
893893
/// Remove the strategy from the game
894894
virtual void DeleteStrategy(const GameStrategy &p_strategy) { throw UndefinedException(); }
895-
/// Returns the number of strategy contingencies in the game
896-
int NumStrategyContingencies() const
897-
{
898-
BuildComputedValues();
899-
return std::transform_reduce(
900-
m_players.begin(), m_players.end(), 0, std::multiplies<>(),
901-
[](const std::shared_ptr<GamePlayerRep> &p) { return p->m_strategies.size(); });
902-
}
903895
/// Returns the total number of actions in the game
904896
virtual int BehavProfileLength() const = 0;
905897
//@}

src/games/gameobject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ template <class T, auto OuterMemFn, auto InnerMemFn> class NestedElementCollecti
335335
return result;
336336
}
337337

338+
/// @brief Returns the Cartesian product of the sizes of the inner ranges
339+
std::size_t extent_product() const
340+
{
341+
auto outer = (m_owner.get()->*OuterMemFn)();
342+
return std::accumulate(outer.begin(), outer.end(), static_cast<std::size_t>(1),
343+
[](std::size_t acc, const auto &element) {
344+
return acc * (element.get()->*InnerMemFn)().size();
345+
});
346+
}
347+
338348
iterator begin() const { return {m_owner, false}; }
339349
iterator end() const { return {m_owner, true}; }
340350
};

src/gui/gameframe.cc

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,14 +1111,15 @@ void GameFrame::OnViewStrategic(wxCommandEvent &p_event)
11111111
return;
11121112
}
11131113

1114-
const int ncont = m_doc->GetGame()->NumStrategyContingencies();
1115-
if (!m_nfgPanel && ncont >= 50000) {
1116-
if (wxMessageBox(
1117-
wxString::Format(wxT("This game has %d contingencies in strategic form.\n"), ncont) +
1118-
wxT("Performance in browsing strategic form will be poor,\n") +
1119-
wxT("and may render the program nonresponsive.\n") +
1120-
wxT("Do you wish to continue?"),
1121-
_("Large strategic game warning"), wxOK | wxCANCEL | wxALIGN_CENTER, this) != wxOK) {
1114+
if (const size_t contingencies = m_doc->GetGame()->GetStrategies().extent_product();
1115+
!m_nfgPanel && contingencies >= 50000) {
1116+
if (wxMessageBox(wxString::Format(wxT("This game has %d contingencies in strategic form.\n"),
1117+
contingencies) +
1118+
wxT("Performance in browsing strategic form will be poor,\n") +
1119+
wxT("and may render the program nonresponsive.\n") +
1120+
wxT("Do you wish to continue?"),
1121+
_("Large strategic game warning"), wxOK | wxCANCEL | wxALIGN_CENTER,
1122+
this) != wxOK) {
11221123
return;
11231124
}
11241125
}
@@ -1242,15 +1243,16 @@ void GameFrame::OnToolsEquilibrium(wxCommandEvent &)
12421243

12431244
if (dialog.ShowModal() == wxID_OK) {
12441245
if (dialog.UseStrategic()) {
1245-
const int ncont = m_doc->GetGame()->NumStrategyContingencies();
1246-
if (ncont >= 50000) {
1247-
if (wxMessageBox(wxString::Format(
1248-
wxT("This game has %d contingencies in strategic form.\n"), ncont) +
1249-
wxT("Performance in solving strategic form will be poor,\n") +
1250-
wxT("and may render the program nonresponsive.\n") +
1251-
wxT("Do you wish to continue?"),
1252-
_("Large strategic game warning"), wxOK | wxCANCEL | wxALIGN_CENTER,
1253-
this) != wxOK) {
1246+
if (const int contingencies = m_doc->GetGame()->GetStrategies().extent_product();
1247+
contingencies >= 50000) {
1248+
if (wxMessageBox(
1249+
wxString::Format(wxT("This game has %d contingencies in strategic form.\n"),
1250+
contingencies) +
1251+
wxT("Performance in solving strategic form will be poor,\n") +
1252+
wxT("and may render the program nonresponsive.\n") +
1253+
wxT("Do you wish to continue?"),
1254+
_("Large strategic game warning"), wxOK | wxCANCEL | wxALIGN_CENTER,
1255+
this) != wxOK) {
12541256
return;
12551257
}
12561258
}

0 commit comments

Comments
 (0)