lemon/bits/graph_extender.h
changeset 2039 dacc4ce9474d
parent 1999 2ff283124dfc
child 2046 66d160810c0a
equal deleted inserted replaced
13:d7661f8199c2 14:8f87cd32dda5
   101       NodeIt() {}
   101       NodeIt() {}
   102 
   102 
   103       NodeIt(Invalid i) : Node(i) { }
   103       NodeIt(Invalid i) : Node(i) { }
   104 
   104 
   105       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   105       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   106 	_graph.first(*static_cast<Node*>(this));
   106 	_graph.first(static_cast<Node&>(*this));
   107       }
   107       }
   108 
   108 
   109       NodeIt(const Graph& _graph, const Node& node) 
   109       NodeIt(const Graph& _graph, const Node& node) 
   110 	: Node(node), graph(&_graph) {}
   110 	: Node(node), graph(&_graph) {}
   111 
   111 
   124       EdgeIt() { }
   124       EdgeIt() { }
   125 
   125 
   126       EdgeIt(Invalid i) : Edge(i) { }
   126       EdgeIt(Invalid i) : Edge(i) { }
   127 
   127 
   128       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   128       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   129 	_graph.first(*static_cast<Edge*>(this));
   129 	_graph.first(static_cast<Edge&>(*this));
   130       }
   130       }
   131 
   131 
   132       EdgeIt(const Graph& _graph, const Edge& e) : 
   132       EdgeIt(const Graph& _graph, const Edge& e) : 
   133 	Edge(e), graph(&_graph) { }
   133 	Edge(e), graph(&_graph) { }
   134 
   134 
   230 
   230 
   231       NodeMap& operator=(const NodeMap& cmap) {
   231       NodeMap& operator=(const NodeMap& cmap) {
   232 	return operator=<NodeMap>(cmap);
   232 	return operator=<NodeMap>(cmap);
   233       }
   233       }
   234 
   234 
   235 
       
   236       /// \brief Template assign operator.
       
   237       ///
       
   238       /// The given parameter should be conform to the ReadMap
       
   239       /// concecpt and could be indiced by the current item set of
       
   240       /// the NodeMap. In this case the value for each item
       
   241       /// is assigned by the value of the given ReadMap. 
       
   242       template <typename CMap>
   235       template <typename CMap>
   243       NodeMap& operator=(const CMap& cmap) {
   236       NodeMap& operator=(const CMap& cmap) {
   244 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
   237         Parent::operator=(cmap);
   245 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
   246 	Node it;
       
   247 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
   248 	  Parent::set(it, cmap[it]);
       
   249 	}
       
   250 	return *this;
   238 	return *this;
   251       }
   239       }
   252 
   240 
   253     };
   241     };
   254 
   242 
   268 	return operator=<EdgeMap>(cmap);
   256 	return operator=<EdgeMap>(cmap);
   269       }
   257       }
   270 
   258 
   271       template <typename CMap>
   259       template <typename CMap>
   272       EdgeMap& operator=(const CMap& cmap) {
   260       EdgeMap& operator=(const CMap& cmap) {
   273 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
   261         Parent::operator=(cmap);
   274 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
   275 	Edge it;
       
   276 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
   277 	  Parent::set(it, cmap[it]);
       
   278 	}
       
   279 	return *this;
   262 	return *this;
   280       }
   263       }
   281     };
   264     };
   282 
   265 
   283 
   266 
   429       NodeIt() {}
   412       NodeIt() {}
   430 
   413 
   431       NodeIt(Invalid i) : Node(i) { }
   414       NodeIt(Invalid i) : Node(i) { }
   432 
   415 
   433       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   416       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
   434 	_graph.first(*static_cast<Node*>(this));
   417 	_graph.first(static_cast<Node&>(*this));
   435       }
   418       }
   436 
   419 
   437       NodeIt(const Graph& _graph, const Node& node) 
   420       NodeIt(const Graph& _graph, const Node& node) 
   438 	: Node(node), graph(&_graph) {}
   421 	: Node(node), graph(&_graph) {}
   439 
   422 
   452       EdgeIt() { }
   435       EdgeIt() { }
   453 
   436 
   454       EdgeIt(Invalid i) : Edge(i) { }
   437       EdgeIt(Invalid i) : Edge(i) { }
   455 
   438 
   456       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   439       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
   457 	_graph.first(*static_cast<Edge*>(this));
   440 	_graph.first(static_cast<Edge&>(*this));
   458       }
   441       }
   459 
   442 
   460       EdgeIt(const Graph& _graph, const Edge& e) : 
   443       EdgeIt(const Graph& _graph, const Edge& e) : 
   461 	Edge(e), graph(&_graph) { }
   444 	Edge(e), graph(&_graph) { }
   462 
   445 
   523       UEdgeIt() { }
   506       UEdgeIt() { }
   524 
   507 
   525       UEdgeIt(Invalid i) : UEdge(i) { }
   508       UEdgeIt(Invalid i) : UEdge(i) { }
   526 
   509 
   527       explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
   510       explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
   528 	_graph.first(*static_cast<UEdge*>(this));
   511 	_graph.first(static_cast<UEdge&>(*this));
   529       }
   512       }
   530 
   513 
   531       UEdgeIt(const Graph& _graph, const UEdge& e) : 
   514       UEdgeIt(const Graph& _graph, const UEdge& e) : 
   532 	UEdge(e), graph(&_graph) { }
   515 	UEdge(e), graph(&_graph) { }
   533 
   516 
   620 
   603 
   621       NodeMap& operator=(const NodeMap& cmap) {
   604       NodeMap& operator=(const NodeMap& cmap) {
   622 	return operator=<NodeMap>(cmap);
   605 	return operator=<NodeMap>(cmap);
   623       }
   606       }
   624 
   607 
   625 
       
   626       /// \brief Template assign operator.
       
   627       ///
       
   628       /// The given parameter should be conform to the ReadMap
       
   629       /// concecpt and could be indiced by the current item set of
       
   630       /// the NodeMap. In this case the value for each item
       
   631       /// is assigned by the value of the given ReadMap. 
       
   632       template <typename CMap>
   608       template <typename CMap>
   633       NodeMap& operator=(const CMap& cmap) {
   609       NodeMap& operator=(const CMap& cmap) {
   634 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
   610         Parent::operator=(cmap);
   635 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
   636 	Node it;
       
   637 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
   638 	  Parent::set(it, cmap[it]);
       
   639 	}
       
   640 	return *this;
   611 	return *this;
   641       }
   612       }
   642 
   613 
   643     };
   614     };
   644 
   615 
   658 	return operator=<EdgeMap>(cmap);
   629 	return operator=<EdgeMap>(cmap);
   659       }
   630       }
   660 
   631 
   661       template <typename CMap>
   632       template <typename CMap>
   662       EdgeMap& operator=(const CMap& cmap) {
   633       EdgeMap& operator=(const CMap& cmap) {
   663 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
   634         Parent::operator=(cmap);
   664 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
   665 	Edge it;
       
   666 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
   667 	  Parent::set(it, cmap[it]);
       
   668 	}
       
   669 	return *this;
   635 	return *this;
   670       }
   636       }
   671     };
   637     };
   672 
   638 
   673 
   639 
   678       typedef UGraphExtender Graph;
   644       typedef UGraphExtender Graph;
   679       typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
   645       typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
   680 
   646 
   681       UEdgeMap(const Graph& graph) 
   647       UEdgeMap(const Graph& graph) 
   682 	: Parent(graph) {}
   648 	: Parent(graph) {}
       
   649 
   683       UEdgeMap(const Graph& graph, const _Value& value) 
   650       UEdgeMap(const Graph& graph, const _Value& value) 
   684 	: Parent(graph, value) {}
   651 	: Parent(graph, value) {}
   685 
   652 
   686       UEdgeMap& operator=(const UEdgeMap& cmap) {
   653       UEdgeMap& operator=(const UEdgeMap& cmap) {
   687 	return operator=<UEdgeMap>(cmap);
   654 	return operator=<UEdgeMap>(cmap);
   688       }
   655       }
   689 
   656 
   690       template <typename CMap>
   657       template <typename CMap>
   691       UEdgeMap& operator=(const CMap& cmap) {
   658       UEdgeMap& operator=(const CMap& cmap) {
   692 	checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
   659         Parent::operator=(cmap);
   693 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
   694 	Edge it;
       
   695 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
   696 	  Parent::set(it, cmap[it]);
       
   697 	}
       
   698 	return *this;
   660 	return *this;
   699       }
   661       }
       
   662 
   700     };
   663     };
   701 
   664 
   702     // Alteration extension
   665     // Alteration extension
   703 
   666 
   704     Node addNode() {
   667     Node addNode() {
  1102     
  1065     
  1103       ANodeMap& operator=(const ANodeMap& cmap) {
  1066       ANodeMap& operator=(const ANodeMap& cmap) {
  1104 	return operator=<ANodeMap>(cmap);
  1067 	return operator=<ANodeMap>(cmap);
  1105       }
  1068       }
  1106     
  1069     
  1107 
       
  1108       /// \brief Template assign operator.
       
  1109       ///
       
  1110       /// The given parameter should be conform to the ReadMap
       
  1111       /// concept and could be indiced by the current item set of
       
  1112       /// the ANodeMap. In this case the value for each item
       
  1113       /// is assigned by the value of the given ReadMap. 
       
  1114       template <typename CMap>
  1070       template <typename CMap>
  1115       ANodeMap& operator=(const CMap& cmap) {
  1071       ANodeMap& operator=(const CMap& cmap) {
  1116 	checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
  1072         Parent::operator=(cmap);
  1117 	const typename Parent::Graph* graph = Parent::getGraph();
       
  1118 	ANode it;
       
  1119 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
  1120 	  Parent::set(it, cmap[it]);
       
  1121 	}
       
  1122 	return *this;
  1073 	return *this;
  1123       }
  1074       }
  1124     
  1075     
  1125     };
  1076     };
  1126 
  1077 
  1138     
  1089     
  1139       BNodeMap& operator=(const BNodeMap& cmap) {
  1090       BNodeMap& operator=(const BNodeMap& cmap) {
  1140 	return operator=<BNodeMap>(cmap);
  1091 	return operator=<BNodeMap>(cmap);
  1141       }
  1092       }
  1142     
  1093     
  1143 
       
  1144       /// \brief Template assign operator.
       
  1145       ///
       
  1146       /// The given parameter should be conform to the ReadMap
       
  1147       /// concept and could be indiced by the current item set of
       
  1148       /// the BNodeMap. In this case the value for each item
       
  1149       /// is assigned by the value of the given ReadMap. 
       
  1150       template <typename CMap>
  1094       template <typename CMap>
  1151       BNodeMap& operator=(const CMap& cmap) {
  1095       BNodeMap& operator=(const CMap& cmap) {
  1152 	checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
  1096         Parent::operator=(cmap);
  1153 	const typename Parent::Graph* graph = Parent::getGraph();
       
  1154 	BNode it;
       
  1155 	for (graph->first(it); it != INVALID; graph->next(it)) {
       
  1156 	  Parent::set(it, cmap[it]);
       
  1157 	}
       
  1158 	return *this;
  1097 	return *this;
  1159       }
  1098       }
  1160     
  1099     
  1161     };
  1100     };
  1162 
  1101 
  1163   protected:
  1102   public:
  1164 
  1103 
  1165     template <typename _Value>
  1104     template <typename _Value>
  1166     class NodeMapBase {
  1105     class NodeMap {
  1167     public:
  1106     public:
  1168       typedef BpUGraphExtender Graph;
  1107       typedef BpUGraphExtender Graph;
  1169 
  1108 
  1170       typedef Node Key;
  1109       typedef Node Key;
  1171       typedef _Value Value;
  1110       typedef _Value Value;
  1175       /// The const reference type of the map;
  1114       /// The const reference type of the map;
  1176       typedef typename ANodeMap<_Value>::ConstReference ConstReference;
  1115       typedef typename ANodeMap<_Value>::ConstReference ConstReference;
  1177 
  1116 
  1178       typedef True ReferenceMapTag;
  1117       typedef True ReferenceMapTag;
  1179 
  1118 
  1180       NodeMapBase(const Graph& graph) 
  1119       NodeMap(const Graph& _graph) 
  1181 	: aNodeMap(graph), bNodeMap(graph) {}
  1120 	: graph(_graph), aNodeMap(_graph), bNodeMap(_graph) {}
  1182       NodeMapBase(const Graph& graph, const _Value& value) 
  1121       NodeMap(const Graph& _graph, const _Value& _value) 
  1183 	: aNodeMap(graph, value), bNodeMap(graph, value) {}
  1122 	: graph(_graph), aNodeMap(_graph, _value), bNodeMap(_graph, _value) {}
       
  1123 
       
  1124       NodeMap& operator=(const NodeMap& cmap) {
       
  1125 	return operator=<NodeMap>(cmap);
       
  1126       }
       
  1127     
       
  1128       template <typename CMap>
       
  1129       NodeMap& operator=(const CMap& cmap) {
       
  1130 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
       
  1131 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
  1132 	Edge it;
       
  1133 	for (graph.first(it); it != INVALID; graph.next(it)) {
       
  1134 	  Parent::set(it, cmap[it]);
       
  1135 	}
       
  1136 	return *this;
       
  1137       }
  1184 
  1138 
  1185       ConstReference operator[](const Key& node) const {
  1139       ConstReference operator[](const Key& node) const {
  1186 	if (Parent::aNode(node)) {
  1140 	if (Parent::aNode(node)) {
  1187 	  return aNodeMap[node];
  1141 	  return aNodeMap[node];
  1188 	} else {
  1142 	} else {
  1204 	} else {
  1158 	} else {
  1205 	  bNodeMap.set(node, value);
  1159 	  bNodeMap.set(node, value);
  1206 	}
  1160 	}
  1207       }
  1161       }
  1208 
  1162 
  1209       const Graph* getGraph() const {
  1163       class MapIt : public NodeIt {
  1210         return aNodeMap.getGraph();
  1164       public:
  1211       }
  1165 
  1212 
  1166         typedef NodeIt Parent;
       
  1167         
       
  1168         explicit MapIt(NodeMap& _map) 
       
  1169           : Parent(_map.graph), map(_map) {}
       
  1170         
       
  1171         typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
       
  1172           return map[*this];
       
  1173         }
       
  1174         
       
  1175         typename MapTraits<NodeMap>::ReturnValue operator*() {
       
  1176           return map[*this];
       
  1177         }
       
  1178         
       
  1179         void set(const Value& value) {
       
  1180           map.set(*this, value);
       
  1181         }
       
  1182 
       
  1183       private:
       
  1184         NodeMap& map;
       
  1185       };
       
  1186 
       
  1187       class ConstMapIt : public NodeIt {
       
  1188       public:
       
  1189 
       
  1190         typedef NodeIt Parent;
       
  1191         
       
  1192         explicit ConstMapIt(const NodeMap& _map) 
       
  1193           : Parent(_map.graph), map(_map) {}
       
  1194         
       
  1195         typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
       
  1196           return map[*this];
       
  1197         }
       
  1198         
       
  1199       private:
       
  1200         const NodeMap& map;
       
  1201       };
       
  1202 
       
  1203       class ItemIt : public NodeIt {
       
  1204       public:
       
  1205 
       
  1206         typedef NodeIt Parent;
       
  1207 
       
  1208         explicit ItemIt(const NodeMap& _map)
       
  1209           : Parent(_map.graph) {}
       
  1210         
       
  1211       };
       
  1212       
  1213     private:
  1213     private:
       
  1214       const Graph& graph;
  1214       ANodeMap<_Value> aNodeMap;
  1215       ANodeMap<_Value> aNodeMap;
  1215       BNodeMap<_Value> bNodeMap;
  1216       BNodeMap<_Value> bNodeMap;
  1216     };
  1217     };
  1217     
  1218     
  1218   public:
       
  1219 
       
  1220     template <typename _Value>
       
  1221     class NodeMap 
       
  1222       : public MapExtender<NodeMapBase<_Value> > {
       
  1223     public:
       
  1224       typedef BpUGraphExtender Graph;
       
  1225       typedef MapExtender< NodeMapBase<_Value> > Parent;
       
  1226     
       
  1227       NodeMap(const Graph& graph) 
       
  1228 	: Parent(graph) {}
       
  1229       NodeMap(const Graph& graph, const _Value& value) 
       
  1230 	: Parent(graph, value) {}
       
  1231     
       
  1232       NodeMap& operator=(const NodeMap& cmap) {
       
  1233 	return operator=<NodeMap>(cmap);
       
  1234       }
       
  1235     
       
  1236 
       
  1237       /// \brief Template assign operator.
       
  1238       ///
       
  1239       /// The given parameter should be conform to the ReadMap
       
  1240       /// concept and could be indiced by the current item set of
       
  1241       /// the NodeMap. In this case the value for each item
       
  1242       /// is assigned by the value of the given ReadMap. 
       
  1243       template <typename CMap>
       
  1244       NodeMap& operator=(const CMap& cmap) {
       
  1245 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
       
  1246 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
  1247 	Edge it;
       
  1248 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
  1249 	  Parent::set(it, cmap[it]);
       
  1250 	}
       
  1251 	return *this;
       
  1252       }
       
  1253     
       
  1254     };
       
  1255 
       
  1256 
       
  1257 
  1219 
  1258     template <typename _Value>
  1220     template <typename _Value>
  1259     class EdgeMap 
  1221     class EdgeMap 
  1260       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
  1222       : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
  1261     public:
  1223     public:
  1271 	return operator=<EdgeMap>(cmap);
  1233 	return operator=<EdgeMap>(cmap);
  1272       }
  1234       }
  1273     
  1235     
  1274       template <typename CMap>
  1236       template <typename CMap>
  1275       EdgeMap& operator=(const CMap& cmap) {
  1237       EdgeMap& operator=(const CMap& cmap) {
  1276 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
  1238         Parent::operator=(cmap);
  1277 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
  1278 	Edge it;
       
  1279 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
  1280 	  Parent::set(it, cmap[it]);
       
  1281 	}
       
  1282 	return *this;
  1239 	return *this;
  1283       }
  1240       }
  1284     };
  1241     };
  1285 
  1242 
  1286     template <typename _Value>
  1243     template <typename _Value>
  1299 	return operator=<UEdgeMap>(cmap);
  1256 	return operator=<UEdgeMap>(cmap);
  1300       }
  1257       }
  1301     
  1258     
  1302       template <typename CMap>
  1259       template <typename CMap>
  1303       UEdgeMap& operator=(const CMap& cmap) {
  1260       UEdgeMap& operator=(const CMap& cmap) {
  1304 	checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
  1261         Parent::operator=(cmap);
  1305 	const typename Parent::Notifier* notifier = Parent::getNotifier();
       
  1306 	Edge it;
       
  1307 	for (notifier->first(it); it != INVALID; notifier->next(it)) {
       
  1308 	  Parent::set(it, cmap[it]);
       
  1309 	}
       
  1310 	return *this;
  1262 	return *this;
  1311       }
  1263       }
  1312     };
  1264     };
  1313 
  1265 
  1314   
  1266