src/work/deba/map_registry.h
changeset 1365 c280de819a73
parent 1364 ee5959aa4410
child 1366 d00b85f8be45
     1.1 --- a/src/work/deba/map_registry.h	Sun Apr 17 18:57:22 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,246 +0,0 @@
     1.4 -#ifndef MAP_REGISTRY_H
     1.5 -#define MAP_REGISTRY_H
     1.6 -
     1.7 -#include <vector>
     1.8 -
     1.9 -using namespace std;
    1.10 -
    1.11 -namespace lemon {
    1.12 -
    1.13 -/** 
    1.14 - * Registry class to register edge or node maps into the graph. The
    1.15 - * registry helps you to implement an observer pattern. If you add
    1.16 - * or erase an edge or node you must notify all the maps about the
    1.17 - * event.
    1.18 -*/
    1.19 -  template <typename G, typename K, typename KIt>
    1.20 -  class MapRegistry {
    1.21 -  public:
    1.22 -    typedef G Graph;
    1.23 -    typedef K Key;
    1.24 -    typedef KIt KeyIt;
    1.25 -	
    1.26 -
    1.27 -
    1.28 -    /**
    1.29 -     * MapBase is the base class of the registered maps.
    1.30 -     * It defines the core modification operations on the maps and
    1.31 -     * implements some helper functions. 
    1.32 -     */
    1.33 -    class MapBase {
    1.34 -    public:
    1.35 -      typedef G Graph;
    1.36 -      typedef MapRegistry<G, K, KIt> Registry;
    1.37 -      typedef K Key;
    1.38 -      typedef KIt KeyIt;
    1.39 -	
    1.40 -      friend class Registry;
    1.41 -
    1.42 -      /**
    1.43 -       * Default constructor for MapBase.
    1.44 -       */
    1.45 -
    1.46 -      MapBase() : graph(0), registry(0) {}
    1.47 -		
    1.48 -      /** 
    1.49 -       * Simple constructor to register into a graph registry.
    1.50 -      */
    1.51 -	
    1.52 -      MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
    1.53 -	r.attach(*this);
    1.54 -      }
    1.55 -
    1.56 -      /** 
    1.57 -       * Copy constructor to register into the registry.
    1.58 -      */	
    1.59 -	
    1.60 -      MapBase(const MapBase& copy) : registry(0), graph(copy.graph) {
    1.61 -	if (copy.registry) {
    1.62 -	  copy.registry->attach(*this);
    1.63 -	}
    1.64 -      } 
    1.65 -	
    1.66 -      /** 
    1.67 -       * Assign operator.
    1.68 -      */	
    1.69 -
    1.70 -      const MapBase& operator=(const MapBase& copy) {
    1.71 -	if (registry) {
    1.72 -	  registry->detach(*this);
    1.73 -	}
    1.74 -	graph = copy.graph;
    1.75 -	if (copy.registry) {
    1.76 -	  copy.registry->attach(*this);
    1.77 -	}
    1.78 -      }
    1.79 -	
    1.80 -
    1.81 -      /** 
    1.82 -       * Destructor. 
    1.83 -      */		
    1.84 -
    1.85 -      virtual ~MapBase() {
    1.86 -	if (registry) {
    1.87 -	  registry->detach(*this);
    1.88 -	}
    1.89 -      }
    1.90 -
    1.91 -      /*
    1.92 -       * Returns the graph that the map belongs to.
    1.93 -      */
    1.94 -
    1.95 -      const Graph* getGraph() const { return graph; }
    1.96 -	
    1.97 -    protected:
    1.98 -		
    1.99 -      const Graph* graph;     
   1.100 -      Registry* registry;
   1.101 -
   1.102 -      int registry_index;
   1.103 -
   1.104 -    protected:
   1.105 -	
   1.106 -      /**
   1.107 -	 Helper function to implement constructors in the subclasses.
   1.108 -      */
   1.109 -	
   1.110 -      virtual void init() {
   1.111 -	for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
   1.112 -	  add(it);
   1.113 -	}
   1.114 -      }
   1.115 -	
   1.116 -      /**
   1.117 -	 Helper function to implement the destructor in the subclasses.
   1.118 -      */
   1.119 -	
   1.120 -      virtual void destroy() {
   1.121 -	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
   1.122 -	  erase(it);
   1.123 -	}
   1.124 -      }
   1.125 -	
   1.126 -      /** 
   1.127 -	  The add member function should be overloaded in the subclasses.
   1.128 -	  \e Add extends the map with the new node.
   1.129 -      */
   1.130 -	
   1.131 -      virtual void add(const Key&) = 0;	
   1.132 -      /** 
   1.133 -	  The erase member function should be overloaded in the subclasses.
   1.134 -	  \e Erase removes the node from the map.
   1.135 -      */
   1.136 -	
   1.137 -      virtual void erase(const Key&) = 0;
   1.138 -	
   1.139 -      /**
   1.140 -	 Exception class to throw at unsupported operation.
   1.141 -      */
   1.142 -	
   1.143 -      class NotSupportedOperationException {};
   1.144 -
   1.145 -    };
   1.146 -	
   1.147 -  protected:
   1.148 -	
   1.149 -    /** 
   1.150 -     * The container type of the maps.
   1.151 -     */
   1.152 -    typedef std::vector<MapBase*> Container; 
   1.153 -
   1.154 -    /**
   1.155 -     * The container of the registered maps.
   1.156 -     */
   1.157 -    Container container;
   1.158 -
   1.159 -		
   1.160 -  public:
   1.161 -	
   1.162 -    /**
   1.163 -     * Default Constructor of the MapRegistry. It creates an empty registry.
   1.164 -     */
   1.165 -    MapRegistry() {}
   1.166 -	
   1.167 -    /**
   1.168 -     * Copy Constructor of the MapRegistry. The new registry does not steal
   1.169 -     * the maps from the right value. The new registry will be an empty.
   1.170 -     */
   1.171 -    MapRegistry(const MapRegistry&) {}
   1.172 -		
   1.173 -    /**
   1.174 -     * Assign operator. The left value does not steal the maps 
   1.175 -     * from the right value. The left value will be an empty registry.
   1.176 -     */
   1.177 -    MapRegistry& operator=(const MapRegistry&) {
   1.178 -      for (it = container.begin(); it != container.end(); ++it) {
   1.179 -	(*it)->destroy();
   1.180 -	(*it)->graph = 0;
   1.181 -	(*it)->registry = 0;
   1.182 -      }
   1.183 -    }
   1.184 -				
   1.185 -    /**
   1.186 -     * Destructor of the MapRegistry.
   1.187 -     */
   1.188 -    ~MapRegistry() {
   1.189 -      typename Container::iterator it;
   1.190 -      for (it = container.begin(); it != container.end(); ++it) {
   1.191 -	(*it)->destroy();
   1.192 -	(*it)->registry = 0;
   1.193 -	(*it)->graph = 0;
   1.194 -      }
   1.195 -    }
   1.196 -	
   1.197 -	
   1.198 -    public:
   1.199 -	
   1.200 -    /**
   1.201 -     * Attach a map into thr registry. If the map has been attached
   1.202 -     * into an other registry it is detached from that automaticly.
   1.203 -     */
   1.204 -    void attach(MapBase& map) {
   1.205 -      if (map.registry) {
   1.206 -	map.registry->detach(map);
   1.207 -      }
   1.208 -      container.push_back(&map);
   1.209 -      map.registry = this;
   1.210 -      map.registry_index = container.size()-1;
   1.211 -    } 
   1.212 -	
   1.213 -    /**
   1.214 -     * Detach the map from the registry.
   1.215 -     */
   1.216 -    void detach(MapBase& map) {
   1.217 -      container.back()->registry_index = map.registry_index; 
   1.218 -      container[map.registry_index] = container.back();
   1.219 -      container.pop_back();
   1.220 -      map.registry = 0;
   1.221 -      map.graph = 0;
   1.222 -    }
   1.223 -	
   1.224 -		
   1.225 -    /**
   1.226 -     * Notify all the registered maps about a Key added.
   1.227 -     */
   1.228 -    virtual void add(Key& key) {
   1.229 -      typename Container::iterator it;
   1.230 -      for (it = container.begin(); it != container.end(); ++it) {
   1.231 -	(*it)->add(key);
   1.232 -      }
   1.233 -    }	
   1.234 -		
   1.235 -    /**
   1.236 -     * Notify all the registered maps about a Key erased.
   1.237 -     */ 
   1.238 -    virtual void erase(Key& key) {
   1.239 -      typename Container::iterator it;
   1.240 -      for (it = container.begin(); it != container.end(); ++it) {
   1.241 -	(*it)->erase(key);
   1.242 -      }
   1.243 -    }
   1.244 -
   1.245 -  };
   1.246 -
   1.247 -}
   1.248 -
   1.249 -#endif