lemon/bits/default_map.h
changeset 1820 22099ef840d7
parent 1703 eb90e3d6bddc
child 1842 8abf74160dc4
     1.1 --- a/lemon/bits/default_map.h	Mon Nov 21 12:07:05 2005 +0000
     1.2 +++ b/lemon/bits/default_map.h	Mon Nov 21 17:48:00 2005 +0000
     1.3 @@ -266,6 +266,272 @@
     1.4  
     1.5    };
     1.6  
     1.7 +
     1.8 +  template <typename _Base>
     1.9 +  class MappableUndirBipartiteGraphExtender : public _Base {
    1.10 +  public:
    1.11 +
    1.12 +    typedef _Base Parent;
    1.13 +    typedef MappableUndirBipartiteGraphExtender Graph;
    1.14 +
    1.15 +    typedef typename Parent::Node Node;
    1.16 +    typedef typename Parent::UpperNode UpperNode;
    1.17 +    typedef typename Parent::LowerNode LowerNode;
    1.18 +    typedef typename Parent::Edge Edge;
    1.19 +    typedef typename Parent::UndirEdge UndirEdge;
    1.20 +    
    1.21 +    template <typename _Value>
    1.22 +    class UpperNodeMap 
    1.23 +      : public IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > {
    1.24 +    public:
    1.25 +      typedef MappableUndirBipartiteGraphExtender Graph;
    1.26 +      typedef IterableMapExtender<DefaultMap<Graph, UpperNode, _Value> > 
    1.27 +      Parent;
    1.28 +    
    1.29 +      UpperNodeMap(const Graph& _g) 
    1.30 +	: Parent(_g) {}
    1.31 +      UpperNodeMap(const Graph& _g, const _Value& _v) 
    1.32 +	: Parent(_g, _v) {}
    1.33 +    
    1.34 +      UpperNodeMap& operator=(const UpperNodeMap& cmap) {
    1.35 +	return operator=<UpperNodeMap>(cmap);
    1.36 +      }
    1.37 +    
    1.38 +
    1.39 +      /// \brief Template assign operator.
    1.40 +      ///
    1.41 +      /// The given parameter should be conform to the ReadMap
    1.42 +      /// concept and could be indiced by the current item set of
    1.43 +      /// the UpperNodeMap. In this case the value for each item
    1.44 +      /// is assigned by the value of the given ReadMap. 
    1.45 +      template <typename CMap>
    1.46 +      UpperNodeMap& operator=(const CMap& cmap) {
    1.47 +	checkConcept<concept::ReadMap<UpperNode, _Value>, CMap>();
    1.48 +	const typename Parent::Graph* graph = Parent::getGraph();
    1.49 +	UpperNode it;
    1.50 +	for (graph->first(it); it != INVALID; graph->next(it)) {
    1.51 +	  Parent::set(it, cmap[it]);
    1.52 +	}
    1.53 +	return *this;
    1.54 +      }
    1.55 +    
    1.56 +    };
    1.57 +
    1.58 +    template <typename _Value>
    1.59 +    class LowerNodeMap 
    1.60 +      : public IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > {
    1.61 +    public:
    1.62 +      typedef MappableUndirBipartiteGraphExtender Graph;
    1.63 +      typedef IterableMapExtender<DefaultMap<Graph, LowerNode, _Value> > 
    1.64 +      Parent;
    1.65 +    
    1.66 +      LowerNodeMap(const Graph& _g) 
    1.67 +	: Parent(_g) {}
    1.68 +      LowerNodeMap(const Graph& _g, const _Value& _v) 
    1.69 +	: Parent(_g, _v) {}
    1.70 +    
    1.71 +      LowerNodeMap& operator=(const LowerNodeMap& cmap) {
    1.72 +	return operator=<LowerNodeMap>(cmap);
    1.73 +      }
    1.74 +    
    1.75 +
    1.76 +      /// \brief Template assign operator.
    1.77 +      ///
    1.78 +      /// The given parameter should be conform to the ReadMap
    1.79 +      /// concept and could be indiced by the current item set of
    1.80 +      /// the LowerNodeMap. In this case the value for each item
    1.81 +      /// is assigned by the value of the given ReadMap. 
    1.82 +      template <typename CMap>
    1.83 +      LowerNodeMap& operator=(const CMap& cmap) {
    1.84 +	checkConcept<concept::ReadMap<LowerNode, _Value>, CMap>();
    1.85 +	const typename Parent::Graph* graph = Parent::getGraph();
    1.86 +	LowerNode it;
    1.87 +	for (graph->first(it); it != INVALID; graph->next(it)) {
    1.88 +	  Parent::set(it, cmap[it]);
    1.89 +	}
    1.90 +	return *this;
    1.91 +      }
    1.92 +    
    1.93 +    };
    1.94 +
    1.95 +  protected:
    1.96 +
    1.97 +    template <typename _Value>
    1.98 +    class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
    1.99 +    public:
   1.100 +      typedef MappableUndirBipartiteGraphExtender Graph;
   1.101 +
   1.102 +      typedef Node Key;
   1.103 +      typedef _Value Value;
   1.104 +
   1.105 +      /// The reference type of the map;
   1.106 +      typedef typename LowerNodeMap<_Value>::Reference Reference;
   1.107 +      /// The pointer type of the map;
   1.108 +      typedef typename LowerNodeMap<_Value>::Pointer Pointer;
   1.109 +      
   1.110 +      /// The const value type of the map.
   1.111 +      typedef const Value ConstValue;
   1.112 +      /// The const reference type of the map;
   1.113 +      typedef typename LowerNodeMap<_Value>::ConstReference ConstReference;
   1.114 +      /// The pointer type of the map;
   1.115 +      typedef typename LowerNodeMap<_Value>::ConstPointer ConstPointer;
   1.116 +
   1.117 +      typedef True ReferenceMapTag;
   1.118 +
   1.119 +      NodeMapBase(const Graph& _g) 
   1.120 +	: graph(&_g), lowerMap(_g), upperMap(_g) {
   1.121 +	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   1.122 +      }
   1.123 +      NodeMapBase(const Graph& _g, const _Value& _v) 
   1.124 +	: graph(&_g), lowerMap(_g, _v), 
   1.125 +	  upperMap(_g, _v) {
   1.126 +	Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
   1.127 +      }
   1.128 +
   1.129 +      virtual ~NodeMapBase() {      
   1.130 +	if (Parent::NodeNotifier::ObserverBase::attached()) {
   1.131 +	  Parent::NodeNotifier::ObserverBase::detach();
   1.132 +	}
   1.133 +      }
   1.134 +    
   1.135 +      ConstReference operator[](const Key& node) const {
   1.136 +	if (Parent::upper(node)) {
   1.137 +	  return upperMap[node];
   1.138 +	} else {
   1.139 +	  return lowerMap[node];
   1.140 +	}
   1.141 +      } 
   1.142 +
   1.143 +      Reference operator[](const Key& node) {
   1.144 +	if (Parent::upper(node)) {
   1.145 +	  return upperMap[node];
   1.146 +	} else {
   1.147 +	  return lowerMap[node];
   1.148 +	}
   1.149 +      }
   1.150 +
   1.151 +      void set(const Key& node, const Value& value) {
   1.152 +	if (Parent::upper(node)) {
   1.153 +	  upperMap.set(node, value);
   1.154 +	} else {
   1.155 +	  lowerMap.set(node, value);
   1.156 +	}
   1.157 +      }
   1.158 +
   1.159 +    protected:
   1.160 +      
   1.161 +      virtual void add(const Node&) {}
   1.162 +      virtual void erase(const Node&) {}
   1.163 +      virtual void clear() {}
   1.164 +      virtual void build() {}
   1.165 +
   1.166 +      const Graph* getGraph() const { return graph; }
   1.167 +      
   1.168 +    private:
   1.169 +      const Graph* graph;
   1.170 +      LowerNodeMap<_Value> lowerMap;
   1.171 +      UpperNodeMap<_Value> upperMap;
   1.172 +    };
   1.173 +    
   1.174 +  public:
   1.175 +
   1.176 +    template <typename _Value>
   1.177 +    class NodeMap 
   1.178 +      : public IterableMapExtender<NodeMapBase<_Value> > {
   1.179 +    public:
   1.180 +      typedef MappableUndirBipartiteGraphExtender Graph;
   1.181 +      typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
   1.182 +    
   1.183 +      NodeMap(const Graph& _g) 
   1.184 +	: Parent(_g) {}
   1.185 +      NodeMap(const Graph& _g, const _Value& _v) 
   1.186 +	: Parent(_g, _v) {}
   1.187 +    
   1.188 +      NodeMap& operator=(const NodeMap& cmap) {
   1.189 +	return operator=<NodeMap>(cmap);
   1.190 +      }
   1.191 +    
   1.192 +
   1.193 +      /// \brief Template assign operator.
   1.194 +      ///
   1.195 +      /// The given parameter should be conform to the ReadMap
   1.196 +      /// concept and could be indiced by the current item set of
   1.197 +      /// the NodeMap. In this case the value for each item
   1.198 +      /// is assigned by the value of the given ReadMap. 
   1.199 +      template <typename CMap>
   1.200 +      NodeMap& operator=(const CMap& cmap) {
   1.201 +	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
   1.202 +	const typename Parent::Graph* graph = Parent::getGraph();
   1.203 +	Node it;
   1.204 +	for (graph->first(it); it != INVALID; graph->next(it)) {
   1.205 +	  Parent::set(it, cmap[it]);
   1.206 +	}
   1.207 +	return *this;
   1.208 +      }
   1.209 +    
   1.210 +    };
   1.211 +
   1.212 +
   1.213 +
   1.214 +    template <typename _Value>
   1.215 +    class EdgeMap 
   1.216 +      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
   1.217 +    public:
   1.218 +      typedef MappableUndirBipartiteGraphExtender Graph;
   1.219 +      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   1.220 +    
   1.221 +      EdgeMap(const Graph& _g) 
   1.222 +	: Parent(_g) {}
   1.223 +      EdgeMap(const Graph& _g, const _Value& _v) 
   1.224 +	: Parent(_g, _v) {}
   1.225 +    
   1.226 +      EdgeMap& operator=(const EdgeMap& cmap) {
   1.227 +	return operator=<EdgeMap>(cmap);
   1.228 +      }
   1.229 +    
   1.230 +      template <typename CMap>
   1.231 +      EdgeMap& operator=(const CMap& cmap) {
   1.232 +	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
   1.233 +	const typename Parent::Graph* graph = Parent::getGraph();
   1.234 +	Edge it;
   1.235 +	for (graph->first(it); it != INVALID; graph->next(it)) {
   1.236 +	  Parent::set(it, cmap[it]);
   1.237 +	}
   1.238 +	return *this;
   1.239 +      }
   1.240 +    };
   1.241 +
   1.242 +    template <typename _Value>
   1.243 +    class UndirEdgeMap 
   1.244 +      : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
   1.245 +    public:
   1.246 +      typedef MappableUndirBipartiteGraphExtender Graph;
   1.247 +      typedef IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > 
   1.248 +      Parent;
   1.249 +    
   1.250 +      UndirEdgeMap(const Graph& _g) 
   1.251 +	: Parent(_g) {}
   1.252 +      UndirEdgeMap(const Graph& _g, const _Value& _v) 
   1.253 +	: Parent(_g, _v) {}
   1.254 +    
   1.255 +      UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
   1.256 +	return operator=<UndirEdgeMap>(cmap);
   1.257 +      }
   1.258 +    
   1.259 +      template <typename CMap>
   1.260 +      UndirEdgeMap& operator=(const CMap& cmap) {
   1.261 +	checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
   1.262 +	const typename Parent::Graph* graph = Parent::getGraph();
   1.263 +	UndirEdge it;
   1.264 +	for (graph->first(it); it != INVALID; graph->next(it)) {
   1.265 +	  Parent::set(it, cmap[it]);
   1.266 +	}
   1.267 +	return *this;
   1.268 +      }
   1.269 +    };
   1.270 +  
   1.271 +  };
   1.272 +
   1.273  }
   1.274  
   1.275  #endif