2  * src/hugo/map_registry.h - Part of HUGOlib, a generic C++ optimization library
 
     4  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
 
     7  * Permission to use, modify and distribute this software is granted
 
     8  * provided that this copyright notice appears in all copies. For
 
     9  * precise terms see the accompanying LICENSE file.
 
    11  * This software is provided "AS IS" with no warranty of any kind,
 
    12  * express or implied, and with no claim as to its suitability for any
 
    17 #ifndef HUGO_MAP_REGISTRY_H
 
    18 #define HUGO_MAP_REGISTRY_H
 
    22 ///\ingroup graphmapfactory
 
    24 ///\brief Map registry for graph maps.
 
    30 /// \addtogroup graphmapfactory
 
    34  * Registry class to register edge or node maps into the graph. The
 
    35  * registry helps you to implement an observer pattern. If you add
 
    36  * or erase an edge or node you must notify all the maps about the
 
    39   template <typename G, typename K, typename KIt>
 
    47      * MapBase is the base class of the registered maps.
 
    48      * It defines the core modification operations on the maps and
 
    49      * implements some helper functions. 
 
    57       typedef MapRegistry<G, K, KIt> Registry;
 
    59       friend class MapRegistry<G, K, KIt>;
 
    62        * Default constructor for MapBase.
 
    65       MapBase() : graph(0), registry(0) {}
 
    68        * Simple constructor to register into a graph registry.
 
    71       MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
 
    76        * Copy constructor to register into the registry.
 
    79       MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
 
    81 	  copy.registry->attach(*this);
 
    88       const MapBase& operator=(const MapBase& copy) {
 
    90 	  registry->detach(*this);
 
    94 	  copy.registry->attach(*this);
 
   106 	  registry->detach(*this);
 
   111        * Returns the graph that the map belongs to.
 
   114       const Graph* getGraph() const { return graph; }
 
   126 	 Helper function to implement constructors in the subclasses.
 
   129       virtual void init() {
 
   130 	for (KeyIt it(*graph); it != INVALID; ++it) {
 
   136 	 Helper function to implement the destructor in the subclasses.
 
   139       virtual void destroy() {
 
   140 	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
 
   146 	  The add member function should be overloaded in the subclasses.
 
   147 	  \e Add extends the map with the new node.
 
   150       virtual void add(const KeyType&) = 0;	
 
   152 	  The erase member function should be overloaded in the subclasses.
 
   153 	  \e Erase removes the node from the map.
 
   156       virtual void erase(const KeyType&) = 0;
 
   159        *  The clear member function should be overloaded in the subclasses.
 
   160        *  \e Clear makes empty the data structure.
 
   163       virtual void clear() = 0;
 
   166 	 Exception class to throw at unsupported operation.
 
   169       class NotSupportedOperationException {};
 
   176      * The container type of the maps.
 
   178     typedef std::vector<MapBase*> Container; 
 
   181      * The container of the registered maps.
 
   189      * Default Constructor of the MapRegistry. It creates an empty registry.
 
   194      * Copy Constructor of the MapRegistry. The new registry does not steal
 
   195      * the maps from the right value. The new registry will be an empty.
 
   197     MapRegistry(const MapRegistry&) {}
 
   200      * Assign operator. The left value does not steal the maps 
 
   201      * from the right value. The left value will be an empty registry.
 
   203     MapRegistry& operator=(const MapRegistry&) {
 
   204       typename Container::iterator it;
 
   205       for (it = container.begin(); it != container.end(); ++it) {
 
   213      * Destructor of the MapRegistry.
 
   216       typename Container::iterator it;
 
   217       for (it = container.begin(); it != container.end(); ++it) {
 
   228      * Attach a map into thr registry. If the map has been attached
 
   229      * into an other registry it is detached from that automaticly.
 
   231     void attach(MapBase& map) {
 
   233 	map.registry->detach(map);
 
   235       container.push_back(&map);
 
   237       map.registry_index = container.size()-1;
 
   241      * Detach the map from the registry.
 
   243     void detach(MapBase& map) {
 
   244       container.back()->registry_index = map.registry_index; 
 
   245       container[map.registry_index] = container.back();
 
   246       container.pop_back();
 
   253      * Notify all the registered maps about a Key added.
 
   255     void add(const KeyType& key) {
 
   256       typename Container::iterator it;
 
   257       for (it = container.begin(); it != container.end(); ++it) {
 
   263      * Notify all the registered maps about a Key erased.
 
   265     void erase(const KeyType& key) {
 
   266       typename Container::iterator it;
 
   267       for (it = container.begin(); it != container.end(); ++it) {
 
   273      * Notify all the registered maps about the map should be cleared.
 
   276       typename Container::iterator it;
 
   277       for (it = container.begin(); it != container.end(); ++it) {