Skip to content

Commit

Permalink
Rewrite some Array operations to be more std::vector-like
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Dec 23, 2024
1 parent 4502065 commit 45d8d10
Show file tree
Hide file tree
Showing 41 changed files with 193 additions and 204 deletions.
29 changes: 21 additions & 8 deletions src/core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ template <class T> class Array {

public:
class iterator {
friend class Array;

private:
Array *m_array;
int m_index;

public:
using iterator_category = std::forward_iterator_tag;
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = value_type *;
Expand All @@ -81,6 +83,11 @@ template <class T> class Array {
m_index++;
return *this;
}
iterator &operator--()
{
m_index--;
return *this;
}
iterator operator++(int)
{
auto ret = *this;
Expand All @@ -100,7 +107,7 @@ template <class T> class Array {
int m_index;

public:
using iterator_category = std::forward_iterator_tag;
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = value_type *;
Expand All @@ -114,6 +121,11 @@ template <class T> class Array {
m_index++;
return *this;
}
const_iterator &operator--()
{
m_index--;
return *this;
}
const_iterator operator++(int)
{
auto ret = *this;
Expand Down Expand Up @@ -257,9 +269,6 @@ template <class T> class Array {
;
return (i <= this->maxdex) ? i : (mindex - 1);
}

/// Return true if the element is currently residing in the array
bool Contains(const T &t) const { return Find(t) != mindex - 1; }
//@}

/// @name Modifying the contents of the array
Expand All @@ -276,6 +285,8 @@ template <class T> class Array {
: ((n > this->maxdex + 1) ? this->maxdex + 1 : n));
}

void erase(iterator pos) { Remove(pos.m_index); }

/// \brief Remove an element from the array.
///
/// Remove the element at a given index from the array. Returns the value
Expand Down Expand Up @@ -333,9 +344,11 @@ template <class T> class Array {
/// leaving the container with a size of 0.
void clear()
{
delete[] (this->data + this->mindex);
this->data = 0;
this->maxdex = this->mindex - 1;
if (maxdex >= mindex) {
delete[] (data + mindex);
}
data = nullptr;
maxdex = mindex - 1;
}
///@}
};
Expand Down
3 changes: 2 additions & 1 deletion src/games/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void GameStrategyRep::DeleteStrategy()
}

m_player->GetGame()->IncrementVersion();
m_player->m_strategies.Remove(m_player->m_strategies.Find(this));
m_player->m_strategies.erase(
std::find(m_player->m_strategies.begin(), m_player->m_strategies.end(), this));
for (int st = 1; st <= m_player->m_strategies.Length(); st++) {
m_player->m_strategies[st]->m_number = st;
}
Expand Down
1 change: 0 additions & 1 deletion src/games/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ class GameNodeRep : public GameObject {
virtual void SetLabel(const std::string &p_label) = 0;

virtual int GetNumber() const = 0;
virtual int NumberInInfoset() const = 0;

virtual int NumChildren() const = 0;
virtual GameNode GetChild(int i) const = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/games/gameexpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Array<int> GameExplicitRep::NumStrategies() const
{
const_cast<GameExplicitRep *>(this)->BuildComputedValues();
Array<int> dim(m_players.size());
for (int pl = 1; pl <= m_players.size(); pl++) {
for (size_t pl = 1; pl <= m_players.size(); pl++) {
dim[pl] = m_players[pl]->m_strategies.size();
}
return dim;
Expand Down
3 changes: 2 additions & 1 deletion src/games/gametable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ void GameTableRep::DeleteOutcome(const GameOutcome &p_outcome)
m_results[i] = 0;
}
}
m_outcomes.Remove(m_outcomes.Find(p_outcome))->Invalidate();
p_outcome->Invalidate();
m_outcomes.erase(std::find(m_outcomes.begin(), m_outcomes.end(), p_outcome));
for (int outc = 1; outc <= m_outcomes.Length(); outc++) {
m_outcomes[outc]->m_number = outc;
}
Expand Down
85 changes: 33 additions & 52 deletions src/games/gametree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ void GameTreeInfosetRep::SetPlayer(GamePlayer p_player)
}

m_efg->IncrementVersion();
m_player->m_infosets.Remove(m_player->m_infosets.Find(this));
m_player->m_infosets.erase(
std::find(m_player->m_infosets.begin(), m_player->m_infosets.end(), this));
m_player = p_player;
p_player->m_infosets.push_back(this);

Expand Down Expand Up @@ -269,13 +270,15 @@ void GameTreeInfosetRep::RemoveAction(int which)
void GameTreeInfosetRep::RemoveMember(GameTreeNodeRep *p_node)
{
m_efg->IncrementVersion();
m_members.Remove(m_members.Find(p_node));
if (m_members.Length() == 0) {
m_player->m_infosets.Remove(m_player->m_infosets.Find(this));
for (int i = 1; i <= m_player->m_infosets.Length(); i++) {
m_player->m_infosets[i]->m_number = i;
}
m_members.erase(std::find(m_members.begin(), m_members.end(), p_node));
if (m_members.empty()) {
m_player->m_infosets.erase(
std::find(m_player->m_infosets.begin(), m_player->m_infosets.end(), this));
Invalidate();
int iset = 1;
for (auto &infoset : m_player->m_infosets) {
infoset->m_number = iset++;
}
}
}

Expand Down Expand Up @@ -348,28 +351,18 @@ Array<GameNode> GameTreeNodeRep::GetChildren() const

GameNode GameTreeNodeRep::GetNextSibling() const
{
if (!m_parent) {
if (!m_parent || m_parent->children.back() == this) {
return nullptr;
}
if (m_parent->children.back() == this) {
return nullptr;
}
else {
return m_parent->children[m_parent->children.Find(const_cast<GameTreeNodeRep *>(this)) + 1];
}
return *std::next(std::find(m_parent->children.begin(), m_parent->children.end(), this));
}

GameNode GameTreeNodeRep::GetPriorSibling() const
{
if (!m_parent) {
if (!m_parent || m_parent->children.front() == this) {
return nullptr;
}
if (m_parent->children.front() == this) {
return nullptr;
}
else {
return m_parent->children[m_parent->children.Find(const_cast<GameTreeNodeRep *>(this)) - 1];
}
return *std::prev(std::find(m_parent->children.begin(), m_parent->children.end(), this));
}

GameAction GameTreeNodeRep::GetPriorAction() const
Expand Down Expand Up @@ -451,11 +444,12 @@ void GameTreeNodeRep::DeleteParent()
m_efg->IncrementVersion();
GameTreeNodeRep *oldParent = m_parent;

oldParent->children.Remove(oldParent->children.Find(this));
oldParent->children.erase(
std::find(oldParent->children.begin(), oldParent->children.end(), this));
oldParent->DeleteTree();
m_parent = oldParent->m_parent;
if (m_parent) {
m_parent->children[m_parent->children.Find(oldParent)] = this;
std::replace(m_parent->children.begin(), m_parent->children.end(), oldParent, this);
}
else {
m_efg->m_root = this;
Expand Down Expand Up @@ -538,21 +532,9 @@ void GameTreeNodeRep::MoveTree(GameNode p_src)
}
m_efg->IncrementVersion();
auto *src = dynamic_cast<GameTreeNodeRep *>(p_src.operator->());

if (src->m_parent == m_parent) {
int srcChild = src->m_parent->children.Find(src);
int destChild = src->m_parent->children.Find(this);
src->m_parent->children[srcChild] = this;
src->m_parent->children[destChild] = src;
}
else {
GameTreeNodeRep *parent = src->m_parent;
parent->children[parent->children.Find(src)] = this;
m_parent->children[m_parent->children.Find(this)] = src;
src->m_parent = m_parent;
m_parent = parent;
}

std::iter_swap(std::find(src->m_parent->children.begin(), src->m_parent->children.end(), src),
std::find(m_parent->children.begin(), m_parent->children.end(), this));
std::swap(src->m_parent, m_parent);
m_label = "";
outcome = nullptr;

Expand Down Expand Up @@ -675,7 +657,7 @@ GameInfoset GameTreeNodeRep::InsertMove(GameInfoset p_infoset)
dynamic_cast<GameTreeInfosetRep *>(p_infoset.operator->())->AddMember(newNode);

if (m_parent) {
m_parent->children[m_parent->children.Find(this)] = newNode;
std::replace(m_parent->children.begin(), m_parent->children.end(), this, newNode);
}
else {
m_efg->m_root = newNode;
Expand Down Expand Up @@ -886,12 +868,12 @@ void GameTreeRep::Canonicalize()

void GameTreeRep::ClearComputedValues() const
{
for (int pl = 1; pl <= m_players.Length(); pl++) {
while (m_players[pl]->m_strategies.Length() > 0) {
m_players[pl]->m_strategies.Remove(1)->Invalidate();
for (auto player : m_players) {
for (auto strategy : player->m_strategies) {
strategy->Invalidate();
}
player->m_strategies.clear();
}

m_computedValues = false;
}

Expand All @@ -900,13 +882,10 @@ void GameTreeRep::BuildComputedValues()
if (m_computedValues) {
return;
}

Canonicalize();

for (int pl = 1; pl <= m_players.Length(); pl++) {
m_players[pl]->MakeReducedStrats(m_root, nullptr);
for (const auto &player : m_players) {
player->MakeReducedStrats(m_root, nullptr);
}

m_computedValues = true;
}

Expand Down Expand Up @@ -1073,9 +1052,11 @@ void GameTreeRep::DeleteOutcome(const GameOutcome &p_outcome)
{
IncrementVersion();
m_root->DeleteOutcome(p_outcome);
m_outcomes.Remove(m_outcomes.Find(p_outcome))->Invalidate();
for (int outc = 1; outc <= m_outcomes.Length(); outc++) {
m_outcomes[outc]->m_number = outc;
m_outcomes.erase(std::find(m_outcomes.begin(), m_outcomes.end(), p_outcome));
p_outcome->Invalidate();
int outc = 1;
for (auto &outcome : m_outcomes) {
outcome->m_number = outc++;
}
ClearComputedValues();
}
Expand Down Expand Up @@ -1110,7 +1091,7 @@ Game GameTreeRep::SetChanceProbs(const GameInfoset &p_infoset, const Array<Numbe
throw UndefinedException(
"Action probabilities can only be specified for chance information sets");
}
if (p_infoset->NumActions() != p_probs.size()) {
if (p_infoset->NumActions() != static_cast<int>(p_probs.size())) {
throw DimensionException("The number of probabilities given must match the number of actions");
}
IncrementVersion();
Expand Down
5 changes: 0 additions & 5 deletions src/games/gametree.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ class GameTreeNodeRep : public GameNodeRep {
void SetLabel(const std::string &p_label) override { m_label = p_label; }

int GetNumber() const override { return number; }
int NumberInInfoset() const override
{
return infoset->m_members.Find(const_cast<GameTreeNodeRep *>(this));
}

int NumChildren() const override { return children.size(); }
GameNode GetChild(int i) const override { return children[i]; }
GameNode GetChild(const GameAction &p_action) const override
Expand Down
2 changes: 1 addition & 1 deletion src/games/stratpure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Gambit {
PureStrategyProfileRep::PureStrategyProfileRep(const Game &p_game)
: m_nfg(p_game), m_profile(p_game->NumPlayers())
{
for (size_t pl = 1; pl <= m_nfg->NumPlayers(); pl++) {
for (int pl = 1; pl <= m_nfg->NumPlayers(); pl++) {
m_profile[pl] = m_nfg->GetPlayer(pl)->GetStrategy(1);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/gui/analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ MixedStrategyProfile<T> OutputToMixedProfile(gbtGameDocument *p_doc, const wxStr

if (tok.GetNextToken() == wxT("NE")) {
if (tok.CountTokens() == (unsigned int)profile.MixedProfileLength()) {
for (int i = 1; i <= profile.MixedProfileLength(); i++) {
for (size_t i = 1; i <= profile.MixedProfileLength(); i++) {
profile[i] =
lexical_cast<Rational>(std::string((const char *)tok.GetNextToken().mb_str()));
}
Expand All @@ -79,7 +79,7 @@ MixedBehaviorProfile<T> OutputToBehavProfile(gbtGameDocument *p_doc, const wxStr

if (tok.GetNextToken() == wxT("NE")) {
if (tok.CountTokens() == (unsigned int)profile.BehaviorProfileLength()) {
for (int i = 1; i <= profile.BehaviorProfileLength(); i++) {
for (size_t i = 1; i <= profile.BehaviorProfileLength(); i++) {
profile[i] =
lexical_cast<Rational>(std::string((const char *)tok.GetNextToken().mb_str()));
}
Expand Down Expand Up @@ -156,7 +156,7 @@ MixedStrategyProfile<T> TextToMixedProfile(gbtGameDocument *p_doc, const wxStrin

wxStringTokenizer tok(p_text, wxT(","));

for (int i = 1; i <= profile.MixedProfileLength(); i++) {
for (size_t i = 1; i <= profile.MixedProfileLength(); i++) {
profile[i] = lexical_cast<Rational>(std::string((const char *)tok.GetNextToken().mb_str()));
}

Expand All @@ -169,7 +169,7 @@ MixedBehaviorProfile<T> TextToBehavProfile(gbtGameDocument *p_doc, const wxStrin
MixedBehaviorProfile<T> profile(p_doc->GetGame());

wxStringTokenizer tok(p_text, wxT(","));
for (int i = 1; i <= profile.BehaviorProfileLength(); i++) {
for (size_t i = 1; i <= profile.BehaviorProfileLength(); i++) {
profile[i] = lexical_cast<Rational>(std::string((const char *)tok.GetNextToken().mb_str()));
}

Expand Down Expand Up @@ -441,7 +441,7 @@ template <class T> void gbtAnalysisProfileList<T>::Save(std::ostream &p_file) co
for (int j = 1; j <= NumProfiles(); j++) {
const MixedBehaviorProfile<T> &behav = *m_behavProfiles[j];
p_file << "<profile type=\"behav\">\n";
for (int k = 1; k <= behav.BehaviorProfileLength(); k++) {
for (size_t k = 1; k <= behav.BehaviorProfileLength(); k++) {
p_file << behav[k];
if (k < behav.BehaviorProfileLength()) {
p_file << ",";
Expand All @@ -457,7 +457,7 @@ template <class T> void gbtAnalysisProfileList<T>::Save(std::ostream &p_file) co
for (int j = 1; j <= NumProfiles(); j++) {
const MixedStrategyProfile<T> &mixed = *m_mixedProfiles[j];
p_file << "<profile type=\"mixed\">\n";
for (int k = 1; k <= mixed.MixedProfileLength(); k++) {
for (size_t k = 1; k <= mixed.MixedProfileLength(); k++) {
p_file << mixed[k];
if (k < mixed.MixedProfileLength()) {
p_file << ",";
Expand Down
5 changes: 4 additions & 1 deletion src/gui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class gbtApplication : public wxApp {
//!
//@{
void AddDocument(gbtGameDocument *p_doc) { m_documents.push_back(p_doc); }
void RemoveDocument(gbtGameDocument *p_doc) { m_documents.Remove(m_documents.Find(p_doc)); }
void RemoveDocument(gbtGameDocument *p_doc)
{
m_documents.erase(std::find(m_documents.begin(), m_documents.end(), p_doc));
}
bool AreDocumentsModified() const;
//@}
};
Expand Down
6 changes: 3 additions & 3 deletions src/gui/dlefglogit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void gbtLogitBehavList::AddProfile(const wxString &p_text, bool p_forceShow)
m_lambdas.push_back((double)Gambit::lexical_cast<Gambit::Rational>(
std::string((const char *)tok.GetNextToken().mb_str())));

for (int i = 1; i <= profile->BehaviorProfileLength(); i++) {
for (size_t i = 1; i <= profile->BehaviorProfileLength(); i++) {
(*profile)[i] = Gambit::lexical_cast<Gambit::Rational>(
std::string((const char *)tok.GetNextToken().mb_str()));
}
Expand Down Expand Up @@ -207,8 +207,8 @@ END_EVENT_TABLE()

gbtLogitBehavDialog::gbtLogitBehavDialog(wxWindow *p_parent, gbtGameDocument *p_doc)
: wxDialog(p_parent, wxID_ANY, wxT("Compute quantal response equilibria"), wxDefaultPosition),
m_doc(p_doc), m_process(nullptr), m_timer(this, GBT_ID_TIMER),
m_behavList(new gbtLogitBehavList(this, m_doc))
m_doc(p_doc), m_process(nullptr), m_behavList(new gbtLogitBehavList(this, m_doc)),
m_timer(this, GBT_ID_TIMER)
{
auto *sizer = new wxBoxSizer(wxVERTICAL);

Expand Down
Loading

0 comments on commit 45d8d10

Please sign in to comment.