Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <algorithm>
#include <numeric>
#include <map>
#include <optional>

namespace Gambit {

Expand Down Expand Up @@ -135,6 +136,60 @@ template <typename Container, typename T> class exclude_value {
iterator m_begin, m_end;
};

template <typename Value, typename Range> class prepend_value {
public:
using Iter = decltype(std::begin(std::declval<Range &>()));

class iterator {
public:
using iterator_category = std::forward_iterator_tag;
;
using value_type = Value;
using difference_type = std::ptrdiff_t;
using reference = Value;
using pointer = Value;

iterator(std::optional<Value> first, Iter current, Iter end)
: m_first(std::move(first)), m_current(current), m_end(end)
{
}

reference operator*() const { return m_first ? *m_first : *m_current; }

iterator &operator++()
{
if (m_first) {
m_first.reset();
}
else {
++m_current;
}
return *this;
}

bool operator==(const iterator &other) const
{
return m_first == other.m_first && m_current == other.m_current;
}

bool operator!=(const iterator &other) const { return !(*this == other); }

private:
std::optional<Value> m_first;
Iter m_current, m_end;
};

prepend_value(Value first, Range range) : m_first(first), m_range(std::move(range)) {}

iterator begin() const { return {m_first, std::begin(m_range), std::end(m_range)}; }

iterator end() const { return {std::nullopt, std::end(m_range), std::end(m_range)}; }

private:
Value m_first;
Range m_range;
};

/// @brief Returns the maximum value of the function over the *non-empty* container
template <class Container, class Func>
auto maximize_function(const Container &p_container, const Func &p_function)
Expand Down
3 changes: 1 addition & 2 deletions src/games/behavspt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ BehaviorSupportProfile::BehaviorSupportProfile(const Game &p_efg) : m_efg(p_efg)
}

// Initialize the list of reachable information sets and nodes
for (size_t pl = 0; pl <= GetGame()->NumPlayers(); pl++) {
const GamePlayer player = (pl == 0) ? GetGame()->GetChance() : GetGame()->GetPlayer(pl);
for (const auto &player : p_efg->GetPlayersWithChance()) {
for (const auto &infoset : player->GetInfosets()) {
m_infosetReachable[infoset] = true;
for (const auto &member : infoset->GetMembers()) {
Expand Down
1 change: 1 addition & 0 deletions src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
}
/// Returns the chance (nature) player
virtual GamePlayer GetChance() const = 0;
auto GetPlayersWithChance() const { return prepend_value(GetChance(), GetPlayers()); }
/// Creates a new player in the game, with no moves
virtual GamePlayer NewPlayer() = 0;
//@}
Expand Down