src/work/deba/mappedgraph.h
changeset 315 7b97540cd743
equal deleted inserted replaced
-1:000000000000 0:45cf239a2944
       
     1 #ifndef MAPPEDGRAPH_H
       
     2 #define MAPPEDGRAPH_H
       
     3 
       
     4 #include <vector>
       
     5 
       
     6 template <typename GB, typename K>
       
     7 class MapBase;
       
     8 
       
     9 template <typename GB>
       
    10 class MappedGraph : public GB {
       
    11 public:
       
    12 	typedef GB GraphBase;
       
    13 	
       
    14 	typedef MapBase<GB, typename GB::Edge> EdgeMapBase;
       
    15 	typedef MapBase<GB, typename GB::Node> NodeMapBase;
       
    16 
       
    17 protected:
       
    18 	typedef std::vector<EdgeMapBase*> EdgeMaps; 
       
    19 	typedef std::vector<NodeMapBase*> NodeMaps;
       
    20 	
       
    21 	EdgeMaps edge_maps;
       
    22 	NodeMaps node_maps;
       
    23 	
       
    24 	void add(EdgeMapBase& map_base) {
       
    25 		if (map_base.graph) {
       
    26 			map_base.graph->erase(map_base);
       
    27 		}
       
    28 		edge_maps.push_back(&map_base);
       
    29 		map_base.graph_index = map_base.size()-1;
       
    30 	} 
       
    31 	
       
    32 	void erase(EdgeMapBase& map_base) {
       
    33 		if (map_base.graph != this) return;
       
    34 		edge_maps.back()->graph_index = map_base.graph_index; 
       
    35 		edge_maps[map_base.graph_index] = edge_maps.back();
       
    36 		edge_maps.pop_back();
       
    37 		map_base.graph = 0;
       
    38 	}
       
    39 	
       
    40 	void addEdge(typename GB::Edge& edge) {
       
    41 		typename EdgeMaps::iterator it;
       
    42 		for (it = edge_maps.begin(); it != edge_maps.end(); ++it) {
       
    43 			(*it)->add(edge);
       
    44 		}
       
    45 	}
       
    46 	
       
    47 	void eraseEdge(typename GB::Edge& edge) {
       
    48 		typename EdgeMaps::iterator it;
       
    49 		for (it = edge_maps.begin(); it != edge_maps.end(); ++it) {
       
    50 			(*it)->erase(edge);
       
    51 		}
       
    52 	}
       
    53 
       
    54 	void add(NodeMapBase& map_base) {
       
    55 		if (map_base.graph) {
       
    56 			map_base.graph->erase(map_base);
       
    57 		}
       
    58 		node_maps.push_back(&map_base);
       
    59 		map_base.graph_index = node_maps.size()-1;
       
    60 	} 
       
    61 	
       
    62 	void erase(NodeMapBase& map_base) {
       
    63 		if (map_base.graph != this) return;
       
    64 		node_maps.back()->graph_index = map_base.graph_index; 
       
    65 		node_maps[map_base.graph_index] = node_maps.back();
       
    66 		node_maps.pop_back();
       
    67 		map_base.graph = 0;
       
    68 	}
       
    69 
       
    70 	void addNode(typename GB::Node& node) {
       
    71 		typename NodeMaps::iterator it;
       
    72 		for (it = node_maps.begin(); it != node_maps.end(); ++it) {
       
    73 			(*it)->add(node);
       
    74 		}
       
    75 	}
       
    76 	
       
    77 	void eraseNode(typename GB::Node& node) {
       
    78 		typename NodeMaps::iterator it;
       
    79 		for (it = node_maps.begin(); it != node_maps.end(); ++it) {
       
    80 			(*it)->erase(node);
       
    81 		}
       
    82 	}
       
    83 	
       
    84 	friend class EdgeMapBase;
       
    85 	friend class NodeMapBase;
       
    86 };
       
    87 
       
    88 #endif