Skip to content

Commit dc57a11

Browse files
committed
use hash set for closed node lookup
1 parent d2335d0 commit dc57a11

1 file changed

Lines changed: 15 additions & 42 deletions

File tree

‎cpp/stlastar.h‎

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,6 @@ template <class UserState> class AStarSearch
104104
UserState m_UserState;
105105
};
106106

107-
// template<>
108-
// struct std::hash<Node>
109-
// {
110-
// std::size_t operator()(Node const& n) const noexcept
111-
// {
112-
// std::size_t h1 = std::hash<std::string>{}(n.g);
113-
// std::size_t h2 = std::hash<std::string>{}(n.h);
114-
// return h1 ^ (h2 << 1); // or use boost::hash_combine
115-
// }
116-
// };
117-
118-
119107
// For sorting the heap the STL needs compare function that lets us compare
120108
// the f value of two nodes
121109

@@ -337,16 +325,6 @@ template <class UserState> class AStarSearch
337325
}
338326
}
339327

340-
// typename vector< Node * >::iterator closedlist_result;
341-
342-
// for( closedlist_result = m_ClosedList.begin(); closedlist_result != m_ClosedList.end(); closedlist_result ++ )
343-
// {
344-
// if( (*closedlist_result)->m_UserState.IsSameState( (*successor)->m_UserState ) )
345-
// {
346-
// break;
347-
// }
348-
// }
349-
350328
auto closedlist_result = m_ClosedList.find(*successor);
351329

352330
if( closedlist_result != m_ClosedList.end() )
@@ -701,7 +679,7 @@ template <class UserState> class AStarSearch
701679
m_OpenList.clear();
702680

703681
// iterate closed list and delete unused nodes
704-
typename unordered_set< Node * >::iterator iterClosed;
682+
typename unordered_set<Node*, NodeHash, NodeEqual>::iterator iterClosed;
705683

706684
for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ )
707685
{
@@ -742,7 +720,7 @@ template <class UserState> class AStarSearch
742720
m_OpenList.clear();
743721

744722
// iterate closed list and delete unused nodes
745-
typename unordered_set< Node * >::iterator iterClosed;
723+
typename unordered_set<Node*, NodeHash, NodeEqual>::iterator iterClosed;
746724

747725
for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ )
748726
{
@@ -799,24 +777,18 @@ template <class UserState> class AStarSearch
799777
// Heap (simple vector but used as a heap, cf. Steve Rabin's game gems article)
800778
vector< Node *> m_OpenList;
801779

802-
// Closed list is a vector.
803-
// struct NodePtrComp
804-
// {
805-
// bool operator()(const Node* lhs, const Node* rhs) const { return lhs->f < rhs->f;} //#TODO: read the is same state/use unordered_set which has a better condition
806-
// };
807-
// set< Node*, NodePtrComp > m_ClosedList;
808-
// struct NodeHash {
809-
// size_t operator() (Node* const& n) const {
810-
// // std::size_t seed = 0;
811-
// // boost::hash_combine(seed, n->g);
812-
// // boost::hash_combine(seed, n->h);
813-
// // return seed;
814-
// std::size_t h1 = 0;//std::hash<float>{}(n->m_UserState.g);
815-
// std::size_t h2 = 1;//std::hash<float>{}(n->m_UserState.h);
816-
// return h1 ^ (h2 << 1); // or use boost::hash_combine
817-
// };
818-
// unordered_set<Node*, NodeHash> m_ClosedList;
819-
unordered_set<Node*> m_ClosedList;
780+
// Closed list is a set
781+
struct NodeHash {
782+
size_t operator() (Node* const& n) const {
783+
return n->m_UserState.Hash();
784+
}
785+
};
786+
struct NodeEqual {
787+
bool operator()(Node* a, Node* b) const {
788+
return a->m_UserState.IsSameState(b->m_UserState);
789+
}
790+
};
791+
unordered_set<Node*, NodeHash, NodeEqual> m_ClosedList;
820792

821793

822794
// Successors is a vector filled out by the user each type successors to a node
@@ -861,6 +833,7 @@ template <class T> class AStarState
861833
virtual bool GetSuccessors( AStarSearch<T> *astarsearch, T *parent_node ) = 0; // Retrieves all successors to this node and adds them via astarsearch.addSuccessor()
862834
virtual float GetCost( T &successor ) = 0; // Computes the cost of travelling from this node to the successor node
863835
virtual bool IsSameState( T &rhs ) = 0; // Returns true if this node is the same as the rhs node
836+
virtual float Hash() = 0;
864837
};
865838

866839
#endif

0 commit comments

Comments
 (0)