src/work/deba/mappedgraph.h
changeset 261 796101caedb7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/deba/mappedgraph.h	Mon Mar 29 20:34:24 2004 +0000
     1.3 @@ -0,0 +1,88 @@
     1.4 +#ifndef MAPPEDGRAPH_H
     1.5 +#define MAPPEDGRAPH_H
     1.6 +
     1.7 +#include <vector>
     1.8 +
     1.9 +template <typename GB, typename K>
    1.10 +class MapBase;
    1.11 +
    1.12 +template <typename GB>
    1.13 +class MappedGraph : public GB {
    1.14 +public:
    1.15 +	typedef GB GraphBase;
    1.16 +	
    1.17 +	typedef MapBase<GB, typename GB::Edge> EdgeMapBase;
    1.18 +	typedef MapBase<GB, typename GB::Node> NodeMapBase;
    1.19 +
    1.20 +protected:
    1.21 +	typedef std::vector<EdgeMapBase*> EdgeMaps; 
    1.22 +	typedef std::vector<NodeMapBase*> NodeMaps;
    1.23 +	
    1.24 +	EdgeMaps edge_maps;
    1.25 +	NodeMaps node_maps;
    1.26 +	
    1.27 +	void add(EdgeMapBase& map_base) {
    1.28 +		if (map_base.graph) {
    1.29 +			map_base.graph->erase(map_base);
    1.30 +		}
    1.31 +		edge_maps.push_back(&map_base);
    1.32 +		map_base.graph_index = map_base.size()-1;
    1.33 +	} 
    1.34 +	
    1.35 +	void erase(EdgeMapBase& map_base) {
    1.36 +		if (map_base.graph != this) return;
    1.37 +		edge_maps.back()->graph_index = map_base.graph_index; 
    1.38 +		edge_maps[map_base.graph_index] = edge_maps.back();
    1.39 +		edge_maps.pop_back();
    1.40 +		map_base.graph = 0;
    1.41 +	}
    1.42 +	
    1.43 +	void addEdge(typename GB::Edge& edge) {
    1.44 +		typename EdgeMaps::iterator it;
    1.45 +		for (it = edge_maps.begin(); it != edge_maps.end(); ++it) {
    1.46 +			(*it)->add(edge);
    1.47 +		}
    1.48 +	}
    1.49 +	
    1.50 +	void eraseEdge(typename GB::Edge& edge) {
    1.51 +		typename EdgeMaps::iterator it;
    1.52 +		for (it = edge_maps.begin(); it != edge_maps.end(); ++it) {
    1.53 +			(*it)->erase(edge);
    1.54 +		}
    1.55 +	}
    1.56 +
    1.57 +	void add(NodeMapBase& map_base) {
    1.58 +		if (map_base.graph) {
    1.59 +			map_base.graph->erase(map_base);
    1.60 +		}
    1.61 +		node_maps.push_back(&map_base);
    1.62 +		map_base.graph_index = node_maps.size()-1;
    1.63 +	} 
    1.64 +	
    1.65 +	void erase(NodeMapBase& map_base) {
    1.66 +		if (map_base.graph != this) return;
    1.67 +		node_maps.back()->graph_index = map_base.graph_index; 
    1.68 +		node_maps[map_base.graph_index] = node_maps.back();
    1.69 +		node_maps.pop_back();
    1.70 +		map_base.graph = 0;
    1.71 +	}
    1.72 +
    1.73 +	void addNode(typename GB::Node& node) {
    1.74 +		typename NodeMaps::iterator it;
    1.75 +		for (it = node_maps.begin(); it != node_maps.end(); ++it) {
    1.76 +			(*it)->add(node);
    1.77 +		}
    1.78 +	}
    1.79 +	
    1.80 +	void eraseNode(typename GB::Node& node) {
    1.81 +		typename NodeMaps::iterator it;
    1.82 +		for (it = node_maps.begin(); it != node_maps.end(); ++it) {
    1.83 +			(*it)->erase(node);
    1.84 +		}
    1.85 +	}
    1.86 +	
    1.87 +	friend class EdgeMapBase;
    1.88 +	friend class NodeMapBase;
    1.89 +};
    1.90 +
    1.91 +#endif