lemon/bits/default_map.h
changeset 1924 9fdc893e2199
parent 1909 2d806130e700
child 1946 17eb3eaad9f8
equal deleted inserted replaced
8:0f3ca6c0d6a1 9:62446984e996
     1 /* -*- C++ -*-
     1 /* -*- C++ -*-
     2  * lemon/default_map.h - Part of LEMON, a generic C++ optimization library
     2  * lemon/default_map.h - Part of LEMON, a generic C++ optimization library
     3  *
     3  *
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     5  * (Egervary Research Groin on Combinatorial Optimization, EGRES).
     6  *
     6  *
     7  * Permission to use, modify and distribute this software is granted
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
     9  * precise terms see the accompanying LICENSE file.
    10  *
    10  *
    19 
    19 
    20 
    20 
    21 #include <lemon/bits/array_map.h>
    21 #include <lemon/bits/array_map.h>
    22 #include <lemon/bits/vector_map.h>
    22 #include <lemon/bits/vector_map.h>
    23 
    23 
    24 ///\ingroup graphmapfactory
    24 ///\ingroin graphmapfactory
    25 ///\file
    25 ///\file
    26 ///\brief Graph maps that construct and destruct
    26 ///\brief Graph maps that construct and destruct
    27 ///their elements dynamically.
    27 ///their elements dynamically.
    28 
    28 
    29 namespace lemon {
    29 namespace lemon {
   350 
   350 
   351   };
   351   };
   352 
   352 
   353 
   353 
   354   template <typename _Base>
   354   template <typename _Base>
   355   class MappableUBipartiteGraphExtender : public _Base {
   355   class MappableBpUGraphExtender : public _Base {
   356   public:
   356   public:
   357 
   357 
   358     typedef _Base Parent;
   358     typedef _Base Parent;
   359     typedef MappableUBipartiteGraphExtender Graph;
   359     typedef MappableBpUGraphExtender Graph;
   360 
   360 
   361     typedef typename Parent::Node Node;
   361     typedef typename Parent::Node Node;
   362     typedef typename Parent::UpperNode UpperNode;
   362     typedef typename Parent::ANode ANode;
   363     typedef typename Parent::LowerNode LowerNode;
   363     typedef typename Parent::BNode BNode;
   364     typedef typename Parent::Edge Edge;
   364     typedef typename Parent::Edge Edge;
   365     typedef typename Parent::UEdge UEdge;
   365     typedef typename Parent::UEdge UEdge;
   366     
   366     
   367     template <typename _Value>
   367     template <typename _Value>
   368     class UpperNodeMap 
   368     class ANodeMap 
   369       : public IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > {
   369       : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {
   370     public:
   370     public:
   371       typedef MappableUBipartiteGraphExtender Graph;
   371       typedef MappableBpUGraphExtender Graph;
   372       typedef IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > 
   372       typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> > 
   373       Parent;
   373       Parent;
   374     
   374     
   375       UpperNodeMap(const Graph& _g) 
   375       ANodeMap(const Graph& _g) 
   376 	: Parent(_g) {}
   376 	: Parent(_g) {}
   377       UpperNodeMap(const Graph& _g, const _Value& _v) 
   377       ANodeMap(const Graph& _g, const _Value& _v) 
   378 	: Parent(_g, _v) {}
   378 	: Parent(_g, _v) {}
   379     
   379     
   380       UpperNodeMap& operator=(const UpperNodeMap& cmap) {
   380       ANodeMap& operator=(const ANodeMap& cmap) {
   381 	return operator=<UpperNodeMap>(cmap);
   381 	return operator=<ANodeMap>(cmap);
   382       }
   382       }
   383     
   383     
   384 
   384 
   385       /// \brief Template assign operator.
   385       /// \brief Template assign operator.
   386       ///
   386       ///
   387       /// The given parameter should be conform to the ReadMap
   387       /// The given parameter should be conform to the ReadMap
   388       /// concept and could be indiced by the current item set of
   388       /// concept and could be indiced by the current item set of
   389       /// the UpperNodeMap. In this case the value for each item
   389       /// the ANodeMap. In this case the value for each item
   390       /// is assigned by the value of the given ReadMap. 
   390       /// is assigned by the value of the given ReadMap. 
   391       template <typename CMap>
   391       template <typename CMap>
   392       UpperNodeMap& operator=(const CMap& cmap) {
   392       ANodeMap& operator=(const CMap& cmap) {
   393 	checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
   393 	checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
   394 	const typename Parent::Graph* graph = Parent::getGraph();
   394 	const typename Parent::Graph* graph = Parent::getGraph();
   395 	UpperNode it;
   395 	ANode it;
   396 	for (graph->first(it); it != INVALID; graph->next(it)) {
   396 	for (graph->first(it); it != INVALID; graph->next(it)) {
   397 	  Parent::set(it, cmap[it]);
   397 	  Parent::set(it, cmap[it]);
   398 	}
   398 	}
   399 	return *this;
   399 	return *this;
   400       }
   400       }
   401     
   401     
   402     };
   402     };
   403 
   403 
   404     template <typename _Value>
   404     template <typename _Value>
   405     class LowerNodeMap 
   405     class BNodeMap 
   406       : public IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > {
   406       : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {
   407     public:
   407     public:
   408       typedef MappableUBipartiteGraphExtender Graph;
   408       typedef MappableBpUGraphExtender Graph;
   409       typedef IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > 
   409       typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> > 
   410       Parent;
   410       Parent;
   411     
   411     
   412       LowerNodeMap(const Graph& _g) 
   412       BNodeMap(const Graph& _g) 
   413 	: Parent(_g) {}
   413 	: Parent(_g) {}
   414       LowerNodeMap(const Graph& _g, const _Value& _v) 
   414       BNodeMap(const Graph& _g, const _Value& _v) 
   415 	: Parent(_g, _v) {}
   415 	: Parent(_g, _v) {}
   416     
   416     
   417       LowerNodeMap& operator=(const LowerNodeMap& cmap) {
   417       BNodeMap& operator=(const BNodeMap& cmap) {
   418 	return operator=<LowerNodeMap>(cmap);
   418 	return operator=<BNodeMap>(cmap);
   419       }
   419       }
   420     
   420     
   421 
   421 
   422       /// \brief Template assign operator.
   422       /// \brief Template assign operator.
   423       ///
   423       ///
   424       /// The given parameter should be conform to the ReadMap
   424       /// The given parameter should be conform to the ReadMap
   425       /// concept and could be indiced by the current item set of
   425       /// concept and could be indiced by the current item set of
   426       /// the LowerNodeMap. In this case the value for each item
   426       /// the BNodeMap. In this case the value for each item
   427       /// is assigned by the value of the given ReadMap. 
   427       /// is assigned by the value of the given ReadMap. 
   428       template <typename CMap>
   428       template <typename CMap>
   429       LowerNodeMap& operator=(const CMap& cmap) {
   429       BNodeMap& operator=(const CMap& cmap) {
   430 	checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
   430 	checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
   431 	const typename Parent::Graph* graph = Parent::getGraph();
   431 	const typename Parent::Graph* graph = Parent::getGraph();
   432 	LowerNode it;
   432 	BNode it;
   433 	for (graph->first(it); it != INVALID; graph->next(it)) {
   433 	for (graph->first(it); it != INVALID; graph->next(it)) {
   434 	  Parent::set(it, cmap[it]);
   434 	  Parent::set(it, cmap[it]);
   435 	}
   435 	}
   436 	return *this;
   436 	return *this;
   437       }
   437       }
   441   protected:
   441   protected:
   442 
   442 
   443     template <typename _Value>
   443     template <typename _Value>
   444     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
   444     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
   445     public:
   445     public:
   446       typedef MappableUBipartiteGraphExtender Graph;
   446       typedef MappableBpUGraphExtender Graph;
   447 
   447 
   448       typedef Node Key;
   448       typedef Node Key;
   449       typedef _Value Value;
   449       typedef _Value Value;
   450 
   450 
   451       /// The reference type of the map;
   451       /// The reference type of the map;
   452       typedef typename LowerNodeMap<_Value>::Reference Reference;
   452       typedef typename BNodeMap<_Value>::Reference Reference;
   453       /// The pointer type of the map;
   453       /// The pointer type of the map;
   454       typedef typename LowerNodeMap<_Value>::Pointer Pointer;
   454       typedef typename BNodeMap<_Value>::Pointer Pointer;
   455       
   455       
   456       /// The const value type of the map.
   456       /// The const value type of the map.
   457       typedef const Value ConstValue;
   457       typedef const Value ConstValue;
   458       /// The const reference type of the map;
   458       /// The const reference type of the map;
   459       typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
   459       typedef typename BNodeMap<_Value>::ConstReference ConstReference;
   460       /// The pointer type of the map;
   460       /// The pointer type of the map;
   461       typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
   461       typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
   462 
   462 
   463       typedef True ReferenceMapTag;
   463       typedef True ReferenceMapTag;
   464 
   464 
   465       NodeMapBase(const Graph& _g) 
   465       NodeMapBase(const Graph& _g) 
   466 	: graph(&_g), lowerMap(_g), upperMap(_g) {
   466 	: graph(&_g), bNodeMap(_g), aNodeMap(_g) {
   467 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   467 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   468       }
   468       }
   469       NodeMapBase(const Graph& _g, const _Value& _v) 
   469       NodeMapBase(const Graph& _g, const _Value& _v) 
   470 	: graph(&_g), lowerMap(_g, _v), 
   470 	: graph(&_g), bNodeMap(_g, _v), 
   471 	  upperMap(_g, _v) {
   471 	  aNodeMap(_g, _v) {
   472 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   472 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   473       }
   473       }
   474 
   474 
   475       virtual ~NodeMapBase() {      
   475       virtual ~NodeMapBase() {      
   476 	if (Parent::NodeNotifier::ObserverBase::attached()) {
   476 	if (Parent::NodeNotifier::ObserverBase::attached()) {
   477 	  Parent::NodeNotifier::ObserverBase::detach();
   477 	  Parent::NodeNotifier::ObserverBase::detach();
   478 	}
   478 	}
   479       }
   479       }
   480     
   480     
   481       ConstReference operator[](const Key& node) const {
   481       ConstReference operator[](const Key& node) const {
   482 	if (Parent::upper(node)) {
   482 	if (Parent::aNode(node)) {
   483 	  return upperMap[node];
   483 	  return aNodeMap[node];
   484 	} else {
   484 	} else {
   485 	  return lowerMap[node];
   485 	  return bNodeMap[node];
   486 	}
   486 	}
   487       } 
   487       } 
   488 
   488 
   489       Reference operator[](const Key& node) {
   489       Reference operator[](const Key& node) {
   490 	if (Parent::upper(node)) {
   490 	if (Parent::aNode(node)) {
   491 	  return upperMap[node];
   491 	  return aNodeMap[node];
   492 	} else {
   492 	} else {
   493 	  return lowerMap[node];
   493 	  return bNodeMap[node];
   494 	}
   494 	}
   495       }
   495       }
   496 
   496 
   497       void set(const Key& node, const Value& value) {
   497       void set(const Key& node, const Value& value) {
   498 	if (Parent::upper(node)) {
   498 	if (Parent::aNode(node)) {
   499 	  upperMap.set(node, value);
   499 	  aNodeMap.set(node, value);
   500 	} else {
   500 	} else {
   501 	  lowerMap.set(node, value);
   501 	  bNodeMap.set(node, value);
   502 	}
   502 	}
   503       }
   503       }
   504 
   504 
   505     protected:
   505     protected:
   506       
   506       
   511 
   511 
   512       const Graph* getGraph() const { return graph; }
   512       const Graph* getGraph() const { return graph; }
   513       
   513       
   514     private:
   514     private:
   515       const Graph* graph;
   515       const Graph* graph;
   516       LowerNodeMap<_Value> lowerMap;
   516       BNodeMap<_Value> bNodeMap;
   517       UpperNodeMap<_Value> upperMap;
   517       ANodeMap<_Value> aNodeMap;
   518     };
   518     };
   519     
   519     
   520   public:
   520   public:
   521 
   521 
   522     template <typename _Value>
   522     template <typename _Value>
   523     class NodeMap 
   523     class NodeMap 
   524       : public IterableMapExtender<NodeMapBase<_Value> > {
   524       : public IterableMapExtender<NodeMapBase<_Value> > {
   525     public:
   525     public:
   526       typedef MappableUBipartiteGraphExtender Graph;
   526       typedef MappableBpUGraphExtender Graph;
   527       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
   527       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
   528     
   528     
   529       NodeMap(const Graph& _g) 
   529       NodeMap(const Graph& _g) 
   530 	: Parent(_g) {}
   530 	: Parent(_g) {}
   531       NodeMap(const Graph& _g, const _Value& _v) 
   531       NodeMap(const Graph& _g, const _Value& _v) 
   559 
   559 
   560     template <typename _Value>
   560     template <typename _Value>
   561     class EdgeMap 
   561     class EdgeMap 
   562       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
   562       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
   563     public:
   563     public:
   564       typedef MappableUBipartiteGraphExtender Graph;
   564       typedef MappableBpUGraphExtender Graph;
   565       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   565       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   566     
   566     
   567       EdgeMap(const Graph& _g) 
   567       EdgeMap(const Graph& _g) 
   568 	: Parent(_g) {}
   568 	: Parent(_g) {}
   569       EdgeMap(const Graph& _g, const _Value& _v) 
   569       EdgeMap(const Graph& _g, const _Value& _v) 
   587 
   587 
   588     template <typename _Value>
   588     template <typename _Value>
   589     class UEdgeMap 
   589     class UEdgeMap 
   590       : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
   590       : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
   591     public:
   591     public:
   592       typedef MappableUBipartiteGraphExtender Graph;
   592       typedef MappableBpUGraphExtender Graph;
   593       typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > 
   593       typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > 
   594       Parent;
   594       Parent;
   595     
   595     
   596       UEdgeMap(const Graph& _g) 
   596       UEdgeMap(const Graph& _g) 
   597 	: Parent(_g) {}
   597 	: Parent(_g) {}