Skip to content

Commit 9e9f81f

Browse files
committed
Refactor BuildInfosetParents defining the position using ActiveEdge
1 parent c46ecd1 commit 9e9f81f

3 files changed

Lines changed: 20 additions & 90 deletions

File tree

src/games/game.h

Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -57,82 +57,6 @@ using GameNode = GameObjectPtr<GameNodeRep>;
5757
class GameRep;
5858
using Game = std::shared_ptr<GameRep>;
5959

60-
template <class P, class T> class ElementCollection {
61-
P m_owner{nullptr};
62-
const std::vector<T *> *m_container{nullptr};
63-
64-
public:
65-
class iterator {
66-
P m_owner{nullptr};
67-
const std::vector<T *> *m_container{nullptr};
68-
size_t m_index{0};
69-
70-
public:
71-
using iterator_category = std::bidirectional_iterator_tag;
72-
using difference_type = std::ptrdiff_t;
73-
using value_type = GameObjectPtr<T>;
74-
using pointer = value_type *;
75-
using reference = value_type &;
76-
77-
iterator() = default;
78-
iterator(const P &p_owner, const std::vector<T *> *p_container, size_t p_index = 0)
79-
: m_owner(p_owner), m_container(p_container), m_index(p_index)
80-
{
81-
}
82-
iterator(const iterator &) = default;
83-
~iterator() = default;
84-
iterator &operator=(const iterator &) = default;
85-
86-
bool operator==(const iterator &p_iter) const
87-
{
88-
return m_owner == p_iter.m_owner && m_container == p_iter.m_container &&
89-
m_index == p_iter.m_index;
90-
}
91-
bool operator!=(const iterator &p_iter) const
92-
{
93-
return m_owner != p_iter.m_owner || m_container != p_iter.m_container ||
94-
m_index != p_iter.m_index;
95-
}
96-
97-
iterator &operator++()
98-
{
99-
m_index++;
100-
return *this;
101-
}
102-
iterator &operator--()
103-
{
104-
m_index--;
105-
return *this;
106-
}
107-
value_type operator*() const { return m_container->at(m_index); }
108-
const P &GetOwner() const { return m_owner; }
109-
};
110-
111-
ElementCollection() = default;
112-
explicit ElementCollection(const P &p_owner, const std::vector<T *> *p_container)
113-
: m_owner(p_owner), m_container(p_container)
114-
{
115-
}
116-
ElementCollection(const ElementCollection<P, T> &) = default;
117-
~ElementCollection() = default;
118-
ElementCollection &operator=(const ElementCollection<P, T> &) = default;
119-
120-
bool operator==(const ElementCollection<P, T> &p_other) const
121-
{
122-
return m_owner == p_other.m_owner && m_container == p_other.m_container;
123-
}
124-
125-
bool empty() const { return m_container->empty(); }
126-
size_t size() const { return m_container->size(); }
127-
GameObjectPtr<T> front() const { return m_container->front(); }
128-
GameObjectPtr<T> back() const { return m_container->back(); }
129-
130-
iterator begin() const { return {m_owner, m_container, 0}; }
131-
iterator end() const { return {m_owner, m_container, (m_owner) ? m_container->size() : 0}; }
132-
iterator cbegin() const { return {m_owner, m_container, 0}; }
133-
iterator cend() const { return {m_owner, m_container, (m_owner) ? m_container->size() : 0}; }
134-
};
135-
13660
//
13761
// Forward declarations of classes defined elsewhere.
13862
//
@@ -491,17 +415,23 @@ class GameNodeRep : public std::enable_shared_from_this<GameNodeRep> {
491415
/// @brief A range class for iterating over a node's (action, child) pairs.
492416
class Actions {
493417
private:
494-
GameNode m_owner{nullptr};
418+
const GameNodeRep *m_owner{nullptr};
495419

496420
public:
497421
class iterator;
498422

499-
Actions(const GameNode p_owner);
423+
Actions(const GameNodeRep *p_owner);
500424

501425
iterator begin() const;
502426
iterator end() const;
503427
};
504428

429+
GameNodeRep(GameRep *e, GameNodeRep *p);
430+
~GameNodeRep();
431+
432+
bool IsValid() const { return m_valid; }
433+
void Invalidate() { m_valid = false; }
434+
505435
/// @brief Returns a collection for iterating over this node's (action, child) pairs.
506436
Actions GetActions() const;
507437

@@ -601,9 +531,9 @@ class GameNodeRep::Actions::iterator {
601531
GameNode GetOwner() const;
602532
};
603533

604-
inline GameNodeRep::Actions::Actions(GameNode p_owner) : m_owner(p_owner) {}
534+
inline GameNodeRep::Actions::Actions(const GameNodeRep *p_owner) : m_owner(p_owner) {}
605535

606-
inline GameNodeRep::Actions GameNodeRep::GetActions() const { return Actions(this); }
536+
inline GameNodeRep::Actions GameNodeRep::GetActions() const { return {Actions(this)}; }
607537

608538
inline GameNodeRep::Actions::iterator GameNodeRep::Actions::begin() const
609539
{

src/games/gameobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ template <class P, class T> class ElementCollection {
142142
}
143143
value_type operator*() const { return m_container->at(m_index); }
144144

145-
inline P& GetOwner() const { return m_owner; }
145+
inline const P& GetOwner() const { return m_owner; }
146146
};
147147

148148
ElementCollection() = default;

src/games/gametree.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ void GameTreeRep::BuildInfosetParents()
899899
prior_actions[GamePlayer(m_chance)].emplace(nullptr);
900900

901901
position.emplace(m_root->GetActions().begin());
902-
prior_actions[m_root->m_infoset->m_player].emplace(nullptr);
902+
prior_actions[m_root->GetPlayer()].emplace(nullptr);
903903
if (m_root->m_infoset) {
904904
m_infosetParents[m_root->m_infoset].insert(nullptr);
905905
}
@@ -914,15 +914,15 @@ void GameTreeRep::BuildInfosetParents()
914914
node = current_it.GetOwner();
915915

916916
if (current_it == node->GetActions().end()) {
917-
prior_actions.at(node->m_infoset->m_player).pop();
917+
prior_actions.at(node->GetPlayer()).pop();
918918
position.pop();
919-
path_choices.erase(node->m_infoset);
919+
path_choices.erase(node->GetInfoset());
920920
continue;
921921
}
922922
else {
923923
std::tie(action, child) = *current_it;
924924
++current_it;
925-
path_choices[node->m_infoset] = action;
925+
path_choices[node->GetInfoset()] = action;
926926
}
927927
}
928928
else {
@@ -931,15 +931,15 @@ void GameTreeRep::BuildInfosetParents()
931931
child = node->GetChild(action);
932932
}
933933

934-
prior_actions.at(node->m_infoset->m_player).top() = action;
934+
prior_actions.at(node->GetPlayer()).top() = action;
935935

936936
if (!child->IsTerminal()) {
937-
auto child_player = child->m_infoset->m_player;
937+
auto child_player = child->GetPlayer();
938938
auto prior_action = prior_actions.at(child_player).top();
939-
m_infosetParents[child->m_infoset].insert(prior_action);
939+
m_infosetParents[child->m_infoset].insert(prior_action ? prior_action.get() : nullptr);
940940

941-
if (path_choices.find(child->m_infoset) != path_choices.end()) {
942-
const GameAction replay_action = path_choices.at(child->m_infoset);
941+
if (path_choices.find(child->GetInfoset()) != path_choices.end()) {
942+
const GameAction replay_action = path_choices.at(child->GetInfoset());
943943
position.emplace(AbsentMindedEdge{replay_action, child});
944944
}
945945
else {

0 commit comments

Comments
 (0)