src/work/deba/test_graph.h
changeset 375 d9a58896ab43
parent 262 60de0f16a4a1
child 377 33fe0ee01dc5
equal deleted inserted replaced
0:afa36c3e246b 1:09cd7acd7ec9
     4 
     4 
     5 #include <iostream>
     5 #include <iostream>
     6 #include <vector>
     6 #include <vector>
     7 
     7 
     8 #include <invalid.h>
     8 #include <invalid.h>
       
     9 
       
    10 #include "vector_map.h"
       
    11 #include "edge_map_registry.h"
       
    12 #include "node_map_registry.h"
       
    13 #include "edge_map_base.h"
       
    14 #include "node_map_base.h"
     9 
    15 
    10 namespace hugo {
    16 namespace hugo {
    11 
    17 
    12   template <typename It>
    18   template <typename It>
    13   int count(It it) { 
    19   int count(It it) { 
    26     class EdgeIt;
    32     class EdgeIt;
    27     class OutEdgeIt;
    33     class OutEdgeIt;
    28     class InEdgeIt;
    34     class InEdgeIt;
    29     class SymEdgeIt;
    35     class SymEdgeIt;
    30     
    36     
    31     template <typename T> class NodeMap;
    37 //    template <typename T> class NodeMap;
    32     template <typename T> class EdgeMap;
    38 //    template <typename T> class EdgeMap;
    33   private:
    39   private:
    34     template <typename T> friend class NodeMap;
    40 //    template <typename T> friend class NodeMap;
    35     template <typename T> friend class EdgeMap;
    41  //   template <typename T> friend class EdgeMap;
    36  
    42  
       
    43 		NodeMapRegistry<ListGraph, Node> node_maps;
       
    44 
    37     template <typename T>
    45     template <typename T>
    38     class NodeMap {
    46     class NodeMap : public VectorMap<ListGraph, Edge, T, EdgeMapBase> {};
    39       const ListGraph& G; 
    47 		
    40       std::vector<T> container;
    48 		EdgeMapRegistry<ListGraph, Edge> edge_maps;
    41     public:
       
    42       typedef T ValueType;
       
    43       typedef Node KeyType;
       
    44       NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { }
       
    45       NodeMap(const ListGraph& _G, T a) : 
       
    46 	G(_G), container(G.node_id, a) { }
       
    47       void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; }
       
    48       T get(Node n) const { return container[/*G.id(n)*/n.node->id]; }
       
    49       typename std::vector<T>::reference operator[](Node n) { 
       
    50 	return container[/*G.id(n)*/n.node->id]; }
       
    51       typename std::vector<T>::const_reference operator[](Node n) const { 
       
    52 	return container[/*G.id(n)*/n.node->id]; 
       
    53       }
       
    54       void update() { container.resize(G.node_id); }
       
    55       void update(T a) { container.resize(G.node_id, a); }
       
    56     };
       
    57 
    49 
    58     template <typename T>
    50     template <typename T>
    59     class EdgeMap {
    51     class EdgeMap : public VectorMap<Graph, Node, T, NodeMapBase> {};
    60       const ListGraph& G; 
    52 
    61       std::vector<T> container;
       
    62     public:
       
    63       typedef T ValueType;
       
    64       typedef Edge KeyType;
       
    65       EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
       
    66       EdgeMap(const ListGraph& _G, T a) : 
       
    67 	G(_G), container(G.edge_id, a) { }
       
    68       void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
       
    69       T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
       
    70       typename std::vector<T>::reference operator[](Edge e) { 
       
    71 	return container[/*G.id(e)*/e.edge->id]; } 
       
    72       typename std::vector<T>::const_reference operator[](Edge e) const { 
       
    73 	return container[/*G.id(e)*/e.edge->id]; 
       
    74       } 
       
    75       void update() { container.resize(G.edge_id); }
       
    76       void update(T a) { container.resize(G.edge_id, a); }
       
    77     };
       
    78 
    53 
    79     int node_id;
    54     int node_id;
    80     int edge_id;
    55     int edge_id;
    81     int _node_num;
    56     int _node_num;
    82     int _edge_num;
    57     int _edge_num;
   321     int id(Node v) const { return v.node->id; }
   296     int id(Node v) const { return v.node->id; }
   322     int id(Edge e) const { return e.edge->id; }
   297     int id(Edge e) const { return e.edge->id; }
   323 
   298 
   324     /* adding nodes and edges */
   299     /* adding nodes and edges */
   325 
   300 
   326     Node addNode() { return Node(_add_node()); }
   301     Node addNode() { 
       
   302 			Node n = _add_node();
       
   303 			node_maps.add(n);
       
   304 			return n; 
       
   305 		}
   327     Edge addEdge(Node u, Node v) {
   306     Edge addEdge(Node u, Node v) {
   328       return Edge(_add_edge(u.node, v.node)); 
   307 			Edge e = _add_edge(u.node, v.node);
       
   308 			edge_maps.add(e);
       
   309       return e; 
   329     }
   310     }
   330 
   311 
   331     void erase(Node i) { 
   312     void erase(Node i) { 
       
   313 			node_map.erase(i);
   332       while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
   314       while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
   333       while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
   315       while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
   334       _delete_node(i.node); 
   316       _delete_node(i.node); 
   335     }
   317     }
   336   
   318   
   337     void erase(Edge e) { _delete_edge(e.edge); }
   319     void erase(Edge e) { 
       
   320 			edge_maps.erase(e);
       
   321 			_delete_edge(e.edge); 
       
   322 		}
   338 
   323 
   339     void clear() { 
   324     void clear() { 
   340       while (first<NodeIt>().valid()) erase(first<NodeIt>());
   325       while (first<NodeIt>().valid()) erase(first<NodeIt>());
   341     }
   326     }
   342 
   327