3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2006
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #ifndef LEMON_DEFAULT_MAP_H
 
    20 #define LEMON_DEFAULT_MAP_H
 
    23 #include <lemon/bits/array_map.h>
 
    24 #include <lemon/bits/vector_map.h>
 
    26 ///\ingroup graphmapfactory
 
    28 ///\brief Graph maps that construct and destruct
 
    29 ///their elements dynamically.
 
    35   template <typename _Graph, typename _Item, typename _Value>
 
    36   struct DefaultMapSelector {
 
    37     typedef ArrayMap<_Graph, _Item, _Value> Map;
 
    42   template <typename _Graph, typename _Item, typename _Value>
 
    43   struct DefaultMapSelector {
 
    44     typedef VectorMap<_Graph, _Item, _Value> Map;
 
    50   template <typename _Graph, typename _Item>
 
    51   struct DefaultMapSelector<_Graph, _Item, bool> {
 
    52     typedef VectorMap<_Graph, _Item, bool> Map;
 
    56   template <typename _Graph, typename _Item>
 
    57   struct DefaultMapSelector<_Graph, _Item, char> {
 
    58     typedef VectorMap<_Graph, _Item, char> Map;
 
    61   template <typename _Graph, typename _Item>
 
    62   struct DefaultMapSelector<_Graph, _Item, signed char> {
 
    63     typedef VectorMap<_Graph, _Item, signed char> Map;
 
    66   template <typename _Graph, typename _Item>
 
    67   struct DefaultMapSelector<_Graph, _Item, unsigned char> {
 
    68     typedef VectorMap<_Graph, _Item, unsigned char> Map;
 
    73   template <typename _Graph, typename _Item>
 
    74   struct DefaultMapSelector<_Graph, _Item, signed int> {
 
    75     typedef VectorMap<_Graph, _Item, signed int> Map;
 
    78   template <typename _Graph, typename _Item>
 
    79   struct DefaultMapSelector<_Graph, _Item, unsigned int> {
 
    80     typedef VectorMap<_Graph, _Item, unsigned int> Map;
 
    85   template <typename _Graph, typename _Item>
 
    86   struct DefaultMapSelector<_Graph, _Item, signed short> {
 
    87     typedef VectorMap<_Graph, _Item, signed short> Map;
 
    90   template <typename _Graph, typename _Item>
 
    91   struct DefaultMapSelector<_Graph, _Item, unsigned short> {
 
    92     typedef VectorMap<_Graph, _Item, unsigned short> Map;
 
    97   template <typename _Graph, typename _Item>
 
    98   struct DefaultMapSelector<_Graph, _Item, signed long> {
 
    99     typedef VectorMap<_Graph, _Item, signed long> Map;
 
   102   template <typename _Graph, typename _Item>
 
   103   struct DefaultMapSelector<_Graph, _Item, unsigned long> {
 
   104     typedef VectorMap<_Graph, _Item, unsigned long> Map;
 
   108 #ifndef __STRICT_ANSI__
 
   111   template <typename _Graph, typename _Item>
 
   112   struct DefaultMapSelector<_Graph, _Item, signed long long> {
 
   113     typedef VectorMap<_Graph, _Item, signed long long> Map;
 
   116   template <typename _Graph, typename _Item>
 
   117   struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
 
   118     typedef VectorMap<_Graph, _Item, unsigned long long> Map;
 
   125   template <typename _Graph, typename _Item>
 
   126   struct DefaultMapSelector<_Graph, _Item, float> {
 
   127     typedef VectorMap<_Graph, _Item, float> Map;
 
   132   template <typename _Graph, typename _Item>
 
   133   struct DefaultMapSelector<_Graph, _Item, double> {
 
   134     typedef VectorMap<_Graph, _Item,  double> Map;
 
   139   template <typename _Graph, typename _Item>
 
   140   struct DefaultMapSelector<_Graph, _Item, long double> {
 
   141     typedef VectorMap<_Graph, _Item, long double> Map;
 
   146   template <typename _Graph, typename _Item, typename _Ptr>
 
   147   struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
 
   148     typedef VectorMap<_Graph, _Item, _Ptr*> Map;
 
   157     : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
 
   159     typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
 
   160     typedef DefaultMap<_Graph, _Item, _Value> Map;
 
   162     typedef typename Parent::Graph Graph;
 
   163     typedef typename Parent::Value Value;
 
   165     DefaultMap(const Graph& _g) : Parent(_g) {}
 
   166     DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
 
   172   template <typename _Base> 
 
   173   class MappableGraphExtender : public _Base {
 
   176     typedef MappableGraphExtender<_Base> Graph;
 
   177     typedef _Base Parent;
 
   179     typedef typename Parent::Node Node;
 
   180     typedef typename Parent::NodeIt NodeIt;
 
   182     typedef typename Parent::Edge Edge;
 
   183     typedef typename Parent::EdgeIt EdgeIt;
 
   186     template <typename _Value>
 
   188       : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
 
   190       typedef MappableGraphExtender Graph;
 
   191       typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
 
   193       NodeMap(const Graph& _g) 
 
   195       NodeMap(const Graph& _g, const _Value& _v) 
 
   198       NodeMap& operator=(const NodeMap& cmap) {
 
   199 	return operator=<NodeMap>(cmap);
 
   203       /// \brief Template assign operator.
 
   205       /// The given parameter should be conform to the ReadMap
 
   206       /// concecpt and could be indiced by the current item set of
 
   207       /// the NodeMap. In this case the value for each item
 
   208       /// is assigned by the value of the given ReadMap. 
 
   209       template <typename CMap>
 
   210       NodeMap& operator=(const CMap& cmap) {
 
   211 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
 
   212 	const typename Parent::Graph* graph = Parent::getGraph();
 
   214 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   215 	  Parent::set(it, cmap[it]);
 
   222     template <typename _Value>
 
   224       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
 
   226       typedef MappableGraphExtender Graph;
 
   227       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
 
   229       EdgeMap(const Graph& _g) 
 
   231       EdgeMap(const Graph& _g, const _Value& _v) 
 
   234       EdgeMap& operator=(const EdgeMap& cmap) {
 
   235 	return operator=<EdgeMap>(cmap);
 
   238       template <typename CMap>
 
   239       EdgeMap& operator=(const CMap& cmap) {
 
   240 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
 
   241 	const typename Parent::Graph* graph = Parent::getGraph();
 
   243 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   244 	  Parent::set(it, cmap[it]);
 
   253   template <typename _Base> 
 
   254   class MappableEdgeSetExtender : public _Base {
 
   257     typedef MappableEdgeSetExtender<_Base> Graph;
 
   258     typedef _Base Parent;
 
   260     typedef typename Parent::Edge Edge;
 
   261     typedef typename Parent::EdgeIt EdgeIt;
 
   263     template <typename _Value>
 
   265       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
 
   267       typedef MappableEdgeSetExtender Graph;
 
   268       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
 
   270       EdgeMap(const Graph& _g) 
 
   272       EdgeMap(const Graph& _g, const _Value& _v) 
 
   275       EdgeMap& operator=(const EdgeMap& cmap) {
 
   276 	return operator=<EdgeMap>(cmap);
 
   279       template <typename CMap>
 
   280       EdgeMap& operator=(const CMap& cmap) {
 
   281 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
 
   282 	const typename Parent::Graph* graph = Parent::getGraph();
 
   284 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   285 	  Parent::set(it, cmap[it]);
 
   294   template <typename _Base> 
 
   295   class MappableUGraphExtender : 
 
   296     public MappableGraphExtender<_Base> {
 
   299     typedef MappableUGraphExtender Graph;
 
   300     typedef MappableGraphExtender<_Base> Parent;
 
   302     typedef typename Parent::UEdge UEdge;
 
   304     template <typename _Value>
 
   306       : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
 
   308       typedef MappableUGraphExtender Graph;
 
   309       typedef IterableMapExtender<
 
   310 	DefaultMap<Graph, UEdge, _Value> > Parent;
 
   312       UEdgeMap(const Graph& _g) 
 
   314       UEdgeMap(const Graph& _g, const _Value& _v) 
 
   317       UEdgeMap& operator=(const UEdgeMap& cmap) {
 
   318 	return operator=<UEdgeMap>(cmap);
 
   321       template <typename CMap>
 
   322       UEdgeMap& operator=(const CMap& cmap) {
 
   323 	checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
 
   324 	const typename Parent::Graph* graph = Parent::getGraph();
 
   326 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   327 	  Parent::set(it, cmap[it]);
 
   337   template <typename _Base> 
 
   338   class MappableUEdgeSetExtender : 
 
   339     public MappableEdgeSetExtender<_Base> {
 
   342     typedef MappableUEdgeSetExtender Graph;
 
   343     typedef MappableEdgeSetExtender<_Base> Parent;
 
   345     typedef typename Parent::UEdge UEdge;
 
   347     template <typename _Value>
 
   349       : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
 
   351       typedef MappableUEdgeSetExtender Graph;
 
   352       typedef IterableMapExtender<
 
   353 	DefaultMap<Graph, UEdge, _Value> > Parent;
 
   355       UEdgeMap(const Graph& _g) 
 
   357       UEdgeMap(const Graph& _g, const _Value& _v) 
 
   360       UEdgeMap& operator=(const UEdgeMap& cmap) {
 
   361 	return operator=<UEdgeMap>(cmap);
 
   364       template <typename CMap>
 
   365       UEdgeMap& operator=(const CMap& cmap) {
 
   366 	checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
 
   367 	const typename Parent::Graph* graph = Parent::getGraph();
 
   369 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   370 	  Parent::set(it, cmap[it]);
 
   380   template <typename _Base>
 
   381   class MappableBpUGraphExtender : public _Base {
 
   384     typedef _Base Parent;
 
   385     typedef MappableBpUGraphExtender Graph;
 
   387     typedef typename Parent::Node Node;
 
   388     typedef typename Parent::ANode ANode;
 
   389     typedef typename Parent::BNode BNode;
 
   390     typedef typename Parent::Edge Edge;
 
   391     typedef typename Parent::UEdge UEdge;
 
   393     template <typename _Value>
 
   395       : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {
 
   397       typedef MappableBpUGraphExtender Graph;
 
   398       typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> > 
 
   401       ANodeMap(const Graph& _g) 
 
   403       ANodeMap(const Graph& _g, const _Value& _v) 
 
   406       ANodeMap& operator=(const ANodeMap& cmap) {
 
   407 	return operator=<ANodeMap>(cmap);
 
   411       /// \brief Template assign operator.
 
   413       /// The given parameter should be conform to the ReadMap
 
   414       /// concept and could be indiced by the current item set of
 
   415       /// the ANodeMap. In this case the value for each item
 
   416       /// is assigned by the value of the given ReadMap. 
 
   417       template <typename CMap>
 
   418       ANodeMap& operator=(const CMap& cmap) {
 
   419 	checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
 
   420 	const typename Parent::Graph* graph = Parent::getGraph();
 
   422 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   423 	  Parent::set(it, cmap[it]);
 
   430     template <typename _Value>
 
   432       : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {
 
   434       typedef MappableBpUGraphExtender Graph;
 
   435       typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> > 
 
   438       BNodeMap(const Graph& _g) 
 
   440       BNodeMap(const Graph& _g, const _Value& _v) 
 
   443       BNodeMap& operator=(const BNodeMap& cmap) {
 
   444 	return operator=<BNodeMap>(cmap);
 
   448       /// \brief Template assign operator.
 
   450       /// The given parameter should be conform to the ReadMap
 
   451       /// concept and could be indiced by the current item set of
 
   452       /// the BNodeMap. In this case the value for each item
 
   453       /// is assigned by the value of the given ReadMap. 
 
   454       template <typename CMap>
 
   455       BNodeMap& operator=(const CMap& cmap) {
 
   456 	checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
 
   457 	const typename Parent::Graph* graph = Parent::getGraph();
 
   459 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   460 	  Parent::set(it, cmap[it]);
 
   469     template <typename _Value>
 
   470     class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
 
   472       typedef MappableBpUGraphExtender Graph;
 
   475       typedef _Value Value;
 
   477       /// The reference type of the map;
 
   478       typedef typename BNodeMap<_Value>::Reference Reference;
 
   479       /// The pointer type of the map;
 
   480       typedef typename BNodeMap<_Value>::Pointer Pointer;
 
   482       /// The const value type of the map.
 
   483       typedef const Value ConstValue;
 
   484       /// The const reference type of the map;
 
   485       typedef typename BNodeMap<_Value>::ConstReference ConstReference;
 
   486       /// The pointer type of the map;
 
   487       typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
 
   489       typedef True ReferenceMapTag;
 
   491       NodeMapBase(const Graph& _g) 
 
   492 	: graph(&_g), bNodeMap(_g), aNodeMap(_g) {
 
   493 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
 
   495       NodeMapBase(const Graph& _g, const _Value& _v) 
 
   496 	: graph(&_g), bNodeMap(_g, _v), 
 
   498 	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
 
   501       virtual ~NodeMapBase() {      
 
   502 	if (Parent::NodeNotifier::ObserverBase::attached()) {
 
   503 	  Parent::NodeNotifier::ObserverBase::detach();
 
   507       ConstReference operator[](const Key& node) const {
 
   508 	if (Parent::aNode(node)) {
 
   509 	  return aNodeMap[node];
 
   511 	  return bNodeMap[node];
 
   515       Reference operator[](const Key& node) {
 
   516 	if (Parent::aNode(node)) {
 
   517 	  return aNodeMap[node];
 
   519 	  return bNodeMap[node];
 
   523       void set(const Key& node, const Value& value) {
 
   524 	if (Parent::aNode(node)) {
 
   525 	  aNodeMap.set(node, value);
 
   527 	  bNodeMap.set(node, value);
 
   533       virtual void add(const Node&) {}
 
   534       virtual void add(const std::vector<Node>&) {}
 
   535       virtual void erase(const Node&) {}
 
   536       virtual void erase(const std::vector<Node>&) {}
 
   537       virtual void clear() {}
 
   538       virtual void build() {}
 
   540       const Graph* getGraph() const { return graph; }
 
   544       BNodeMap<_Value> bNodeMap;
 
   545       ANodeMap<_Value> aNodeMap;
 
   550     template <typename _Value>
 
   552       : public IterableMapExtender<NodeMapBase<_Value> > {
 
   554       typedef MappableBpUGraphExtender Graph;
 
   555       typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
 
   557       NodeMap(const Graph& _g) 
 
   559       NodeMap(const Graph& _g, const _Value& _v) 
 
   562       NodeMap& operator=(const NodeMap& cmap) {
 
   563 	return operator=<NodeMap>(cmap);
 
   567       /// \brief Template assign operator.
 
   569       /// The given parameter should be conform to the ReadMap
 
   570       /// concept and could be indiced by the current item set of
 
   571       /// the NodeMap. In this case the value for each item
 
   572       /// is assigned by the value of the given ReadMap. 
 
   573       template <typename CMap>
 
   574       NodeMap& operator=(const CMap& cmap) {
 
   575 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
 
   576 	const typename Parent::Graph* graph = Parent::getGraph();
 
   578 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   579 	  Parent::set(it, cmap[it]);
 
   588     template <typename _Value>
 
   590       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
 
   592       typedef MappableBpUGraphExtender Graph;
 
   593       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
 
   595       EdgeMap(const Graph& _g) 
 
   597       EdgeMap(const Graph& _g, const _Value& _v) 
 
   600       EdgeMap& operator=(const EdgeMap& cmap) {
 
   601 	return operator=<EdgeMap>(cmap);
 
   604       template <typename CMap>
 
   605       EdgeMap& operator=(const CMap& cmap) {
 
   606 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
 
   607 	const typename Parent::Graph* graph = Parent::getGraph();
 
   609 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   610 	  Parent::set(it, cmap[it]);
 
   616     template <typename _Value>
 
   618       : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
 
   620       typedef MappableBpUGraphExtender Graph;
 
   621       typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > 
 
   624       UEdgeMap(const Graph& _g) 
 
   626       UEdgeMap(const Graph& _g, const _Value& _v) 
 
   629       UEdgeMap& operator=(const UEdgeMap& cmap) {
 
   630 	return operator=<UEdgeMap>(cmap);
 
   633       template <typename CMap>
 
   634       UEdgeMap& operator=(const CMap& cmap) {
 
   635 	checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
 
   636 	const typename Parent::Graph* graph = Parent::getGraph();
 
   638 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   639 	  Parent::set(it, cmap[it]);