lemon/bits/static_map.h
changeset 1938 c339ade3ffc3
parent 1909 2d806130e700
child 1946 17eb3eaad9f8
equal deleted inserted replaced
5:4d433ac433d4 6:b157b0c55f49
     1 /* -*- C++ -*-
     1 /* -*- C++ -*-
     2  * lemon/static_map.h - Part of LEMON, a generic C++ optimization library
     2  * lemon/static_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  *
    25 #include <lemon/bits/alteration_notifier.h>
    25 #include <lemon/bits/alteration_notifier.h>
    26 #include <lemon/error.h>
    26 #include <lemon/error.h>
    27 #include <lemon/concept_check.h>
    27 #include <lemon/concept_check.h>
    28 #include <lemon/concept/maps.h>
    28 #include <lemon/concept/maps.h>
    29 
    29 
    30 /// \ingroup graphmaps
    30 /// \ingroin graphmaps
    31 ///
    31 ///
    32 ///\file
    32 ///\file
    33 ///\brief Static sized graph maps.
    33 ///\brief Static sized graph maps.
    34 
    34 
    35 namespace lemon {
    35 namespace lemon {
    36 
    36 
    37   /// \ingroup graphmaps
    37   /// \ingroin graphmaps
    38   ///
    38   ///
    39   /// \brief Graph map with static sized storage.
    39   /// \brief Graph map with static sized storage.
    40   ///
    40   ///
    41   /// The StaticMap template class is graph map structure what
    41   /// The StaticMap template class is graph map structure what
    42   /// does not update automatically the map when a key is added to or 
    42   /// does not indate automatically the map when a key is added to or 
    43   /// erased from the map rather it throws an exception. This map factory 
    43   /// erased from the map rather it throws an exception. This map factory 
    44   /// uses the allocators to implement the container functionality.
    44   /// uses the allocators to implement the container functionality.
    45   ///
    45   ///
    46   /// \param Registry The AlterationNotifier that will notify this map.
    46   /// \param Registry The AlterationNotifier that will notify this map.
    47   /// \param Item The item type of the graph items.
    47   /// \param Item The item type of the graph items.
    52 
    52 
    53   template <typename _Graph, typename _Item, typename _Value>
    53   template <typename _Graph, typename _Item, typename _Value>
    54   class StaticMap : public AlterationNotifier<_Item>::ObserverBase {
    54   class StaticMap : public AlterationNotifier<_Item>::ObserverBase {
    55   public:
    55   public:
    56 
    56 
    57     /// \brief Exception class for unsupported exceptions.
    57     /// \brief Exception class for unsinported exceptions.
    58     class UnsupportedOperation : public lemon::LogicError {
    58     class UnsinportedOperation : public lemon::LogicError {
    59     public:
    59     public:
    60       virtual const char* exceptionName() const {
    60       virtual const char* exceptionName() const {
    61 	return "lemon::StaticMap::UnsupportedOperation";
    61 	return "lemon::StaticMap::UnsinportedOperation";
    62       }
    62       }
    63     };
    63     };
    64 
    64 
    65   private:
    65   private:
    66 		
    66 		
   183     ///		
   183     ///		
   184     /// It adds a new key to the map. It called by the observer registry
   184     /// It adds a new key to the map. It called by the observer registry
   185     /// and it overrides the add() member function of the observer base.
   185     /// and it overrides the add() member function of the observer base.
   186      
   186      
   187     void add(const Key&) {
   187     void add(const Key&) {
   188       throw UnsupportedOperation();
   188       throw UnsinportedOperation();
   189     }
   189     }
   190 
   190 
   191     /// \brief Erases a key from the map.
   191     /// \brief Erases a key from the map.
   192     ///
   192     ///
   193     /// Erase a key from the map. It called by the observer registry
   193     /// Erase a key from the map. It called by the observer registry
   194     /// and it overrides the erase() member function of the observer base.     
   194     /// and it overrides the erase() member function of the observer base.     
   195     void erase(const Key&) {
   195     void erase(const Key&) {
   196       throw UnsupportedOperation();
   196       throw UnsinportedOperation();
   197     }
   197     }
   198 
   198 
   199     /// Buildes the map.
   199     /// Buildes the map.
   200 		
   200 		
   201     /// It buildes the map. It called by the observer registry
   201     /// It buildes the map. It called by the observer registry
   344     };
   344     };
   345 
   345 
   346   };
   346   };
   347 
   347 
   348   template <typename _Base>
   348   template <typename _Base>
   349   class StaticMappableUBipartiteGraphExtender : public _Base {
   349   class StaticMappableBpUGraphExtender : public _Base {
   350   public:
   350   public:
   351 
   351 
   352     typedef _Base Parent;
   352     typedef _Base Parent;
   353     typedef StaticMappableUBipartiteGraphExtender Graph;
   353     typedef StaticMappableBpUGraphExtender Graph;
   354 
   354 
   355     typedef typename Parent::Node Node;
   355     typedef typename Parent::Node Node;
   356     typedef typename Parent::UpperNode UpperNode;
   356     typedef typename Parent::ANode ANode;
   357     typedef typename Parent::LowerNode LowerNode;
   357     typedef typename Parent::BNode BNode;
   358     typedef typename Parent::Edge Edge;
   358     typedef typename Parent::Edge Edge;
   359     typedef typename Parent::UEdge UEdge;
   359     typedef typename Parent::UEdge UEdge;
   360     
   360     
   361     template <typename _Value>
   361     template <typename _Value>
   362     class UpperNodeMap 
   362     class ANodeMap 
   363       : public IterableMapExtender<StaticMap<Graph, UpperNode, _Value> > {
   363       : public IterableMapExtender<StaticMap<Graph, ANode, _Value> > {
   364     public:
   364     public:
   365       typedef StaticMappableUBipartiteGraphExtender Graph;
   365       typedef StaticMappableBpUGraphExtender Graph;
   366       typedef IterableMapExtender<StaticMap<Graph, UpperNode, _Value> > 
   366       typedef IterableMapExtender<StaticMap<Graph, ANode, _Value> > 
   367       Parent;
   367       Parent;
   368     
   368     
   369       UpperNodeMap(const Graph& _g) 
   369       ANodeMap(const Graph& _g) 
   370 	: Parent(_g) {}
   370 	: Parent(_g) {}
   371       UpperNodeMap(const Graph& _g, const _Value& _v) 
   371       ANodeMap(const Graph& _g, const _Value& _v) 
   372 	: Parent(_g, _v) {}
   372 	: Parent(_g, _v) {}
   373     
   373     
   374       UpperNodeMap& operator=(const UpperNodeMap& cmap) {
   374       ANodeMap& operator=(const ANodeMap& cmap) {
   375 	return operator=<UpperNodeMap>(cmap);
   375 	return operator=<ANodeMap>(cmap);
   376       }
   376       }
   377     
   377     
   378 
   378 
   379       /// \brief Template assign operator.
   379       /// \brief Template assign operator.
   380       ///
   380       ///
   381       /// The given parameter should be conform to the ReadMap
   381       /// The given parameter should be conform to the ReadMap
   382       /// concept and could be indiced by the current item set of
   382       /// concept and could be indiced by the current item set of
   383       /// the UpperNodeMap. In this case the value for each item
   383       /// the ANodeMap. In this case the value for each item
   384       /// is assigned by the value of the given ReadMap. 
   384       /// is assigned by the value of the given ReadMap. 
   385       template <typename CMap>
   385       template <typename CMap>
   386       UpperNodeMap& operator=(const CMap& cmap) {
   386       ANodeMap& operator=(const CMap& cmap) {
   387 	checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
   387 	checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
   388 	const typename Parent::Graph* graph = Parent::getGraph();
   388 	const typename Parent::Graph* graph = Parent::getGraph();
   389 	UpperNode it;
   389 	ANode it;
   390 	for (graph->first(it); it != INVALID; graph->next(it)) {
   390 	for (graph->first(it); it != INVALID; graph->next(it)) {
   391 	  Parent::set(it, cmap[it]);
   391 	  Parent::set(it, cmap[it]);
   392 	}
   392 	}
   393 	return *this;
   393 	return *this;
   394       }
   394       }
   395     
   395     
   396     };
   396     };
   397 
   397 
   398     template <typename _Value>
   398     template <typename _Value>
   399     class LowerNodeMap 
   399     class BNodeMap 
   400       : public IterableMapExtender<StaticMap<Graph, LowerNode, _Value> > {
   400       : public IterableMapExtender<StaticMap<Graph, BNode, _Value> > {
   401     public:
   401     public:
   402       typedef StaticMappableUBipartiteGraphExtender Graph;
   402       typedef StaticMappableBpUGraphExtender Graph;
   403       typedef IterableMapExtender<StaticMap<Graph, LowerNode, _Value> > 
   403       typedef IterableMapExtender<StaticMap<Graph, BNode, _Value> > 
   404       Parent;
   404       Parent;
   405     
   405     
   406       LowerNodeMap(const Graph& _g) 
   406       BNodeMap(const Graph& _g) 
   407 	: Parent(_g) {}
   407 	: Parent(_g) {}
   408       LowerNodeMap(const Graph& _g, const _Value& _v) 
   408       BNodeMap(const Graph& _g, const _Value& _v) 
   409 	: Parent(_g, _v) {}
   409 	: Parent(_g, _v) {}
   410     
   410     
   411       LowerNodeMap& operator=(const LowerNodeMap& cmap) {
   411       BNodeMap& operator=(const BNodeMap& cmap) {
   412 	return operator=<LowerNodeMap>(cmap);
   412 	return operator=<BNodeMap>(cmap);
   413       }
   413       }
   414     
   414     
   415 
   415 
   416       /// \brief Template assign operator.
   416       /// \brief Template assign operator.
   417       ///
   417       ///
   418       /// The given parameter should be conform to the ReadMap
   418       /// The given parameter should be conform to the ReadMap
   419       /// concept and could be indiced by the current item set of
   419       /// concept and could be indiced by the current item set of
   420       /// the LowerNodeMap. In this case the value for each item
   420       /// the BNodeMap. In this case the value for each item
   421       /// is assigned by the value of the given ReadMap. 
   421       /// is assigned by the value of the given ReadMap. 
   422       template <typename CMap>
   422       template <typename CMap>
   423       LowerNodeMap& operator=(const CMap& cmap) {
   423       BNodeMap& operator=(const CMap& cmap) {
   424 	checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
   424 	checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
   425 	const typename Parent::Graph* graph = Parent::getGraph();
   425 	const typename Parent::Graph* graph = Parent::getGraph();
   426 	LowerNode it;
   426 	BNode it;
   427 	for (graph->first(it); it != INVALID; graph->next(it)) {
   427 	for (graph->first(it); it != INVALID; graph->next(it)) {
   428 	  Parent::set(it, cmap[it]);
   428 	  Parent::set(it, cmap[it]);
   429 	}
   429 	}
   430 	return *this;
   430 	return *this;
   431       }
   431       }
   435   protected:
   435   protected:
   436 
   436 
   437     template <typename _Value>
   437     template <typename _Value>
   438     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
   438     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
   439     public:
   439     public:
   440       typedef StaticMappableUBipartiteGraphExtender Graph;
   440       typedef StaticMappableBpUGraphExtender Graph;
   441 
   441 
   442       typedef Node Key;
   442       typedef Node Key;
   443       typedef _Value Value;
   443       typedef _Value Value;
   444 
   444 
   445       /// The reference type of the map;
   445       /// The reference type of the map;
   446       typedef typename LowerNodeMap<_Value>::Reference Reference;
   446       typedef typename BNodeMap<_Value>::Reference Reference;
   447       /// The pointer type of the map;
   447       /// The pointer type of the map;
   448       typedef typename LowerNodeMap<_Value>::Pointer Pointer;
   448       typedef typename BNodeMap<_Value>::Pointer Pointer;
   449       
   449       
   450       /// The const value type of the map.
   450       /// The const value type of the map.
   451       typedef const Value ConstValue;
   451       typedef const Value ConstValue;
   452       /// The const reference type of the map;
   452       /// The const reference type of the map;
   453       typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
   453       typedef typename BNodeMap<_Value>::ConstReference ConstReference;
   454       /// The pointer type of the map;
   454       /// The pointer type of the map;
   455       typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
   455       typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
   456 
   456 
   457       typedef True ReferenceMapTag;
   457       typedef True ReferenceMapTag;
   458 
   458 
   459       NodeMapBase(const Graph& _g) 
   459       NodeMapBase(const Graph& _g) 
   460 	: graph(&_g), lowerMap(_g), upperMap(_g) {
   460 	: graph(&_g), bNodeMap(_g), aNodeMap(_g) {
   461 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   461 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   462       }
   462       }
   463       NodeMapBase(const Graph& _g, const _Value& _v) 
   463       NodeMapBase(const Graph& _g, const _Value& _v) 
   464 	: graph(&_g), lowerMap(_g, _v), 
   464 	: graph(&_g), bNodeMap(_g, _v), 
   465 	  upperMap(_g, _v) {
   465 	  aNodeMap(_g, _v) {
   466 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   466 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   467       }
   467       }
   468 
   468 
   469       virtual ~NodeMapBase() {      
   469       virtual ~NodeMapBase() {      
   470 	if (Parent::NodeNotifier::ObserverBase::attached()) {
   470 	if (Parent::NodeNotifier::ObserverBase::attached()) {
   471 	  Parent::NodeNotifier::ObserverBase::detach();
   471 	  Parent::NodeNotifier::ObserverBase::detach();
   472 	}
   472 	}
   473       }
   473       }
   474     
   474     
   475       ConstReference operator[](const Key& node) const {
   475       ConstReference operator[](const Key& node) const {
   476 	if (Parent::upper(node)) {
   476 	if (Parent::aNode(node)) {
   477 	  return upperMap[node];
   477 	  return aNodeMap[node];
   478 	} else {
   478 	} else {
   479 	  return lowerMap[node];
   479 	  return bNodeMap[node];
   480 	}
   480 	}
   481       } 
   481       } 
   482 
   482 
   483       Reference operator[](const Key& node) {
   483       Reference operator[](const Key& node) {
   484 	if (Parent::upper(node)) {
   484 	if (Parent::aNode(node)) {
   485 	  return upperMap[node];
   485 	  return aNodeMap[node];
   486 	} else {
   486 	} else {
   487 	  return lowerMap[node];
   487 	  return bNodeMap[node];
   488 	}
   488 	}
   489       }
   489       }
   490 
   490 
   491       void set(const Key& node, const Value& value) {
   491       void set(const Key& node, const Value& value) {
   492 	if (Parent::upper(node)) {
   492 	if (Parent::aNode(node)) {
   493 	  upperMap.set(node, value);
   493 	  aNodeMap.set(node, value);
   494 	} else {
   494 	} else {
   495 	  lowerMap.set(node, value);
   495 	  bNodeMap.set(node, value);
   496 	}
   496 	}
   497       }
   497       }
   498 
   498 
   499     protected:
   499     protected:
   500       
   500       
   505 
   505 
   506       const Graph* getGraph() const { return graph; }
   506       const Graph* getGraph() const { return graph; }
   507       
   507       
   508     private:
   508     private:
   509       const Graph* graph;
   509       const Graph* graph;
   510       LowerNodeMap<_Value> lowerMap;
   510       BNodeMap<_Value> bNodeMap;
   511       UpperNodeMap<_Value> upperMap;
   511       ANodeMap<_Value> aNodeMap;
   512     };
   512     };
   513     
   513     
   514   public:
   514   public:
   515 
   515 
   516     template <typename _Value>
   516     template <typename _Value>
   517     class NodeMap 
   517     class NodeMap 
   518       : public IterableMapExtender<NodeMapBase<_Value> > {
   518       : public IterableMapExtender<NodeMapBase<_Value> > {
   519     public:
   519     public:
   520       typedef StaticMappableUBipartiteGraphExtender Graph;
   520       typedef StaticMappableBpUGraphExtender Graph;
   521       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
   521       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
   522     
   522     
   523       NodeMap(const Graph& _g) 
   523       NodeMap(const Graph& _g) 
   524 	: Parent(_g) {}
   524 	: Parent(_g) {}
   525       NodeMap(const Graph& _g, const _Value& _v) 
   525       NodeMap(const Graph& _g, const _Value& _v) 
   553 
   553 
   554     template <typename _Value>
   554     template <typename _Value>
   555     class EdgeMap 
   555     class EdgeMap 
   556       : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
   556       : public IterableMapExtender<StaticMap<Graph, Edge, _Value> > {
   557     public:
   557     public:
   558       typedef StaticMappableUBipartiteGraphExtender Graph;
   558       typedef StaticMappableBpUGraphExtender Graph;
   559       typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
   559       typedef IterableMapExtender<StaticMap<Graph, Edge, _Value> > Parent;
   560     
   560     
   561       EdgeMap(const Graph& _g) 
   561       EdgeMap(const Graph& _g) 
   562 	: Parent(_g) {}
   562 	: Parent(_g) {}
   563       EdgeMap(const Graph& _g, const _Value& _v) 
   563       EdgeMap(const Graph& _g, const _Value& _v) 
   581 
   581 
   582     template <typename _Value>
   582     template <typename _Value>
   583     class UEdgeMap 
   583     class UEdgeMap 
   584       : public IterableMapExtender<StaticMap<Graph, UEdge, _Value> > {
   584       : public IterableMapExtender<StaticMap<Graph, UEdge, _Value> > {
   585     public:
   585     public:
   586       typedef StaticMappableUBipartiteGraphExtender Graph;
   586       typedef StaticMappableBpUGraphExtender Graph;
   587       typedef IterableMapExtender<StaticMap<Graph, UEdge, _Value> > 
   587       typedef IterableMapExtender<StaticMap<Graph, UEdge, _Value> > 
   588       Parent;
   588       Parent;
   589     
   589     
   590       UEdgeMap(const Graph& _g) 
   590       UEdgeMap(const Graph& _g) 
   591 	: Parent(_g) {}
   591 	: Parent(_g) {}