src/lemon/map_registry.h
changeset 940 50a153b08f07
parent 921 818510fa3d99
child 943 cb0ac054ea92
equal deleted inserted replaced
0:7f398e7fc906 1:d6f320795279
    25 
    25 
    26 using namespace std;
    26 using namespace std;
    27 
    27 
    28 namespace lemon {
    28 namespace lemon {
    29 
    29 
    30 /// \addtogroup graphmapfactory
    30   /// \addtogroup graphmapfactory
    31 /// @{
    31   /// @{
    32 
    32 
    33 /** 
    33   /// Map registry for graph maps.
    34  * Registry class to register edge or node maps into the graph. The
    34 
    35  * registry helps you to implement an observer pattern. If you add
    35   /** 
    36  * or erase an edge or node you must notify all the maps about the
    36    * Registry class to register edge or node maps into the graph. The
    37  * event.
    37    * registry helps you to implement an observer pattern. If you add
    38 */
    38    * or erase an edge or node you must notify all the maps about the
       
    39    * event.
       
    40    *
       
    41    * \param G The graph type to register maps.
       
    42    * \param K The key type of the maps registered into the registry.
       
    43    * \param KIt The key iterator type iterates on the keys.
       
    44    *
       
    45    * \author Balazs Dezso
       
    46    */
       
    47 
    39   template <typename G, typename K, typename KIt>
    48   template <typename G, typename K, typename KIt>
    40   class MapRegistry {
    49   class MapRegistry {
    41   public:
    50   public:
    42     typedef G Graph;
    51     typedef G Graph;
    43     typedef K KeyType;
    52     typedef K KeyType;
    44     typedef KIt KeyIt;
    53     typedef KIt KeyIt;
    45 	
    54 
    46     /**
    55     /// MapBase is the base class of the dynamic maps.
    47      * MapBase is the base class of the registered maps.
    56 	
       
    57     /**
       
    58      * MapBase is the base class of the dynamic maps.
    48      * It defines the core modification operations on the maps and
    59      * It defines the core modification operations on the maps and
    49      * implements some helper functions. 
    60      * implements some helper functions. 
    50      */
    61      */
    51     class MapBase {
    62     class MapBase {
    52     public:
    63     public:
    56 
    67 
    57       typedef MapRegistry<G, K, KIt> Registry;
    68       typedef MapRegistry<G, K, KIt> Registry;
    58 	
    69 	
    59       friend class MapRegistry<G, K, KIt>;
    70       friend class MapRegistry<G, K, KIt>;
    60 
    71 
       
    72       /// Default constructor.
       
    73 
    61       /**
    74       /**
    62        * Default constructor for MapBase.
    75        * Default constructor for MapBase.
    63        */
    76        */
    64 
    77 
    65       MapBase() : graph(0), registry(0) {}
    78       MapBase() : graph(0), registry(0) {}
    66 		
    79 
    67       /** 
    80       /// Constructor to register map into a graph registry.
    68        * Simple constructor to register into a graph registry.
    81 		
       
    82       /** 
       
    83        * Simple constructor to register dynamic map into a graph registry.
    69       */
    84       */
    70 	
    85 	
    71       MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
    86       MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
    72 	r.attach(*this);
    87 	r.attach(*this);
    73       }
    88       }
    74 
    89 
       
    90       /// Copy constructor.
       
    91 
    75       /** 
    92       /** 
    76        * Copy constructor to register into the registry.
    93        * Copy constructor to register into the registry.
       
    94        * If the copiable map is registered into a registry
       
    95        * the construated map will be registered to the same registry.
    77       */	
    96       */	
    78 	
    97 	
    79       MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
    98       MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
    80 	if (copy.registry) {
    99 	if (copy.registry) {
    81 	  copy.registry->attach(*this);
   100 	  copy.registry->attach(*this);
    82 	}
   101 	}
    83       } 
   102       } 
    84 	
   103 
    85       /** 
   104       /// Assign operator.
    86        * Assign operator.
   105 	
       
   106       /** 
       
   107        * Assign operator. This member detach first the map
       
   108        * from the current registry and then it attach to the
       
   109        * copiable map's registry if it exists.
    87       */	
   110       */	
    88       const MapBase& operator=(const MapBase& copy) {
   111       const MapBase& operator=(const MapBase& copy) {
    89 	if (registry) {
   112 	if (registry) {
    90 	  registry->detach(*this);
   113 	  registry->detach(*this);
    91 	}
   114 	}
    94 	  copy.registry->attach(*this);
   117 	  copy.registry->attach(*this);
    95 	}
   118 	}
    96 	return *this;
   119 	return *this;
    97       }
   120       }
    98 	
   121 	
    99 
   122       /// Destructor
   100       /** 
   123 
   101        * Destructor. 
   124       /** 
       
   125        * This member detach the map from the its registry if the
       
   126        * registry exists.
   102       */		
   127       */		
   103 
   128 
   104       virtual ~MapBase() {
   129       virtual ~MapBase() {
   105 	if (registry) {
   130 	if (registry) {
   106 	  registry->detach(*this);
   131 	  registry->detach(*this);
   107 	}
   132 	}
   108       }
   133       }
   109 
   134 
       
   135       /// The graph of the map.
       
   136 
   110       /*
   137       /*
   111        * Returns the graph that the map belongs to.
   138        * Returns the graph that the map belongs to.
   112       */
   139       */
   113 
   140 
   114       const Graph* getGraph() const { return graph; }
   141       const Graph* getGraph() const { return graph; }
   119       Registry* registry;
   146       Registry* registry;
   120 
   147 
   121       int registry_index;
   148       int registry_index;
   122 
   149 
   123     protected:
   150     protected:
   124 	
   151 
   125       /**
   152       /// Helper function to implement constructors in the subclasses.
   126 	 Helper function to implement constructors in the subclasses.
   153 	
       
   154       /**
       
   155        * Helper function to implement constructors in the subclasses.
       
   156        * It adds all of the nodes or edges to the map via the 
       
   157        * \ref MapRegistry::MapBase::add add
       
   158        * member function.
   127       */
   159       */
   128 	
   160 	
   129       virtual void init() {
   161       virtual void init() {
   130 	for (KeyIt it(*graph); it != INVALID; ++it) {
   162 	for (KeyIt it(*graph); it != INVALID; ++it) {
   131 	  add(it);
   163 	  add(it);
   132 	}
   164 	}
   133       }
   165       }
   134 	
   166 	
   135       /**
   167 
   136 	 Helper function to implement the destructor in the subclasses.
   168       /// Helper function to implement destructors in the subclasses.
       
   169 	
       
   170       /**
       
   171        * Helper function to implement destructors in the subclasses.
       
   172        * It erases all of the nodes or edges of the map via the 
       
   173        * \ref MapRegistry::MapBase::erase erase
       
   174        * member function. It can be used by the clear function also.
   137       */
   175       */
   138 	
   176 	
   139       virtual void destroy() {
   177       virtual void destroy() {
   140 	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
   178 	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
   141 	  erase(it);
   179 	  erase(it);
   142 	}
   180 	}
   143       }
   181       }
       
   182 
       
   183       /// The member function to add new node or edge to the map.
   144 	
   184 	
   145       /** 
   185       /** 
   146 	  The add member function should be overloaded in the subclasses.
   186 	  The add member function should be overloaded in the subclasses.
   147 	  \e Add extends the map with the new node.
   187 	  \e Add extends the map with the new node or edge.
   148       */
   188       */
   149 	
   189 	
   150       virtual void add(const KeyType&) = 0;	
   190       virtual void add(const KeyType&) = 0;	
       
   191 
       
   192 
       
   193       /// The member function to erase a node or edge from the map.
       
   194 
   151       /** 
   195       /** 
   152 	  The erase member function should be overloaded in the subclasses.
   196 	  The erase member function should be overloaded in the subclasses.
   153 	  \e Erase removes the node from the map.
   197 	  \e Erase removes the node or edge from the map.
   154       */
   198       */
   155 	
   199 	
   156       virtual void erase(const KeyType&) = 0;
   200       virtual void erase(const KeyType&) = 0;
   157 
   201 
   158       /**
   202       /**
   159        *  The clear member function should be overloaded in the subclasses.
   203        *  The clear member function should be overloaded in the subclasses.
   160        *  \e Clear makes empty the data structure.
   204        *  \e Clear makes empty the data structure.
   161        */
   205        */
   162 
   206 
   163       virtual void clear() = 0;
   207       virtual void clear() = 0;
   164 	
   208 
   165       /**
   209       /// Exception class to throw at unsupported operation.
   166 	 Exception class to throw at unsupported operation.
   210 	
       
   211       /**
       
   212        * Exception class to throw at unsupported operation.
       
   213        * If the map does not support erasing or adding new
       
   214        * node or key it must be throwed.
   167       */
   215       */
   168 	
   216 	
   169       class NotSupportedOperationException {};
   217       class NotSupportedOperationException {};
   170 
   218 
   171     };
   219     };
   172 	
   220 	
   173   protected:
   221   protected:
   174 	
   222 	
   175     /** 
   223 
   176      * The container type of the maps.
       
   177      */
       
   178     typedef std::vector<MapBase*> Container; 
   224     typedef std::vector<MapBase*> Container; 
   179 
   225 
   180     /**
       
   181      * The container of the registered maps.
       
   182      */
       
   183     Container container;
   226     Container container;
   184 
   227 
   185 		
   228 		
   186   public:
   229   public:
   187 	
   230 
   188     /**
   231     /// Default constructor.
   189      * Default Constructor of the MapRegistry. It creates an empty registry.
   232 	
       
   233     /**
       
   234      * Default constructor of the \e MapRegistry. 
       
   235      * It creates an empty registry.
   190      */
   236      */
   191     MapRegistry() {}
   237     MapRegistry() {}
   192 	
   238 
   193     /**
   239     /// Copy Constructor of the MapRegistry. 
   194      * Copy Constructor of the MapRegistry. The new registry does not steal
   240 	
   195      * the maps from the right value. The new registry will be an empty.
   241     /**
       
   242      * Copy constructor of the \e MapRegistry. 
       
   243      * The new registry does not steal
       
   244      * the maps from the copiable registry. 
       
   245      * The new registry will be empty.
   196      */
   246      */
   197     MapRegistry(const MapRegistry&) {}
   247     MapRegistry(const MapRegistry&) {}
   198 		
   248 
   199     /**
   249     /// Assign operator.
   200      * Assign operator. The left value does not steal the maps 
   250 		
   201      * from the right value. The left value will be an empty registry.
   251     /**
       
   252      * Assign operator. This registry does not steal the maps 
       
   253      * from the copiable registry. This registry will be an empty registry.
       
   254      * This operator will be called when a graph is assigned.
   202      */
   255      */
   203     MapRegistry& operator=(const MapRegistry&) {
   256     MapRegistry& operator=(const MapRegistry&) {
   204       typename Container::iterator it;
   257       typename Container::iterator it;
   205       for (it = container.begin(); it != container.end(); ++it) {
   258       for (it = container.begin(); it != container.end(); ++it) {
   206 	(*it)->destroy();
   259 	(*it)->clear();
   207 	(*it)->graph = 0;
   260 	(*it)->graph = 0;
   208 	(*it)->registry = 0;
   261 	(*it)->registry = 0;
   209       }
   262       }
   210     }
   263     }
       
   264 
       
   265     /// Destructor.
   211 				
   266 				
   212     /**
   267     /**
   213      * Destructor of the MapRegistry.
   268      * Destructor of the MapRegistry. It makes empty the attached map
       
   269      * first then detachs them.
   214      */
   270      */
   215     ~MapRegistry() {
   271     ~MapRegistry() {
   216       typename Container::iterator it;
   272       typename Container::iterator it;
   217       for (it = container.begin(); it != container.end(); ++it) {
   273       for (it = container.begin(); it != container.end(); ++it) {
   218 	(*it)->destroy();
   274 	(*it)->clear();
   219 	(*it)->registry = 0;
   275 	(*it)->registry = 0;
   220 	(*it)->graph = 0;
   276 	(*it)->graph = 0;
   221       }
   277       }
   222     }
   278     }
   223 	
   279 	
   224 	
   280 	
   225     public:
   281     public:
   226 	
   282 
   227     /**
   283     /// Attachs a map to the \e MapRegistry.
   228      * Attach a map into thr registry. If the map has been attached
   284 	
       
   285     /**
       
   286      * Attachs a map into thr registry. If the map has been attached
   229      * into an other registry it is detached from that automaticly.
   287      * into an other registry it is detached from that automaticly.
   230      */
   288      */
   231     void attach(MapBase& map) {
   289     void attach(MapBase& map) {
   232       if (map.registry) {
   290       if (map.registry) {
   233 	map.registry->detach(map);
   291 	map.registry->detach(map);
   234       }
   292       }
   235       container.push_back(&map);
   293       container.push_back(&map);
   236       map.registry = this;
   294       map.registry = this;
   237       map.registry_index = container.size()-1;
   295       map.registry_index = container.size()-1;
   238     } 
   296     } 
   239 	
   297 
   240     /**
   298     /// Detachs a map from the \e MapRegistry.
   241      * Detach the map from the registry.
   299 	
       
   300     /**
       
   301      * Detachs a map from the \e MapRegistry.
   242      */
   302      */
   243     void detach(MapBase& map) {
   303     void detach(MapBase& map) {
   244       container.back()->registry_index = map.registry_index; 
   304       container.back()->registry_index = map.registry_index; 
   245       container[map.registry_index] = container.back();
   305       container[map.registry_index] = container.back();
   246       container.pop_back();
   306       container.pop_back();
   247       map.registry = 0;
   307       map.registry = 0;
   248       map.graph = 0;
   308       map.graph = 0;
   249     }
   309     }
   250 	
   310 	
       
   311     /// Notify all the registered maps about a Key added.
   251 		
   312 		
   252     /**
   313     /**
   253      * Notify all the registered maps about a Key added.
   314      * Notify all the registered maps about a Key added.
   254      */
   315      * This member should be called whenever a node or edge
   255     void add(KeyType& key) {
   316      * is added to the graph.
       
   317      */
       
   318     void add(const KeyType& key) {
   256       typename Container::iterator it;
   319       typename Container::iterator it;
   257       for (it = container.begin(); it != container.end(); ++it) {
   320       for (it = container.begin(); it != container.end(); ++it) {
   258 	(*it)->add(key);
   321 	(*it)->add(key);
   259       }
   322       }
   260     }	
   323     }	
       
   324 
       
   325     /// Notify all the registered maps about a Key erased.
   261 		
   326 		
   262     /**
   327     /**
   263      * Notify all the registered maps about a Key erased.
   328      * Notify all the registered maps about a Key erased.
       
   329      * This member should be called whenever a node or edge
       
   330      * is erased from the graph.
   264      */ 
   331      */ 
   265     void erase(KeyType& key) {
   332     void erase(const KeyType& key) {
   266       typename Container::iterator it;
   333       typename Container::iterator it;
   267       for (it = container.begin(); it != container.end(); ++it) {
   334       for (it = container.begin(); it != container.end(); ++it) {
   268 	(*it)->erase(key);
   335 	(*it)->erase(key);
   269       }
   336       }
   270     }
   337     }
   271 
   338 
       
   339 
       
   340     /// Notify all the registered maps about all the Keys are erased.
       
   341 
   272     /**
   342     /**
   273      * Notify all the registered maps about the map should be cleared.
   343      * Notify all the registered maps about the map should be cleared.
       
   344      * This member should be called whenever all of the nodes or edges
       
   345      * are erased from the graph.
   274      */ 
   346      */ 
   275     void clear() {
   347     void clear() {
   276       typename Container::iterator it;
   348       typename Container::iterator it;
   277       for (it = container.begin(); it != container.end(); ++it) {
   349       for (it = container.begin(); it != container.end(); ++it) {
   278 	(*it)->clear();
   350 	(*it)->clear();