C++11-fy Contractor/

This commit is contained in:
Dennis Luxen 2014-05-09 14:21:33 +02:00
parent 5f0ee417b1
commit c98eed612a
6 changed files with 545 additions and 549 deletions

File diff suppressed because it is too large Load Diff

View File

@ -26,16 +26,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "EdgeBasedGraphFactory.h" #include "EdgeBasedGraphFactory.h"
#include "../Algorithms/BFSComponentExplorer.h"
#include "../DataStructures/Percent.h"
#include "../Util/ComputeAngle.h" #include "../Util/ComputeAngle.h"
#include "../Util/LuaUtil.h"
#include "../Util/SimpleLogger.h"
#include "../Util/TimingUtil.h" #include "../Util/TimingUtil.h"
#include "BFSComponentExplorer.h"
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/foreach.hpp>
#include <fstream> #include <fstream>
#include <iomanip> #include <limits>
#include <numeric>
EdgeBasedGraphFactory::EdgeBasedGraphFactory( EdgeBasedGraphFactory::EdgeBasedGraphFactory(
const std::shared_ptr<NodeBasedDynamicGraph> &node_based_graph, const std::shared_ptr<NodeBasedDynamicGraph> &node_based_graph,
@ -52,7 +53,6 @@ EdgeBasedGraphFactory::EdgeBasedGraphFactory(
// insert into unordered sets for fast lookup // insert into unordered sets for fast lookup
m_barrier_nodes.insert(barrier_node_list.begin(), barrier_node_list.end()); m_barrier_nodes.insert(barrier_node_list.begin(), barrier_node_list.end());
m_traffic_lights.insert(traffic_light_node_list.begin(), traffic_light_node_list.end()); m_traffic_lights.insert(traffic_light_node_list.begin(), traffic_light_node_list.end());
} }
@ -65,7 +65,7 @@ void EdgeBasedGraphFactory::GetEdgeBasedEdges(DeallocatingVector<EdgeBasedEdge>
void EdgeBasedGraphFactory::GetEdgeBasedNodes(std::vector<EdgeBasedNode> &nodes) void EdgeBasedGraphFactory::GetEdgeBasedNodes(std::vector<EdgeBasedNode> &nodes)
{ {
#ifndef NDEBUG #ifndef NDEBUG
BOOST_FOREACH (const EdgeBasedNode &node, m_edge_based_node_list) for (const EdgeBasedNode &node : m_edge_based_node_list)
{ {
BOOST_ASSERT(m_node_info_list.at(node.u).lat != INT_MAX); BOOST_ASSERT(m_node_info_list.at(node.u).lat != INT_MAX);
@ -77,10 +77,8 @@ void EdgeBasedGraphFactory::GetEdgeBasedNodes(std::vector<EdgeBasedNode> &nodes)
nodes.swap(m_edge_based_node_list); nodes.swap(m_edge_based_node_list);
} }
void EdgeBasedGraphFactory::InsertEdgeBasedNode(NodeIterator u, void
NodeIterator v, EdgeBasedGraphFactory::InsertEdgeBasedNode(NodeID u, NodeID v, EdgeID e1, bool belongs_to_tiny_cc)
EdgeIterator e1,
bool belongs_to_tiny_cc)
{ {
// merge edges together into one EdgeBasedNode // merge edges together into one EdgeBasedNode
BOOST_ASSERT(u != SPECIAL_NODEID); BOOST_ASSERT(u != SPECIAL_NODEID);
@ -179,19 +177,18 @@ void EdgeBasedGraphFactory::InsertEdgeBasedNode(NodeIterator u,
BOOST_ASSERT(current_edge_target_coordinate_id != current_edge_start_coordinate_id); BOOST_ASSERT(current_edge_target_coordinate_id != current_edge_start_coordinate_id);
// build edges // build edges
m_edge_based_node_list.emplace_back( m_edge_based_node_list.emplace_back(forward_data.edgeBasedNodeID,
EdgeBasedNode(forward_data.edgeBasedNodeID, reverse_data.edgeBasedNodeID,
reverse_data.edgeBasedNodeID, current_edge_start_coordinate_id,
current_edge_start_coordinate_id, current_edge_target_coordinate_id,
current_edge_target_coordinate_id, forward_data.nameID,
forward_data.nameID, forward_geometry[i].second,
forward_geometry[i].second, reverse_geometry[i].second,
reverse_geometry[i].second, forward_dist_prefix_sum[i],
forward_dist_prefix_sum[i], reverse_dist_prefix_sum[i],
reverse_dist_prefix_sum[i], m_geometry_compressor.GetPositionForID(e1),
m_geometry_compressor.GetPositionForID(e1), i,
i, belongs_to_tiny_cc);
belongs_to_tiny_cc));
current_edge_start_coordinate_id = current_edge_target_coordinate_id; current_edge_start_coordinate_id = current_edge_target_coordinate_id;
BOOST_ASSERT(m_edge_based_node_list.back().IsCompressed()); BOOST_ASSERT(m_edge_based_node_list.back().IsCompressed());
@ -278,10 +275,10 @@ void EdgeBasedGraphFactory::Run(const std::string &original_edge_data_filename,
m_geometry_compressor.SerializeInternalVector(geometry_filename); m_geometry_compressor.SerializeInternalVector(geometry_filename);
SimpleLogger().Write() << "Timing statistics for edge-expanded graph:"; SimpleLogger().Write() << "Timing statistics for edge-expanded graph:";
SimpleLogger().Write() << "Geometry compression: " << TIMER_SEC(geometry) << "s"; SimpleLogger().Write() << "Geometry compression: " << TIMER_MSEC(geometry)*0.001 << "s";
SimpleLogger().Write() << "Renumbering edges: " << TIMER_SEC(renumber) << "s"; SimpleLogger().Write() << "Renumbering edges: " << TIMER_MSEC(renumber)*0.001 << "s";
SimpleLogger().Write() << "Generating nodes: " << TIMER_SEC(generate_nodes) << "s"; SimpleLogger().Write() << "Generating nodes: " << TIMER_MSEC(generate_nodes)*0.001 << "s";
SimpleLogger().Write() << "Generating edges: " << TIMER_SEC(generate_edges) << "s"; SimpleLogger().Write() << "Generating edges: " << TIMER_MSEC(generate_edges)*0.001 << "s";
} }
void EdgeBasedGraphFactory::CompressGeometry() void EdgeBasedGraphFactory::CompressGeometry()
@ -312,26 +309,26 @@ void EdgeBasedGraphFactory::CompressGeometry()
const bool reverse_edge_order = const bool reverse_edge_order =
!(m_node_based_graph->GetEdgeData(m_node_based_graph->BeginEdges(v)).forward); !(m_node_based_graph->GetEdgeData(m_node_based_graph->BeginEdges(v)).forward);
const EdgeIterator forward_e2 = m_node_based_graph->BeginEdges(v) + reverse_edge_order; const EdgeID forward_e2 = m_node_based_graph->BeginEdges(v) + reverse_edge_order;
BOOST_ASSERT(SPECIAL_EDGEID != forward_e2); BOOST_ASSERT(SPECIAL_EDGEID != forward_e2);
const EdgeIterator reverse_e2 = m_node_based_graph->BeginEdges(v) + 1 - reverse_edge_order; const EdgeID reverse_e2 = m_node_based_graph->BeginEdges(v) + 1 - reverse_edge_order;
BOOST_ASSERT(SPECIAL_EDGEID != reverse_e2); BOOST_ASSERT(SPECIAL_EDGEID != reverse_e2);
const EdgeData &fwd_edge_data2 = m_node_based_graph->GetEdgeData(forward_e2); const EdgeData &fwd_edge_data2 = m_node_based_graph->GetEdgeData(forward_e2);
const EdgeData &rev_edge_data2 = m_node_based_graph->GetEdgeData(reverse_e2); const EdgeData &rev_edge_data2 = m_node_based_graph->GetEdgeData(reverse_e2);
const NodeIterator w = m_node_based_graph->GetTarget(forward_e2); const NodeID w = m_node_based_graph->GetTarget(forward_e2);
BOOST_ASSERT(SPECIAL_NODEID != w); BOOST_ASSERT(SPECIAL_NODEID != w);
BOOST_ASSERT(v != w); BOOST_ASSERT(v != w);
const NodeIterator u = m_node_based_graph->GetTarget(reverse_e2); const NodeID u = m_node_based_graph->GetTarget(reverse_e2);
BOOST_ASSERT(SPECIAL_NODEID != u); BOOST_ASSERT(SPECIAL_NODEID != u);
BOOST_ASSERT(u != v); BOOST_ASSERT(u != v);
const EdgeIterator forward_e1 = m_node_based_graph->FindEdge(u, v); const EdgeID forward_e1 = m_node_based_graph->FindEdge(u, v);
BOOST_ASSERT(m_node_based_graph->EndEdges(u) != forward_e1); BOOST_ASSERT(m_node_based_graph->EndEdges(u) != forward_e1);
BOOST_ASSERT(SPECIAL_EDGEID != forward_e1); BOOST_ASSERT(SPECIAL_EDGEID != forward_e1);
BOOST_ASSERT(v == m_node_based_graph->GetTarget(forward_e1)); BOOST_ASSERT(v == m_node_based_graph->GetTarget(forward_e1));
const EdgeIterator reverse_e1 = m_node_based_graph->FindEdge(w, v); const EdgeID reverse_e1 = m_node_based_graph->FindEdge(w, v);
BOOST_ASSERT(SPECIAL_EDGEID != reverse_e1); BOOST_ASSERT(SPECIAL_EDGEID != reverse_e1);
BOOST_ASSERT(v == m_node_based_graph->GetTarget(reverse_e1)); BOOST_ASSERT(v == m_node_based_graph->GetTarget(reverse_e1));
@ -345,7 +342,8 @@ void EdgeBasedGraphFactory::CompressGeometry()
} }
if ( // TODO: rename to IsCompatibleTo if ( // TODO: rename to IsCompatibleTo
fwd_edge_data1.IsEqualTo(fwd_edge_data2) && rev_edge_data1.IsEqualTo(rev_edge_data2)) fwd_edge_data1.IsEqualTo(fwd_edge_data2) &&
rev_edge_data1.IsEqualTo(rev_edge_data2))
{ {
// Get distances before graph is modified // Get distances before graph is modified
const int forward_weight1 = m_node_based_graph->GetEdgeData(forward_e1).distance; const int forward_weight1 = m_node_based_graph->GetEdgeData(forward_e1).distance;
@ -426,10 +424,10 @@ void EdgeBasedGraphFactory::CompressGeometry()
} }
} }
SimpleLogger().Write() << "new nodes: " << new_node_count << ", edges " << new_edge_count; SimpleLogger().Write() << "new nodes: " << new_node_count << ", edges " << new_edge_count;
SimpleLogger().Write() << "Node compression ratio: " SimpleLogger().Write() << "Node compression ratio: " << new_node_count /
<< new_node_count / (double)original_number_of_nodes; (double)original_number_of_nodes;
SimpleLogger().Write() << "Edge compression ratio: " SimpleLogger().Write() << "Edge compression ratio: " << new_edge_count /
<< new_edge_count / (double)original_number_of_edges; (double)original_number_of_edges;
} }
/** /**
@ -442,7 +440,7 @@ void EdgeBasedGraphFactory::RenumberEdges()
for (NodeID current_node = 0; current_node < m_node_based_graph->GetNumberOfNodes(); for (NodeID current_node = 0; current_node < m_node_based_graph->GetNumberOfNodes();
++current_node) ++current_node)
{ {
for (EdgeIterator current_edge = m_node_based_graph->BeginEdges(current_node); for (EdgeID current_edge = m_node_based_graph->BeginEdges(current_node);
current_edge < m_node_based_graph->EndEdges(current_node); current_edge < m_node_based_graph->EndEdges(current_node);
++current_edge) ++current_edge)
{ {
@ -459,7 +457,6 @@ void EdgeBasedGraphFactory::RenumberEdges()
BOOST_ASSERT(SPECIAL_NODEID != edge_data.edgeBasedNodeID); BOOST_ASSERT(SPECIAL_NODEID != edge_data.edgeBasedNodeID);
} }
} }
m_number_of_edge_based_nodes = numbered_edges_count; m_number_of_edge_based_nodes = numbered_edges_count;
} }
@ -476,14 +473,14 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedNodes()
component_explorer.run(); component_explorer.run();
SimpleLogger().Write() << "identified: " << component_explorer.getNumberOfComponents() SimpleLogger().Write() << "identified: " << component_explorer.GetNumberOfComponents()
<< " many components"; << " many components";
SimpleLogger().Write() << "generating edge-expanded nodes"; SimpleLogger().Write() << "generating edge-expanded nodes";
Percent p(m_node_based_graph->GetNumberOfNodes()); Percent p(m_node_based_graph->GetNumberOfNodes());
// loop over all edges and generate new set of nodes // loop over all edges and generate new set of nodes
for (NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u) for (NodeID u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u)
{ {
BOOST_ASSERT(u != SPECIAL_NODEID); BOOST_ASSERT(u != SPECIAL_NODEID);
BOOST_ASSERT(u < m_node_based_graph->GetNumberOfNodes()); BOOST_ASSERT(u < m_node_based_graph->GetNumberOfNodes());
@ -513,8 +510,8 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedNodes()
// Note: edges that end on barrier nodes or on a turn restriction // Note: edges that end on barrier nodes or on a turn restriction
// may actually be in two distinct components. We choose the smallest // may actually be in two distinct components. We choose the smallest
const unsigned size_of_component = std::min(component_explorer.getComponentSize(u), const unsigned size_of_component = std::min(component_explorer.GetComponentSize(u),
component_explorer.getComponentSize(v)); component_explorer.GetComponentSize(v));
const bool component_is_tiny = (size_of_component < 1000); const bool component_is_tiny = (size_of_component < 1000);
InsertEdgeBasedNode(u, v, e1, component_is_tiny); InsertEdgeBasedNode(u, v, e1, component_is_tiny);
@ -555,10 +552,10 @@ EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string &original_edg
Percent p(m_node_based_graph->GetNumberOfNodes()); Percent p(m_node_based_graph->GetNumberOfNodes());
for (NodeIterator u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u) for (NodeID u = 0, end = m_node_based_graph->GetNumberOfNodes(); u < end; ++u)
{ {
for (EdgeIterator e1 = m_node_based_graph->BeginEdges(u), for (EdgeID e1 = m_node_based_graph->BeginEdges(u),
last_edge_u = m_node_based_graph->EndEdges(u); last_edge_u = m_node_based_graph->EndEdges(u);
e1 < last_edge_u; e1 < last_edge_u;
++e1) ++e1)
{ {
@ -568,13 +565,13 @@ EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string &original_edg
} }
++node_based_edge_counter; ++node_based_edge_counter;
const NodeIterator v = m_node_based_graph->GetTarget(e1); const NodeID v = m_node_based_graph->GetTarget(e1);
const NodeID to_node_of_only_restriction = const NodeID to_node_of_only_restriction =
m_restriction_map->CheckForEmanatingIsOnlyTurn(u, v); m_restriction_map->CheckForEmanatingIsOnlyTurn(u, v);
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());
for (EdgeIterator e2 = m_node_based_graph->BeginEdges(v), for (EdgeID e2 = m_node_based_graph->BeginEdges(v),
last_edge_v = m_node_based_graph->EndEdges(v); last_edge_v = m_node_based_graph->EndEdges(v);
e2 < last_edge_v; e2 < last_edge_v;
++e2) ++e2)
{ {
@ -582,7 +579,7 @@ EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string &original_edg
{ {
continue; continue;
} }
const NodeIterator w = m_node_based_graph->GetTarget(e2); const NodeID w = m_node_based_graph->GetTarget(e2);
if ((to_node_of_only_restriction != SPECIAL_NODEID) && if ((to_node_of_only_restriction != SPECIAL_NODEID) &&
(w != to_node_of_only_restriction)) (w != to_node_of_only_restriction))
@ -648,11 +645,11 @@ EdgeBasedGraphFactory::GenerateEdgeExpandedEdges(const std::string &original_edg
++compressed; ++compressed;
} }
original_edge_data_vector.push_back(OriginalEdgeData( original_edge_data_vector.emplace_back(
(edge_is_compressed ? m_geometry_compressor.GetPositionForID(e1) : v), (edge_is_compressed ? m_geometry_compressor.GetPositionForID(e1) : v),
edge_data1.nameID, edge_data1.nameID,
turn_instruction, turn_instruction,
edge_is_compressed)); edge_is_compressed);
++original_edges_counter; ++original_edges_counter;
@ -706,10 +703,7 @@ int EdgeBasedGraphFactory::GetTurnPenalty(const NodeID u,
// call lua profile to compute turn penalty // call lua profile to compute turn penalty
return luabind::call_function<int>(lua_state, "turn_function", 180. - angle); return luabind::call_function<int>(lua_state, "turn_function", 180. - angle);
} }
catch (const luabind::error &er) catch (const luabind::error &er) { SimpleLogger().Write(logWARNING) << er.what(); }
{
SimpleLogger().Write(logWARNING) << er.what();
}
} }
return 0; return 0;
} }
@ -722,8 +716,8 @@ TurnInstruction EdgeBasedGraphFactory::AnalyzeTurn(const NodeID u, const NodeID
return TurnInstruction::UTurn; return TurnInstruction::UTurn;
} }
const EdgeIterator edge1 = m_node_based_graph->FindEdge(u, v); const EdgeID edge1 = m_node_based_graph->FindEdge(u, v);
const EdgeIterator edge2 = m_node_based_graph->FindEdge(v, w); const EdgeID edge2 = m_node_based_graph->FindEdge(v, w);
const EdgeData &data1 = m_node_based_graph->GetEdgeData(edge1); const EdgeData &data1 = m_node_based_graph->GetEdgeData(edge1);
const EdgeData &data2 = m_node_based_graph->GetEdgeData(edge2); const EdgeData &data2 = m_node_based_graph->GetEdgeData(edge2);

View File

@ -34,32 +34,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../DataStructures/DeallocatingVector.h" #include "../DataStructures/DeallocatingVector.h"
#include "../DataStructures/DynamicGraph.h" #include "../DataStructures/DynamicGraph.h"
#include "../DataStructures/EdgeBasedNode.h" #include "../DataStructures/EdgeBasedNode.h"
#include "../DataStructures/HashTable.h"
#include "../DataStructures/OriginalEdgeData.h" #include "../DataStructures/OriginalEdgeData.h"
#include "../DataStructures/Percent.h"
#include "../DataStructures/QueryEdge.h"
#include "../DataStructures/QueryNode.h" #include "../DataStructures/QueryNode.h"
#include "../DataStructures/TurnInstructions.h" #include "../DataStructures/TurnInstructions.h"
#include "../DataStructures/Restriction.h" #include "../DataStructures/Restriction.h"
#include "../DataStructures/NodeBasedGraph.h" #include "../DataStructures/NodeBasedGraph.h"
#include "../DataStructures/RestrictionMap.h" #include "../DataStructures/RestrictionMap.h"
#include "../Util/LuaUtil.h"
#include "../Util/SimpleLogger.h"
#include "GeometryCompressor.h" #include "GeometryCompressor.h"
#include <boost/noncopyable.hpp>
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp> #include <boost/unordered_set.hpp>
#include <algorithm> #include <algorithm>
#include <iosfwd> #include <iosfwd>
#include <memory>
#include <queue> #include <queue>
#include <string>
#include <vector> #include <vector>
class EdgeBasedGraphFactory : boost::noncopyable struct lua_State;
class EdgeBasedGraphFactory
{ {
public: public:
EdgeBasedGraphFactory() = delete;
EdgeBasedGraphFactory(const EdgeBasedGraphFactory &) = delete;
struct SpeedProfileProperties; struct SpeedProfileProperties;
explicit EdgeBasedGraphFactory(const std::shared_ptr<NodeBasedDynamicGraph> &node_based_graph, explicit EdgeBasedGraphFactory(const std::shared_ptr<NodeBasedDynamicGraph> &node_based_graph,
@ -71,7 +71,7 @@ class EdgeBasedGraphFactory : boost::noncopyable
void Run(const std::string &original_edge_data_filename, void Run(const std::string &original_edge_data_filename,
const std::string &geometry_filename, const std::string &geometry_filename,
lua_State *myLuaState); lua_State *lua_state);
void GetEdgeBasedEdges(DeallocatingVector<EdgeBasedEdge> &edges); void GetEdgeBasedEdges(DeallocatingVector<EdgeBasedEdge> &edges);
@ -79,7 +79,7 @@ class EdgeBasedGraphFactory : boost::noncopyable
TurnInstruction AnalyzeTurn(const NodeID u, const NodeID v, const NodeID w) const; TurnInstruction AnalyzeTurn(const NodeID u, const NodeID v, const NodeID w) const;
int GetTurnPenalty(const NodeID u, const NodeID v, const NodeID w, lua_State *myLuaState) const; int GetTurnPenalty(const NodeID u, const NodeID v, const NodeID w, lua_State *lua_state) const;
unsigned GetNumberOfEdgeBasedNodes() const; unsigned GetNumberOfEdgeBasedNodes() const;
@ -96,8 +96,6 @@ class EdgeBasedGraphFactory : boost::noncopyable
} speed_profile; } speed_profile;
private: private:
typedef NodeBasedDynamicGraph::NodeIterator NodeIterator;
typedef NodeBasedDynamicGraph::EdgeIterator EdgeIterator;
typedef NodeBasedDynamicGraph::EdgeData EdgeData; typedef NodeBasedDynamicGraph::EdgeData EdgeData;
unsigned m_number_of_edge_based_nodes; unsigned m_number_of_edge_based_nodes;
@ -120,10 +118,7 @@ class EdgeBasedGraphFactory : boost::noncopyable
void GenerateEdgeExpandedEdges(const std::string &original_edge_data_filename, void GenerateEdgeExpandedEdges(const std::string &original_edge_data_filename,
lua_State *lua_state); lua_State *lua_state);
void InsertEdgeBasedNode(NodeBasedDynamicGraph::NodeIterator u, void InsertEdgeBasedNode(NodeID u, NodeID v, EdgeID e1, bool belongsToTinyComponent);
NodeBasedDynamicGraph::NodeIterator v,
NodeBasedDynamicGraph::EdgeIterator e1,
bool belongsToTinyComponent);
void FlushVectorToStream(std::ofstream &edge_data_file, void FlushVectorToStream(std::ofstream &edge_data_file,
std::vector<OriginalEdgeData> &original_edge_data_vector) const; std::vector<OriginalEdgeData> &original_edge_data_vector) const;

View File

@ -48,7 +48,7 @@ void GeometryCompressor::IncreaseFreeList()
m_compressed_geometries.resize(m_compressed_geometries.size() + 100); m_compressed_geometries.resize(m_compressed_geometries.size() + 100);
for (unsigned i = 100; i > 0; --i) for (unsigned i = 100; i > 0; --i)
{ {
m_free_list.push_back(current_free_list_maximum); m_free_list.emplace_back(current_free_list_maximum);
++current_free_list_maximum; ++current_free_list_maximum;
} }
} }
@ -60,8 +60,7 @@ bool GeometryCompressor::HasEntryForID(const EdgeID edge_id) const
unsigned GeometryCompressor::GetPositionForID(const EdgeID edge_id) const unsigned GeometryCompressor::GetPositionForID(const EdgeID edge_id) const
{ {
boost::unordered_map<EdgeID, unsigned>::const_iterator map_iterator; auto map_iterator = m_edge_id_to_list_index_map.find(edge_id);
map_iterator = m_edge_id_to_list_index_map.find(edge_id);
BOOST_ASSERT(map_iterator != m_edge_id_to_list_index_map.end()); BOOST_ASSERT(map_iterator != m_edge_id_to_list_index_map.end());
BOOST_ASSERT(map_iterator->second < m_compressed_geometries.size()); BOOST_ASSERT(map_iterator->second < m_compressed_geometries.size());
return map_iterator->second; return map_iterator->second;
@ -77,7 +76,7 @@ void GeometryCompressor::SerializeInternalVector(const std::string &path) const
// write indices array // write indices array
unsigned prefix_sum_of_list_indices = 0; unsigned prefix_sum_of_list_indices = 0;
for (auto &elem : m_compressed_geometries) for (const auto &elem : m_compressed_geometries)
{ {
geometry_out_stream.write((char *)&prefix_sum_of_list_indices, sizeof(unsigned)); geometry_out_stream.write((char *)&prefix_sum_of_list_indices, sizeof(unsigned));
@ -146,8 +145,7 @@ void GeometryCompressor::CompressEdge(const EdgeID edge_id_1,
m_free_list.pop_back(); m_free_list.pop_back();
} }
const boost::unordered_map<EdgeID, unsigned>::const_iterator iter = const auto iter = m_edge_id_to_list_index_map.find(edge_id_1);
m_edge_id_to_list_index_map.find(edge_id_1);
BOOST_ASSERT(iter != m_edge_id_to_list_index_map.end()); BOOST_ASSERT(iter != m_edge_id_to_list_index_map.end());
const unsigned edge_bucket_id1 = iter->second; const unsigned edge_bucket_id1 = iter->second;
BOOST_ASSERT(edge_bucket_id1 == GetPositionForID(edge_id_1)); BOOST_ASSERT(edge_bucket_id1 == GetPositionForID(edge_id_1));
@ -157,7 +155,7 @@ void GeometryCompressor::CompressEdge(const EdgeID edge_id_1,
if (edge_bucket_list1.empty()) if (edge_bucket_list1.empty())
{ {
edge_bucket_list1.push_back(std::make_pair(via_node_id, weight1)); edge_bucket_list1.emplace_back(via_node_id, weight1);
} }
BOOST_ASSERT(0 < edge_bucket_list1.size()); BOOST_ASSERT(0 < edge_bucket_list1.size());
@ -182,13 +180,13 @@ void GeometryCompressor::CompressEdge(const EdgeID edge_id_1,
m_edge_id_to_list_index_map.find(edge_id_2)); m_edge_id_to_list_index_map.find(edge_id_2));
edge_bucket_list2.clear(); edge_bucket_list2.clear();
BOOST_ASSERT(0 == edge_bucket_list2.size()); BOOST_ASSERT(0 == edge_bucket_list2.size());
m_free_list.push_back(list_to_remove_index); m_free_list.emplace_back(list_to_remove_index);
BOOST_ASSERT(list_to_remove_index == m_free_list.back()); BOOST_ASSERT(list_to_remove_index == m_free_list.back());
} }
else else
{ {
// we are certain that the second edge is atomic. // we are certain that the second edge is atomic.
edge_bucket_list1.push_back(std::make_pair(target_node_id, weight2)); edge_bucket_list1.emplace_back(target_node_id, weight2);
} }
} }

View File

@ -27,6 +27,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "TemporaryStorage.h" #include "TemporaryStorage.h"
StreamData::StreamData()
: write_mode(true), temp_path(boost::filesystem::unique_path(temp_directory.append(
TemporaryFilePattern.begin(), TemporaryFilePattern.end()))),
temp_file(new boost::filesystem::fstream(
temp_path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary)),
readWriteMutex(std::make_shared<boost::mutex>())
{
if (temp_file->fail())
{
throw OSRMException("temporary file could not be created");
}
}
TemporaryStorage::TemporaryStorage() { temp_directory = boost::filesystem::temp_directory_path(); } TemporaryStorage::TemporaryStorage() { temp_directory = boost::filesystem::temp_directory_path(); }
TemporaryStorage &TemporaryStorage::GetInstance() TemporaryStorage &TemporaryStorage::GetInstance()
@ -50,14 +63,8 @@ void TemporaryStorage::RemoveAll()
int TemporaryStorage::AllocateSlot() int TemporaryStorage::AllocateSlot()
{ {
boost::mutex::scoped_lock lock(mutex); boost::mutex::scoped_lock lock(mutex);
try try { stream_data_list.push_back(StreamData()); }
{ catch (boost::filesystem::filesystem_error &e) { Abort(e); }
stream_data_list.push_back(StreamData());
}
catch (boost::filesystem::filesystem_error &e)
{
Abort(e);
}
CheckIfTemporaryDeviceFull(); CheckIfTemporaryDeviceFull();
return stream_data_list.size() - 1; return stream_data_list.size() - 1;
} }
@ -79,10 +86,7 @@ void TemporaryStorage::DeallocateSlot(const int slot_id)
boost::filesystem::remove(data.temp_path); boost::filesystem::remove(data.temp_path);
} }
catch (boost::filesystem::filesystem_error &e) catch (boost::filesystem::filesystem_error &e) { Abort(e); }
{
Abort(e);
}
} }
void TemporaryStorage::WriteToSlot(const int slot_id, char *pointer, const std::size_t size) void TemporaryStorage::WriteToSlot(const int slot_id, char *pointer, const std::size_t size)
@ -103,10 +107,7 @@ void TemporaryStorage::WriteToSlot(const int slot_id, char *pointer, const std::
} }
data.buffer.insert(data.buffer.end(), pointer, pointer + size); data.buffer.insert(data.buffer.end(), pointer, pointer + size);
} }
catch (boost::filesystem::filesystem_error &e) catch (boost::filesystem::filesystem_error &e) { Abort(e); }
{
Abort(e);
}
} }
void TemporaryStorage::ReadFromSlot(const int slot_id, char *pointer, const std::size_t size) void TemporaryStorage::ReadFromSlot(const int slot_id, char *pointer, const std::size_t size)
{ {
@ -125,10 +126,7 @@ void TemporaryStorage::ReadFromSlot(const int slot_id, char *pointer, const std:
BOOST_ASSERT(!data.write_mode); BOOST_ASSERT(!data.write_mode);
data.temp_file->read(pointer, size); data.temp_file->read(pointer, size);
} }
catch (boost::filesystem::filesystem_error &e) catch (boost::filesystem::filesystem_error &e) { Abort(e); }
{
Abort(e);
}
} }
uint64_t TemporaryStorage::GetFreeBytesOnTemporaryDevice() uint64_t TemporaryStorage::GetFreeBytesOnTemporaryDevice()
@ -140,10 +138,7 @@ uint64_t TemporaryStorage::GetFreeBytesOnTemporaryDevice()
boost::filesystem::space_info s = boost::filesystem::space(p); boost::filesystem::space_info s = boost::filesystem::space(p);
value = s.free; value = s.free;
} }
catch (boost::filesystem::filesystem_error &e) catch (boost::filesystem::filesystem_error &e) { Abort(e); }
{
Abort(e);
}
return value; return value;
} }
@ -166,10 +161,7 @@ boost::filesystem::fstream::pos_type TemporaryStorage::Tell(const int slot_id)
boost::mutex::scoped_lock lock(*data.readWriteMutex); boost::mutex::scoped_lock lock(*data.readWriteMutex);
position = data.temp_file->tellp(); position = data.temp_file->tellp();
} }
catch (boost::filesystem::filesystem_error &e) catch (boost::filesystem::filesystem_error &e) { Abort(e); }
{
Abort(e);
}
return position; return position;
} }

View File

@ -42,15 +42,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <memory>
/** struct StreamData
* This class implements a singleton file storage for temporary data. {
* temporary slots can be accessed by other objects through an int bool write_mode;
* On deallocation every slot gets deallocated boost::filesystem::path temp_path;
* std::shared_ptr<boost::filesystem::fstream> temp_file;
* Access is sequential, which means, that there is no random access std::shared_ptr<boost::mutex> readWriteMutex;
* -> Data is written in first phase and reread in second. std::vector<char> buffer;
*/
StreamData();
};
// This class implements a singleton file storage for temporary data.
// temporary slots can be accessed by other objects through an int
// On deallocation every slot gets deallocated
//
// Access is sequential, which means, that there is no random access
// -> Data is written in first phase and reread in second.
static boost::filesystem::path temp_directory; static boost::filesystem::path temp_directory;
static std::string TemporaryFilePattern("OSRM-%%%%-%%%%-%%%%"); static std::string TemporaryFilePattern("OSRM-%%%%-%%%%-%%%%");
@ -78,27 +88,6 @@ class TemporaryStorage
void Abort(const boost::filesystem::filesystem_error &e); void Abort(const boost::filesystem::filesystem_error &e);
void CheckIfTemporaryDeviceFull(); void CheckIfTemporaryDeviceFull();
struct StreamData
{
bool write_mode;
boost::filesystem::path temp_path;
std::shared_ptr<boost::filesystem::fstream> temp_file;
std::shared_ptr<boost::mutex> readWriteMutex;
std::vector<char> buffer;
StreamData()
: write_mode(true), temp_path(boost::filesystem::unique_path(temp_directory.append(
TemporaryFilePattern.begin(), TemporaryFilePattern.end()))),
temp_file(new boost::filesystem::fstream(
temp_path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary)),
readWriteMutex(std::make_shared<boost::mutex>())
{
if (temp_file->fail())
{
throw OSRMException("temporary file could not be created");
}
}
};
// vector of file streams that is used to store temporary data // vector of file streams that is used to store temporary data
boost::mutex mutex; boost::mutex mutex;
std::vector<StreamData> stream_data_list; std::vector<StreamData> stream_data_list;