src/work/deba/edge_map_base.h
changeset 359 8cc53a6b1e61
parent 336 8ff3b3e05478
child 377 33fe0ee01dc5
equal deleted inserted replaced
1:5fdeb94210e0 2:0c18d6511c96
     1 #ifndef EDGE_MAP_BASE_H
     1 #ifndef EDGE_MAP_BASE_H
     2 #define EDGE_MAP_BASE_H
     2 #define EDGE_MAP_BASE_H
     3 
     3 
     4 template <class G, class K>
     4 /**
       
     5 	Template base class for implementing mapping on edges.
       
     6 	\param The first template parameter is the Graph class. The Graph
       
     7 		must have an \emp edge_maps member with \emp EdgeMapRegistry class.
       
     8 	\param The second template parameter is the Edge type of the class.
       
     9 	
       
    10 */
       
    11 
       
    12 template <typename G, typename K>
     5 class EdgeMapBase {
    13 class EdgeMapBase {
     6 public:
    14 public:
     7 	typedef G Graph;
    15 	typedef G Graph;
     8 
    16 
     9 	typedef K KeyType;
    17 	typedef K KeyType;
    10 	
    18 	
       
    19 	/** 
       
    20 		Default constructor.
       
    21 	*/	
       
    22 	
    11 	EdgeMapBase() : graph(0) {}
    23 	EdgeMapBase() : graph(0) {}
       
    24 
       
    25 	/** 
       
    26 		Simple constructor to register into a graph.
       
    27 	*/
       
    28 	
    12 	EdgeMapBase(Graph& g) : graph(&g) {
    29 	EdgeMapBase(Graph& g) : graph(&g) {
    13 		graph->edge_maps.add(*this);
    30 		graph->edge_maps.add(*this);
    14 	}
    31 	}
       
    32 
       
    33 	/** 
       
    34 		Copy constructor with registering into the map.
       
    35 	*/	
       
    36 	
       
    37 	EdgeMapBase(const EdgeMapBase& copy) : graph(copy.graph) {
       
    38 		if (graph) {
       
    39 			graph->edge_maps.add(*this);
       
    40 		}
       
    41 	} 
       
    42 	
       
    43 	/** 
       
    44 		Assign operator.
       
    45 	*/	
       
    46 
       
    47 	const EdgeMapBase& operator=(const EdgeMapBase& copy) {
       
    48 		if (graph) {
       
    49 			graph.edge_maps.erase(*this);
       
    50 		}
       
    51 		graph = copy.graph;
       
    52 		if (graph) {
       
    53 			graph->edge_maps.add(*this);
       
    54 		}
       
    55 		
       
    56 	}
       
    57 	
       
    58 
       
    59 	/** 
       
    60 		Destructor.
       
    61 	*/	
    15 
    62 
    16 	virtual ~EdgeMapBase() {
    63 	virtual ~EdgeMapBase() {
    17 		if (graph) {
    64 		if (graph) {
    18 			graph.edge_maps.erase(*this);
    65 			graph.edge_maps.erase(*this);
    19 		}
    66 		}
    23 	
    70 	
    24 	Graph* graph;
    71 	Graph* graph;
    25 
    72 
    26 	int graph_index;
    73 	int graph_index;
    27 	
    74 	
       
    75 	/**
       
    76 		Helper function to implement the default constructor in the subclasses.
       
    77 	*/
       
    78 	
    28 	void init() {
    79 	void init() {
    29 		for (Graph::EdgeIt it(g); g.valid(it); g.next(it)) {
    80 		for (Graph::EdgeIt it(g); g.valid(it); g.next(it)) {
    30 			add(it);
    81 			add(it);
    31 		}
    82 		}
    32 	}
    83 	}
       
    84 	
       
    85 	/**
       
    86 		Helper function to implement the destructor in the subclasses.
       
    87 	*/
    33 	
    88 	
    34 	void destroy() {
    89 	void destroy() {
    35 		for (Graph::EdgeIt it(g); g.valid(it); g.next(it)) {
    90 		for (Graph::EdgeIt it(g); g.valid(it); g.next(it)) {
    36 			erase(it);
    91 			erase(it);
    37 		}
    92 		}
    38 	}
    93 	}
    39 	
    94 	
       
    95 	/** 
       
    96 		The add member function should be overloaded in the subclasses.
       
    97 		\e Add extends the map with the new edge.
       
    98 	*/
       
    99 	
    40 	virtual void add(const KeyType&) = 0;
   100 	virtual void add(const KeyType&) = 0;
       
   101 	
       
   102 	/** 
       
   103 		The erase member function should be overloaded in the subclasses.
       
   104 		\e Erase removes the edge from the map.
       
   105 	*/
       
   106 	
    41 	virtual void erase(const KeyType&) = 0;
   107 	virtual void erase(const KeyType&) = 0;
       
   108 	
       
   109 	/**
       
   110 		Exception class to throw at unsupported operation.
       
   111 	*/
       
   112 	
       
   113 	class NotSupportedOperationException {};
    42 
   114 
    43 	friend class Graph;
   115 	friend class Graph;
    44 };
   116 };
    45 
   117 
    46 #endif
   118 #endif