src/lemon/vector_map.h
changeset 984 f7538f6f4c61
parent 979 b5fb023cdb7b
child 987 87f7c54892df
equal deleted inserted replaced
5:a74593f7ed69 6:3466d6e34842
    44   /// \author Balazs Dezso
    44   /// \author Balazs Dezso
    45   	
    45   	
    46 
    46 
    47   template <typename _Graph, 
    47   template <typename _Graph, 
    48 	    typename _Item,
    48 	    typename _Item,
    49 	    typename _IdMap,
       
    50 	    typename _Value>
    49 	    typename _Value>
    51   class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase {
    50   class VectorMap : public AlterationObserverRegistry<_Item>::ObserverBase {
    52   public:
    51   public:
    53 		
    52 		
    54     /// The graph type of the map. 
    53     /// The graph type of the map. 
    55     typedef _Graph Graph;
    54     typedef _Graph Graph;
    56     /// The key type of the map.
    55     /// The key type of the map.
    57     typedef _Item KeyType;
    56     typedef _Item KeyType;
    58     /// The id map type of the map.
    57     /// The id map type of the map.
    59     typedef _IdMap IdMap;
       
    60     /// The registry type of the map.
       
    61     typedef AlterationObserverRegistry<_Item> Registry;
    58     typedef AlterationObserverRegistry<_Item> Registry;
    62     /// The value type of the map.
    59     /// The value type of the map.
    63     typedef _Value Value;
    60     typedef _Value Value;
    64 
    61 
    65     /// The map type.
    62     /// The map type.
    91     /// Constructor to attach the new map into the registry.
    88     /// Constructor to attach the new map into the registry.
    92 
    89 
    93     /// It construates a map and attachs it into the registry.
    90     /// It construates a map and attachs it into the registry.
    94     /// It adds all the items of the graph to the map.
    91     /// It adds all the items of the graph to the map.
    95      
    92      
    96     VectorMap(const Graph& _g, Registry& _r) : graph(&_g) {
    93     VectorMap(const Graph& _g) : graph(&_g) {
    97       attach(_r);
    94       attach(_g.getObserverRegistry(_Item()));
    98       build();
    95       build();
    99     }
    96     }
   100 
    97 
   101     /// Constructor uses given value to initialize the map. 
    98     /// Constructor uses given value to initialize the map. 
   102 
    99 
   103     /// It construates a map uses a given value to initialize the map. 
   100     /// It construates a map uses a given value to initialize the map. 
   104     /// It adds all the items of the graph to the map.
   101     /// It adds all the items of the graph to the map.
   105      
   102      
   106     VectorMap(const Graph& g, Registry& r, const Value& v) : graph(&g) { 
   103     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
   107       attach(r);
   104       attach(_g.getObserverRegistry(_Item()));
   108       container.resize(IdMap(*graph).maxId() + 1, v);
   105       container.resize(graph->maxId(_Item()) + 1, _v);
   109     }
   106     }
   110 
   107 
   111     VectorMap(const VectorMap& copy) : graph(copy.getGraph()) {
   108     VectorMap(const VectorMap& _copy) : graph(_copy.getGraph()) {
   112       if (copy.attached()) {
   109       if (_copy.attached()) {
   113 	attach(*copy.getRegistry());
   110 	attach(*_copy.getRegistry());
   114 	container = copy.container;
   111 	container = _copy.container;
   115       }
   112       }
   116     }
   113     }
   117 
   114 
   118     using Parent::attach;
   115     using Parent::attach;
   119     using Parent::detach;
   116     using Parent::detach;
   152 
   149 
   153     /// The subscript operator. The map can be subscripted by the
   150     /// The subscript operator. The map can be subscripted by the
   154     /// actual items of the graph. 
   151     /// actual items of the graph. 
   155      
   152      
   156     ReferenceType operator[](const KeyType& key) {
   153     ReferenceType operator[](const KeyType& key) {
   157       return container[IdMap(*graph)[key]];
   154       return container[graph->id(key)];
   158     } 
   155     } 
   159 		
   156 		
   160     /// The const subcript operator.
   157     /// The const subcript operator.
   161 
   158 
   162     /// The const subscript operator. The map can be subscripted by the
   159     /// The const subscript operator. The map can be subscripted by the
   163     /// actual items of the graph. 
   160     /// actual items of the graph. 
   164      
   161      
   165     ConstReferenceType operator[](const KeyType& key) const {
   162     ConstReferenceType operator[](const KeyType& key) const {
   166       return container[IdMap(*graph)[key]];
   163       return container[graph->id(key)];
   167     }
   164     }
   168 
   165 
   169 
   166 
   170     /// The setter function of the map.
   167     /// The setter function of the map.
   171 
   168 
   180 		
   177 		
   181     /// It adds a new key to the map. It called by the observer registry
   178     /// It adds a new key to the map. It called by the observer registry
   182     /// and it overrides the add() member function of the observer base.
   179     /// and it overrides the add() member function of the observer base.
   183      
   180      
   184     void add(const KeyType& key) {
   181     void add(const KeyType& key) {
   185       int id = IdMap(*graph)[key];
   182       int id = graph->id(key);
   186       if (id >= (int)container.size()) {
   183       if (id >= (int)container.size()) {
   187 	container.resize(id + 1);
   184 	container.resize(id + 1);
   188       }
   185       }
   189     }
   186     }
   190 
   187 
   198 		
   195 		
   199     /// It buildes the map. It called by the observer registry
   196     /// It buildes the map. It called by the observer registry
   200     /// and it overrides the build() member function of the observer base.
   197     /// and it overrides the build() member function of the observer base.
   201 
   198 
   202     void build() { 
   199     void build() { 
   203       container.resize(IdMap(*graph).maxId() + 1);
   200       container.resize(graph->maxId(_Item()) + 1);
   204     }
   201     }
   205 
   202 
   206     /// Clear the map.
   203     /// Clear the map.
   207 
   204 
   208     /// It erase all items from the map. It called by the observer registry
   205     /// It erase all items from the map. It called by the observer registry
   237     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   234     typedef typename Parent::EdgeObserverRegistry EdgeObserverRegistry;
   238 
   235 
   239     
   236     
   240 
   237 
   241     template <typename _Value>
   238     template <typename _Value>
   242     class NodeMap : public VectorMap<Graph, Node, NodeIdMap, _Value> {
   239     class NodeMap : public VectorMap<Graph, Node, _Value> {
   243     public:
   240     public:
   244       typedef VectorMappableGraphExtender<_Base> Graph;
   241       typedef VectorMappableGraphExtender<_Base> Graph;
   245 
   242 
   246       typedef typename Graph::Node Node;
   243       typedef typename Graph::Node Node;
   247       typedef typename Graph::NodeIdMap NodeIdMap;
   244 
   248 
   245       typedef VectorMap<Graph, Node, _Value> Parent;
   249       typedef VectorMap<Graph, Node, NodeIdMap, _Value> Parent;
       
   250 
   246 
   251       //typedef typename Parent::Graph Graph;
   247       //typedef typename Parent::Graph Graph;
   252       typedef typename Parent::Value Value;
   248       typedef typename Parent::Value Value;
   253 
   249 
   254       NodeMap(const Graph& g) 
   250       NodeMap(const Graph& g) 
   255 	: Parent(g, g.getNodeObserverRegistry()) {}
   251 	: Parent(g) {}
   256       NodeMap(const Graph& g, const Value& v) 
   252       NodeMap(const Graph& g, const Value& v) 
   257 	: Parent(g, g.getNodeObserverRegistry(), v) {}
   253 	: Parent(g, v) {}
   258 
   254 
   259     };
   255     };
   260 
   256 
   261     template <typename _Value>
   257     template <typename _Value>
   262     class EdgeMap : public VectorMap<Graph, Edge, EdgeIdMap, _Value> {
   258     class EdgeMap : public VectorMap<Graph, Edge, _Value> {
   263     public:
   259     public:
   264       typedef VectorMappableGraphExtender<_Base> Graph;
   260       typedef VectorMappableGraphExtender<_Base> Graph;
   265 
   261 
   266       typedef typename Graph::Edge Edge;
   262       typedef typename Graph::Edge Edge;
   267       typedef typename Graph::EdgeIdMap EdgeIdMap;
   263 
   268 
   264       typedef VectorMap<Graph, Edge, _Value> Parent;
   269       typedef VectorMap<Graph, Edge, EdgeIdMap, _Value> Parent;
       
   270 
   265 
   271       //typedef typename Parent::Graph Graph;
   266       //typedef typename Parent::Graph Graph;
   272       typedef typename Parent::Value Value;
   267       typedef typename Parent::Value Value;
   273 
   268 
   274       EdgeMap(const Graph& g) 
   269       EdgeMap(const Graph& g) 
   275 	: Parent(g, g.getEdgeObserverRegistry()) {}
   270 	: Parent(g) {}
   276       EdgeMap(const Graph& g, const Value& v) 
   271       EdgeMap(const Graph& g, const Value& v) 
   277 	: Parent(g, g.getEdgeObserverRegistry(), v) {}
   272 	: Parent(g, v) {}
   278 
   273 
   279     };
   274     };
   280     
   275     
   281   };
   276   };
   282   
   277