@@ -640,7 +640,6 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
640640 struct Frame {
641641 GameNode m_node;
642642 ChildIterator m_current, m_end;
643- bool m_expanded = false ;
644643 };
645644
646645 Game m_owner{nullptr };
@@ -673,32 +672,18 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
673672 static Frame make_frame (const GameNode &p_node)
674673 {
675674 if (p_node->IsTerminal ()) {
676- return Frame{p_node, {}, {}, true };
675+ return Frame{p_node, {}, {}};
677676 }
678677 const auto children = p_node->GetChildren ();
679- return Frame{p_node, children.begin (), children.end (), false };
680- }
681-
682- void descend_postorder ()
683- {
684- while (!m_stack.empty ()) {
685- auto &f = m_stack.top ();
686- if (f.m_expanded || f.m_current == f.m_end ) {
687- return ;
688- }
689- f.m_expanded = true ;
690- const GameNode child = *f.m_current ;
691- ++f.m_current ;
692- m_stack.push (make_frame (child));
693- }
678+ return Frame{p_node, children.begin (), children.end ()};
694679 }
695680
696681 GameNode advance_preorder ()
697682 {
698- if (auto &top = m_stack.top (); !top. m_node ->IsTerminal ()) {
699- const auto children = top. m_node ->GetChildren ();
700- top. m_current = children.begin ();
701- top. m_end = children.end ();
683+ if (auto &[node, current, m_end] = m_stack.top (); !node ->IsTerminal ()) {
684+ const auto children = node ->GetChildren ();
685+ current = children.begin ();
686+ m_end = children.end ();
702687 }
703688 while (!m_stack.empty ()) {
704689 if (auto &f = m_stack.top (); f.m_current != f.m_end ) {
@@ -712,6 +697,23 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
712697 return nullptr ;
713698 }
714699
700+ void descend_postorder ()
701+ {
702+ while (!m_stack.empty ()) {
703+ auto &f = m_stack.top ();
704+ if (f.m_current == f.m_end ) {
705+ return ;
706+ }
707+ const auto child = *f.m_current ;
708+ ++f.m_current ;
709+ m_stack.push (make_frame (child));
710+ if (!child->IsTerminal ()) {
711+ continue ;
712+ }
713+ return ;
714+ }
715+ }
716+
715717 GameNode advance_postorder ()
716718 {
717719 m_stack.pop ();
@@ -743,7 +745,7 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
743745 if (!m_current) {
744746 return *this ;
745747 }
746- const GameNode next =
748+ const auto next =
747749 (m_order == TraversalOrder::Preorder) ? advance_preorder () : advance_postorder ();
748750 m_current = next;
749751 if (!m_current) {
@@ -1001,7 +1003,10 @@ class GameRep : public std::enable_shared_from_this<GameRep> {
10011003 // / Returns the root node of the game
10021004 virtual GameNode GetRoot () const = 0;
10031005 // / Returns a range that can be used to iterate over the nodes of the game
1004- Nodes GetNodes () const { return {std::const_pointer_cast<GameRep>(shared_from_this ())}; }
1006+ Nodes GetNodes (TraversalOrder p_traversal = TraversalOrder::Preorder) const
1007+ {
1008+ return {std::const_pointer_cast<GameRep>(shared_from_this ()), p_traversal};
1009+ }
10051010 // / Returns the number of nodes in the game
10061011 virtual size_t NumNodes () const = 0;
10071012 // / Returns the number of non-terminal nodes in the game
0 commit comments