Skip to content

Commit 2a7e06f

Browse files
committed
Merge pull request justinhj#2 from hoshi10/master
Fixed memory leak, added a couple of features
2 parents e11620c + d4e78f1 commit 2a7e06f

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎stlastar.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ using namespace std;
4949
// occurs in stl headers
5050
#pragma warning( disable : 4786 )
5151

52+
template <class T> class AStarState;
53+
5254
// The AStar search class. UserState is the users state space type
5355
template <class UserState> class AStarSearch
5456
{
5557

58+
#if (defined(__cplusplus) && __cplusplus > 199711L) || (defined(_MSC_VER) && _MSC_VER >= 1600)
59+
static_assert(is_base_of<AStarState<UserState>, UserState>::value, "UserState must be derived from AStarState!");
60+
#endif
61+
5662
public: // data
5763

5864
enum
@@ -214,6 +220,7 @@ template <class UserState> class AStarSearch
214220
// The user is going to use the Goal Node he passed in
215221
// so copy the parent pointer of n
216222
m_Goal->parent = n->parent;
223+
m_Goal->g = n->g;
217224

218225
// A special case is that the goal was passed in as the start state
219226
// so handle that here
@@ -513,6 +520,20 @@ template <class UserState> class AStarSearch
513520
return NULL;
514521
}
515522

523+
// Get final cost of solution
524+
// Returns FLT_MAX if goal is not defined or there is no solution
525+
float GetSolutionCost()
526+
{
527+
if( m_Goal && m_State == SEARCH_STATE_SUCCEEDED )
528+
{
529+
return m_Goal->g;
530+
}
531+
else
532+
{
533+
return FLT_MAX;
534+
}
535+
}
536+
516537
// For educational use and debugging it is useful to be able to view
517538
// the open and closed list at each step, here are two functions to allow that.
518539

@@ -717,6 +738,7 @@ template <class UserState> class AStarSearch
717738
#if !USE_FSA_MEMORY
718739
delete node;
719740
#else
741+
node->~Node();
720742
m_FixedSizeAllocator.free( node );
721743
#endif
722744
}
@@ -762,6 +784,17 @@ template <class UserState> class AStarSearch
762784

763785
};
764786

787+
template <class T> class AStarState
788+
{
789+
public:
790+
virtual ~AStarState() {}
791+
virtual float GoalDistanceEstimate( T &nodeGoal ) = 0; // Heuristic function which computes the estimated cost to the goal node
792+
virtual bool IsGoal( T &nodeGoal ) = 0; // Returns true if this node is the goal node
793+
virtual bool GetSuccessors( AStarSearch<T> *astarsearch, T *parent_node ) = 0; // Retrieves all successors to this node and adds them via astarsearch.addSuccessor()
794+
virtual float GetCost( T &successor ) = 0; // Computes the cost of travelling from this node to the successor node
795+
virtual bool IsSameState( T &rhs ) = 0; // Returns true if this node is the same as the rhs node
796+
};
797+
765798
#endif
766799

767800

0 commit comments

Comments
 (0)