lemon/bits/default_map.h
changeset 1820 22099ef840d7
parent 1703 eb90e3d6bddc
child 1842 8abf74160dc4
equal deleted inserted replaced
4:16d90c16ae42 5:9a45db00d6d0
   264     };
   264     };
   265 
   265 
   266 
   266 
   267   };
   267   };
   268 
   268 
       
   269 
       
   270   template <typename _Base>
       
   271   class MappableUndirBipartiteGraphExtender : public _Base {
       
   272   public:
       
   273 
       
   274     typedef _Base Parent;
       
   275     typedef MappableUndirBipartiteGraphExtender Graph;
       
   276 
       
   277     typedef typename Parent::Node Node;
       
   278     typedef typename Parent::UpperNode UpperNode;
       
   279     typedef typename Parent::LowerNode LowerNode;
       
   280     typedef typename Parent::Edge Edge;
       
   281     typedef typename Parent::UndirEdge UndirEdge;
       
   282     
       
   283     template <typename _Value>
       
   284     class UpperNodeMap 
       
   285       : public IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > {
       
   286     public:
       
   287       typedef MappableUndirBipartiteGraphExtender Graph;
       
   288       typedef IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > 
       
   289       Parent;
       
   290     
       
   291       UpperNodeMap(const Graph& _g) 
       
   292 	: Parent(_g) {}
       
   293       UpperNodeMap(const Graph& _g, const _Value& _v) 
       
   294 	: Parent(_g, _v) {}
       
   295     
       
   296       UpperNodeMap& operator=(const UpperNodeMap& cmap) {
       
   297 	return operator=<UpperNodeMap>(cmap);
       
   298       }
       
   299     
       
   300 
       
   301       /// \brief Template assign operator.
       
   302       ///
       
   303       /// The given parameter should be conform to the ReadMap
       
   304       /// concept and could be indiced by the current item set of
       
   305       /// the UpperNodeMap. In this case the value for each item
       
   306       /// is assigned by the value of the given ReadMap. 
       
   307       template <typename CMap>
       
   308       UpperNodeMap& operator=(const CMap& cmap) {
       
   309 	checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
       
   310 	const typename Parent::Graph* graph = Parent::getGraph();
       
   311 	UpperNode it;
       
   312 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   313 	  Parent::set(it, cmap[it]);
       
   314 	}
       
   315 	return *this;
       
   316       }
       
   317     
       
   318     };
       
   319 
       
   320     template <typename _Value>
       
   321     class LowerNodeMap 
       
   322       : public IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > {
       
   323     public:
       
   324       typedef MappableUndirBipartiteGraphExtender Graph;
       
   325       typedef IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > 
       
   326       Parent;
       
   327     
       
   328       LowerNodeMap(const Graph& _g) 
       
   329 	: Parent(_g) {}
       
   330       LowerNodeMap(const Graph& _g, const _Value& _v) 
       
   331 	: Parent(_g, _v) {}
       
   332     
       
   333       LowerNodeMap& operator=(const LowerNodeMap& cmap) {
       
   334 	return operator=<LowerNodeMap>(cmap);
       
   335       }
       
   336     
       
   337 
       
   338       /// \brief Template assign operator.
       
   339       ///
       
   340       /// The given parameter should be conform to the ReadMap
       
   341       /// concept and could be indiced by the current item set of
       
   342       /// the LowerNodeMap. In this case the value for each item
       
   343       /// is assigned by the value of the given ReadMap. 
       
   344       template <typename CMap>
       
   345       LowerNodeMap& operator=(const CMap& cmap) {
       
   346 	checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
       
   347 	const typename Parent::Graph* graph = Parent::getGraph();
       
   348 	LowerNode it;
       
   349 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   350 	  Parent::set(it, cmap[it]);
       
   351 	}
       
   352 	return *this;
       
   353       }
       
   354     
       
   355     };
       
   356 
       
   357   protected:
       
   358 
       
   359     template <typename _Value>
       
   360     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
       
   361     public:
       
   362       typedef MappableUndirBipartiteGraphExtender Graph;
       
   363 
       
   364       typedef Node Key;
       
   365       typedef _Value Value;
       
   366 
       
   367       /// The reference type of the map;
       
   368       typedef typename LowerNodeMap<_Value>::Reference Reference;
       
   369       /// The pointer type of the map;
       
   370       typedef typename LowerNodeMap<_Value>::Pointer Pointer;
       
   371       
       
   372       /// The const value type of the map.
       
   373       typedef const Value ConstValue;
       
   374       /// The const reference type of the map;
       
   375       typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
       
   376       /// The pointer type of the map;
       
   377       typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
       
   378 
       
   379       typedef True ReferenceMapTag;
       
   380 
       
   381       NodeMapBase(const Graph& _g) 
       
   382 	: graph(&_g), lowerMap(_g), upperMap(_g) {
       
   383 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
       
   384       }
       
   385       NodeMapBase(const Graph& _g, const _Value& _v) 
       
   386 	: graph(&_g), lowerMap(_g, _v), 
       
   387 	  upperMap(_g, _v) {
       
   388 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
       
   389       }
       
   390 
       
   391       virtual ~NodeMapBase() {      
       
   392 	if (Parent::NodeNotifier::ObserverBase::attached()) {
       
   393 	  Parent::NodeNotifier::ObserverBase::detach();
       
   394 	}
       
   395       }
       
   396     
       
   397       ConstReference operator[](const Key& node) const {
       
   398 	if (Parent::upper(node)) {
       
   399 	  return upperMap[node];
       
   400 	} else {
       
   401 	  return lowerMap[node];
       
   402 	}
       
   403       } 
       
   404 
       
   405       Reference operator[](const Key& node) {
       
   406 	if (Parent::upper(node)) {
       
   407 	  return upperMap[node];
       
   408 	} else {
       
   409 	  return lowerMap[node];
       
   410 	}
       
   411       }
       
   412 
       
   413       void set(const Key& node, const Value& value) {
       
   414 	if (Parent::upper(node)) {
       
   415 	  upperMap.set(node, value);
       
   416 	} else {
       
   417 	  lowerMap.set(node, value);
       
   418 	}
       
   419       }
       
   420 
       
   421     protected:
       
   422       
       
   423       virtual void add(const Node&) {}
       
   424       virtual void erase(const Node&) {}
       
   425       virtual void clear() {}
       
   426       virtual void build() {}
       
   427 
       
   428       const Graph* getGraph() const { return graph; }
       
   429       
       
   430     private:
       
   431       const Graph* graph;
       
   432       LowerNodeMap<_Value> lowerMap;
       
   433       UpperNodeMap<_Value> upperMap;
       
   434     };
       
   435     
       
   436   public:
       
   437 
       
   438     template <typename _Value>
       
   439     class NodeMap 
       
   440       : public IterableMapExtender<NodeMapBase<_Value> > {
       
   441     public:
       
   442       typedef MappableUndirBipartiteGraphExtender Graph;
       
   443       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
       
   444     
       
   445       NodeMap(const Graph& _g) 
       
   446 	: Parent(_g) {}
       
   447       NodeMap(const Graph& _g, const _Value& _v) 
       
   448 	: Parent(_g, _v) {}
       
   449     
       
   450       NodeMap& operator=(const NodeMap& cmap) {
       
   451 	return operator=<NodeMap>(cmap);
       
   452       }
       
   453     
       
   454 
       
   455       /// \brief Template assign operator.
       
   456       ///
       
   457       /// The given parameter should be conform to the ReadMap
       
   458       /// concept and could be indiced by the current item set of
       
   459       /// the NodeMap. In this case the value for each item
       
   460       /// is assigned by the value of the given ReadMap. 
       
   461       template <typename CMap>
       
   462       NodeMap& operator=(const CMap& cmap) {
       
   463 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
       
   464 	const typename Parent::Graph* graph = Parent::getGraph();
       
   465 	Node it;
       
   466 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   467 	  Parent::set(it, cmap[it]);
       
   468 	}
       
   469 	return *this;
       
   470       }
       
   471     
       
   472     };
       
   473 
       
   474 
       
   475 
       
   476     template <typename _Value>
       
   477     class EdgeMap 
       
   478       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
       
   479     public:
       
   480       typedef MappableUndirBipartiteGraphExtender Graph;
       
   481       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
       
   482     
       
   483       EdgeMap(const Graph& _g) 
       
   484 	: Parent(_g) {}
       
   485       EdgeMap(const Graph& _g, const _Value& _v) 
       
   486 	: Parent(_g, _v) {}
       
   487     
       
   488       EdgeMap& operator=(const EdgeMap& cmap) {
       
   489 	return operator=<EdgeMap>(cmap);
       
   490       }
       
   491     
       
   492       template <typename CMap>
       
   493       EdgeMap& operator=(const CMap& cmap) {
       
   494 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
       
   495 	const typename Parent::Graph* graph = Parent::getGraph();
       
   496 	Edge it;
       
   497 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   498 	  Parent::set(it, cmap[it]);
       
   499 	}
       
   500 	return *this;
       
   501       }
       
   502     };
       
   503 
       
   504     template <typename _Value>
       
   505     class UndirEdgeMap 
       
   506       : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
       
   507     public:
       
   508       typedef MappableUndirBipartiteGraphExtender Graph;
       
   509       typedef IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > 
       
   510       Parent;
       
   511     
       
   512       UndirEdgeMap(const Graph& _g) 
       
   513 	: Parent(_g) {}
       
   514       UndirEdgeMap(const Graph& _g, const _Value& _v) 
       
   515 	: Parent(_g, _v) {}
       
   516     
       
   517       UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
       
   518 	return operator=<UndirEdgeMap>(cmap);
       
   519       }
       
   520     
       
   521       template <typename CMap>
       
   522       UndirEdgeMap& operator=(const CMap& cmap) {
       
   523 	checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
       
   524 	const typename Parent::Graph* graph = Parent::getGraph();
       
   525 	UndirEdge it;
       
   526 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   527 	  Parent::set(it, cmap[it]);
       
   528 	}
       
   529 	return *this;
       
   530       }
       
   531     };
       
   532   
       
   533   };
       
   534 
   269 }
   535 }
   270 
   536 
   271 #endif
   537 #endif