1
0
Fork 0

Codechange: Cleaned up and renamed NodeList

pull/13077/head
Koen Bussemaker 2024-11-09 16:45:12 +01:00 committed by Kuhnovic
parent bbc1f7b3dc
commit 3e195df3c7
15 changed files with 124 additions and 136 deletions

View File

@ -25,11 +25,11 @@
void AyStar::OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g)
{
/* Add a new Node to the OpenList */
PathNode *new_node = this->nodes.CreateNewNode();
new_node->Set(parent, node->tile, node->td, true);
new_node->estimate = f;
new_node->cost = g;
this->nodes.InsertOpenNode(*new_node);
PathNode &new_node = this->nodes.CreateNewNode();
new_node.Set(parent, node->tile, node->td, true);
new_node.estimate = f;
new_node.cost = g;
this->nodes.InsertOpenNode(new_node);
}
/**

View File

@ -136,7 +136,7 @@ struct AyStar {
void CheckTile(AyStarNode *current, PathNode *parent);
protected:
CNodeList_HashTableT<PathNode, 8, 10> nodes;
NodeList<PathNode, 8, 10> nodes;
void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g);
};

View File

@ -15,38 +15,28 @@
/**
* Hash table based node list multi-container class.
* Implements open list, closed list and priority queue for A-star
* path finder.
* Implements open list, closed list and priority queue for A-star pathfinder.
*/
template <class Titem_, int Thash_bits_open_, int Thash_bits_closed_>
class CNodeList_HashTableT {
template <class Titem, int Thash_bits_open, int Thash_bits_closed>
class NodeList {
public:
typedef Titem_ Titem; ///< Make #Titem_ visible from outside of class.
typedef typename Titem_::Key Key; ///< Make Titem_::Key a property of this class.
using CItemArray = std::deque<Titem_>; ///< Type that we will use as item container.
typedef HashTable<Titem_, Thash_bits_open_ > COpenList; ///< How pointers to open nodes will be stored.
typedef HashTable<Titem_, Thash_bits_closed_> CClosedList; ///< How pointers to closed nodes will be stored.
typedef CBinaryHeapT<Titem_> CPriorityQueue; ///< How the priority queue will be managed.
using Item = Titem;
using Key = typename Titem::Key;
protected:
CItemArray items; ///< Here we store full item data (Titem_).
COpenList open_nodes; ///< Hash table of pointers to open item data.
CClosedList closed_nodes; ///< Hash table of pointers to closed item data.
CPriorityQueue open_queue; ///< Priority queue of pointers to open item data.
Titem *new_node; ///< New open node under construction.
std::deque<Titem> items; ///< Storage of the nodes.
HashTable<Titem, Thash_bits_open> open_nodes; ///< Hash table of pointers to open nodes.
HashTable<Titem, Thash_bits_closed> closed_nodes; ///< Hash table of pointers to closed nodes.
CBinaryHeapT<Titem> open_queue; ///< Priority queue of pointers to open nodes.
Titem *new_node; ///< New node under construction.
public:
/** default constructor */
CNodeList_HashTableT() : open_queue(2048)
NodeList() : open_queue(2048)
{
this->new_node = nullptr;
}
/** destructor */
~CNodeList_HashTableT()
{
}
/** return number of open nodes */
inline int OpenCount()
{
@ -59,15 +49,21 @@ public:
return this->closed_nodes.Count();
}
/** return the total number of nodes. */
inline int TotalCount()
{
return this->items.Length();
}
/** allocate new data item from items */
inline Titem_ *CreateNewNode()
inline Titem &CreateNewNode()
{
if (this->new_node == nullptr) this->new_node = &this->items.emplace_back();
return this->new_node;
return *this->new_node;
}
/** Notify the nodelist that we don't want to discard the given node. */
inline void FoundBestNode(Titem_ &item)
inline void FoundBestNode(Titem &item)
{
/* for now it is enough to invalidate m_new_node if it is our given node */
if (&item == this->new_node) {
@ -77,7 +73,7 @@ public:
}
/** insert given item as open node (into m_open and m_open_queue) */
inline void InsertOpenNode(Titem_ &item)
inline void InsertOpenNode(Titem &item)
{
assert(this->closed_nodes.Find(item.GetKey()) == nullptr);
this->open_nodes.Push(item);
@ -88,7 +84,7 @@ public:
}
/** return the best open node */
inline Titem_ *GetBestOpenNode()
inline Titem *GetBestOpenNode()
{
if (!this->open_queue.IsEmpty()) {
return this->open_queue.Begin();
@ -97,10 +93,10 @@ public:
}
/** remove and return the best open node */
inline Titem_ *PopBestOpenNode()
inline Titem *PopBestOpenNode()
{
if (!this->open_queue.IsEmpty()) {
Titem_ *item = this->open_queue.Shift();
Titem *item = this->open_queue.Shift();
this->open_nodes.Pop(*item);
return item;
}
@ -108,49 +104,42 @@ public:
}
/** return the open node specified by a key or nullptr if not found */
inline Titem_ *FindOpenNode(const Key &key)
inline Titem *FindOpenNode(const Key &key)
{
Titem_ *item = this->open_nodes.Find(key);
return item;
return this->open_nodes.Find(key);
}
/** remove and return the open node specified by a key */
inline Titem_ &PopOpenNode(const Key &key)
inline Titem &PopOpenNode(const Key &key)
{
Titem_ &item = this->open_nodes.Pop(key);
size_t idxPop = this->open_queue.FindIndex(item);
this->open_queue.Remove(idxPop);
Titem &item = this->open_nodes.Pop(key);
size_t index = this->open_queue.FindIndex(item);
this->open_queue.Remove(index);
return item;
}
/** close node */
inline void InsertClosedNode(Titem_ &item)
inline void InsertClosedNode(Titem &item)
{
assert(this->open_nodes.Find(item.GetKey()) == nullptr);
this->closed_nodes.Push(item);
}
/** return the closed node specified by a key or nullptr if not found */
inline Titem_ *FindClosedNode(const Key &key)
inline Titem *FindClosedNode(const Key &key)
{
Titem_ *item = this->closed_nodes.Find(key);
return item;
}
/** The number of items. */
inline int TotalCount()
{
return this->items.Length();
return this->closed_nodes.Find(key);
}
/** Get a particular item. */
inline Titem_ &ItemAt(int idx)
inline Titem &ItemAt(int index)
{
return this->items[idx];
return this->items[index];
}
/** Helper for creating output of this array. */
template <class D> void Dump(D &dmp) const
template <class D>
void Dump(D &dmp) const
{
dmp.WriteStructT("data", &this->items);
}

View File

@ -28,7 +28,7 @@
* NodeList needs to have defined local type Titem - defines the pathfinder node type.
* Node needs to define local type Key - the node key in the collection ()
*
* For node list you can use template class CNodeList_HashTableT, for which
* For node list you can use template class NodeList, for which
* you need to declare only your node type. Look at test_yapf.h for an example.
*
*
@ -52,10 +52,9 @@ public:
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList NodeList; ///< our node list
typedef typename Types::VehicleType VehicleType; ///< the type of vehicle
typedef typename NodeList::Titem Node; ///< this will be our node type
typedef typename NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
NodeList nodes; ///< node list multi-container
protected:
@ -156,7 +155,7 @@ public:
*/
inline Node &CreateNewNode()
{
Node &node = *this->nodes.CreateNewNode();
Node &node = this->nodes.CreateNewNode();
return node;
}

View File

@ -23,7 +23,7 @@ class CYapfOriginTileT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -63,7 +63,7 @@ class CYapfOriginTileTwoWayT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -121,7 +121,7 @@ class CYapfDestinationTileT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:

View File

@ -25,7 +25,7 @@ class CYapfSegmentCostCacheNoneT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
/**
* Called by YAPF to attach cached or local segment cost data to the given node.
@ -106,7 +106,7 @@ template <class Types>
class CYapfSegmentCostCacheGlobalT {
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
typedef typename Node::CachedData CachedData;
typedef typename CachedData::Key CacheKey;

View File

@ -22,7 +22,7 @@ class CYapfCostRailT : public CYapfCostBase {
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
typedef typename Node::CachedData CachedData;

View File

@ -40,7 +40,7 @@ template <class Types>
class CYapfDestinationAnyDepotRailT : public CYapfDestinationRailBase {
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
/** to access inherited path finder */
@ -76,7 +76,7 @@ template <class Types>
class CYapfDestinationAnySafeTileRailT : public CYapfDestinationRailBase {
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
typedef typename Types::TrackFollower TrackFollower; ///< TrackFollower. Need to typedef for gcc 2.95
@ -114,7 +114,7 @@ template <class Types>
class CYapfDestinationTileOrStationRailT : public CYapfDestinationRailBase {
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:

View File

@ -215,7 +215,7 @@ typedef CYapfRailNodeT<CYapfNodeKeyExitDir> CYapfRailNodeExitDir;
typedef CYapfRailNodeT<CYapfNodeKeyTrackDir> CYapfRailNodeTrackDir;
/* Default NodeList types */
typedef CNodeList_HashTableT<CYapfRailNodeExitDir , 8, 10> CRailNodeListExitDir;
typedef CNodeList_HashTableT<CYapfRailNodeTrackDir, 8, 10> CRailNodeListTrackDir;
typedef NodeList<CYapfRailNodeExitDir , 8, 10> CRailNodeListExitDir;
typedef NodeList<CYapfRailNodeTrackDir, 8, 10> CRailNodeListTrackDir;
#endif /* YAPF_NODE_RAIL_HPP */

View File

@ -36,7 +36,7 @@ typedef CYapfRoadNodeT<CYapfNodeKeyExitDir> CYapfRoadNodeExitDir;
typedef CYapfRoadNodeT<CYapfNodeKeyTrackDir> CYapfRoadNodeTrackDir;
/* Default NodeList types */
typedef CNodeList_HashTableT<CYapfRoadNodeExitDir , 8, 10> CRoadNodeListExitDir;
typedef CNodeList_HashTableT<CYapfRoadNodeTrackDir, 8, 10> CRoadNodeListTrackDir;
typedef NodeList<CYapfRoadNodeExitDir , 8, 10> CRoadNodeListExitDir;
typedef NodeList<CYapfRoadNodeTrackDir, 8, 10> CRoadNodeListTrackDir;
#endif /* YAPF_NODE_ROAD_HPP */

View File

@ -36,7 +36,7 @@ typedef CYapfShipNodeT<CYapfNodeKeyExitDir> CYapfShipNodeExitDir;
typedef CYapfShipNodeT<CYapfNodeKeyTrackDir> CYapfShipNodeTrackDir;
/* Default NodeList types */
typedef CNodeList_HashTableT<CYapfShipNodeExitDir , 10, 12> CShipNodeListExitDir;
typedef CNodeList_HashTableT<CYapfShipNodeTrackDir, 10, 12> CShipNodeListTrackDir;
typedef NodeList<CYapfShipNodeExitDir , 10, 12> CShipNodeListExitDir;
typedef NodeList<CYapfShipNodeTrackDir, 10, 12> CShipNodeListTrackDir;
#endif /* YAPF_NODE_SHIP_HPP */

View File

@ -38,7 +38,7 @@ class CYapfReserveTrack
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
protected:
/** to access inherited pathfinder */
@ -205,7 +205,7 @@ class CYapfFollowAnyDepotRailT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -296,7 +296,7 @@ class CYapfFollowAnySafeTileRailT : public CYapfReserveTrack<Types>
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -378,7 +378,7 @@ class CYapfFollowRailT : public CYapfReserveTrack<Types>
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:

View File

@ -21,7 +21,7 @@ class CYapfCostRoadT
public:
typedef typename Types::Tpf Tpf; ///< pathfinder (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower; ///< track follower helper
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -189,7 +189,7 @@ class CYapfDestinationAnyDepotRoadT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
/** to access inherited path finder */
@ -227,7 +227,7 @@ class CYapfDestinationTileRoadT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
@ -328,7 +328,7 @@ class CYapfFollowRoadT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Types::NodeList::Item Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:

View File

@ -30,7 +30,7 @@ class CYapfDestinationTileWaterT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class).
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type.
typedef typename Types::NodeList::Item Node; ///< this will be our node type.
typedef typename Node::Key Key; ///< key to hash tables.
protected:
@ -129,7 +129,7 @@ class CYapfFollowShipT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class).
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type.
typedef typename Types::NodeList::Item Node; ///< this will be our node type.
typedef typename Node::Key Key; ///< key to hash tables.
protected:
@ -329,7 +329,7 @@ class CYapfCostShipT
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class).
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type.
typedef typename Types::NodeList::Item Node; ///< this will be our node type.
typedef typename Node::Key Key; ///< key to hash tables.
/** to access inherited path finder */

View File

@ -90,7 +90,7 @@ class CYapfOriginRegionT
{
public:
typedef typename Types::Tpf Tpf; ///< The pathfinder class (derived from THIS class).
typedef typename Types::NodeList::Titem Node; ///< This will be our node type.
typedef typename Types::NodeList::Item Node; ///< This will be our node type.
typedef typename Node::Key Key; ///< Key to hash tables.
protected:
@ -127,7 +127,7 @@ class CYapfDestinationRegionT
{
public:
typedef typename Types::Tpf Tpf; ///< The pathfinder class (derived from THIS class).
typedef typename Types::NodeList::Titem Node; ///< This will be our node type.
typedef typename Types::NodeList::Item Node; ///< This will be our node type.
typedef typename Node::Key Key; ///< Key to hash tables.
protected:
@ -168,7 +168,7 @@ class CYapfFollowRegionT
public:
typedef typename Types::Tpf Tpf; ///< The pathfinder class (derived from THIS class).
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< This will be our node type.
typedef typename Types::NodeList::Item Node; ///< This will be our node type.
typedef typename Node::Key Key; ///< Key to hash tables.
protected:
@ -240,7 +240,7 @@ class CYapfCostRegionT
public:
typedef typename Types::Tpf Tpf; ///< The pathfinder class (derived from THIS class).
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< This will be our node type.
typedef typename Types::NodeList::Item Node; ///< This will be our node type.
typedef typename Node::Key Key; ///< Key to hash tables.
protected:
@ -293,7 +293,7 @@ struct CYapfRegion_TypesT
typedef CYapfCostRegionT<Types> PfCost; ///< Cost provider.
};
typedef CNodeList_HashTableT<CYapfRegionNodeT<CYapfRegionPatchNodeKey>, 12, 12> CRegionNodeListWater;
typedef NodeList<CYapfRegionNodeT<CYapfRegionPatchNodeKey>, 12, 12> CRegionNodeListWater;
struct CYapfRegionWater : CYapfT<CYapfRegion_TypesT<CYapfRegionWater, CRegionNodeListWater>>
{