From c06f411cc97432a5a5604cfe4aca14051e1999d1 Mon Sep 17 00:00:00 2001 From: Tomas Andrle Date: Sun, 31 Aug 2025 00:02:22 +0200 Subject: [PATCH 1/2] Make sure stlastar.h works without using namespace std. --- cpp/8puzzle.cpp | 4 ++-- cpp/findpath.cpp | 4 ++-- cpp/min_path_to_Bucharest.cpp | 4 ++-- cpp/stlastar.h | 26 +++++++++++++------------- cpp/tests.cpp | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cpp/8puzzle.cpp b/cpp/8puzzle.cpp index 6067739..f47e934 100755 --- a/cpp/8puzzle.cpp +++ b/cpp/8puzzle.cpp @@ -14,8 +14,6 @@ #include -using namespace std; - // Configuration #define NUM_TIMES_TO_RUN_SEARCH 1 @@ -27,6 +25,8 @@ using namespace std; // AStar search class #include "stlastar.h" // See header for copyright and usage information +using namespace std; + // Global data #define BOARD_WIDTH (3) diff --git a/cpp/findpath.cpp b/cpp/findpath.cpp index 4f4ed56..cb02ce7 100755 --- a/cpp/findpath.cpp +++ b/cpp/findpath.cpp @@ -12,10 +12,10 @@ #include #include -using namespace std; - #include "stlastar.h" // See header for copyright and usage information +using namespace std; + #define DEBUG_LISTS 0 #define DEBUG_LIST_LENGTHS_ONLY 0 diff --git a/cpp/min_path_to_Bucharest.cpp b/cpp/min_path_to_Bucharest.cpp index ee611df..7b45496 100644 --- a/cpp/min_path_to_Bucharest.cpp +++ b/cpp/min_path_to_Bucharest.cpp @@ -23,10 +23,10 @@ #include #include -using namespace std; - #include "stlastar.h" +using namespace std; + #define DEBUG_LISTS 0 #define DEBUG_LIST_LENGTHS_ONLY 0 diff --git a/cpp/stlastar.h b/cpp/stlastar.h index 58282ed..d6d406d 100755 --- a/cpp/stlastar.h +++ b/cpp/stlastar.h @@ -268,7 +268,7 @@ template class AStarSearch if( !ret ) { - typename vector< Node * >::iterator successor; + typename std::vector< Node * >::iterator successor; // free the nodes that may previously have been added for( successor = m_Successors.begin(); successor != m_Successors.end(); successor ++ ) @@ -287,7 +287,7 @@ template class AStarSearch } // Now handle each successor to the current node ... - for( typename vector< Node * >::iterator successor = m_Successors.begin(); successor != m_Successors.end(); successor ++ ) + for( typename std::vector< Node * >::iterator successor = m_Successors.begin(); successor != m_Successors.end(); successor ++ ) { // The g value for this successor ... float newg = n->g + n->m_UserState.GetCost( (*successor)->m_UserState ); @@ -298,7 +298,7 @@ template class AStarSearch // First linear search of open list to find node - typename vector< Node * >::iterator openlist_result; + typename std::vector< Node * >::iterator openlist_result; for( openlist_result = m_OpenList.begin(); openlist_result != m_OpenList.end(); openlist_result ++ ) { @@ -321,7 +321,7 @@ template class AStarSearch continue; } } - typename unordered_set::iterator closedlist_result; + typename std::unordered_set::iterator closedlist_result; closedlist_result = m_ClosedList.find(*successor); @@ -664,7 +664,7 @@ template class AStarSearch void FreeAllNodes() { // iterate open list and delete all nodes - typename vector< Node * >::iterator iterOpen = m_OpenList.begin(); + typename std::vector< Node * >::iterator iterOpen = m_OpenList.begin(); while( iterOpen != m_OpenList.end() ) { @@ -677,7 +677,7 @@ template class AStarSearch m_OpenList.clear(); // iterate closed list and delete unused nodes - typename unordered_set::iterator iterClosed; + typename std::unordered_set::iterator iterClosed; for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ ) { @@ -699,7 +699,7 @@ template class AStarSearch void FreeUnusedNodes() { // iterate open list and delete unused nodes - typename vector< Node * >::iterator iterOpen = m_OpenList.begin(); + typename std::vector< Node * >::iterator iterOpen = m_OpenList.begin(); while( iterOpen != m_OpenList.end() ) { @@ -718,7 +718,7 @@ template class AStarSearch m_OpenList.clear(); // iterate closed list and delete unused nodes - typename unordered_set::iterator iterClosed; + typename std::unordered_set::iterator iterClosed; for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ ) { @@ -773,7 +773,7 @@ template class AStarSearch private: // data // Heap (simple vector but used as a heap, cf. Steve Rabin's game gems article) - vector< Node *> m_OpenList; + std::vector< Node *> m_OpenList; // Closed is an unordered_set struct NodeHash { @@ -786,12 +786,12 @@ template class AStarSearch return a->m_UserState.IsSameState(b->m_UserState); } }; - unordered_set m_ClosedList; + std::unordered_set m_ClosedList; // Successors is a vector filled out by the user each type successors to a node // are generated - vector< Node * > m_Successors; + std::vector< Node * > m_Successors; // State unsigned int m_State; @@ -812,8 +812,8 @@ template class AStarSearch //Debug : need to keep these two iterators around // for the user Dbg functions - typename vector< Node * >::iterator iterDbgOpen; - typename vector< Node * >::iterator iterDbgClosed; + typename std::vector< Node * >::iterator iterDbgOpen; + typename std::vector< Node * >::iterator iterDbgClosed; // debugging : count memory allocation and free's int m_AllocateNodeCount; diff --git a/cpp/tests.cpp b/cpp/tests.cpp index b5d276d..2ddc68b 100644 --- a/cpp/tests.cpp +++ b/cpp/tests.cpp @@ -4,10 +4,10 @@ #include #include -using namespace std; - #include "stlastar.h" +using namespace std; + const int MAP_WIDTH = 20; const int MAP_HEIGHT = 20; From ee5a11507f4c5850561ed98257953b60fa8d422d Mon Sep 17 00:00:00 2001 From: Tomas Andrle Date: Sun, 31 Aug 2025 00:02:59 +0200 Subject: [PATCH 2/2] Fix compile error by using reference instad of pointer dereference. --- cpp/stlastar.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/stlastar.h b/cpp/stlastar.h index d6d406d..7a7a491 100755 --- a/cpp/stlastar.h +++ b/cpp/stlastar.h @@ -96,7 +96,7 @@ template class AStarSearch bool operator==(const Node& otherNode) const { - return this->m_UserState.IsSameState(otherNode->m_UserState); + return this->m_UserState.IsSameState(otherNode.m_UserState); } UserState m_UserState;