COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/map_registry.h @ 943:cb0ac054ea92

Last change on this file since 943:cb0ac054ea92 was 943:cb0ac054ea92, checked in by beckerjc, 20 years ago

Bugfix. (removed forgotten "using namespace std")

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