lemon/bits/static_map.h
changeset 1837 8dd6160ff699
parent 1810 474d093466a5
child 1875 98698b69a902
equal deleted inserted replaced
2:c77e9b06617f 3:b7f8a478e406
   341 	}
   341 	}
   342 	return *this;
   342 	return *this;
   343       }
   343       }
   344     };
   344     };
   345 
   345 
   346 
       
   347   };
   346   };
   348 
   347 
       
   348   template <typename _Base>
       
   349   class StaticMappableUndirBipartiteGraphExtender : public _Base {
       
   350   public:
       
   351 
       
   352     typedef _Base Parent;
       
   353     typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   354 
       
   355     typedef typename Parent::Node Node;
       
   356     typedef typename Parent::UpperNode UpperNode;
       
   357     typedef typename Parent::LowerNode LowerNode;
       
   358     typedef typename Parent::Edge Edge;
       
   359     typedef typename Parent::UndirEdge UndirEdge;
       
   360     
       
   361     template <typename _Value>
       
   362     class UpperNodeMap 
       
   363       : public IterableMapExtender<StaticMap<Graph, UpperNode, _Value> > {
       
   364     public:
       
   365       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   366       typedef IterableMapExtender<StaticMap<Graph, UpperNode, _Value> > 
       
   367       Parent;
       
   368     
       
   369       UpperNodeMap(const Graph& _g) 
       
   370 	: Parent(_g) {}
       
   371       UpperNodeMap(const Graph& _g, const _Value& _v) 
       
   372 	: Parent(_g, _v) {}
       
   373     
       
   374       UpperNodeMap& operator=(const UpperNodeMap& cmap) {
       
   375 	return operator=<UpperNodeMap>(cmap);
       
   376       }
       
   377     
       
   378 
       
   379       /// \brief Template assign operator.
       
   380       ///
       
   381       /// The given parameter should be conform to the ReadMap
       
   382       /// concept and could be indiced by the current item set of
       
   383       /// the UpperNodeMap. In this case the value for each item
       
   384       /// is assigned by the value of the given ReadMap. 
       
   385       template <typename CMap>
       
   386       UpperNodeMap& operator=(const CMap& cmap) {
       
   387 	checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
       
   388 	const typename Parent::Graph* graph = Parent::getGraph();
       
   389 	UpperNode it;
       
   390 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   391 	  Parent::set(it, cmap[it]);
       
   392 	}
       
   393 	return *this;
       
   394       }
       
   395     
       
   396     };
       
   397 
       
   398     template <typename _Value>
       
   399     class LowerNodeMap 
       
   400       : public IterableMapExtender<StaticMap<Graph, LowerNode, _Value> > {
       
   401     public:
       
   402       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   403       typedef IterableMapExtender<StaticMap<Graph, LowerNode, _Value> > 
       
   404       Parent;
       
   405     
       
   406       LowerNodeMap(const Graph& _g) 
       
   407 	: Parent(_g) {}
       
   408       LowerNodeMap(const Graph& _g, const _Value& _v) 
       
   409 	: Parent(_g, _v) {}
       
   410     
       
   411       LowerNodeMap& operator=(const LowerNodeMap& cmap) {
       
   412 	return operator=<LowerNodeMap>(cmap);
       
   413       }
       
   414     
       
   415 
       
   416       /// \brief Template assign operator.
       
   417       ///
       
   418       /// The given parameter should be conform to the ReadMap
       
   419       /// concept and could be indiced by the current item set of
       
   420       /// the LowerNodeMap. In this case the value for each item
       
   421       /// is assigned by the value of the given ReadMap. 
       
   422       template <typename CMap>
       
   423       LowerNodeMap& operator=(const CMap& cmap) {
       
   424 	checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
       
   425 	const typename Parent::Graph* graph = Parent::getGraph();
       
   426 	LowerNode it;
       
   427 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   428 	  Parent::set(it, cmap[it]);
       
   429 	}
       
   430 	return *this;
       
   431       }
       
   432     
       
   433     };
       
   434 
       
   435   protected:
       
   436 
       
   437     template <typename _Value>
       
   438     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
       
   439     public:
       
   440       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   441 
       
   442       typedef Node Key;
       
   443       typedef _Value Value;
       
   444 
       
   445       /// The reference type of the map;
       
   446       typedef typename LowerNodeMap<_Value>::Reference Reference;
       
   447       /// The pointer type of the map;
       
   448       typedef typename LowerNodeMap<_Value>::Pointer Pointer;
       
   449       
       
   450       /// The const value type of the map.
       
   451       typedef const Value ConstValue;
       
   452       /// The const reference type of the map;
       
   453       typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
       
   454       /// The pointer type of the map;
       
   455       typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
       
   456 
       
   457       typedef True ReferenceMapTag;
       
   458 
       
   459       NodeMapBase(const Graph& _g) 
       
   460 	: graph(&_g), lowerMap(_g), upperMap(_g) {
       
   461 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
       
   462       }
       
   463       NodeMapBase(const Graph& _g, const _Value& _v) 
       
   464 	: graph(&_g), lowerMap(_g, _v), 
       
   465 	  upperMap(_g, _v) {
       
   466 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
       
   467       }
       
   468 
       
   469       virtual ~NodeMapBase() {      
       
   470 	if (Parent::NodeNotifier::ObserverBase::attached()) {
       
   471 	  Parent::NodeNotifier::ObserverBase::detach();
       
   472 	}
       
   473       }
       
   474     
       
   475       ConstReference operator[](const Key& node) const {
       
   476 	if (Parent::upper(node)) {
       
   477 	  return upperMap[node];
       
   478 	} else {
       
   479 	  return lowerMap[node];
       
   480 	}
       
   481       } 
       
   482 
       
   483       Reference operator[](const Key& node) {
       
   484 	if (Parent::upper(node)) {
       
   485 	  return upperMap[node];
       
   486 	} else {
       
   487 	  return lowerMap[node];
       
   488 	}
       
   489       }
       
   490 
       
   491       void set(const Key& node, const Value& value) {
       
   492 	if (Parent::upper(node)) {
       
   493 	  upperMap.set(node, value);
       
   494 	} else {
       
   495 	  lowerMap.set(node, value);
       
   496 	}
       
   497       }
       
   498 
       
   499     protected:
       
   500       
       
   501       virtual void add(const Node&) {}
       
   502       virtual void erase(const Node&) {}
       
   503       virtual void clear() {}
       
   504       virtual void build() {}
       
   505 
       
   506       const Graph* getGraph() const { return graph; }
       
   507       
       
   508     private:
       
   509       const Graph* graph;
       
   510       LowerNodeMap<_Value> lowerMap;
       
   511       UpperNodeMap<_Value> upperMap;
       
   512     };
       
   513     
       
   514   public:
       
   515 
       
   516     template <typename _Value>
       
   517     class NodeMap 
       
   518       : public IterableMapExtender<NodeMapBase<_Value> > {
       
   519     public:
       
   520       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   521       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
       
   522     
       
   523       NodeMap(const Graph& _g) 
       
   524 	: Parent(_g) {}
       
   525       NodeMap(const Graph& _g, const _Value& _v) 
       
   526 	: Parent(_g, _v) {}
       
   527     
       
   528       NodeMap& operator=(const NodeMap& cmap) {
       
   529 	return operator=<NodeMap>(cmap);
       
   530       }
       
   531     
       
   532 
       
   533       /// \brief Template assign operator.
       
   534       ///
       
   535       /// The given parameter should be conform to the ReadMap
       
   536       /// concept and could be indiced by the current item set of
       
   537       /// the NodeMap. In this case the value for each item
       
   538       /// is assigned by the value of the given ReadMap. 
       
   539       template <typename CMap>
       
   540       NodeMap& operator=(const CMap& cmap) {
       
   541 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
       
   542 	const typename Parent::Graph* graph = Parent::getGraph();
       
   543 	Node it;
       
   544 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   545 	  Parent::set(it, cmap[it]);
       
   546 	}
       
   547 	return *this;
       
   548       }
       
   549     
       
   550     };
       
   551 
       
   552 
       
   553 
       
   554     template <typename _Value>
       
   555     class EdgeMap 
       
   556       : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
       
   557     public:
       
   558       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   559       typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
       
   560     
       
   561       EdgeMap(const Graph& _g) 
       
   562 	: Parent(_g) {}
       
   563       EdgeMap(const Graph& _g, const _Value& _v) 
       
   564 	: Parent(_g, _v) {}
       
   565     
       
   566       EdgeMap& operator=(const EdgeMap& cmap) {
       
   567 	return operator=<EdgeMap>(cmap);
       
   568       }
       
   569     
       
   570       template <typename CMap>
       
   571       EdgeMap& operator=(const CMap& cmap) {
       
   572 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
       
   573 	const typename Parent::Graph* graph = Parent::getGraph();
       
   574 	Edge it;
       
   575 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   576 	  Parent::set(it, cmap[it]);
       
   577 	}
       
   578 	return *this;
       
   579       }
       
   580     };
       
   581 
       
   582     template <typename _Value>
       
   583     class UndirEdgeMap 
       
   584       : public IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> > {
       
   585     public:
       
   586       typedef StaticMappableUndirBipartiteGraphExtender Graph;
       
   587       typedef IterableMapExtender<StaticMap<Graph, UndirEdge, _Value> > 
       
   588       Parent;
       
   589     
       
   590       UndirEdgeMap(const Graph& _g) 
       
   591 	: Parent(_g) {}
       
   592       UndirEdgeMap(const Graph& _g, const _Value& _v) 
       
   593 	: Parent(_g, _v) {}
       
   594     
       
   595       UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
       
   596 	return operator=<UndirEdgeMap>(cmap);
       
   597       }
       
   598     
       
   599       template <typename CMap>
       
   600       UndirEdgeMap& operator=(const CMap& cmap) {
       
   601 	checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
       
   602 	const typename Parent::Graph* graph = Parent::getGraph();
       
   603 	UndirEdge it;
       
   604 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
   605 	  Parent::set(it, cmap[it]);
       
   606 	}
       
   607 	return *this;
       
   608       }
       
   609     };
       
   610   
       
   611   };
       
   612 
   349 }
   613 }
   350 
   614 
   351 #endif
   615 #endif