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. |
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 |