Skip to content

Commit

Permalink
Place all of src under clang-format checks in pre-commit, add clang-f…
Browse files Browse the repository at this point in the history
…ormat check to CI.
  • Loading branch information
tturocy committed Feb 9, 2024
1 parent d2e38f7 commit 7133074
Show file tree
Hide file tree
Showing 97 changed files with 41,050 additions and 38,690 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ on:
pull_request:

jobs:
clang-format:
name: clang-format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run clang-format style check for C/C++
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: '17'
check-path: 'src'
include-regex: '^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))|(imp))$'

clang-tidy:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
Expand Down
16 changes: 4 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ repos:
hooks:
- id: cython-lint
- id: double-quote-cython-strings
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
hooks:
- id: clang-format
files: src\/(gambit|core|gui|solvers|tools)
args: ["-i"]
# - id: clang-tidy
# args: ["--", "-I src", "-I", "src/labenski/include"]
# - id: oclint
# - id: uncrustify
# - id: cppcheck
# args: ["--suppressions-list=.cppcheck", "-Isrc", "-Isrc/labenski/include"]
# - id: cpplint
# - id: include-what-you-use
files: ^src\/.*\.(c|cc|cpp|h|imp)$
types_or: [c++, c]
3 changes: 1 addition & 2 deletions src/gambit.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ inline double abs(double x) { return std::fabs(x); }

inline double sqr(double x) { return x * x; }

template <class Key, class T>
bool contains(const std::map<Key, T>& map, const Key& key)
template <class Key, class T> bool contains(const std::map<Key, T> &map, const Key &key)
// TODO: remove when we move to C++20 which already includes a "contains" method
{
return map.find(key) != map.end();
Expand Down
12 changes: 6 additions & 6 deletions src/games/agg/agg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ AGG::AGG(int numPlayers, std::vector<int> &_actions, int numANodes, int _numPNod
vector<projtype> &projTypes, vector<vector<aggdistrib>> &projS,
vector<vector<vector<config>>> &proj, vector<vector<projtype>> &projF,
vector<vector<vector<int>>> &Po, vector<aggdistrib> &P, vector<aggpayoff> &_payoffs)
: numPlayers(numPlayers), totalActions(0), maxActions(0),
numActionNodes(numANodes), numPNodes(_numPNodes),
actionSets(_actionSets), neighbors(neighb), projectionTypes(projTypes), payoffs(_payoffs),
projection(proj), projectedStrat(projS), fullProjectedStrat(projS), projFunctions(projF),
Porder(Po), Pr(P), isPure(numANodes, true), node2Action(numANodes, vector<int>(numPlayers)),
cache(numPlayers + 1), player2Class(numPlayers), kSymStrategyOffset(1, 0)
: numPlayers(numPlayers), totalActions(0), maxActions(0), numActionNodes(numANodes),
numPNodes(_numPNodes), actionSets(_actionSets), neighbors(neighb), projectionTypes(projTypes),
payoffs(_payoffs), projection(proj), projectedStrat(projS), fullProjectedStrat(projS),
projFunctions(projF), Porder(Po), Pr(P), isPure(numANodes, true),
node2Action(numANodes, vector<int>(numPlayers)), cache(numPlayers + 1),
player2Class(numPlayers), kSymStrategyOffset(1, 0)

{
// actions
Expand Down
98 changes: 53 additions & 45 deletions src/games/behavmixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ template <class T> class MixedBehaviorProfile {
unsigned int m_gameversion;

// structures for storing cached data: nodes
mutable std::map<GameNode,T> map_realizProbs, map_beliefs;
mutable std::map<GameNode,std::map<GamePlayer, T>> map_nodeValues;
mutable std::map<GameNode, T> map_realizProbs, map_beliefs;
mutable std::map<GameNode, std::map<GamePlayer, T>> map_nodeValues;

// structures for storing cached data: information sets
mutable std::map<GameInfoset,T> map_infosetValues;
mutable std::map<GameInfoset, T> map_infosetValues;

// structures for storing cached data: actions
mutable std::map<GameAction,T> map_actionValues; // aka conditional payoffs
mutable std::map<GameAction,T> map_regret;
mutable std::map<GameAction, T> map_actionValues; // aka conditional payoffs
mutable std::map<GameAction, T> map_regret;

/// @name Auxiliary functions for computation of interesting values
//@{
Expand All @@ -59,9 +59,9 @@ template <class T> class MixedBehaviorProfile {

/// @name Converting mixed strategies to behavior
//@{
void BehaviorStrat(GamePlayer &, GameNode &, std::map<GameNode,T> &, std::map<GameNode,T> &);
void BehaviorStrat(GamePlayer &, GameNode &, std::map<GameNode, T> &, std::map<GameNode, T> &);
void RealizationProbs(const MixedStrategyProfile<T> &, GamePlayer &, const Array<int> &,
GameTreeNodeRep *, std::map<GameNode,T> &, std::map<GameNode,T> &);
GameTreeNodeRep *, std::map<GameNode, T> &, std::map<GameNode, T> &);
//@}

/// Check underlying game has not changed; raise exception if it has
Expand All @@ -83,49 +83,62 @@ template <class T> class MixedBehaviorProfile {

MixedBehaviorProfile<T> &operator=(const MixedBehaviorProfile<T> &);
MixedBehaviorProfile<T> &operator=(const Vector<T> &p)
{ InvalidateCache(); m_probs = p; return *this;}
{
InvalidateCache();
m_probs = p;
return *this;
}
MixedBehaviorProfile<T> &operator=(const T &x)
{ InvalidateCache(); m_probs = x; return *this; }
{
InvalidateCache();
m_probs = x;
return *this;
}

//@}

/// @name Operator overloading
//@{
bool operator==(const MixedBehaviorProfile<T> &) const;
bool operator!=(const MixedBehaviorProfile<T> &x) const
{ return !(*this == x); }
bool operator!=(const MixedBehaviorProfile<T> &x) const { return !(*this == x); }

bool operator==(const DVector<T> &x) const
{ return m_probs == x; }
bool operator!=(const DVector<T> &x) const
{ return m_probs != x; }
bool operator==(const DVector<T> &x) const { return m_probs == x; }
bool operator!=(const DVector<T> &x) const { return m_probs != x; }

const T &operator[](const GameAction &p_action) const
{ return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(),
m_support.GetIndex(p_action)); }
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}
T &operator[](const GameAction &p_action)
{ return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(),
m_support.GetIndex(p_action)); }
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}

const T &operator()(const GameAction &p_action) const
{ return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(),
m_support.GetIndex(p_action)); }
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}
T &operator()(const GameAction &p_action)
{ return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(),
m_support.GetIndex(p_action)); }
{
return (*this)(p_action->GetInfoset()->GetPlayer()->GetNumber(),
p_action->GetInfoset()->GetNumber(), m_support.GetIndex(p_action));
}

const T &operator()(int a, int b, int c) const
{ return m_probs(a, b, c); }
const T &operator()(int a, int b, int c) const { return m_probs(a, b, c); }
T &operator()(int a, int b, int c)
{ InvalidateCache(); return m_probs(a, b, c); }
const T &operator[](int a) const
{ return m_probs[a]; }
{
InvalidateCache();
return m_probs(a, b, c);
}
const T &operator[](int a) const { return m_probs[a]; }
T &operator[](int a)
{ InvalidateCache(); return m_probs[a]; }
{
InvalidateCache();
return m_probs[a];
}

operator const Vector<T> &() const { return m_probs; }
//@}
Expand All @@ -138,14 +151,14 @@ template <class T> class MixedBehaviorProfile {
/// We also clear
/// map_nodeValues, map_actionValues
/// as otherwise we would need to reset them to 0 while populating them
void InvalidateCache() const {
void InvalidateCache() const
{
map_realizProbs.clear();
map_nodeValues.clear();
map_actionValues.clear();
}
/// Reset certain cached values


/// Set the profile to the centroid
void SetCentroid();
/// Set the behavior at any undefined information set to the centroid
Expand All @@ -167,18 +180,15 @@ template <class T> class MixedBehaviorProfile {
Game GetGame() const { return m_support.GetGame(); }
const BehaviorSupportProfile &GetSupport() const { return m_support; }
/// Returns whether the profile has been invalidated by a subsequent revision to the game
bool IsInvalidated() const
{ return m_gameversion != m_support.GetGame()->GetVersion(); }
bool IsInvalidated() const { return m_gameversion != m_support.GetGame()->GetVersion(); }

bool IsDefinedAt(GameInfoset p_infoset) const;
//@}

/// @name Computation of interesting quantities
//@{
T GetPayoff(int p_player) const;
T GetPayoff(const GamePlayer &p_player) const {
return GetPayoff(p_player->GetNumber());
}
T GetPayoff(const GamePlayer &p_player) const { return GetPayoff(p_player->GetNumber()); }
T GetLiapValue() const;

const T &GetRealizProb(const GameNode &node) const;
Expand Down Expand Up @@ -216,12 +226,10 @@ template <class T> class MixedBehaviorProfile {
/// GetRegret(const GameAction &) const
T GetMaxRegret() const;

T DiffActionValue(const GameAction &action,
const GameAction &oppAction) const;
T DiffRealizProb(const GameNode &node,
const GameAction &oppAction) const;
T DiffActionValue(const GameAction &action, const GameAction &oppAction) const;
T DiffRealizProb(const GameNode &node, const GameAction &oppAction) const;
T DiffNodeValue(const GameNode &node, const GamePlayer &player,
const GameAction &oppAction) const;
const GameAction &oppAction) const;

MixedStrategyProfile<T> ToMixedProfile() const;

Expand Down

0 comments on commit 7133074

Please sign in to comment.