From 76becc00bdf2a3694de414e254a405a8b8c35c2e Mon Sep 17 00:00:00 2001 From: nitrocaster Date: Fri, 11 Dec 2015 01:55:53 +0300 Subject: [PATCH] Reformat code. --- src/xrAICore/Navigation/a_star.h | 104 +++-- src/xrAICore/Navigation/a_star_inline.h | 391 +++++++++--------- .../Navigation/data_storage_binary_heap.h | 64 +-- .../data_storage_binary_heap_inline.h | 97 +++-- .../Navigation/data_storage_bucket_list.h | 98 ++--- .../data_storage_bucket_list_inline.h | 339 +++++++-------- .../Navigation/data_storage_constructor.h | 47 +-- src/xrAICore/Navigation/dijkstra.h | 95 ++--- src/xrAICore/Navigation/dijkstra_inline.h | 330 ++++++++------- src/xrAICore/Navigation/edge_path.h | 52 +-- src/xrAICore/Navigation/edge_path_inline.h | 83 ++-- .../Navigation/vertex_allocator_fixed.h | 46 +-- .../vertex_allocator_fixed_inline.h | 57 +-- .../Navigation/vertex_manager_fixed.h | 103 +++-- .../Navigation/vertex_manager_fixed_inline.h | 130 +++--- .../Navigation/vertex_manager_hash_fixed.h | 122 +++--- .../vertex_manager_hash_fixed_inline.h | 228 +++++----- src/xrAICore/Navigation/vertex_path.h | 50 +-- src/xrAICore/Navigation/vertex_path_inline.h | 78 ++-- 19 files changed, 1215 insertions(+), 1299 deletions(-) diff --git a/src/xrAICore/Navigation/a_star.h b/src/xrAICore/Navigation/a_star.h index 8f2e4e087e2..6d3910d0fb4 100644 --- a/src/xrAICore/Navigation/a_star.h +++ b/src/xrAICore/Navigation/a_star.h @@ -1,9 +1,9 @@ //////////////////////////////////////////////////////////////////////////// -// Module : a_star.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Implementation of the A* (a-star) algorithm +// Module : a_star.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Implementation of the A* (a-star) algorithm //////////////////////////////////////////////////////////////////////////// #pragma once @@ -12,68 +12,66 @@ #include "xrAICore/Navigation/data_storage_constructor.h" #include "xrAICore/Navigation/dijkstra.h" -template +template struct AStarVertexData { - template - struct VertexData : TVertexData::template VertexData + template + struct VertexData : TVertexData::template VertexData { - typedef _dist_type _dist_type; + using Distance = TDistance; - _dist_type _g; - _dist_type _h; + Distance _g; + Distance _h; - _dist_type &g() { return _g; } - _dist_type &h() { return _h; } - }; + Distance &g() { return _g; } + Distance &h() { return _h; } + }; }; template < - typename _dist_type, - typename _priority_queue, - typename _vertex_manager, - typename _vertex_allocator, - bool euclidian_heuristics = true, - typename _data_storage_base = CVertexPath, - typename _iteration_type = u32, + typename TDistance, + typename TPriorityQueue, + typename TVertexManager, + typename TVertexAllocator, + bool EuclidianHeuristics = true, + typename TPathBuilder = CVertexPath, + typename TIteration = u32, typename TVertexData = EmptyVertexData -> class CAStar : public CDijkstra< - _dist_type, - _priority_queue, - _vertex_manager, - _vertex_allocator, - euclidian_heuristics, - _data_storage_base, - _iteration_type, - AStarVertexData<_dist_type, TVertexData> - > +> +class CAStar : public CDijkstra< + TDistance, + TPriorityQueue, + TVertexManager, + TVertexAllocator, + EuclidianHeuristics, + TPathBuilder, + TIteration, + AStarVertexData +> { protected: - typedef CDijkstra < - _dist_type, - _priority_queue, - _vertex_manager, - _vertex_allocator, - euclidian_heuristics, - _data_storage_base, - _iteration_type, - AStarVertexData<_dist_type, TVertexData> - > inherited; - typedef typename inherited::CGraphVertex CGraphVertex; - typedef typename CGraphVertex::_dist_type _dist_type; - typedef typename CGraphVertex::_index_type _index_type; + using Inherited = CDijkstra< + TDistance, + TPriorityQueue, + TVertexManager, + TVertexAllocator, + EuclidianHeuristics, + TPathBuilder, + TIteration, + AStarVertexData + >; protected: - template - IC void initialize (_PathManager &path_manager); - template - IC bool step (_PathManager &path_manager); + template + inline void initialize(TPathManager &path_manager); + template + inline bool step(TPathManager &path_manager); public: - IC CAStar (const u32 max_vertex_count); - virtual ~CAStar (); - template - IC bool find (_PathManager &path_manager); + inline CAStar(const u32 max_vertex_count); + virtual ~CAStar(); + template + inline bool find(TPathManager &path_manager); }; -#include "xrAICore/Navigation/a_star_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/a_star_inline.h" diff --git a/src/xrAICore/Navigation/a_star_inline.h b/src/xrAICore/Navigation/a_star_inline.h index 2ff81bdd480..289f6a00363 100644 --- a/src/xrAICore/Navigation/a_star_inline.h +++ b/src/xrAICore/Navigation/a_star_inline.h @@ -1,222 +1,219 @@ //////////////////////////////////////////////////////////////////////////// -// Module : a_star_inline.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Implementation of the A* (a-star) algorithm : inline functions +// Module : a_star_inline.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Implementation of the A*(a-star) algorithm : inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template <\ - typename _dist_type,\ - typename _priority_queue,\ - typename _vertex_manager,\ - typename _vertex_allocator,\ - bool euclidian_heuristics,\ - typename _data_storage_base,\ - typename _iteration_type,\ +#define TEMPLATE_SPECIALIZATION\ + template <\ + typename TDistance,\ + typename TPriorityQueue,\ + typename TVertexManager,\ + typename TVertexAllocator,\ + bool EuclidianHeuristics,\ + typename TPathBuilder,\ + typename TIteration,\ typename TVertexData\ - > + > -#define CSAStar CAStar<\ - _dist_type,\ - _priority_queue,\ - _vertex_manager,\ - _vertex_allocator,\ - euclidian_heuristics,\ - _data_storage_base,\ - _iteration_type,\ - TVertexData\ -> +#define CSAStar\ + CAStar<\ + TDistance,\ + TPriorityQueue,\ + TVertexManager,\ + TVertexAllocator,\ + EuclidianHeuristics,\ + TPathBuilder,\ + TIteration,\ + TVertexData\ + > TEMPLATE_SPECIALIZATION -IC CSAStar::CAStar (const u32 max_vertex_count) : - inherited (max_vertex_count) -{ -} +inline CSAStar::CAStar(const u32 max_vertex_count) : + Inherited(max_vertex_count) +{} TEMPLATE_SPECIALIZATION -CSAStar::~CAStar () -{ -} +inline CSAStar::~CAStar() +{} TEMPLATE_SPECIALIZATION -template -IC void CSAStar::initialize (_PathManager &path_manager) +template +inline void CSAStar::initialize(TPathManager &path_manager) { - THROW2 (!m_search_started,"Recursive graph engine usage is not allowed!"); - m_search_started = true; - // initialize data structures before we started path search - data_storage().init (); - - // initialize path manager before we started path search - path_manager.init (); - - // create a node - CGraphVertex &start = data_storage().create_vertex(path_manager.start_node()); - - // assign correspoding values to the created node - start.g() = _dist_type(0); - start.h() = path_manager.estimate(start.index()); - start.f() = start.g() + start.h(); - - // assign null parent to the start node - data_storage().assign_parent (start,0); - - // add start node to the opened list - data_storage().add_opened (start); + THROW2(!m_search_started, "Recursive graph engine usage is not allowed!"); + m_search_started = true; + // initialize data structures before we started path search + data_storage().init(); + // initialize path manager before we started path search + path_manager.init(); + // create a node + Vertex &start = data_storage().create_vertex(path_manager.start_node()); + // assign correspoding values to the created node + start.g() = Distance(0); + start.h() = path_manager.estimate(start.index()); + start.f() = start.g()+start.h(); + // assign null parent to the start node + data_storage().assign_parent(start, 0); + // add start node to the opened list + data_storage().add_opened(start); } TEMPLATE_SPECIALIZATION -template -IC bool CSAStar::step (_PathManager &path_manager) +template +inline bool CSAStar::step(TPathManager &path_manager) { - // get the best node, i.e. a node with the minimum 'f' - CGraphVertex &best = data_storage().get_best(); - - // check if this node is the one we are searching for - if (path_manager.is_goal_reached(best.index())) { - // we reached the goal, so we have to create a path - path_manager.init_path (); - path_manager.create_path (best); - // and return success - return (true); - } - - // put best node to the closed list - data_storage().add_best_closed(); - // and remove this node from the opened one - data_storage().remove_best_opened(); - - // iterating on the best node neighbours - _PathManager::const_iterator i; - _PathManager::const_iterator e; - path_manager.begin (best.index(),i,e); - for ( ; i != e; ++i) { - const _index_type &neighbour_index = path_manager.get_value(i); - // check if neighbour is accessible - if (!path_manager.is_accessible(neighbour_index)) - continue; - // check if neighbour is visited, i.e. is in the opened or - // closed lists - if (data_storage().is_visited(neighbour_index)) { - // so, this neighbour node has been already visited - // therefore get the pointer to this node - CGraphVertex &neighbour = data_storage().get_node(neighbour_index); - // check if this node is in the opened list - if (data_storage().is_opened(neighbour)) { - // compute 'g' for the node - _dist_type g = best.g() + path_manager.evaluate(best.index(),neighbour_index,i); - // check if new path is better than the older one - if (neighbour.g() > g) { - // so, new path is better - // assign corresponding values to the node - _dist_type d = neighbour.f(); - neighbour.g() = g; - neighbour.f() = neighbour.g() + neighbour.h(); - // assign correct parent to the node to be able - // to retreive a path - data_storage().assign_parent (neighbour,&best,path_manager.edge(i)); - // notify data storage about node decreasing value - data_storage().decrease_opened(neighbour,d); - // continue iterating on neighbours - continue; - } - // so, new path is worse - // continue iterating on neighbours - continue; - } - // so, our node is in the closed list - // here is a _nuance_ : if we don't use any heuristics, - // i.e. it is not A*, but Dijkstra algorithm, or we use - // a heuristics which _guarantees_ that found path is - // the best among the others (if we have a path_manager with - // euclidian metrics and use distance between current - // and goal points as an estimation value), then it is - // impossible that we can find a better path for a node - // which is in the closed list and therefore we have to do - // nothing here. - if (!path_manager.is_metric_euclidian()) { - // so, we use a heurictics which doesn't gurantee that - // found path is the best, then we have to update all - // of the our node successors but we still can't be sure - // that when the condition 'is_goal_reached' is true, - // then we found the _best_ path - - // check if new path is better than the older one - _dist_type g = best.g() + path_manager.evaluate(best.index(),neighbour_index,i); - if (neighbour.g() > g) { - // so, new path is better - // assign corresponding values to the node - neighbour.g() = g; - neighbour.f() = neighbour.g() + neighbour.h(); - // assign correct parent to the node to be able - // to retreive a path - data_storage().assign_parent (neighbour,&best,path_manager.edge(i)); - // notify data storage about node decreasing value - // to make it modify all the node successors - data_storage().update_successors(neighbour); - // continue iterating on neighbours - continue; - } - // so, new path is worse - // continue iterating on neighbours - continue; - } - // continue iterating on neighbours - continue; - } - else { - // so, this neighbour node is not in the opened or closed lists - // put neighbour node to the opened list - CGraphVertex &neighbour = data_storage().create_vertex(neighbour_index); - // fill the corresponding node parameters - neighbour.g() = best.g() + path_manager.evaluate(best.index(),neighbour_index,i); - neighbour.h() = path_manager.estimate(neighbour.index()); - neighbour.f() = neighbour.g() + neighbour.h(); - // assign best node as its parent - data_storage().assign_parent(neighbour,&best,path_manager.edge(i)); - // add start node to the opened list - data_storage().add_opened (neighbour); - // continue iterating on neighbours - continue; - } - } - - // this iteration haven't got the goal node, therefore return failure - return (false); + // get the best node, i.e. a node with the minimum 'f' + Vertex &best = data_storage().get_best(); + // check if this node is the one we are searching for + if (path_manager.is_goal_reached(best.index())) + { + // we reached the goal, so we have to create a path + path_manager.init_path(); + path_manager.create_path(best); + // and return success + return true; + } + // put best node to the closed list + data_storage().add_best_closed(); + // and remove this node from the opened one + data_storage().remove_best_opened(); + // iterating on the best node neighbours + TPathManager::const_iterator i, e; + path_manager.begin(best.index(), i, e); + for (; i != e; i++) + { + const Index &neighbour_index = path_manager.get_value(i); + // check if neighbour is accessible + if (!path_manager.is_accessible(neighbour_index)) + continue; + // check if neighbour is visited, i.e. is in the opened or + // closed lists + if (data_storage().is_visited(neighbour_index)) + { + // so, this neighbour node has been already visited + // therefore get the pointer to this node + Vertex &neighbour = data_storage().get_node(neighbour_index); + // check if this node is in the opened list + if (data_storage().is_opened(neighbour)) + { + // compute 'g' for the node + Distance g = best.g()+path_manager.evaluate(best.index(), neighbour_index, i); + // check if new path is better than the older one + if (neighbour.g()>g) + { + // so, new path is better + // assign corresponding values to the node + Distance d = neighbour.f(); + neighbour.g() = g; + neighbour.f() = neighbour.g()+neighbour.h(); + // assign correct parent to the node to be able + // to retreive a path + data_storage().assign_parent(neighbour, &best, path_manager.edge(i)); + // notify data storage about node decreasing value + data_storage().decrease_opened(neighbour, d); + // continue iterating on neighbours + continue; + } + // so, new path is worse + // continue iterating on neighbours + continue; + } + // so, our node is in the closed list + // here is a _nuance_ : if we don't use any heuristics, + // i.e. it is not A*, but Dijkstra algorithm, or we use + // a heuristics which _guarantees_ that found path is + // the best among the others(if we have a path_manager with + // euclidian metrics and use distance between current + // and goal points as an estimation value), then it is + // impossible that we can find a better path for a node + // which is in the closed list and therefore we have to do + // nothing here. + if (!path_manager.is_metric_euclidian()) + { + // so, we use a heurictics which doesn't gurantee that + // found path is the best, then we have to update all + // of the our node successors but we still can't be sure + // that when the condition 'is_goal_reached' is true, + // then we found the _best_ path. + // check if new path is better than the older one + Distance g = best.g()+path_manager.evaluate(best.index(), neighbour_index, i); + if (neighbour.g()>g) + { + // so, new path is better + // assign corresponding values to the node + neighbour.g() = g; + neighbour.f() = neighbour.g()+neighbour.h(); + // assign correct parent to the node to be able + // to retreive a path + data_storage().assign_parent(neighbour, &best, path_manager.edge(i)); + // notify data storage about node decreasing value + // to make it modify all the node successors + data_storage().update_successors(neighbour); + // continue iterating on neighbours + continue; + } + // so, new path is worse + // continue iterating on neighbours + continue; + } + // continue iterating on neighbours + continue; + } + else + { + // so, this neighbour node is not in the opened or closed lists + // put neighbour node to the opened list + Vertex &neighbour = data_storage().create_vertex(neighbour_index); + // fill the corresponding node parameters + neighbour.g() = best.g()+path_manager.evaluate(best.index(), neighbour_index, i); + neighbour.h() = path_manager.estimate(neighbour.index()); + neighbour.f() = neighbour.g()+neighbour.h(); + // assign best node as its parent + data_storage().assign_parent(neighbour, &best, path_manager.edge(i)); + // add start node to the opened list + data_storage().add_opened(neighbour); + // continue iterating on neighbours + continue; + } + } + // this iteration haven't got the goal node, therefore return failure + return false; } TEMPLATE_SPECIALIZATION -template -IC bool CSAStar::find (_PathManager &path_manager) +template +inline bool CSAStar::find(TPathManager &path_manager) { - // initialize data structures with new search - initialize (path_manager); - // iterate while opened list is not empty - for (_iteration_type i = _iteration_type(0); !data_storage().is_opened_empty(); ++i) { - // check if we reached limit - if (path_manager.is_limit_reached(i)) { - // so we reached limit, return failure - finalize (path_manager); - return (false); - } - - // so, limit is not reached - // check if new step will get us success - if (step(path_manager)) { - // so this step reached the goal, return success - finalize (path_manager); - return (true); - } - } - - // so, opened list is empty, return failure - finalize (path_manager); - return (false); + // initialize data structures with new search + initialize(path_manager); + // iterate while opened list is not empty + for (TIteration i = TIteration(0); !data_storage().is_opened_empty(); i++) + { + // check if we reached limit + if (path_manager.is_limit_reached(i)) + { + // so we reached limit, return failure + finalize(path_manager); + return false; + } + // so, limit is not reached + // check if new step will get us success + if (step(path_manager)) + { + // so this step reached the goal, return success + finalize(path_manager); + return true; + } + } + // so, opened list is empty, return failure + finalize(path_manager); + return false; } #undef TEMPLATE_SPECIALIZATION -#undef CSAStar \ No newline at end of file +#undef CSAStar diff --git a/src/xrAICore/Navigation/data_storage_binary_heap.h b/src/xrAICore/Navigation/data_storage_binary_heap.h index 220e009789c..0a9b0cb4b1b 100644 --- a/src/xrAICore/Navigation/data_storage_binary_heap.h +++ b/src/xrAICore/Navigation/data_storage_binary_heap.h @@ -1,50 +1,50 @@ //////////////////////////////////////////////////////////////////////////// -// Module : data_storage_binary_heap.h -// Created : 21.03.2002 -// Modified : 26.02.2004 -// Author : Dmitriy Iassenev -// Description : Binary heap data storage +// Module : data_storage_binary_heap.h +// Created : 21.03.2002 +// Modified : 26.02.2004 +// Author : Dmitriy Iassenev +// Description : Binary heap data storage //////////////////////////////////////////////////////////////////////////// #pragma once struct CDataStorageBinaryHeap { - template + template struct VertexData {}; - template + template class CDataStorage : public TManagerDataStorage { - public: - typedef TManagerDataStorage inherited; - typedef typename TManagerDataStorage::CGraphVertex CGraphVertex; - typedef typename CGraphVertex::_dist_type _dist_type; - typedef typename CGraphVertex::_index_type _index_type; + public: + using Inherited = TManagerDataStorage; + using Vertex = typename Inherited::Vertex; + using Distance = typename Vertex::Distance; + using Index = typename Vertex::Index; - struct CGraphNodePredicate + struct VertexPredicate { - bool operator()(CGraphVertex *node1, CGraphVertex *node2) - { return node1->f() > node2->f(); } - }; + bool operator()(Vertex *a, Vertex *b) + { return a->f() > b->f(); } + }; - protected: - CGraphVertex **m_heap; - CGraphVertex **m_heap_head; - CGraphVertex **m_heap_tail; + protected: + Vertex **m_heap; + Vertex **m_heap_head; + Vertex **m_heap_tail; - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void init (); - IC bool is_opened_empty () const; - IC void add_opened (CGraphVertex &vertex); - IC void decrease_opened (CGraphVertex &vertex, const _dist_type value); - IC void remove_best_opened (); - IC void add_best_closed (); - IC CGraphVertex &get_best () const; - }; + public: + inline CDataStorage(const u32 vertex_count); + inline virtual ~CDataStorage(); + inline void init(); + inline bool is_opened_empty() const; + inline void add_opened(Vertex &vertex); + inline void decrease_opened(Vertex &vertex, const Distance value); + inline void remove_best_opened(); + inline void add_best_closed(); + inline Vertex &get_best() const; + }; }; -#include "xrAICore/Navigation/data_storage_binary_heap_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/data_storage_binary_heap_inline.h" diff --git a/src/xrAICore/Navigation/data_storage_binary_heap_inline.h b/src/xrAICore/Navigation/data_storage_binary_heap_inline.h index e17d54de200..ac370b745df 100644 --- a/src/xrAICore/Navigation/data_storage_binary_heap_inline.h +++ b/src/xrAICore/Navigation/data_storage_binary_heap_inline.h @@ -1,93 +1,88 @@ //////////////////////////////////////////////////////////////////////////// -// Module : data_storage_binary_heap_inline.h -// Created : 21.03.2002 -// Modified : 26.02.2004 -// Author : Dmitriy Iassenev -// Description : Binary m_heap data storage inline functions +// Module : data_storage_binary_heap_inline.h +// Created : 21.03.2002 +// Modified : 26.02.2004 +// Author : Dmitriy Iassenev +// Description : Binary m_heap data storage inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION template +#define TEMPLATE_SPECIALIZATION template -#define CBinaryHeap CDataStorageBinaryHeap::CDataStorage +#define CBinaryHeap CDataStorageBinaryHeap::CDataStorage TEMPLATE_SPECIALIZATION -IC CBinaryHeap::CDataStorage (const u32 vertex_count) : - inherited(vertex_count) +inline CBinaryHeap::CDataStorage(const u32 vertex_count) : + Inherited(vertex_count) { - u32 memory_usage = 0; - u32 byte_count; - - byte_count = vertex_count*sizeof(CGraphVertex*); - m_heap = xr_alloc(vertex_count); - ZeroMemory (m_heap,byte_count); - memory_usage += byte_count; + m_heap = xr_alloc(vertex_count); + ZeroMemory(m_heap, vertex_count*sizeof(Vertex*)); } TEMPLATE_SPECIALIZATION -CBinaryHeap::~CDataStorage () -{ - xr_free (m_heap); -} +CBinaryHeap::~CDataStorage() +{ xr_free(m_heap); } TEMPLATE_SPECIALIZATION -IC void CBinaryHeap::init () +inline void CBinaryHeap::init() { - inherited::init (); - m_heap_head = m_heap_tail = m_heap; + Inherited::init(); + m_heap_head = m_heap_tail = m_heap; } TEMPLATE_SPECIALIZATION -IC bool CBinaryHeap::is_opened_empty () const +inline bool CBinaryHeap::is_opened_empty() const { - VERIFY (m_heap_head <= m_heap_tail); - return (m_heap_head == m_heap_tail); + VERIFY(m_heap_head<=m_heap_tail); + return m_heap_head==m_heap_tail; } TEMPLATE_SPECIALIZATION -IC void CBinaryHeap::add_opened (CGraphVertex &vertex) +inline void CBinaryHeap::add_opened(Vertex &vertex) { - VERIFY (m_heap_head <= m_heap_tail); - inherited::add_opened (vertex); - if (!*m_heap_head || ((*m_heap_head)->f() < vertex.f())) { - *m_heap_tail = &vertex; - } - else { - *m_heap_tail = *m_heap_head; - *m_heap_head = &vertex; - } - std::push_heap (m_heap_head,++m_heap_tail,CGraphNodePredicate()); + VERIFY(m_heap_head<=m_heap_tail); + Inherited::add_opened(vertex); + if (!*m_heap_head || (*m_heap_head)->f() < vertex.f()) + { + *m_heap_tail = &vertex; + } + else + { + *m_heap_tail = *m_heap_head; + *m_heap_head = &vertex; + } + std::push_heap(m_heap_head, ++m_heap_tail, VertexPredicate()); } TEMPLATE_SPECIALIZATION -IC void CBinaryHeap::decrease_opened (CGraphVertex &vertex, const _dist_type value) +inline void CBinaryHeap::decrease_opened(Vertex &vertex, const Distance value) { - VERIFY (!is_opened_empty()); - for (CGraphVertex **i = m_heap_head; *i != &vertex; ++i); - std::push_heap (m_heap_head,i + 1,CGraphNodePredicate()); + VERIFY(!is_opened_empty()); + for (Vertex **i = m_heap_head; *i!=&vertex; i++); + std::push_heap(m_heap_head, i+1, VertexPredicate()); } TEMPLATE_SPECIALIZATION -IC void CBinaryHeap::remove_best_opened () +inline void CBinaryHeap::remove_best_opened() { - VERIFY (!is_opened_empty()); - std::pop_heap (m_heap_head,m_heap_tail--,CGraphNodePredicate()); + VERIFY(!is_opened_empty()); + std::pop_heap(m_heap_head, m_heap_tail--, VertexPredicate()); } TEMPLATE_SPECIALIZATION -IC void CBinaryHeap::add_best_closed () +inline void CBinaryHeap::add_best_closed() { - VERIFY (!is_opened_empty()); - inherited::add_closed (**m_heap_head); + VERIFY(!is_opened_empty()); + Inherited::add_closed(**m_heap_head); } TEMPLATE_SPECIALIZATION -IC typename CBinaryHeap::CGraphVertex &CBinaryHeap::get_best () const +inline typename CBinaryHeap::Vertex &CBinaryHeap::get_best() const { - VERIFY (!is_opened_empty()); - return (**m_heap_head); + VERIFY(!is_opened_empty()); + return **m_heap_head; } #undef TEMPLATE_SPECIALIZATION -#undef CBinaryHeap \ No newline at end of file +#undef CBinaryHeap diff --git a/src/xrAICore/Navigation/data_storage_bucket_list.h b/src/xrAICore/Navigation/data_storage_bucket_list.h index bc3bee03c32..6c361534ef5 100644 --- a/src/xrAICore/Navigation/data_storage_bucket_list.h +++ b/src/xrAICore/Navigation/data_storage_bucket_list.h @@ -1,69 +1,69 @@ //////////////////////////////////////////////////////////////////////////// -// Module : data_storage_bucket_list.h -// Created : 21.03.2002 -// Modified : 26.02.2004 -// Author : Dmitriy Iassenev -// Description : Bucket list data storage +// Module : data_storage_bucket_list.h +// Created : 21.03.2002 +// Modified : 26.02.2004 +// Author : Dmitriy Iassenev +// Description : Bucket list data storage //////////////////////////////////////////////////////////////////////////// #pragma once template < - typename _path_id_type, - typename _bucket_id_type, - u32 bucket_count, - bool clear_buckets + typename TPathId, + typename TBucketId, + u32 BucketCount, + bool ClearBuckets > struct CDataStorageBucketList { - template - struct VertexData + template + struct VertexData { TCompoundVertex *_next; TCompoundVertex *_prev; - _path_id_type m_path_id; - _bucket_id_type m_bucket_id; + TPathId m_path_id; + TBucketId m_bucket_id; TCompoundVertex *&next() { return _next; } TCompoundVertex *&prev() { return _prev; } - }; + }; - template - class CDataStorage : public TManagerDataStorage + template + class CDataStorage : public TManagerDataStorage { - public: - typedef TManagerDataStorage inherited; - typedef typename TManagerDataStorage::CGraphVertex CGraphVertex; - typedef typename CGraphVertex::_dist_type _dist_type; - typedef typename CGraphVertex::_index_type _index_type; + public: + using Inherited = TManagerDataStorage; + using Vertex = typename TManagerDataStorage::Vertex; + using Distance = typename Vertex::Distance; + using Index = typename Vertex::Index; - protected: - _dist_type m_max_distance; - CGraphVertex m_list_data[2]; - CGraphVertex *m_list_head; - CGraphVertex *m_list_tail; - _dist_type m_switch_factor; - _dist_type m_min_bucket_value; - _dist_type m_max_bucket_value; - CGraphVertex *m_buckets[bucket_count]; - u32 m_min_bucket_id; + protected: + Distance m_max_distance; + Vertex m_list_data[2]; + Vertex *m_list_head; + Vertex *m_list_tail; + Distance m_switch_factor; + Distance m_min_bucket_value; + Distance m_max_bucket_value; + Vertex *m_buckets[BucketCount]; + u32 m_min_bucket_id; - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void init (); - IC void add_best_closed (); - IC void set_switch_factor (const _dist_type _switch_factor); - IC bool is_opened_empty (); - IC u32 compute_bucket_id (CGraphVertex &vertex) const; - IC void verify_buckets () const; - IC void add_to_bucket (CGraphVertex &vertex, u32 bucket_id); - IC void add_opened (CGraphVertex &vertex); - IC void decrease_opened (CGraphVertex &vertex, const _dist_type value); - IC void remove_best_opened (); - IC CGraphVertex &get_best (); - IC void set_min_bucket_value(const _dist_type min_bucket_value); - IC void set_max_bucket_value(const _dist_type max_bucket_value); - }; + public: + inline CDataStorage(const u32 vertex_count); + virtual ~CDataStorage(); + inline void init(); + inline void add_best_closed(); + inline void set_switch_factor(const Distance _switch_factor); + inline bool is_opened_empty(); + inline u32 compute_bucket_id(Vertex &vertex) const; + inline void verify_buckets() const; + inline void add_to_bucket(Vertex &vertex, u32 bucket_id); + inline void add_opened(Vertex &vertex); + inline void decrease_opened(Vertex &vertex, const Distance value); + inline void remove_best_opened(); + inline Vertex &get_best(); + inline void set_min_bucket_value(const Distance min_bucket_value); + inline void set_max_bucket_value(const Distance max_bucket_value); + }; }; -#include "xrAICore/Navigation/data_storage_bucket_list_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/data_storage_bucket_list_inline.h" diff --git a/src/xrAICore/Navigation/data_storage_bucket_list_inline.h b/src/xrAICore/Navigation/data_storage_bucket_list_inline.h index acc0be08d01..7ac93ae2619 100644 --- a/src/xrAICore/Navigation/data_storage_bucket_list_inline.h +++ b/src/xrAICore/Navigation/data_storage_bucket_list_inline.h @@ -1,102 +1,119 @@ //////////////////////////////////////////////////////////////////////////// -// Module : data_storage_bucket_list_רעהרעף.h -// Created : 21.03.2002 -// Modified : 26.02.2004 -// Author : Dmitriy Iassenev -// Description : Bucket list data storage inline functions +// Module : data_storage_bucket_list_inline.h +// Created : 21.03.2002 +// Modified : 26.02.2004 +// Author : Dmitriy Iassenev +// Description : Bucket list data storage inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template <\ - typename _path_id_type,\ - typename _bucket_id_type,\ - u32 bucket_count,\ - bool clear_buckets\ - >\ - template +#define TEMPLATE_SPECIALIZATION\ + template <\ + typename TPathId,\ + typename TBucketId,\ + u32 BucketCount,\ + bool ClearBuckets\ + >\ + template -#define CBucketList CDataStorageBucketList<_path_id_type,_bucket_id_type,bucket_count,clear_buckets>::CDataStorage +#define CBucketList\ + CDataStorageBucketList::CDataStorage TEMPLATE_SPECIALIZATION -IC CBucketList::CDataStorage (const u32 vertex_count) : - inherited(vertex_count) +inline CBucketList::CDataStorage(const u32 vertex_count) : + Inherited(vertex_count) { - m_max_distance = _dist_type(-1); - m_switch_factor = _dist_type(1); - m_min_bucket_value = _dist_type(0); - m_max_bucket_value = _dist_type(1000); - ZeroMemory (m_buckets,bucket_count*sizeof(CGraphVertex*)); + m_max_distance = Distance(-1); + m_switch_factor = Distance(1); + m_min_bucket_value = Distance(0); + m_max_bucket_value = Distance(1000); + ZeroMemory(m_buckets, BucketCount*sizeof(Vertex*)); } TEMPLATE_SPECIALIZATION -CBucketList::~CDataStorage () -{ -} +CBucketList::~CDataStorage() +{} TEMPLATE_SPECIALIZATION -IC void CBucketList::init () +inline void CBucketList::init() { - inherited::init (); - ZeroMemory (m_list_data,2*sizeof(CGraphVertex)); - m_list_head = m_list_data; - m_list_tail = m_list_data + 1; - m_list_head->next() = m_list_tail; - m_list_tail->f() = m_max_distance; - m_list_tail->prev() = m_list_head; - m_min_bucket_id = bucket_count; - if (clear_buckets) - ZeroMemory (m_buckets,bucket_count*sizeof(CGraphVertex*)); + Inherited::init(); + ZeroMemory(m_list_data, 2*sizeof(Vertex)); + m_list_head = m_list_data; + m_list_tail = m_list_data+1; + m_list_head->next() = m_list_tail; + m_list_tail->f() = m_max_distance; + m_list_tail->prev() = m_list_head; + m_min_bucket_id = BucketCount; + if (ClearBuckets) + ZeroMemory(m_buckets, BucketCount*sizeof(Vertex*)); } TEMPLATE_SPECIALIZATION -IC void CBucketList::add_best_closed () +inline void CBucketList::add_best_closed() { - VERIFY (!is_opened_empty()); - inherited::add_closed (*m_buckets[m_min_bucket_id]); + VERIFY(!is_opened_empty()); + Inherited::add_closed(*m_buckets[m_min_bucket_id]); } TEMPLATE_SPECIALIZATION -IC void CBucketList::set_switch_factor (const _dist_type _switch_factor) +inline void CBucketList::set_switch_factor(const Distance _switch_factor) { - if (!sorted) - NODEFAULT; - m_switch_factor = _switch_factor; + if (!sorted) + NODEFAULT; + m_switch_factor = _switch_factor; } TEMPLATE_SPECIALIZATION -IC bool CBucketList::is_opened_empty () +inline bool CBucketList::is_opened_empty() { - if (m_min_bucket_id == bucket_count) - return (true); - if (!m_buckets[m_min_bucket_id]) { - if (!clear_buckets) - for (++m_min_bucket_id; (m_min_bucket_id < bucket_count) && (!m_buckets[m_min_bucket_id] || (m_buckets[m_min_bucket_id]->m_path_id != current_path_id()) || (m_buckets[m_min_bucket_id]->m_bucket_id != m_min_bucket_id)); ++m_min_bucket_id); - else - for (++m_min_bucket_id; (m_min_bucket_id < bucket_count) && !m_buckets[m_min_bucket_id]; ++m_min_bucket_id); - return (m_min_bucket_id >= bucket_count); - } - return (false); + if (m_min_bucket_id==BucketCount) + return true; + if (!m_buckets[m_min_bucket_id]) + { + m_min_bucket_id++; + if (!ClearBuckets) + { + while (m_min_bucket_idm_path_id!=current_path_id() || bucket->m_bucket_id!=m_min_bucket_id) + { + m_min_bucket_id++; + continue; + } + break; + } + } + else + { + while (m_min_bucket_id=BucketCount; + } + return false; } TEMPLATE_SPECIALIZATION -IC u32 CBucketList::compute_bucket_id (CGraphVertex &vertex) const +inline u32 CBucketList::compute_bucket_id(Vertex &vertex) const { - if (vertex.f() >= m_max_bucket_value) - return (bucket_count - 1); - if (vertex.f() <= m_min_bucket_value) - return (0); - return (u32(bucket_count*(vertex.f() - m_min_bucket_value)/(m_max_bucket_value - m_min_bucket_value))); + Distance dist = vertex.f(); + if (dist>=m_max_bucket_value) + return BucketCount-1; + if (dist<=m_min_bucket_value) + return 0; + return u32(BucketCount*(dist-m_min_bucket_value)/(m_max_bucket_value-m_min_bucket_value)); } TEMPLATE_SPECIALIZATION -IC void CBucketList::verify_buckets () const +inline void CBucketList::verify_buckets() const { #if 0 - for (u32 i = 0; iindex()]; @@ -127,129 +144,123 @@ IC void CBucketList::verify_buckets () const } TEMPLATE_SPECIALIZATION -IC void CBucketList::add_to_bucket (CGraphVertex &vertex, u32 m_bucket_id) +inline void CBucketList::add_to_bucket(Vertex &vertex, u32 m_bucket_id) { - if (m_bucket_id < m_min_bucket_id) - m_min_bucket_id = m_bucket_id; - - CGraphVertex *i = m_buckets[m_bucket_id]; - if (!i || (!clear_buckets && ((i->m_path_id != current_path_id()) || (i->m_bucket_id != m_bucket_id)))) { - vertex.m_bucket_id = m_bucket_id; - vertex.m_path_id = current_path_id(); - m_buckets[m_bucket_id] = &vertex; - vertex.next() = vertex.prev() = 0; - verify_buckets (); - return; - } - - vertex.m_bucket_id = m_bucket_id; - vertex.m_path_id = current_path_id(); - - if (i->f() >= vertex.f()) { - m_buckets[m_bucket_id] = &vertex; - vertex.next() = i; - vertex.prev() = 0; - i->prev() = &vertex; - verify_buckets (); - return; - } - - if (!i->next()) { - vertex.prev() = i; - vertex.next() = 0; - i->next() = &vertex; - verify_buckets (); - return; - } - - for (i = i->next(); i->next(); i = i->next()) { - if (i->f() >= vertex.f()) { - vertex.next() = i; - vertex.prev() = i->prev(); - i->prev()->next() = &vertex; - i->prev() = &vertex; - verify_buckets (); - return; - } - } - - if (i->f() >= vertex.f()) { - vertex.next() = i; - vertex.prev() = i->prev(); - i->prev()->next() = &vertex; - i->prev() = &vertex; - verify_buckets (); - return; - } - else { - vertex.next() = 0; - vertex.prev() = i; - i->next() = &vertex; - verify_buckets (); - return; - } - -// verify_buckets (); + if (m_bucket_idm_path_id!=current_path_id() || i->m_bucket_id!=m_bucket_id)) + { + vertex.m_bucket_id = m_bucket_id; + vertex.m_path_id = current_path_id(); + m_buckets[m_bucket_id] = &vertex; + vertex.next() = vertex.prev() = 0; + verify_buckets(); + return; + } + vertex.m_bucket_id = m_bucket_id; + vertex.m_path_id = current_path_id(); + if (i->f() >= vertex.f()) + { + m_buckets[m_bucket_id] = &vertex; + vertex.next() = i; + vertex.prev() = 0; + i->prev() = &vertex; + verify_buckets(); + return; + } + if (!i->next()) + { + vertex.prev() = i; + vertex.next() = 0; + i->next() = &vertex; + verify_buckets(); + return; + } + for (i = i->next(); i->next(); i = i->next()) + { + if (i->f() >= vertex.f()) + { + vertex.next() = i; + vertex.prev() = i->prev(); + i->prev()->next() = &vertex; + i->prev() = &vertex; + verify_buckets(); + return; + } + } + if (i->f() >= vertex.f()) + { + vertex.next() = i; + vertex.prev() = i->prev(); + i->prev()->next() = &vertex; + i->prev() = &vertex; + verify_buckets(); + return; + } + else + { + vertex.next() = 0; + vertex.prev() = i; + i->next() = &vertex; + verify_buckets(); + return; + } } TEMPLATE_SPECIALIZATION -IC void CBucketList::add_opened (CGraphVertex &vertex) +inline void CBucketList::add_opened(Vertex &vertex) { -// ai().m_visited_nodes.push_back (vertex.index()); - inherited::add_opened (vertex); - add_to_bucket (vertex,compute_bucket_id(vertex)); - verify_buckets (); + Inherited::add_opened(vertex); + add_to_bucket(vertex, compute_bucket_id(vertex)); + verify_buckets(); } TEMPLATE_SPECIALIZATION -IC void CBucketList::decrease_opened (CGraphVertex &vertex, const _dist_type value) +inline void CBucketList::decrease_opened(Vertex &vertex, const Distance value) { - VERIFY (!is_opened_empty()); - u32 node_bucket_id = compute_bucket_id(vertex); - if (vertex.prev()) - vertex.prev()->next() = vertex.next(); - else { - VERIFY (m_buckets[vertex.m_bucket_id] == &vertex); - m_buckets[vertex.m_bucket_id] = vertex.next(); - } - if (vertex.next()) - vertex.next()->prev() = vertex.prev(); - - verify_buckets (); - add_to_bucket (vertex,node_bucket_id); - verify_buckets (); + VERIFY(!is_opened_empty()); + u32 node_bucket_id = compute_bucket_id(vertex); + if (vertex.prev()) + vertex.prev()->next() = vertex.next(); + else + { + VERIFY(m_buckets[vertex.m_bucket_id] == &vertex); + m_buckets[vertex.m_bucket_id] = vertex.next(); + } + if (vertex.next()) + vertex.next()->prev() = vertex.prev(); + verify_buckets(); + add_to_bucket(vertex, node_bucket_id); + verify_buckets(); } TEMPLATE_SPECIALIZATION -IC void CBucketList::remove_best_opened() +inline void CBucketList::remove_best_opened() { - VERIFY (!is_opened_empty()); - verify_buckets (); - VERIFY (m_buckets[m_min_bucket_id] && is_visited(m_buckets[m_min_bucket_id]->index())); - m_buckets[m_min_bucket_id] = m_buckets[m_min_bucket_id]->next(); - if (m_buckets[m_min_bucket_id]) - m_buckets[m_min_bucket_id]->prev() = 0; - verify_buckets (); + VERIFY(!is_opened_empty()); + verify_buckets(); + VERIFY(m_buckets[m_min_bucket_id] && is_visited(m_buckets[m_min_bucket_id]->index())); + m_buckets[m_min_bucket_id] = m_buckets[m_min_bucket_id]->next(); + if (m_buckets[m_min_bucket_id]) + m_buckets[m_min_bucket_id]->prev() = 0; + verify_buckets(); } TEMPLATE_SPECIALIZATION -IC typename CBucketList::CGraphVertex &CBucketList::get_best () +inline typename CBucketList::Vertex &CBucketList::get_best() { - VERIFY (!is_opened_empty()); - return (*m_buckets[m_min_bucket_id]); + VERIFY(!is_opened_empty()); + return(*m_buckets[m_min_bucket_id]); } TEMPLATE_SPECIALIZATION -IC void CBucketList::set_min_bucket_value (const _dist_type min_bucket_value) -{ - m_min_bucket_value = min_bucket_value; -} +inline void CBucketList::set_min_bucket_value(const Distance min_bucket_value) +{ m_min_bucket_value = min_bucket_value; } TEMPLATE_SPECIALIZATION -IC void CBucketList::set_max_bucket_value (const _dist_type max_bucket_value) -{ - m_max_bucket_value = max_bucket_value; -} +inline void CBucketList::set_max_bucket_value(const Distance max_bucket_value) +{ m_max_bucket_value = max_bucket_value; } #undef TEMPLATE_SPECIALIZATION -#undef CBucketList \ No newline at end of file +#undef CBucketList diff --git a/src/xrAICore/Navigation/data_storage_constructor.h b/src/xrAICore/Navigation/data_storage_constructor.h index a08f3ab9f6e..30dadfbdeec 100644 --- a/src/xrAICore/Navigation/data_storage_constructor.h +++ b/src/xrAICore/Navigation/data_storage_constructor.h @@ -1,43 +1,44 @@ //////////////////////////////////////////////////////////////////////////// -// Module : data_storage_constructor.h -// Created : 21.03.2002 -// Modified : 28.02.2004 -// Author : Dmitriy Iassenev -// Description : Data storage constructor +// Module : data_storage_constructor.h +// Created : 21.03.2002 +// Modified : 28.02.2004 +// Author : Dmitriy Iassenev +// Description : Data storage constructor //////////////////////////////////////////////////////////////////////////// #pragma once struct EmptyVertexData { - template // result mixin type + template // result mixin type struct VertexData {}; }; -template +template struct CompoundVertex : Components::template VertexData>... {}; -template< - typename TStorage, // CDataStorageBucketList|CDataStorageBinaryHeap - typename TVertexManager, // CVertexManagerFixed|CVertexManagerHashFixed - typename TPathBuilder, // CEdgePath|CVertexPath - typename TVertexAllocator, // CVertexAllocatorFixed - typename TCompoundVertex, +template < + typename TPriorityQueuee, // CDataStorageBucketList|CDataStorageBinaryHeap + typename TVertexManager, // CVertexManagerFixed|CVertexManagerHashFixed + typename TPathBuilder, // CEdgePath|CVertexPath + typename TVertexAllocator, // CVertexAllocatorFixed + typename TCompoundVertex, typename TManagerDataStorage = typename TVertexManager::template CDataStorage, - typename TDataStorageBase = typename TStorage::template CDataStorage + typename TDataStorageBase = typename TPriorityQueuee::template CDataStorage > -struct CDataStorageConstructor : public TDataStorageBase +struct PriorityQueueConstructor : public TDataStorageBase { - typedef TDataStorageBase inherited; - typedef TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; + using Inherited = TDataStorageBase; + using Vertex = TCompoundVertex; + using Index = typename Vertex::Index; - CDataStorageConstructor(const u32 vertex_count) : inherited(vertex_count) - {} - void init() { inherited::init(); } - CGraphVertex &create_vertex(const _index_type &vertex_id) - { return inherited::create_vertex(inherited::CDataStorageAllocator::create_vertex(), vertex_id); } + PriorityQueueConstructor(const u32 vertex_count) : + Inherited(vertex_count) + {} + void init() { Inherited::init(); } + Vertex &create_vertex(const Index &vertex_id) + { return Inherited::create_vertex(Inherited::CDataStorageAllocator::create_vertex(), vertex_id); } }; diff --git a/src/xrAICore/Navigation/dijkstra.h b/src/xrAICore/Navigation/dijkstra.h index a00938f213e..1268398285c 100644 --- a/src/xrAICore/Navigation/dijkstra.h +++ b/src/xrAICore/Navigation/dijkstra.h @@ -1,9 +1,9 @@ //////////////////////////////////////////////////////////////////////////// -// Module : dijkstra.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Implementation of the Dijkstra algorithm +// Module : dijkstra.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Implementation of the Dijkstra algorithm //////////////////////////////////////////////////////////////////////////// #pragma once @@ -11,69 +11,64 @@ #include "xrAICore/Navigation/vertex_path.h" #include "xrAICore/Navigation/data_storage_constructor.h" -template +template struct DijkstraVertexData { - template - struct VertexData : TVertexData::template VertexData + template + struct VertexData : TVertexData::template VertexData { - typedef _dist_type _dist_type; - - _dist_type _f; + using Distance = TDistance; + + Distance _f; TCompoundVertex *_back; - - _dist_type &f() { return _f; } - const _dist_type &f() const { return _f; } + + Distance &f() { return _f; } + const Distance &f() const { return _f; } TCompoundVertex *&back() { return _back; } - }; + }; }; template < - typename _dist_type, - typename _priority_queue, - typename _vertex_manager, - typename _vertex_allocator, - bool euclidian_heuristics = true, - typename _data_storage_base = CVertexPath, - typename _iteration_type = u32, + typename TDistance, + typename TPriorityQueue, + typename TVertexManager, + typename TVertexAllocator, + bool EuclidianHeuristics = true, + typename TPathBuilder = CVertexPath, + typename TIteration = u32, typename TVertexData = EmptyVertexData -> class CDijkstra +> +class CDijkstra { public: - using CompoundVertex = CompoundVertex, - _priority_queue, _vertex_manager, _vertex_allocator, _data_storage_base>; - typedef CDataStorageConstructor< - _priority_queue, // algorithm - _vertex_manager, // manager - _data_storage_base, // builder - _vertex_allocator, // allocator - CompoundVertex - > CDataStorage; + using Vertex = CompoundVertex, + TPriorityQueue, TVertexManager, TVertexAllocator, TPathBuilder>; + using CDataStorage = PriorityQueueConstructor< + TPriorityQueue, TVertexManager, TPathBuilder, TVertexAllocator, Vertex>; protected: - typedef CompoundVertex CGraphVertex; - typedef typename CGraphVertex::_dist_type _dist_type; - typedef typename CGraphVertex::_index_type _index_type; + using Distance = typename Vertex::Distance; + using Index = typename Vertex::Index; protected: - bool m_search_started; - CDataStorage *m_data_storage; + bool m_search_started; + CDataStorage *m_data_storage; protected: - template - IC void initialize (_PathManager &path_manager); - template - IC bool step (_PathManager &path_manager); - template - IC void finalize (_PathManager &path_manager); + inline CDijkstra(const u32 max_vertex_count); + virtual ~CDijkstra(); + template + inline void initialize(TPathManager &path_manager); + template + inline bool step(TPathManager &path_manager); + template + inline void finalize(TPathManager &path_manager); public: - IC CDijkstra (const u32 max_vertex_count); - virtual ~CDijkstra (); - template - IC bool find (_PathManager &path_manager); - IC CDataStorage &data_storage (); - IC const CDataStorage &data_storage () const; + template + inline bool find(TPathManager &path_manager); + inline CDataStorage &data_storage(); + inline const CDataStorage &data_storage() const; }; -#include "xrAICore/Navigation/dijkstra_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/dijkstra_inline.h" diff --git a/src/xrAICore/Navigation/dijkstra_inline.h b/src/xrAICore/Navigation/dijkstra_inline.h index 0e5d37a0927..0d44881c68d 100644 --- a/src/xrAICore/Navigation/dijkstra_inline.h +++ b/src/xrAICore/Navigation/dijkstra_inline.h @@ -1,203 +1,195 @@ //////////////////////////////////////////////////////////////////////////// -// Module : dijkstra.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Implementation of the Dijkstra algorithm +// Module : dijkstra.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Implementation of the Dijkstra algorithm //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template <\ - typename _dist_type,\ - typename _priority_queue,\ - typename _vertex_manager,\ - typename _vertex_allocator,\ - bool euclidian_heuristics,\ - typename _data_storage_base,\ - typename _iteration_type,\ +#define TEMPLATE_SPECIALIZATION\ + template <\ + typename TDistance,\ + typename TPriorityQueue,\ + typename TVertexManager,\ + typename TVertexAllocator,\ + bool EuclidianHeuristics,\ + typename TPathBuilder,\ + typename TIteration,\ typename TVertexData\ - > - -#define CSDijkstra CDijkstra<\ - _dist_type,\ - _priority_queue,\ - _vertex_manager,\ - _vertex_allocator,\ - euclidian_heuristics,\ - _data_storage_base,\ - _iteration_type,\ - TVertexData\ -> + > + +#define CSDijkstra\ + CDijkstra<\ + TDistance,\ + TPriorityQueue,\ + TVertexManager,\ + TVertexAllocator,\ + EuclidianHeuristics,\ + TPathBuilder,\ + TIteration,\ + TVertexData\ + > TEMPLATE_SPECIALIZATION -IC CSDijkstra::CDijkstra (const u32 max_vertex_count) +inline CSDijkstra::CDijkstra(const u32 max_vertex_count) { - m_data_storage = xr_new(max_vertex_count); - m_search_started = false; + m_data_storage = xr_new(max_vertex_count); + m_search_started = false; } TEMPLATE_SPECIALIZATION -CSDijkstra::~CDijkstra () -{ - xr_delete (m_data_storage); -} +inline CSDijkstra::~CDijkstra() +{ xr_delete(m_data_storage); } TEMPLATE_SPECIALIZATION -IC typename CSDijkstra::CDataStorage &CSDijkstra::data_storage () -{ - return (*m_data_storage); -} +inline typename CSDijkstra::CDataStorage &CSDijkstra::data_storage() +{ return *m_data_storage; } TEMPLATE_SPECIALIZATION -IC const typename CSDijkstra::CDataStorage &CSDijkstra::data_storage () const -{ - return (*m_data_storage); -} +inline const typename CSDijkstra::CDataStorage &CSDijkstra::data_storage() const +{ return *m_data_storage; } TEMPLATE_SPECIALIZATION -template -IC void CSDijkstra::initialize (_PathManager &path_manager) +template +inline void CSDijkstra::initialize(TPathManager &path_manager) { - THROW2 (!m_search_started,"Recursive graph engine usage is not allowed!"); - m_search_started = true; - // initialize data structures before we started path search - data_storage().init (); - - // initialize path manager before we started path search - path_manager.init (); - - // create a node - CGraphVertex &start = data_storage().create_vertex(path_manager.start_node()); - - // assign correspoding values to the created node - start.f() = _dist_type(0); - - // assign null parent to the start node - data_storage().assign_parent(start,0); - - // add start node to the opened list - data_storage().add_opened (start); + THROW2(!m_search_started, "Recursive graph engine usage is not allowed!"); + m_search_started = true; + // initialize data structures before we started path search + data_storage().init(); + // initialize path manager before we started path search + path_manager.init(); + // create a node + Vertex &start = data_storage().create_vertex(path_manager.start_node()); + // assign correspoding values to the created node + start.f() = Distance(0); + // assign null parent to the start node + data_storage().assign_parent(start, 0); + // add start node to the opened list + data_storage().add_opened(start); } TEMPLATE_SPECIALIZATION -template -IC void CSDijkstra::finalize (_PathManager &path_manager) +template +inline void CSDijkstra::finalize(TPathManager &path_manager) { - // finalize path manager after we finished path search - path_manager.finalize (); - m_search_started = false; + // finalize path manager after we finished path search + path_manager.finalize(); + m_search_started = false; } TEMPLATE_SPECIALIZATION -template -IC bool CSDijkstra::step (_PathManager &path_manager) +template +inline bool CSDijkstra::step(TPathManager &path_manager) { - // get the best node, i.e. a node with the minimum 'f' - CGraphVertex &best = data_storage().get_best(); - - // check if this node is the one we are searching for - if (path_manager.is_goal_reached(best.index())) { - // we reached the goal, so we have to create a path - path_manager.init_path (); - path_manager.create_path (best); - // and return success - return (true); - } - - // put best node to the closed list - data_storage().add_best_closed(); - // and remove this node from the opened one - data_storage().remove_best_opened(); - - // iterating on the best node neighbours - _PathManager::const_iterator i; - _PathManager::const_iterator e; - path_manager.begin (best.index(),i,e); - for ( ; i != e; ++i) { - const _index_type &neighbour_index = path_manager.get_value(i); - // check if neighbour is accessible - if (!path_manager.is_accessible(neighbour_index)) - continue; - // check if neighbour is visited, i.e. is in the opened or - // closed lists - if (data_storage().is_visited(neighbour_index)) { - // so, this neighbour node has been already visited - // therefore get the pointer to this node - CGraphVertex &neighbour = data_storage().get_node(neighbour_index); - // check if this node is in the opened list - if (data_storage().is_opened(neighbour)) { - // compute 'g' for the node - _dist_type f = best.f() + path_manager.evaluate(best.index(),neighbour_index,i); - // check if new path is better than the older one - if (neighbour.f() > f) { - // so, new path is better - // assign corresponding values to the node - _dist_type d = neighbour.f(); - neighbour.f() = f; - // assign correct parent to the node to be able - // to retreive a path - data_storage().assign_parent (neighbour,&best,path_manager.edge(i)); - // notify data storage about node decreasing value - data_storage().decrease_opened(neighbour,d); - // continue iterating on neighbours - continue; - } - // so, new path is worse - // continue iterating on neighbours - continue; - } - // continue iterating on neighbours - continue; - } - else { - // so, this neighbour node is not in the opened or closed lists - // put neighbour node to the opened list - CGraphVertex &neighbour = data_storage().create_vertex(neighbour_index); - // fill the corresponding node parameters - neighbour.f() = best.f() + path_manager.evaluate(best.index(),neighbour_index,i); - // assign best node as its parent - data_storage().assign_parent(neighbour,&best,path_manager.edge(i)); - // add start node to the opened list - data_storage().add_opened (neighbour); - // continue iterating on neighbours - continue; - } - } - - // this iteration haven't got the goal node, therefore return failure - return (false); + // get the best node, i.e. a node with the minimum 'f' + Vertex &best = data_storage().get_best(); + // check if this node is the one we are searching for + if (path_manager.is_goal_reached(best.index())) + { + // we reached the goal, so we have to create a path + path_manager.init_path(); + path_manager.create_path(best); + // and return success + return true; + } + // put best node to the closed list + data_storage().add_best_closed(); + // and remove this node from the opened one + data_storage().remove_best_opened(); + // iterating on the best node neighbours + TPathManager::const_iterator i, e; + path_manager.begin(best.index(), i, e); + for (; i!=e; i++) + { + const Index &neighbour_index = path_manager.get_value(i); + // check if neighbour is accessible + if (!path_manager.is_accessible(neighbour_index)) + continue; + // check if neighbour is visited, i.e. is in the opened or + // closed lists + if (data_storage().is_visited(neighbour_index)) + { + // so, this neighbour node has been already visited + // therefore get the pointer to this node + Vertex &neighbour = data_storage().get_node(neighbour_index); + // check if this node is in the opened list + if (data_storage().is_opened(neighbour)) + { + // compute 'g' for the node + Distance f = best.f()+path_manager.evaluate(best.index(), neighbour_index, i); + // check if new path is better than the older one + if (neighbour.f()>f) + { + // so, new path is better + // assign corresponding values to the node + Distance d = neighbour.f(); + neighbour.f() = f; + // assign correct parent to the node to be able + // to retreive a path + data_storage().assign_parent(neighbour, &best, path_manager.edge(i)); + // notify data storage about node decreasing value + data_storage().decrease_opened(neighbour, d); + // continue iterating on neighbours + continue; + } + // so, new path is worse + // continue iterating on neighbours + continue; + } + // continue iterating on neighbours + continue; + } + else + { + // so, this neighbour node is not in the opened or closed lists + // put neighbour node to the opened list + Vertex &neighbour = data_storage().create_vertex(neighbour_index); + // fill the corresponding node parameters + neighbour.f() = best.f() + path_manager.evaluate(best.index(), neighbour_index, i); + // assign best node as its parent + data_storage().assign_parent(neighbour, &best, path_manager.edge(i)); + // add start node to the opened list + data_storage().add_opened(neighbour); + // continue iterating on neighbours + continue; + } + } + // this iteration haven't got the goal node, therefore return failure + return false; } TEMPLATE_SPECIALIZATION -template -IC bool CSDijkstra::find (_PathManager &path_manager) +template +inline bool CSDijkstra::find(TPathManager &path_manager) { - // initialize data structures with new search - initialize (path_manager); - // iterate while opened list is not empty - for (_iteration_type i = _iteration_type(0); !data_storage().is_opened_empty(); ++i) { - // check if we reached limit - if (path_manager.is_limit_reached(i)) { - // so we reached limit, return failure - finalize (path_manager); - return (false); - } - - // so, limit is not reached - // check if new step will get us success - if (step(path_manager)) { - // so this step reached the goal, return success - finalize (path_manager); - return (true); - } - } - - // so, opened list is empty, return failure - finalize (path_manager); - return (false); + // initialize data structures with new search + initialize(path_manager); + // iterate while opened list is not empty + for (TIteration i = TIteration(0); !data_storage().is_opened_empty(); i++) + { + // check if we reached limit + if (path_manager.is_limit_reached(i)) + { + // so we reached limit, return failure + finalize(path_manager); + return false; + } + // so, limit is not reached + // check if new step will get us success + if (step(path_manager)) + { + // so this step reached the goal, return success + finalize(path_manager); + return true; + } + } + // so, opened list is empty, return failure + finalize(path_manager); + return false; } #undef TEMPLATE_SPECIALIZATION -#undef CSDijkstra \ No newline at end of file +#undef CSDijkstra diff --git a/src/xrAICore/Navigation/edge_path.h b/src/xrAICore/Navigation/edge_path.h index beeaf87b71f..3aa6875fa4f 100644 --- a/src/xrAICore/Navigation/edge_path.h +++ b/src/xrAICore/Navigation/edge_path.h @@ -1,40 +1,40 @@ //////////////////////////////////////////////////////////////////////////// -// Module : edge_path.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Edge path class +// Module : edge_path.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Edge path class //////////////////////////////////////////////////////////////////////////// #pragma once #include "xrAICore/Navigation/vertex_path.h" -template +template struct CEdgePath { - template - struct VertexData : CVertexPath::template VertexData + template + struct VertexData : CVertexPath::template VertexData { - _edge_type _edge; - _edge_type &edge() { return _edge; } - }; + TEdge _edge; + TEdge &edge() { return _edge; } + }; - template - class CDataStorage : public CVertexPath::template CDataStorage + template + class CDataStorage : public CVertexPath::template CDataStorage { - public: - typedef typename CVertexPath::template CDataStorage inherited; - typedef TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; - - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void assign_parent (CGraphVertex &neighbour, CGraphVertex *parent); - IC void assign_parent (CGraphVertex &neighbour, CGraphVertex *parent, const _edge_type &edge); - IC void get_edge_path (xr_vector<_edge_type> &path, CGraphVertex *best, bool reverse_order = false); - }; + public: + using Inherited = typename CVertexPath::template CDataStorage; + using Vertex = TCompoundVertex; + using Index = typename Vertex::Index; + + public: + inline CDataStorage(const u32 vertex_count); + inline virtual ~CDataStorage(); + inline void assign_parent(Vertex &neighbour, Vertex *parent); + inline void assign_parent(Vertex &neighbour, Vertex *parent, const TEdge &edge); + inline void get_edge_path(xr_vector &path, Vertex *best, bool reverse_order = false); + }; }; -#include "xrAICore/Navigation/edge_path_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/edge_path_inline.h" diff --git a/src/xrAICore/Navigation/edge_path_inline.h b/src/xrAICore/Navigation/edge_path_inline.h index e6e0a5df586..a2de39d615f 100644 --- a/src/xrAICore/Navigation/edge_path_inline.h +++ b/src/xrAICore/Navigation/edge_path_inline.h @@ -1,66 +1,61 @@ //////////////////////////////////////////////////////////////////////////// -// Module : edge_path_inline.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Edge path class inline functions +// Module : edge_path_inline.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Edge path class inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template\ - template +#define TEMPLATE_SPECIALIZATION\ + template \ + template - -#define CEdgePathBuilder CEdgePath<_edge_type,bEuclidianHeuristics>::CDataStorage +#define CEdgePathBuilder CEdgePath::CDataStorage TEMPLATE_SPECIALIZATION -IC CEdgePathBuilder::CDataStorage (const u32 vertex_count) : - inherited (vertex_count) -{ -} +inline CEdgePathBuilder::CDataStorage(const u32 vertex_count) : + Inherited(vertex_count) +{} TEMPLATE_SPECIALIZATION -CEdgePathBuilder::~CDataStorage () -{ -} +CEdgePathBuilder::~CDataStorage() +{} TEMPLATE_SPECIALIZATION -IC void CEdgePathBuilder::assign_parent (CGraphVertex &neighbour, CGraphVertex *parent) -{ - inherited::assign_parent (neighbour,parent); -} +inline void CEdgePathBuilder::assign_parent(Vertex &neighbour, Vertex *parent) +{ Inherited::assign_parent(neighbour, parent); } TEMPLATE_SPECIALIZATION -IC void CEdgePathBuilder::assign_parent (CGraphVertex &neighbour, CGraphVertex *parent, const _edge_type &edge) +inline void CEdgePathBuilder::assign_parent(Vertex &neighbour, Vertex *parent, const TEdge &edge) { - inherited::assign_parent (neighbour,parent); - neighbour.edge() = edge; + Inherited::assign_parent(neighbour, parent); + neighbour.edge() = edge; } TEMPLATE_SPECIALIZATION -IC void CEdgePathBuilder::get_edge_path (xr_vector<_edge_type> &path, CGraphVertex *best, bool reverse_order) +inline void CEdgePathBuilder::get_edge_path(xr_vector &path, Vertex *best, bool reverse_order) { - CGraphVertex *t1 = best, *t2 = best->back(); - for (u32 i=1; t2; t1 = t2, t2 = t2->back(), ++i) ; - u32 n = (u32)path.size(); - - path.resize (n + --i); - t2 = best; - - if (!reverse_order) { - xr_vector<_edge_type>::reverse_iterator I = path.rbegin(); - xr_vector<_edge_type>::reverse_iterator E = path.rend(); - for (; t2->back() ; t2 = t2->back(), ++I) - *I = t2->edge(); - } - else { - xr_vector<_edge_type>::iterator I = path.begin() + n; - for (; t2->back() ; t2 = t2->back(), ++I) - *I = t2->edge(); - } + Vertex *t1 = best, *t2 = best->back(); + for (u32 i=1; t2; t1 = t2, t2 = t2->back(), i++); + u32 n = (u32)path.size(); + i--; + path.resize(n+i); + t2 = best; + if (!reverse_order) + { + auto it = path.rbegin(); + for (; t2->back() ; t2 = t2->back(), it++) + *it = t2->edge(); + } + else + { + auto it = path.begin()+n; + for (; t2->back(); t2 = t2->back(), it++) + *it = t2->edge(); + } } #undef TEMPLATE_SPECIALIZATION -#undef CEdgePathBuilder \ No newline at end of file +#undef CEdgePathBuilder diff --git a/src/xrAICore/Navigation/vertex_allocator_fixed.h b/src/xrAICore/Navigation/vertex_allocator_fixed.h index bf4953f903a..0d67272ffd0 100644 --- a/src/xrAICore/Navigation/vertex_allocator_fixed.h +++ b/src/xrAICore/Navigation/vertex_allocator_fixed.h @@ -1,39 +1,39 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_allocator_fixed.h -// Created : 21.03.2002 -// Modified : 27.02.2004 -// Author : Dmitriy Iassenev -// Description : Fixed vertex allocator +// Module : vertex_allocator_fixed.h +// Created : 21.03.2002 +// Modified : 27.02.2004 +// Author : Dmitriy Iassenev +// Description : Fixed vertex allocator //////////////////////////////////////////////////////////////////////////// #pragma once -template +template struct CVertexAllocatorFixed { template struct VertexData {}; - template - class CDataStorage + template + class CDataStorage { - public: - typedef TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; - typedef xr_vector VERTICES; + public: + using Vertex = TCompoundVertex; + using Index = typename Vertex::Index; + using VertexContainer = xr_vector; - protected: - u32 m_vertex_count; - VERTICES m_vertices; + protected: + u32 m_vertex_count; + VertexContainer m_vertices; - public: - IC CDataStorage (); - virtual ~CDataStorage (); - IC void init (); - IC u32 get_visited_node_count () const; - IC CGraphVertex &create_vertex (); - }; + public: + inline CDataStorage(); + inline virtual ~CDataStorage(); + inline void init(); + inline u32 get_visited_node_count() const; + inline Vertex &create_vertex(); + }; }; -#include "vertex_allocator_fixed_inline.h" \ No newline at end of file +#include "vertex_allocator_fixed_inline.h" diff --git a/src/xrAICore/Navigation/vertex_allocator_fixed_inline.h b/src/xrAICore/Navigation/vertex_allocator_fixed_inline.h index e3d8ef1e701..c276157c211 100644 --- a/src/xrAICore/Navigation/vertex_allocator_fixed_inline.h +++ b/src/xrAICore/Navigation/vertex_allocator_fixed_inline.h @@ -1,56 +1,41 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_allocator_fixed_inline.h -// Created : 21.03.2002 -// Modified : 27.02.2004 -// Author : Dmitriy Iassenev -// Description : Fixed vertex allocator inline functions +// Module : vertex_allocator_fixed_inline.h +// Created : 21.03.2002 +// Modified : 27.02.2004 +// Author : Dmitriy Iassenev +// Description : Fixed vertex allocator inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template\ - template +#define TEMPLATE_SPECIALIZATION\ + template\ + template -#define CFixedVertexAllocator CVertexAllocatorFixed::CDataStorage +#define CFixedVertexAllocator CVertexAllocatorFixed::CDataStorage TEMPLATE_SPECIALIZATION -IC CFixedVertexAllocator::CDataStorage () -{ - u32 memory_usage = 0; - u32 byte_count; - - byte_count = (reserved_vertex_count)*sizeof(CGraphVertex); -// m_vertices = xr_alloc(reserved_vertex_count); -// ZeroMemory (m_vertices,byte_count); - m_vertices.resize (reserved_vertex_count); - memory_usage += byte_count; -} +inline CFixedVertexAllocator::CDataStorage() +{ m_vertices.resize(ReserveSize); } TEMPLATE_SPECIALIZATION -CFixedVertexAllocator::~CDataStorage () -{ -// xr_free (m_vertices); -} +inline CFixedVertexAllocator::~CDataStorage() +{} TEMPLATE_SPECIALIZATION -IC void CFixedVertexAllocator::init () -{ - m_vertex_count = 0; -} +inline void CFixedVertexAllocator::init() +{ m_vertex_count = 0; } TEMPLATE_SPECIALIZATION -IC u32 CFixedVertexAllocator::get_visited_node_count () const -{ - return (m_vertex_count); -} +inline u32 CFixedVertexAllocator::get_visited_node_count() const +{ return m_vertex_count; } TEMPLATE_SPECIALIZATION -IC typename CFixedVertexAllocator::CGraphVertex &CFixedVertexAllocator::create_vertex () +inline typename CFixedVertexAllocator::Vertex &CFixedVertexAllocator::create_vertex() { - VERIFY (m_vertex_count < reserved_vertex_count - 1); - return (*(m_vertices.begin() + m_vertex_count++)); + VERIFY(m_vertex_count struct CVertexManagerFixed { template - struct VertexData + struct VertexData { - typedef _index_type _index_type; - _index_type _index : 8*sizeof(_index_type)-mask; - _index_type _opened : mask; + using Index = TIndex; + Index _index : 8*sizeof(Index)-Mask; + Index _opened : Mask; - _index_type index() const { return _index; } - _index_type opened() const { return _opened; } - }; + Index index() const { return _index; } + Index opened() const { return _opened; } + }; - template < - typename _builder, - typename _allocator, + template < + typename TPathBuilder, + typename TVertexAllocator, typename TCompoundVertex - > + > class CDataStorage : - public _builder::template CDataStorage, - public _allocator::template CDataStorage + public TPathBuilder::template CDataStorage, + public TVertexAllocator::template CDataStorage { - public: - typedef typename _builder::template CDataStorage CDataStorageBase; - typedef typename _allocator::template CDataStorage CDataStorageAllocator; - typedef TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; + public: + using CDataStorageBase = typename TPathBuilder::template CDataStorage; + using CDataStorageAllocator = typename TVertexAllocator::template CDataStorage; + using Vertex = TCompoundVertex; + using Index = TIndex; + using PathId = TPathId; -#pragma pack(push,1) - template - struct SGraphIndexVertex +#pragma pack(push, 1) + struct IndexVertex { - _path_id_type m_path_id; - CGraphVertex *m_vertex; - }; + PathId m_path_id; + Vertex *m_vertex; + }; #pragma pack(pop) - typedef _path_id_type _path_id_type; - typedef SGraphIndexVertex<_path_id_type> CGraphIndexVertex; + protected: + PathId m_current_path_id; + u32 m_max_node_count; + IndexVertex *m_indexes; - protected: - _path_id_type m_current_path_id; - u32 m_max_node_count; - CGraphIndexVertex *m_indexes; - - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void init (); - IC bool is_opened (const CGraphVertex &vertex) const; - IC bool is_visited (const _index_type &vertex_id) const; - IC bool is_closed (const CGraphVertex &vertex) const; - IC CGraphVertex &get_node (const _index_type &vertex_id) const; - IC CGraphVertex &create_vertex (CGraphVertex &vertex, const _index_type &vertex_id); - IC void add_opened (CGraphVertex &vertex); - IC void add_closed (CGraphVertex &vertex); - IC _path_id_type current_path_id () const; - }; + public: + inline CDataStorage(const u32 vertex_count); + inline virtual ~CDataStorage(); + inline void init(); + inline bool is_opened(const Vertex &vertex) const; + inline bool is_visited(const Index &vertex_id) const; + inline bool is_closed(const Vertex &vertex) const; + inline Vertex &get_node(const Index &vertex_id) const; + inline Vertex &create_vertex(Vertex &vertex, const Index &vertex_id); + inline void add_opened(Vertex &vertex); + inline void add_closed(Vertex &vertex); + inline PathId current_path_id() const; + }; }; -#include "vertex_manager_fixed_inline.h" \ No newline at end of file +#include "vertex_manager_fixed_inline.h" diff --git a/src/xrAICore/Navigation/vertex_manager_fixed_inline.h b/src/xrAICore/Navigation/vertex_manager_fixed_inline.h index 1692bfaf327..02bdae0aff9 100644 --- a/src/xrAICore/Navigation/vertex_manager_fixed_inline.h +++ b/src/xrAICore/Navigation/vertex_manager_fixed_inline.h @@ -1,116 +1,100 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_manager_fixed_inline.h -// Created : 21.03.2002 -// Modified : 01.03.2004 -// Author : Dmitriy Iassenev -// Description : Fixed vertex manager inline functions +// Module : vertex_manager_fixed_inline.h +// Created : 21.03.2002 +// Modified : 01.03.2004 +// Author : Dmitriy Iassenev +// Description : Fixed vertex manager inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template <\ - typename _path_id_type,\ - typename _index_type,\ - u8 mask\ - >\ - template <\ - typename _builder,\ - typename _allocator,\ - typename TCompoundVertex\ - > - -#define CFixedVertexManager CVertexManagerFixed<_path_id_type,_index_type,mask>::CDataStorage<_builder,_allocator,TCompoundVertex> +#define TEMPLATE_SPECIALIZATION\ + template <\ + typename TPathId,\ + typename TIndex,\ + u8 Mask\ + >\ + template <\ + typename TPathBuilder,\ + typename TVertexAllocator,\ + typename TCompoundVertex\ + > + +#define CFixedVertexManager\ + CVertexManagerFixed::CDataStorage TEMPLATE_SPECIALIZATION -IC CFixedVertexManager::CDataStorage (const u32 vertex_count) : +inline CFixedVertexManager::CDataStorage(const u32 vertex_count) : CDataStorageBase(vertex_count), CDataStorageAllocator() { - m_current_path_id = _path_id_type(0); - m_max_node_count = vertex_count; - - u32 memory_usage = 0; - u32 byte_count; - - byte_count = (vertex_count)*sizeof(CGraphIndexVertex); - m_indexes = xr_alloc(vertex_count); - ZeroMemory (m_indexes,byte_count); - memory_usage += byte_count; + m_current_path_id = PathId(0); + m_max_node_count = vertex_count; + m_indexes = xr_alloc(vertex_count); + ZeroMemory(m_indexes, vertex_count*sizeof(IndexVertex)); } TEMPLATE_SPECIALIZATION -CFixedVertexManager::~CDataStorage () -{ - xr_free (m_indexes); -} +CFixedVertexManager::~CDataStorage() +{ xr_free(m_indexes); } TEMPLATE_SPECIALIZATION -IC void CFixedVertexManager::init () +inline void CFixedVertexManager::init() { CDataStorageBase::init(); CDataStorageAllocator::init(); - ++m_current_path_id; - if (!m_current_path_id) { - ZeroMemory (m_indexes,(m_max_node_count)*sizeof(CGraphIndexVertex)); - ++m_current_path_id; - } + m_current_path_id++; + if (!m_current_path_id) + { + ZeroMemory(m_indexes, m_max_node_count*sizeof(IndexVertex)); + m_current_path_id++; + } } TEMPLATE_SPECIALIZATION -IC bool CFixedVertexManager::is_opened (const CGraphVertex &vertex) const -{ - return (!!vertex.opened()); -} +inline bool CFixedVertexManager::is_opened(const Vertex &vertex) const +{ return !!vertex.opened(); } TEMPLATE_SPECIALIZATION -IC bool CFixedVertexManager::is_visited (const _index_type &vertex_id) const +inline bool CFixedVertexManager::is_visited(const Index &vertex_id) const { - VERIFY (vertex_id < m_max_node_count); - return (m_indexes[vertex_id].m_path_id == m_current_path_id); + VERIFY(vertex_id struct CVertexManagerHashFixed { - template - struct VertexData + template + struct VertexData { - typedef _index_type _index_type; - _index_type _index; - bool _opened; + using Index = TIndex; - const _index_type &index() const { return _index; } - _index_type &index() { return _index; } - bool &opened() { return _opened; } + Index _index; + bool _opened; + + const Index &index() const { return _index; } + Index &index() { return _index; } + bool &opened() { return _opened; } bool opened() const { return _opened; } - }; + }; - template < - typename _builder, - typename _allocator, - typename TCompoundVertex - > + template < + typename TPathBuilder, + typename TVertexAllocator, + typename TCompoundVertex + > class CDataStorage : - public _builder::template CDataStorage, - public _allocator::template CDataStorage + public TPathBuilder::template CDataStorage, + public TVertexAllocator::template CDataStorage { - public: - typedef typename _builder::template CDataStorage CDataStorageBase; - typedef typename _allocator::template CDataStorage CDataStorageAllocator; - typedef typename TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; + public: + using CDataStorageBase = typename TPathBuilder::template CDataStorage; + using CDataStorageAllocator = typename TVertexAllocator::template CDataStorage; + using Vertex = TCompoundVertex; + using Index = TIndex; + using PathId = TPathId; -#pragma pack(push,1) - template - struct SGraphIndexVertex +#pragma pack(push, 1) + struct IndexVertex { - CGraphVertex *m_vertex; - SGraphIndexVertex *m_next; - SGraphIndexVertex *m_prev; - u32 m_hash; - _path_id_type m_path_id; - }; + Vertex *m_vertex; + IndexVertex *m_next; + IndexVertex *m_prev; + u32 m_hash; + PathId m_path_id; + }; #pragma pack(pop) - typedef _path_id_type _path_id_type; - typedef SGraphIndexVertex<_path_id_type> CGraphIndexVertex; - - protected: - _path_id_type m_current_path_id; - CGraphIndexVertex *m_vertices; - CGraphIndexVertex **m_hash; - u32 m_vertex_count; + protected: + PathId m_current_path_id; + IndexVertex *m_vertices; + IndexVertex **m_hash; + u32 m_vertex_count; - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void init (); - IC bool is_opened (const CGraphVertex &vertex) const; - IC bool is_visited (const _index_type &vertex_id) const; - IC bool is_closed (const CGraphVertex &vertex) const; - IC CGraphVertex &get_node (const _index_type &vertex_id) const; - IC CGraphVertex &create_vertex (CGraphVertex &vertex, const _index_type &vertex_id); - IC void add_opened (CGraphVertex &vertex); - IC void add_closed (CGraphVertex &vertex); - IC _path_id_type current_path_id () const; - IC u32 hash_index (const _index_type &vertex_id) const; - }; + public: + inline CDataStorage(const u32 vertex_count); + inline virtual ~CDataStorage(); + inline void init(); + inline bool is_opened(const Vertex &vertex) const; + inline bool is_visited(const Index &vertex_id) const; + inline bool is_closed(const Vertex &vertex) const; + inline Vertex &get_node(const Index &vertex_id) const; + inline Vertex &create_vertex(Vertex &vertex, const Index &vertex_id); + inline void add_opened(Vertex &vertex); + inline void add_closed(Vertex &vertex); + inline PathId current_path_id() const; + inline u32 hash_index(const Index &vertex_id) const; + }; }; -#include "vertex_manager_hash_fixed_inline.h" \ No newline at end of file +#include "vertex_manager_hash_fixed_inline.h" diff --git a/src/xrAICore/Navigation/vertex_manager_hash_fixed_inline.h b/src/xrAICore/Navigation/vertex_manager_hash_fixed_inline.h index 3bdb82ff6a2..2e869c69d15 100644 --- a/src/xrAICore/Navigation/vertex_manager_hash_fixed_inline.h +++ b/src/xrAICore/Navigation/vertex_manager_hash_fixed_inline.h @@ -1,170 +1,152 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_manager_hash_fixed_inline.h -// Created : 21.03.2002 -// Modified : 05.03.2004 -// Author : Dmitriy Iassenev -// Description : Hash fixed vertex manager inline functions +// Module : vertex_manager_hash_fixed_inline.h +// Created : 21.03.2002 +// Modified : 05.03.2004 +// Author : Dmitriy Iassenev +// Description : Hash fixed vertex manager inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template <\ - typename _path_id_type,\ - typename _index_type,\ - u32 hash_size,\ - u32 fix_size\ - >\ - template <\ - typename _builder,\ - typename _allocator,\ - typename TCompoundVertex\ - > - -#define CHashFixedVertexManager CVertexManagerHashFixed<_path_id_type,_index_type,hash_size,fix_size>::CDataStorage<_builder,_allocator,TCompoundVertex> +#define TEMPLATE_SPECIALIZATION\ + template <\ + typename TPathId,\ + typename TIndex,\ + u32 HashSize,\ + u32 FixSize\ + >\ + template <\ + typename TPathBuilder,\ + typename TVertexAllocator,\ + typename TCompoundVertex\ + > + +#define CHashFixedVertexManager\ + CVertexManagerHashFixed::\ + CDataStorage TEMPLATE_SPECIALIZATION -IC CHashFixedVertexManager::CDataStorage (const u32 vertex_count) : +inline CHashFixedVertexManager::CDataStorage(const u32 vertex_count) : CDataStorageBase(vertex_count), CDataStorageAllocator(), - m_current_path_id(_path_id_type(0)) + m_current_path_id(PathId(0)) { - u32 memory_usage = 0; - u32 byte_count; - - byte_count = (hash_size)*sizeof(CGraphIndexVertex*); - m_hash = xr_alloc(hash_size); - ZeroMemory (m_hash,byte_count); - memory_usage += byte_count; - - byte_count = (fix_size)*sizeof(CGraphIndexVertex); - m_vertices = xr_alloc(fix_size); - ZeroMemory (m_vertices,byte_count); - memory_usage += byte_count; + m_hash = xr_alloc(HashSize); + ZeroMemory(m_hash, HashSize*sizeof(IndexVertex*)); + m_vertices = xr_alloc(FixSize); + ZeroMemory(m_vertices, FixSize*sizeof(IndexVertex)); } TEMPLATE_SPECIALIZATION -CHashFixedVertexManager::~CDataStorage () +CHashFixedVertexManager::~CDataStorage() { - xr_free (m_hash); - xr_free (m_vertices); + xr_free(m_hash); + xr_free(m_vertices); } TEMPLATE_SPECIALIZATION -IC void CHashFixedVertexManager::init () +inline void CHashFixedVertexManager::init() { CDataStorageBase::init(); CDataStorageAllocator::init(); - ++m_current_path_id; - m_vertex_count = 0; - if (!m_current_path_id) { - ++m_current_path_id; - ZeroMemory (m_hash,(hash_size)*sizeof(CGraphIndexVertex*)); - ZeroMemory (m_vertices,(fix_size)*sizeof(CGraphIndexVertex)); - } -// ZeroMemory (m_hash,(hash_size)*sizeof(CGraphIndexVertex*)); -// ZeroMemory (m_vertices,(fix_size)*sizeof(CGraphIndexVertex)); + m_current_path_id++; + m_vertex_count = 0; + if (!m_current_path_id) + { + m_current_path_id++; + ZeroMemory(m_hash, HashSize*sizeof(IndexVertex*)); + ZeroMemory(m_vertices, FixSize*sizeof(IndexVertex)); + } } TEMPLATE_SPECIALIZATION -IC void CHashFixedVertexManager::add_opened (CGraphVertex &vertex) -{ - vertex.opened() = 1; -} +inline void CHashFixedVertexManager::add_opened(Vertex &vertex) +{ vertex.opened() = 1; } TEMPLATE_SPECIALIZATION -IC void CHashFixedVertexManager::add_closed (CGraphVertex &vertex) -{ - vertex.opened() = 0; -} +inline void CHashFixedVertexManager::add_closed(Vertex &vertex) +{ vertex.opened() = 0; } TEMPLATE_SPECIALIZATION -IC typename CHashFixedVertexManager::_path_id_type CHashFixedVertexManager::current_path_id () const -{ - return (m_current_path_id); -} +inline typename CHashFixedVertexManager::PathId CHashFixedVertexManager::current_path_id() const +{ return m_current_path_id; } TEMPLATE_SPECIALIZATION -IC bool CHashFixedVertexManager::is_opened (const CGraphVertex &vertex) const -{ - return (vertex.opened()); -} +inline bool CHashFixedVertexManager::is_opened(const Vertex &vertex) const +{ return vertex.opened(); } TEMPLATE_SPECIALIZATION -IC u32 CHashFixedVertexManager::hash_index(const _index_type &vertex_id) const -{ - return (hash_fixed_vertex_manager::to_u32(vertex_id) % hash_size); -} +inline u32 CHashFixedVertexManager::hash_index(const Index &vertex_id) const +{ return hash_fixed_vertex_manager::to_u32(vertex_id)%HashSize; } TEMPLATE_SPECIALIZATION -IC bool CHashFixedVertexManager::is_visited (const _index_type &vertex_id) const +inline bool CHashFixedVertexManager::is_visited(const Index &vertex_id) const { - u32 index = hash_index(vertex_id); - CGraphIndexVertex *vertex = m_hash[index]; - if (!vertex || (vertex->m_path_id != current_path_id()) || (vertex->m_hash != index)) - return (false); - for ( ; vertex; vertex = vertex->m_next) - if (vertex->m_vertex->index() == vertex_id) - return (true); - return (false); + u32 index = hash_index(vertex_id); + IndexVertex *vertex = m_hash[index]; + if (!vertex || vertex->m_path_id!=current_path_id() || vertex->m_hash!=index) + return false; + for (; vertex; vertex = vertex->m_next) + { + if (vertex->m_vertex->index()==vertex_id) + return true; + } + return false; } TEMPLATE_SPECIALIZATION -IC bool CHashFixedVertexManager::is_closed (const CGraphVertex &vertex) const -{ - return (!is_opened(vertex)); -} +inline bool CHashFixedVertexManager::is_closed(const Vertex &vertex) const +{ return !is_opened(vertex); } TEMPLATE_SPECIALIZATION -IC typename CHashFixedVertexManager::CGraphVertex &CHashFixedVertexManager::get_node (const _index_type &vertex_id) const +inline typename CHashFixedVertexManager::Vertex &CHashFixedVertexManager::get_node(const Index &vertex_id) const { - VERIFY (is_visited(vertex_id)); - CGraphIndexVertex *vertex = m_hash[hash_index(vertex_id)]; - for ( ; vertex; vertex = vertex->m_next) - if (vertex->m_vertex->index() == vertex_id) - return (*vertex->m_vertex); - NODEFAULT; -#ifdef DEBUG - return (*vertex->m_vertex); -#endif + VERIFY(is_visited(vertex_id)); + IndexVertex *vertex = m_hash[hash_index(vertex_id)]; + for (; vertex; vertex = vertex->m_next) + { + if (vertex->m_vertex->index()==vertex_id) + return *vertex->m_vertex; + } + NODEFAULT; + return *vertex->m_vertex; } TEMPLATE_SPECIALIZATION -IC typename CHashFixedVertexManager::CGraphVertex &CHashFixedVertexManager::create_vertex (CGraphVertex &vertex, const _index_type &vertex_id) +inline typename CHashFixedVertexManager::Vertex &CHashFixedVertexManager::create_vertex(Vertex &vertex, const Index &vertex_id) { - // allocating new index node - VERIFY (m_vertex_count < fix_size); - CGraphIndexVertex *index_vertex = m_vertices + m_vertex_count++; - // removing old links from the node - if (index_vertex->m_prev) { - index_vertex->m_prev->m_next = index_vertex->m_next; - if (index_vertex->m_next) - index_vertex->m_next->m_prev = index_vertex->m_prev; - } - else { - if (index_vertex->m_next) - index_vertex->m_next->m_prev = 0; - if (m_hash[index_vertex->m_hash] && (m_hash[index_vertex->m_hash]->m_path_id != current_path_id())) - m_hash[index_vertex->m_hash] = 0; - } - - index_vertex->m_vertex = &vertex; - index_vertex->m_path_id = current_path_id(); - vertex.index() = vertex_id; - - u32 index = hash_index(vertex_id); - CGraphIndexVertex *_vertex = m_hash[index]; - if (!_vertex || (_vertex->m_path_id != current_path_id()) || (_vertex->m_hash != index)) - _vertex = 0; - - m_hash[index] = index_vertex; - index_vertex->m_next = _vertex; - index_vertex->m_prev = 0; - if (_vertex) - _vertex->m_prev = index_vertex; - index_vertex->m_hash = index; - return (vertex); + // allocating new index node + VERIFY(m_vertex_countm_prev) + { + index_vertex->m_prev->m_next = index_vertex->m_next; + if (index_vertex->m_next) + index_vertex->m_next->m_prev = index_vertex->m_prev; + } + else + { + if (index_vertex->m_next) + index_vertex->m_next->m_prev = nullptr; + if (m_hash[index_vertex->m_hash] && m_hash[index_vertex->m_hash]->m_path_id!=current_path_id()) + m_hash[index_vertex->m_hash] = nullptr; + } + index_vertex->m_vertex = &vertex; + index_vertex->m_path_id = current_path_id(); + vertex.index() = vertex_id; + u32 index = hash_index(vertex_id); + IndexVertex *_vertex = m_hash[index]; + if (!_vertex || _vertex->m_path_id!=current_path_id() || _vertex->m_hash!=index) + _vertex = nullptr; + m_hash[index] = index_vertex; + index_vertex->m_next = _vertex; + index_vertex->m_prev = nullptr; + if (_vertex) + _vertex->m_prev = index_vertex; + index_vertex->m_hash = index; + return vertex; } #undef TEMPLATE_SPECIALIZATION -#undef CHashFixedVertexManager \ No newline at end of file +#undef CHashFixedVertexManager diff --git a/src/xrAICore/Navigation/vertex_path.h b/src/xrAICore/Navigation/vertex_path.h index f8f0f3d569c..0108606e8e2 100644 --- a/src/xrAICore/Navigation/vertex_path.h +++ b/src/xrAICore/Navigation/vertex_path.h @@ -1,39 +1,39 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_path.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Vertex path class +// Module : vertex_path.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Vertex path class //////////////////////////////////////////////////////////////////////////// #pragma once -template +template struct CVertexPath { -#pragma pack(push,1) - template +#pragma pack(push, 1) + template struct VertexData {}; #pragma pack(pop) - template - class CDataStorage + template + class CDataStorage { - public: - typedef TCompoundVertex CGraphVertex; - typedef typename CGraphVertex::_index_type _index_type; - - public: - IC CDataStorage (const u32 vertex_count); - virtual ~CDataStorage (); - IC void init (); - IC void assign_parent (CGraphVertex &neighbour, CGraphVertex *parent); - template - IC void assign_parent (CGraphVertex &neighbour, CGraphVertex *parent, const T&); - IC void update_successors (CGraphVertex &neighbour); - IC void get_node_path (xr_vector<_index_type> &path, CGraphVertex *best); - }; + public: + using Vertex = TCompoundVertex; + using Index = typename Vertex::Index; + + public: + inline CDataStorage(const u32 vertex_count); + inline virtual ~CDataStorage(); + inline void init(); + inline void assign_parent(Vertex &neighbour, Vertex *parent); + template + inline void assign_parent(Vertex &neighbour, Vertex *parent, const T&); + inline void update_successors(Vertex &neighbour); + inline void get_node_path(xr_vector &path, Vertex *best); + }; }; -#include "xrAICore/Navigation/vertex_path_inline.h" \ No newline at end of file +#include "xrAICore/Navigation/vertex_path_inline.h" diff --git a/src/xrAICore/Navigation/vertex_path_inline.h b/src/xrAICore/Navigation/vertex_path_inline.h index a3e10e5d452..ba64b5e666f 100644 --- a/src/xrAICore/Navigation/vertex_path_inline.h +++ b/src/xrAICore/Navigation/vertex_path_inline.h @@ -1,71 +1,57 @@ //////////////////////////////////////////////////////////////////////////// -// Module : vertex_path_inline.h -// Created : 21.03.2002 -// Modified : 02.03.2004 -// Author : Dmitriy Iassenev -// Description : Vertex path class inline functions +// Module : vertex_path_inline.h +// Created : 21.03.2002 +// Modified : 02.03.2004 +// Author : Dmitriy Iassenev +// Description : Vertex path class inline functions //////////////////////////////////////////////////////////////////////////// #pragma once -#define TEMPLATE_SPECIALIZATION \ - template\ - template +#define TEMPLATE_SPECIALIZATION\ + template \ + template - -#define CVertexPathBuilder CVertexPath::CDataStorage +#define CVertexPathBuilder CVertexPath::CDataStorage TEMPLATE_SPECIALIZATION -IC CVertexPathBuilder::CDataStorage (const u32 vertex_count) -{ -} +inline CVertexPathBuilder::CDataStorage(const u32 vertex_count) +{} TEMPLATE_SPECIALIZATION -CVertexPathBuilder::~CDataStorage () -{ -} +inline CVertexPathBuilder::~CDataStorage() +{} TEMPLATE_SPECIALIZATION -IC void CVertexPathBuilder::init () -{ -} +inline void CVertexPathBuilder::init() +{} TEMPLATE_SPECIALIZATION -IC void CVertexPathBuilder::assign_parent (CGraphVertex &neighbour, CGraphVertex *parent) -{ - neighbour.back() = parent; -} +inline void CVertexPathBuilder::assign_parent(Vertex &neighbour, Vertex *parent) +{ neighbour.back() = parent; } TEMPLATE_SPECIALIZATION template -IC void CVertexPathBuilder::assign_parent (CGraphVertex &neighbour, CGraphVertex *parent, const T&) -{ - assign_parent (neighbour,parent); -} +inline void CVertexPathBuilder::assign_parent(Vertex &neighbour, Vertex *parent, const T&) +{ assign_parent(neighbour, parent); } TEMPLATE_SPECIALIZATION -IC void CVertexPathBuilder::update_successors(CGraphVertex &tpNeighbour) -{ - NODEFAULT; -} +inline void CVertexPathBuilder::update_successors(Vertex &tpNeighbour) +{ NODEFAULT; } TEMPLATE_SPECIALIZATION -IC void CVertexPathBuilder::get_node_path (xr_vector<_index_type> &path, CGraphVertex *best) +inline void CVertexPathBuilder::get_node_path(xr_vector &path, Vertex *best) { - CGraphVertex *t1 = best, *t2 = best->back(); - for (u32 i=1; t2; t1 = t2, t2 = t2->back(), ++i) ; - - path.resize (i); - - t1 = best; - path[--i] = best->index(); - t2 = t1->back(); - - xr_vector<_index_type>::reverse_iterator I = path.rbegin(); - xr_vector<_index_type>::reverse_iterator E = path.rend(); - for (++I ; t2; t2 = t2->back(), ++I) - *I = t2->index(); + Vertex *t1 = best, *t2 = best->back(); + for (u32 i = 1; t2; t1 = t2, t2 = t2->back(), i++); + path.resize(i); + t1 = best; + path[--i] = best->index(); + t2 = t1->back(); + auto it = path.rbegin(); + for (it++; t2; t2 = t2->back(), it++) + *it = t2->index(); } #undef TEMPLATE_SPECIALIZATION -#undef CVertexPathBuilder \ No newline at end of file +#undef CVertexPathBuilder