Add generic graph serialization
This commit is contained in:
		
							parent
							
								
									4e3009260c
								
							
						
					
					
						commit
						5ed686a17b
					
				| @ -17,8 +17,28 @@ | |||||||
| 
 | 
 | ||||||
| namespace osrm | namespace osrm | ||||||
| { | { | ||||||
|  | namespace storage | ||||||
|  | { | ||||||
|  | namespace io | ||||||
|  | { | ||||||
|  | class FileReader; | ||||||
|  | class FileWriter; | ||||||
|  | } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| namespace util | namespace util | ||||||
| { | { | ||||||
|  | template <typename EdgeDataT> class DynamicGraph; | ||||||
|  | 
 | ||||||
|  | namespace serialization | ||||||
|  | { | ||||||
|  | template <typename EdgeDataT, bool UseSharedMemory> | ||||||
|  | void read(storage::io::FileReader &reader, DynamicGraph<EdgeDataT> &graph); | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT, bool UseSharedMemory> | ||||||
|  | void write(storage::io::FileWriter &writer, | ||||||
|  |            const DynamicGraph<EdgeDataT> &graph); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| template <typename EdgeDataT> class DynamicGraph | template <typename EdgeDataT> class DynamicGraph | ||||||
| { | { | ||||||
|  | |||||||
							
								
								
									
										59
									
								
								include/util/serialization.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								include/util/serialization.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,59 @@ | |||||||
|  | #ifndef OSMR_UTIL_SERIALIZATION_HPP | ||||||
|  | #define OSMR_UTIL_SERIALIZATION_HPP | ||||||
|  | 
 | ||||||
|  | #include "util/static_graph.hpp" | ||||||
|  | #include "util/dynamic_graph.hpp" | ||||||
|  | #include "storage/io.hpp" | ||||||
|  | 
 | ||||||
|  | namespace osrm | ||||||
|  | { | ||||||
|  | namespace util | ||||||
|  | { | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT, storage::Ownership Ownership> | ||||||
|  | inline void read(storage::io::FileReader &reader, | ||||||
|  |                  StaticGraph<EdgeDataT, Ownership> &graph) | ||||||
|  | { | ||||||
|  |     reader.DeserializeVector(graph.node_array); | ||||||
|  |     reader.DeserializeVector(graph.edge_array); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT, storage::Ownership Ownership> | ||||||
|  | inline void write(storage::io::FileWriter &writer, | ||||||
|  |                  StaticGraph<EdgeDataT, Ownership> &graph) | ||||||
|  | { | ||||||
|  |     writer.SerializeVector(graph.node_array); | ||||||
|  |     writer.SerializeVector(graph.edge_array); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT> | ||||||
|  | inline void read(storage::io::FileReader &reader, | ||||||
|  |                  DynamicGraph<EdgeDataT> &graph) | ||||||
|  | { | ||||||
|  |     reader.DeserializeVector(graph.node_array); | ||||||
|  |     auto num_edges = reader.ReadElementCount64(); | ||||||
|  |     graph.edge_list.resize(num_edges); | ||||||
|  |     for (auto index : irange<std::size_t>(0, num_edges)) | ||||||
|  |     { | ||||||
|  |         reader.ReadOne(graph.edge_list[index]); | ||||||
|  |     } | ||||||
|  |     graph.number_of_nodes = graph.node_array.size(); | ||||||
|  |     graph.number_of_edges = num_edges; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT> | ||||||
|  | inline void write(storage::io::FileWriter &writer, | ||||||
|  |                  DynamicGraph<EdgeDataT> &graph) | ||||||
|  | { | ||||||
|  |     writer.SerializeVector(graph.node_array); | ||||||
|  |     writer.WriteElementCount64(graph.number_of_edges); | ||||||
|  |     for (auto index : irange<std::size_t>(0, graph.number_of_edges)) | ||||||
|  |     { | ||||||
|  |         writer.WriteOne(graph.edge_list[index]); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
| @ -19,8 +19,28 @@ | |||||||
| 
 | 
 | ||||||
| namespace osrm | namespace osrm | ||||||
| { | { | ||||||
|  | 
 | ||||||
|  | namespace storage | ||||||
|  | { | ||||||
|  | namespace io | ||||||
|  | { | ||||||
|  | class FileReader; | ||||||
|  | class FileWriter; | ||||||
|  | } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| namespace util | namespace util | ||||||
| { | { | ||||||
|  | template <typename EdgeDataT, storage::Ownership Ownership> class StaticGraph; | ||||||
|  | 
 | ||||||
|  | namespace serialization | ||||||
|  | { | ||||||
|  | template <typename EdgeDataT, storage::Ownership Ownership> | ||||||
|  | void read(storage::io::FileReader &reader, StaticGraph<EdgeDataT, Ownership> &graph); | ||||||
|  | 
 | ||||||
|  | template <typename EdgeDataT, storage::Ownership Ownership> | ||||||
|  | void write(storage::io::FileWriter &writer, const StaticGraph<EdgeDataT, Ownership> &graph); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| namespace static_graph_details | namespace static_graph_details | ||||||
| { | { | ||||||
| @ -102,6 +122,8 @@ EntryT edgeToEntry(const OtherEdge &from, std::false_type) | |||||||
| template <typename EdgeDataT, storage::Ownership Ownership = storage::Ownership::Container> | template <typename EdgeDataT, storage::Ownership Ownership = storage::Ownership::Container> | ||||||
| class StaticGraph | class StaticGraph | ||||||
| { | { | ||||||
|  |     template <typename T> using Vector = typename util::ShM<T, Ownership>::vector; | ||||||
|  | 
 | ||||||
|   public: |   public: | ||||||
|     using InputEdge = static_graph_details::SortableEdgeWithData<EdgeDataT>; |     using InputEdge = static_graph_details::SortableEdgeWithData<EdgeDataT>; | ||||||
|     using NodeIterator = static_graph_details::NodeIterator; |     using NodeIterator = static_graph_details::NodeIterator; | ||||||
| @ -125,8 +147,7 @@ class StaticGraph | |||||||
|         InitializeFromSortedEdgeRange(nodes, edges.begin(), edges.end()); |         InitializeFromSortedEdgeRange(nodes, edges.begin(), edges.end()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     StaticGraph(typename util::ShM<NodeArrayEntry, Ownership>::vector node_array_, |     StaticGraph(Vector<NodeArrayEntry> node_array_, Vector<EdgeArrayEntry> edge_array_) | ||||||
|                 typename util::ShM<EdgeArrayEntry, Ownership>::vector edge_array_) |  | ||||||
|         : node_array(std::move(node_array_)), edge_array(std::move(edge_array_)) |         : node_array(std::move(node_array_)), edge_array(std::move(edge_array_)) | ||||||
|     { |     { | ||||||
|         BOOST_ASSERT(!node_array.empty()); |         BOOST_ASSERT(!node_array.empty()); | ||||||
| @ -229,8 +250,12 @@ class StaticGraph | |||||||
|         return current_iterator; |         return current_iterator; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const NodeArrayEntry &GetNode(const NodeID nid) const { return node_array[nid]; } |     friend void serialization::read<EdgeDataT, Ownership>(storage::io::FileReader &reader, | ||||||
|     const EdgeArrayEntry &GetEdge(const EdgeID eid) const { return edge_array[eid]; } |                                                           StaticGraph<EdgeDataT, Ownership> &graph); | ||||||
|  |     friend void | ||||||
|  |     serialization::write<EdgeDataT, Ownership>(storage::io::FileWriter &writer, | ||||||
|  |                                                const StaticGraph<EdgeDataT, Ownership> &graph); | ||||||
|  | 
 | ||||||
|   protected: |   protected: | ||||||
|     template <typename IterT> |     template <typename IterT> | ||||||
|     void InitializeFromSortedEdgeRange(const unsigned nodes, IterT begin, IterT end) |     void InitializeFromSortedEdgeRange(const unsigned nodes, IterT begin, IterT end) | ||||||
| @ -261,8 +286,8 @@ class StaticGraph | |||||||
|     NodeIterator number_of_nodes; |     NodeIterator number_of_nodes; | ||||||
|     EdgeIterator number_of_edges; |     EdgeIterator number_of_edges; | ||||||
| 
 | 
 | ||||||
|     typename ShM<NodeArrayEntry, Ownership>::vector node_array; |     Vector<NodeArrayEntry> node_array; | ||||||
|     typename ShM<EdgeArrayEntry, Ownership>::vector edge_array; |     Vector<EdgeArrayEntry> edge_array; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } // namespace util
 | } // namespace util
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user