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