Apply clang-format on BFSComponentExplorer and RestrictionMap

This commit is contained in:
Patrick Niklaus 2014-05-08 23:29:24 +02:00
parent 22d0861f6d
commit e00ef38305
4 changed files with 194 additions and 193 deletions

View File

@ -12,8 +12,7 @@
* Explores the components of the given graph while respecting turn restrictions * Explores the components of the given graph while respecting turn restrictions
* and barriers. * and barriers.
*/ */
template<typename GraphT> template <typename GraphT> class BFSComponentExplorer
class BFSComponentExplorer
{ {
public: public:
typedef typename GraphT::NodeIterator NodeIterator; typedef typename GraphT::NodeIterator NodeIterator;
@ -22,9 +21,7 @@ public:
BFSComponentExplorer(const GraphT &dynamicGraph, BFSComponentExplorer(const GraphT &dynamicGraph,
const RestrictionMap &restrictions, const RestrictionMap &restrictions,
const boost::unordered_set<NodeID> &barrier_nodes) const boost::unordered_set<NodeID> &barrier_nodes)
: m_graph(dynamicGraph) : m_graph(dynamicGraph), m_restriction_map(restrictions), m_barrier_nodes(barrier_nodes)
, m_restriction_map(restrictions)
, m_barrier_nodes(barrier_nodes)
{ {
BOOST_ASSERT(m_graph.GetNumberOfNodes() > 0); BOOST_ASSERT(m_graph.GetNumberOfNodes() > 0);
} }
@ -39,10 +36,7 @@ public:
return m_component_index_size[m_component_index_list[node]]; return m_component_index_size[m_component_index_list[node]];
} }
inline unsigned int getNumberOfComponents() inline unsigned int getNumberOfComponents() { return m_component_index_size.size(); }
{
return m_component_index_size.size();
}
/*! /*!
* Computes the component sizes. * Computes the component sizes.
@ -57,16 +51,15 @@ public:
unsigned num_nodes = m_graph.GetNumberOfNodes(); unsigned num_nodes = m_graph.GetNumberOfNodes();
m_component_index_list.resize( m_component_index_list.resize(num_nodes, std::numeric_limits<unsigned>::max());
num_nodes,
std::numeric_limits<unsigned>::max()
);
BOOST_ASSERT(num_nodes > 0); BOOST_ASSERT(num_nodes > 0);
// put unexplorered node with parent pointer into queue // put unexplorered node with parent pointer into queue
for( NodeID node = 0; node < num_nodes; ++node) { for (NodeID node = 0; node < num_nodes; ++node)
if(std::numeric_limits<unsigned>::max() == m_component_index_list[node]) { {
if (std::numeric_limits<unsigned>::max() == m_component_index_list[node])
{
unsigned size = exploreComponent(bfs_queue, node, current_component); unsigned size = exploreComponent(bfs_queue, node, current_component);
// push size into vector // push size into vector
@ -80,18 +73,18 @@ private:
/*! /*!
* Explores the current component that starts at node using BFS. * Explores the current component that starts at node using BFS.
*/ */
inline unsigned exploreComponent( inline unsigned exploreComponent(std::queue<std::pair<NodeID, NodeID>> &bfs_queue,
std::queue<std::pair<NodeID, NodeID> > &bfs_queue,
NodeID node, NodeID node,
unsigned current_component unsigned current_component)
) { {
bfs_queue.push(std::make_pair(node, node)); bfs_queue.push(std::make_pair(node, node));
// mark node as read // mark node as read
m_component_index_list[node] = current_component; m_component_index_list[node] = current_component;
unsigned current_component_size = 1; unsigned current_component_size = 1;
while(!bfs_queue.empty()) { while (!bfs_queue.empty())
{
// fetch element from BFS queue // fetch element from BFS queue
std::pair<NodeID, NodeID> current_queue_item = bfs_queue.front(); std::pair<NodeID, NodeID> current_queue_item = bfs_queue.front();
bfs_queue.pop(); bfs_queue.pop();
@ -101,30 +94,31 @@ private:
// increment size counter of current component // increment size counter of current component
++current_component_size; ++current_component_size;
const bool is_barrier_node = (m_barrier_nodes.find(v) != m_barrier_nodes.end()); const bool is_barrier_node = (m_barrier_nodes.find(v) != m_barrier_nodes.end());
if(!is_barrier_node) { if (!is_barrier_node)
const NodeID to_node_of_only_restriction = m_restriction_map.CheckForEmanatingIsOnlyTurn(u, v); {
const NodeID to_node_of_only_restriction =
m_restriction_map.CheckForEmanatingIsOnlyTurn(u, v);
for( for (EdgeIterator e2 = m_graph.BeginEdges(v); e2 < m_graph.EndEdges(v); ++e2)
EdgeIterator e2 = m_graph.BeginEdges(v); {
e2 < m_graph.EndEdges(v);
++e2
) {
NodeIterator w = m_graph.GetTarget(e2); NodeIterator w = m_graph.GetTarget(e2);
if( if (to_node_of_only_restriction != std::numeric_limits<unsigned>::max() &&
to_node_of_only_restriction != std::numeric_limits<unsigned>::max() && w != to_node_of_only_restriction)
w != to_node_of_only_restriction {
) {
// At an only_-restriction but not at the right turn // At an only_-restriction but not at the right turn
continue; continue;
} }
if( u != w ) { if (u != w)
{
// only add an edge if turn is not a U-turn except // only add an edge if turn is not a U-turn except
// when it is at the end of a dead-end street. // when it is at the end of a dead-end street.
if (!m_restriction_map.CheckIfTurnIsRestricted(u, v, w)) { if (!m_restriction_map.CheckIfTurnIsRestricted(u, v, w))
{
// only add an edge if turn is not prohibited // only add an edge if turn is not prohibited
if(std::numeric_limits<unsigned>::max() == m_component_index_list[w]) { if (std::numeric_limits<unsigned>::max() == m_component_index_list[w])
{
// insert next (node, parent) only if w has // insert next (node, parent) only if w has
// not yet been explored // not yet been explored
// mark node as read // mark node as read

View File

@ -4,11 +4,15 @@
#include "DynamicGraph.h" #include "DynamicGraph.h"
#include "ImportEdge.h" #include "ImportEdge.h"
struct NodeBasedEdgeData { struct NodeBasedEdgeData
NodeBasedEdgeData() : distance(INVALID_EDGE_WEIGHT), edgeBasedNodeID(SPECIAL_NODEID), nameID(std::numeric_limits<unsigned>::max()), {
type(std::numeric_limits<short>::max()), isAccessRestricted(false), shortcut(false), forward(false), backward(false), NodeBasedEdgeData()
: distance(INVALID_EDGE_WEIGHT), edgeBasedNodeID(SPECIAL_NODEID),
nameID(std::numeric_limits<unsigned>::max()), type(std::numeric_limits<short>::max()),
isAccessRestricted(false), shortcut(false), forward(false), backward(false),
roundabout(false), ignore_in_grid(false), contraFlow(false) roundabout(false), ignore_in_grid(false), contraFlow(false)
{ } {
}
int distance; int distance;
unsigned edgeBasedNodeID; unsigned edgeBasedNodeID;
@ -22,17 +26,17 @@ struct NodeBasedEdgeData {
bool ignore_in_grid : 1; bool ignore_in_grid : 1;
bool contraFlow : 1; bool contraFlow : 1;
void SwapDirectionFlags() { void SwapDirectionFlags()
{
bool temp_flag = forward; bool temp_flag = forward;
forward = backward; forward = backward;
backward = temp_flag; backward = temp_flag;
} }
bool IsEqualTo( const NodeBasedEdgeData & other ) const { bool IsEqualTo(const NodeBasedEdgeData &other) const
return (forward == other.forward) && {
(backward == other.backward) && return (forward == other.forward) && (backward == other.backward) &&
(nameID == other.nameID) && (nameID == other.nameID) && (ignore_in_grid == other.ignore_in_grid) &&
(ignore_in_grid == other.ignore_in_grid) &&
(contraFlow == other.contraFlow); (contraFlow == other.contraFlow);
} }
}; };
@ -40,10 +44,9 @@ struct NodeBasedEdgeData {
typedef DynamicGraph<NodeBasedEdgeData> NodeBasedDynamicGraph; typedef DynamicGraph<NodeBasedEdgeData> NodeBasedDynamicGraph;
// Factory method to create NodeBasedDynamicGraph from ImportEdges // Factory method to create NodeBasedDynamicGraph from ImportEdges
inline std::shared_ptr<NodeBasedDynamicGraph> NodeBasedDynamicGraphFromImportEdges( inline std::shared_ptr<NodeBasedDynamicGraph>
int number_of_nodes, NodeBasedDynamicGraphFromImportEdges(int number_of_nodes, std::vector<ImportEdge> &input_edge_list)
std::vector<ImportEdge>& input_edge_list {
) {
typedef NodeBasedDynamicGraph::InputEdge DynInputEdge; typedef NodeBasedDynamicGraph::InputEdge DynInputEdge;
std::sort(input_edge_list.begin(), input_edge_list.end()); std::sort(input_edge_list.begin(), input_edge_list.end());
@ -51,21 +54,26 @@ inline std::shared_ptr<NodeBasedDynamicGraph> NodeBasedDynamicGraphFromImportEdg
// TODO: remove duplicate edges // TODO: remove duplicate edges
DeallocatingVector<DynInputEdge> edges_list; DeallocatingVector<DynInputEdge> edges_list;
DynInputEdge edge; DynInputEdge edge;
for(const ImportEdge& import_edge : input_edge_list) { for (const ImportEdge &import_edge : input_edge_list)
{
if( !import_edge.isForward() ) { if (!import_edge.isForward())
{
edge.source = import_edge.target(); edge.source = import_edge.target();
edge.target = import_edge.source(); edge.target = import_edge.source();
edge.data.backward = import_edge.isForward(); edge.data.backward = import_edge.isForward();
edge.data.forward = import_edge.isBackward(); edge.data.forward = import_edge.isBackward();
} else { }
else
{
edge.source = import_edge.source(); edge.source = import_edge.source();
edge.target = import_edge.target(); edge.target = import_edge.target();
edge.data.forward = import_edge.isForward(); edge.data.forward = import_edge.isForward();
edge.data.backward = import_edge.isBackward(); edge.data.backward = import_edge.isBackward();
} }
if( edge.source == edge.target ) { if (edge.source == edge.target)
{
continue; continue;
} }
@ -80,7 +88,8 @@ inline std::shared_ptr<NodeBasedDynamicGraph> NodeBasedDynamicGraphFromImportEdg
edge.data.contraFlow = import_edge.isContraFlow(); edge.data.contraFlow = import_edge.isContraFlow();
edges_list.push_back(edge); edges_list.push_back(edge);
if( !import_edge.IsSplit() ) { if (!import_edge.IsSplit())
{
using std::swap; // enable ADL using std::swap; // enable ADL
swap(edge.source, edge.target); swap(edge.source, edge.target);
edge.data.SwapDirectionFlags(); edge.data.SwapDirectionFlags();
@ -89,10 +98,7 @@ inline std::shared_ptr<NodeBasedDynamicGraph> NodeBasedDynamicGraphFromImportEdg
} }
std::sort(edges_list.begin(), edges_list.end()); std::sort(edges_list.begin(), edges_list.end());
auto graph = std::make_shared<NodeBasedDynamicGraph>( auto graph = std::make_shared<NodeBasedDynamicGraph>(number_of_nodes, edges_list);
number_of_nodes,
edges_list
);
// FIXME probably unneeded since this is the end of scope // FIXME probably unneeded since this is the end of scope
DeallocatingVector<DynInputEdge>().swap(edges_list); DeallocatingVector<DynInputEdge>().swap(edges_list);
@ -101,6 +107,4 @@ inline std::shared_ptr<NodeBasedDynamicGraph> NodeBasedDynamicGraphFromImportEdg
return graph; return graph;
} }
#endif #endif

View File

@ -1,36 +1,42 @@
#include "RestrictionMap.h" #include "RestrictionMap.h"
#include "NodeBasedGraph.h" #include "NodeBasedGraph.h"
RestrictionMap::RestrictionMap(const std::shared_ptr<NodeBasedDynamicGraph>& graph, const std::vector<TurnRestriction> & input_restrictions_list) RestrictionMap::RestrictionMap(const std::shared_ptr<NodeBasedDynamicGraph> &graph,
: m_count(0) const std::vector<TurnRestriction> &input_restrictions_list)
, m_graph(graph) : m_count(0), m_graph(graph)
{ {
// decompose restirction consisting of a start, via and end note into a start-edge // decompose restirction consisting of a start, via and end note into a start-edge
// and all end-nodes // and all end-nodes
for (auto& restriction : input_restrictions_list) { for (auto &restriction : input_restrictions_list)
{
std::pair<NodeID, NodeID> restriction_source = std::pair<NodeID, NodeID> restriction_source =
std::make_pair(restriction.fromNode, restriction.viaNode); std::make_pair(restriction.fromNode, restriction.viaNode);
unsigned index; unsigned index;
auto restriction_iter = m_restriction_map.find(restriction_source); auto restriction_iter = m_restriction_map.find(restriction_source);
if(restriction_iter == m_restriction_map.end()) { if (restriction_iter == m_restriction_map.end())
{
index = m_restriction_bucket_list.size(); index = m_restriction_bucket_list.size();
m_restriction_bucket_list.resize(index + 1); m_restriction_bucket_list.resize(index + 1);
m_restriction_map.emplace(restriction_source, index); m_restriction_map.emplace(restriction_source, index);
} else { }
else
{
index = restriction_iter->second; index = restriction_iter->second;
// Map already contains an is_only_*-restriction // Map already contains an is_only_*-restriction
if(m_restriction_bucket_list.at(index).begin()->second) { if (m_restriction_bucket_list.at(index).begin()->second)
{
continue; continue;
} else if(restriction.flags.isOnly) { }
else if (restriction.flags.isOnly)
{
// We are going to insert an is_only_*-restriction. There can be only one. // We are going to insert an is_only_*-restriction. There can be only one.
m_count -= m_restriction_bucket_list.at(index).size(); m_count -= m_restriction_bucket_list.at(index).size();
m_restriction_bucket_list.at(index).clear(); m_restriction_bucket_list.at(index).clear();
} }
} }
++m_count; ++m_count;
m_restriction_bucket_list.at(index).push_back( m_restriction_bucket_list.at(index)
std::make_pair( restriction.toNode, restriction.flags.isOnly) .push_back(std::make_pair(restriction.toNode, restriction.flags.isOnly));
);
} }
} }
@ -39,11 +45,8 @@ RestrictionMap::RestrictionMap(const std::shared_ptr<NodeBasedDynamicGraph>& gra
* *
* Note: We need access to node based graph. * Note: We need access to node based graph.
*/ */
void RestrictionMap::FixupArrivingTurnRestriction( void RestrictionMap::FixupArrivingTurnRestriction(const NodeID u, const NodeID v, const NodeID w)
const NodeID u, {
const NodeID v,
const NodeID w
) {
BOOST_ASSERT(u != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(u != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(v != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(v != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(w != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(w != std::numeric_limits<unsigned>::max());
@ -52,19 +55,19 @@ void RestrictionMap::FixupArrivingTurnRestriction(
// it is more efficent to get a (small) list of potential start edges // it is more efficent to get a (small) list of potential start edges
// than iterating over all buckets // than iterating over all buckets
std::vector<NodeID> predecessors; std::vector<NodeID> predecessors;
for( for (EdgeID current_edge_id = m_graph->BeginEdges(u); current_edge_id < m_graph->EndEdges(u);
EdgeID current_edge_id = m_graph->BeginEdges(u); ++current_edge_id)
current_edge_id < m_graph->EndEdges(u); {
++current_edge_id
) {
const EdgeData &edge_data = m_graph->GetEdgeData(current_edge_id); const EdgeData &edge_data = m_graph->GetEdgeData(current_edge_id);
const NodeID target = m_graph->GetTarget(current_edge_id); const NodeID target = m_graph->GetTarget(current_edge_id);
if( edge_data.backward && ( v != target) ) { if (edge_data.backward && (v != target))
{
predecessors.push_back(target); predecessors.push_back(target);
} }
} }
for(const NodeID x : predecessors) { for (const NodeID x : predecessors)
{
const std::pair<NodeID, NodeID> restr_start = std::make_pair(x, u); const std::pair<NodeID, NodeID> restr_start = std::make_pair(x, u);
auto restriction_iterator = m_restriction_map.find(restr_start); auto restriction_iterator = m_restriction_map.find(restr_start);
if (restriction_iterator == m_restriction_map.end()) if (restriction_iterator == m_restriction_map.end())
@ -72,8 +75,10 @@ void RestrictionMap::FixupArrivingTurnRestriction(
const unsigned index = restriction_iterator->second; const unsigned index = restriction_iterator->second;
auto &bucket = m_restriction_bucket_list.at(index); auto &bucket = m_restriction_bucket_list.at(index);
for(RestrictionTarget& restriction_target : bucket) { for (RestrictionTarget &restriction_target : bucket)
if( v == restriction_target.first ) { {
if (v == restriction_target.first)
{
restriction_target.first = w; restriction_target.first = w;
} }
} }
@ -83,11 +88,8 @@ void RestrictionMap::FixupArrivingTurnRestriction(
/** /**
* Replaces the start edge (v, w) with (u, w), only start node changes. * Replaces the start edge (v, w) with (u, w), only start node changes.
*/ */
void RestrictionMap::FixupStartingTurnRestriction( void RestrictionMap::FixupStartingTurnRestriction(const NodeID u, const NodeID v, const NodeID w)
const NodeID u, {
const NodeID v,
const NodeID w
) {
BOOST_ASSERT(u != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(u != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(v != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(v != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(w != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(w != std::numeric_limits<unsigned>::max());
@ -95,7 +97,8 @@ void RestrictionMap::FixupStartingTurnRestriction(
const std::pair<NodeID, NodeID> old_start = std::make_pair(v, w); const std::pair<NodeID, NodeID> old_start = std::make_pair(v, w);
auto restriction_iterator = m_restriction_map.find(old_start); auto restriction_iterator = m_restriction_map.find(old_start);
if( restriction_iterator != m_restriction_map.end() ) { if (restriction_iterator != m_restriction_map.end())
{
const unsigned index = restriction_iterator->second; const unsigned index = restriction_iterator->second;
// remove old restriction start (v,w) // remove old restriction start (v,w)
m_restriction_map.erase(restriction_iterator); m_restriction_map.erase(restriction_iterator);
@ -110,21 +113,22 @@ void RestrictionMap::FixupStartingTurnRestriction(
* Check if the edge (u, v) is contained in any turn restriction. * Check if the edge (u, v) is contained in any turn restriction.
* If so returns id of first target node. * If so returns id of first target node.
*/ */
NodeID RestrictionMap::CheckForEmanatingIsOnlyTurn( NodeID RestrictionMap::CheckForEmanatingIsOnlyTurn(const NodeID u, const NodeID v) const
const NodeID u, {
const NodeID v
) const {
BOOST_ASSERT(u != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(u != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(v != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(v != std::numeric_limits<unsigned>::max());
const std::pair<NodeID, NodeID> restriction_source = std::make_pair(u, v); const std::pair<NodeID, NodeID> restriction_source = std::make_pair(u, v);
auto restriction_iter = m_restriction_map.find(restriction_source); auto restriction_iter = m_restriction_map.find(restriction_source);
if (restriction_iter != m_restriction_map.end()) { if (restriction_iter != m_restriction_map.end())
{
const unsigned index = restriction_iter->second; const unsigned index = restriction_iter->second;
auto &bucket = m_restriction_bucket_list.at(index); auto &bucket = m_restriction_bucket_list.at(index);
for(const RestrictionSource& restriction_target : bucket) { for (const RestrictionSource &restriction_target : bucket)
if(restriction_target.second) { {
if (restriction_target.second)
{
return restriction_target.first; return restriction_target.first;
} }
} }
@ -136,11 +140,8 @@ NodeID RestrictionMap::CheckForEmanatingIsOnlyTurn(
/** /**
* Checks if the turn described by start u, via v and targed w is covert by any turn restriction. * Checks if the turn described by start u, via v and targed w is covert by any turn restriction.
*/ */
bool RestrictionMap::CheckIfTurnIsRestricted( bool RestrictionMap::CheckIfTurnIsRestricted(const NodeID u, const NodeID v, const NodeID w) const
const NodeID u, {
const NodeID v,
const NodeID w
) const {
BOOST_ASSERT(u != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(u != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(v != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(v != std::numeric_limits<unsigned>::max());
BOOST_ASSERT(w != std::numeric_limits<unsigned>::max()); BOOST_ASSERT(w != std::numeric_limits<unsigned>::max());
@ -148,14 +149,16 @@ bool RestrictionMap::CheckIfTurnIsRestricted(
const std::pair<NodeID, NodeID> restriction_source = std::make_pair(u, v); const std::pair<NodeID, NodeID> restriction_source = std::make_pair(u, v);
auto restriction_iter = m_restriction_map.find(restriction_source); auto restriction_iter = m_restriction_map.find(restriction_source);
if (restriction_iter != m_restriction_map.end()) { if (restriction_iter != m_restriction_map.end())
{
const unsigned index = restriction_iter->second; const unsigned index = restriction_iter->second;
auto &bucket = m_restriction_bucket_list.at(index); auto &bucket = m_restriction_bucket_list.at(index);
for(const RestrictionTarget & restriction_target : bucket) { for (const RestrictionTarget &restriction_target : bucket)
if( {
( w == restriction_target.first ) && // target found if ((w == restriction_target.first) && // target found
(!restriction_target.second) // and not an only_-restr. (!restriction_target.second) // and not an only_-restr.
) { )
{
return true; return true;
} }
} }