[Lemon-commits] [lemon_svn] deba: r1287 - hugo/branches/graph_factory/src/lemon

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:44:23 CET 2006


Author: deba
Date: Thu Oct 14 17:23:21 2004
New Revision: 1287

Added:
   hugo/branches/graph_factory/src/lemon/alteration_registry.h
      - copied, changed from r1286, /hugo/branches/graph_factory/src/lemon/map_registry.h
Removed:
   hugo/branches/graph_factory/src/lemon/map_registry.h
Modified:
   hugo/branches/graph_factory/src/lemon/vector_map.h

Log:
under construction updates


Copied: hugo/branches/graph_factory/src/lemon/alteration_registry.h (from r1286, /hugo/branches/graph_factory/src/lemon/map_registry.h)
==============================================================================
--- /hugo/branches/graph_factory/src/lemon/map_registry.h	(original)
+++ hugo/branches/graph_factory/src/lemon/alteration_registry.h	Thu Oct 14 17:23:21 2004
@@ -1,5 +1,5 @@
 /* -*- C++ -*-
- * src/lemon/map_registry.h - Part of LEMON, a generic C++ optimization library
+ * src/lemon/observer_registry.h - Part of LEMON, a generic C++ optimization library
  *
  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
  * (Egervary Combinatorial Optimization Research Group, EGRES).
@@ -14,205 +14,189 @@
  *
  */
 
-#ifndef LEMON_MAP_REGISTRY_H
-#define LEMON_MAP_REGISTRY_H
+#ifndef LEMON_OBSERVER_REGISTRY_H
+#define LEMON_OBSERVER_REGISTRY_H
 
 #include <vector>
 
-///\ingroup graphmapfactory
+///\ingroup graphmaps
 ///\file
-///\brief Map registry for graph maps.
+///\brief Observer registry for graph alteration observers.
 
 using namespace std;
 
 namespace lemon {
 
-  /// \addtogroup graphmapfactory
+  /// \addtogroup graphmaps
   /// @{
 
-  /// Map registry for graph maps.
+  /// Registry class to register objects observes alterations in the graph.
 
-  /** 
-   * Registry class to register edge or node maps into the graph. The
-   * registry helps you to implement an observer pattern. If you add
-   * or erase an edge or node you must notify all the maps about the
-   * event.
-   *
-   * \param G The graph type to register maps.
-   * \param K The key type of the maps registered into the registry.
-   * \param KIt The key iterator type iterates on the keys.
-   *
-   * \author Balazs Dezso
-   */
 
-  template <typename G, typename K, typename KIt>
-  class MapRegistry {
+  /// This class is a registry for the objects observes the alterations in 
+  /// the graph. The alteration observers can be attached to and detached 
+  /// from the registry. The observers have to inherit from the /ref 
+  /// AlterationObserverRegistry::ObserverBase and override the virtual functions
+  /// in that. The registry observes only one item type by example edge or node 
+  /// alterations.         
+  ///
+  /// The most important application of the alteration observing is the dynamic 
+  /// map implementation.
+  ///
+  /// \param G The graph type to be observed.
+  /// \param K The key type alteration will be observed.
+  /// \param KIt The key iterator type iterates on the keys.
+  ///
+  /// \author Balazs Dezso
+
+  template <typename G, typename I, typename IIt>
+  class AlterationObserverRegistry {
   public:
     typedef G Graph;
-    typedef K KeyType;
-    typedef KIt KeyIt;
+    typedef I Item;
+    typedef IIt ItemIt;
 
-    /// MapBase is the base class of the dynamic maps.
+    /// ObserverBase is the base class of the dynamic maps.
 	
-    /**
-     * MapBase is the base class of the dynamic maps.
-     * It defines the core modification operations on the maps and
-     * implements some helper functions. 
-     */
-    class MapBase {
+    /// ObserverBase is the base class of the observers.
+    /// It will be notified about an item was inserted into or
+    /// erased from the graph.  
+    ///
+    /// The observer interface contains some virtual function
+    /// to override it. The add() and erase() functions are
+    /// to notify the oberver about one item is added or
+    /// erased. The build() member function notifies the
+    /// observer.
+
+    class ObserverBase {
     public:
       typedef G Graph;
-      typedef K KeyType;
-      typedef KIt KeyIt;
+      typedef I Item;
+      typedef IIt ItemIt;
 
-      typedef MapRegistry<G, K, KIt> Registry;
+      typedef AlterationObserverRegistry<Graph, Item, ItemIt> Registry;
 	
-      friend class MapRegistry<G, K, KIt>;
+      friend class Registry;
 
       /// Default constructor.
 
-      /**
-       * Default constructor for MapBase.
-       */
-
-      MapBase() : graph(0), registry(0) {}
+      /// Default constructor for ObserverBase.
+      /// 
+      ObserverBase() : graph(0), registry(0) {}
 
-      /// Constructor to register map into a graph registry.
+      /// Constructor to register observer into an observer registry.
 		
-      /** 
-       * Simple constructor to register dynamic map into a graph registry.
-      */
-	
-      MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
+      /// It constrates an observer and then registers it into an observer registry.
+      /// This calls also the build() member to notify the observer about it is registered. 
+      ObserverBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
 	r.attach(*this);
+	build();
       }
 
       /// Copy constructor.
 
-      /** 
-       * Copy constructor to register into the registry.
-       * If the copiable map is registered into a registry
-       * the construated map will be registered to the same registry.
-      */	
+      /// Copy constructor to register observer into a registry.
+      /// If the copiable obse is registered into a registry
+      /// the construated map will be registered to the same registry.
+      /// 
 	
-      MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
+      ObserverBase(const ObserverBase& copy) : graph(copy.graph), registry(0) {
 	if (copy.registry) {
 	  copy.registry->attach(*this);
+	  build();
 	}
       } 
 
       /// Assign operator.
 	
-      /** 
-       * Assign operator. This member detach first the map
-       * from the current registry and then it attach to the
-       * copiable map's registry if it exists.
-      */	
-      const MapBase& operator=(const MapBase& copy) {
+      /// Assign operator. This member detach first the observer
+      /// from the current registry and then it attach to the
+      /// copiable observer's registry if it exists.
+
+      const ObserverBase& operator=(const ObserverBase& copy) {
 	if (registry) {
+	  clean();
 	  registry->detach(*this);
 	}
 	graph = copy.graph;
 	if (copy.registry) {
 	  copy.registry->attach(*this);
+	  build();
 	}
 	return *this;
       }
 	
       /// Destructor
 
-      /** 
-       * This member detach the map from the its registry if the
-       * registry exists.
-      */		
+      /// The destructor detach the observer from its registry.
+      /// 
 
-      virtual ~MapBase() {
+      virtual ~ObserverBase() {
 	if (registry) {
 	  registry->detach(*this);
 	}
       }
 
-      /// The graph of the map.
-
-      /*
-       * Returns the graph that the map belongs to.
-      */
+      /// Pointer to the observed graph.
+      
+      /// The getGraph() member function gives back a pointer to the observed graph
+      /// if the observer is already registered. Elsewhere gives back null pointer. 
 
       const Graph* getGraph() const { return graph; }
+
+      Registry* getRegistry() const { return registry; }
 	
     protected:
 		
       const Graph* graph;     
-      Registry* registry;
+      mutable Registry* registry;
 
       int registry_index;
 
     protected:
 
-      /// Helper function to implement constructors in the subclasses.
-	
-      /**
-       * Helper function to implement constructors in the subclasses.
-       * It adds all of the nodes or edges to the map via the 
-       * \ref MapRegistry::MapBase::add add
-       * member function.
-      */
-	
-      virtual void init() {
-	for (KeyIt it(*graph); it != INVALID; ++it) {
-	  add(*it);
-	}
-      }
-	
+      /// The member function to notificate the observer about an item is added to the graph. 
 
-      /// Helper function to implement destructors in the subclasses.
-	
-      /**
-       * Helper function to implement destructors in the subclasses.
-       * It erases all of the nodes or edges of the map via the 
-       * \ref MapRegistry::MapBase::erase erase
-       * member function. It can be used by the clear function also.
-      */
+      /// The add member function notificates the observer about 
+      /// an item is added to the graph. It have to be overrided in the subclasses.
 	
-      virtual void destroy() {
-	for (KeyIt it(*getGraph()); it != INVALID; ++it) {
-	  erase(*it);
-	}
-      }
+      virtual void add(const Item&) = 0;	
 
-      /// The member function to add new node or edge to the map.
-	
-      /** 
-	  The add member function should be overloaded in the subclasses.
-	  \e Add extends the map with the new node or edge.
-      */
+
+      /// The member function to notificate the observer about an item is erased from the graph. 
+
+      /// The erase member function notificates the observer about 
+      /// an item is erased from the graph. It have to be overrided in the subclasses.
 	
-      virtual void add(const KeyType&) = 0;	
+      virtual void erase(const Item&) = 0;
 
+      /// The member function to notificate the observer about the graph is builded. 
 
-      /// The member function to erase a node or edge from the map.
+      /// The build member function notificates the observer about 
+      /// the graph is builded. It can be overrided in the subclasses.
 
-      /** 
-	  The erase member function should be overloaded in the subclasses.
-	  \e Erase removes the node or edge from the map.
-      */
-	
-      virtual void erase(const KeyType&) = 0;
+      virtual void build() {
+	for (ItemIt it(*graph); it != INVALID; ++it) {
+	  add(*it);
+	}
+      }
 
-      /**
-       *  The clear member function should be overloaded in the subclasses.
-       *  \e Clear makes empty the data structure.
-       */
+      /// The member function to notificate the observer about all items are erased from the graph. 
 
-      virtual void clear() = 0;
+      /// The clear member function notificates the observer about 
+      /// all items are erased from the graph. It can be overrided in the subclasses.
+      
+      virtual void clear() {
+	for (ItemIt it(*getGraph()); it != INVALID; ++it) {
+	  erase(*it);
+	}
+      }
 
       /// Exception class to throw at unsupported operation.
 	
-      /**
-       * Exception class to throw at unsupported operation.
-       * If the map does not support erasing or adding new
-       * node or key it must be throwed.
-      */
+      /// Exception class to throw at unsupported operation.
+      /// If the map does not support erasing or adding new
+      /// item then this exception have to be throwed.
 	
       class NotSupportedOperationException {};
 
@@ -221,7 +205,7 @@
   protected:
 	
 
-    typedef std::vector<MapBase*> Container; 
+    typedef std::vector<ObserverBase*> Container; 
 
     Container container;
 
@@ -230,30 +214,25 @@
 
     /// Default constructor.
 	
-    /**
-     * Default constructor of the \e MapRegistry. 
-     * It creates an empty registry.
-     */
-    MapRegistry() {}
-
-    /// Copy Constructor of the MapRegistry. 
+    ///
+    /// The default constructor of the AlterationObserverRegistry. 
+    /// It creates an empty registry.
+    AlterationObserverRegistry() {}
+
+    /// Copy Constructor of the AlterationObserverRegistry. 
 	
-    /**
-     * Copy constructor of the \e MapRegistry. 
-     * The new registry does not steal
-     * the maps from the copiable registry. 
-     * The new registry will be empty.
-     */
-    MapRegistry(const MapRegistry&) {}
+
+    /// Copy constructor of the AlterationObserverRegistry. 
+    /// It creates only an empty registry because the copiable
+    /// registry's observers have to be registered still into that registry.
+    AlterationObserverRegistry(const AlterationObserverRegistry&) {}
 
     /// Assign operator.
 		
-    /**
-     * Assign operator. This registry does not steal the maps 
-     * from the copiable registry. This registry will be an empty registry.
-     * This operator will be called when a graph is assigned.
-     */
-    MapRegistry& operator=(const MapRegistry&) {
+    /// Assign operator for the AlterationObserverRegistry. 
+    /// It makes the registry only empty because the copiable
+    /// registry's observers have to be registered still into that registry.
+    AlterationObserverRegistry& operator=(const AlterationObserverRegistry&) {
       typename Container::iterator it;
       for (it = container.begin(); it != container.end(); ++it) {
 	(*it)->clear();
@@ -264,11 +243,9 @@
 
     /// Destructor.
 				
-    /**
-     * Destructor of the MapRegistry. It makes empty the attached map
-     * first then detachs them.
-     */
-    ~MapRegistry() {
+    /// Destructor of the AlterationObserverRegistry. It calls first the clean() 
+    /// member function of the attached observers and then detachs them.
+    ~AlterationObserverRegistry() {
       typename Container::iterator it;
       for (it = container.begin(); it != container.end(); ++it) {
 	(*it)->clear();
@@ -280,56 +257,54 @@
 	
     public:
 
-    /// Attachs a map to the \e MapRegistry.
+    /// Attachs the observer into the registry.
 	
-    /**
-     * Attachs a map into thr registry. If the map has been attached
-     * into an other registry it is detached from that automaticly.
-     */
-    void attach(MapBase& map) {
-      if (map.registry) {
-	map.registry->detach(map);
-      }
-      container.push_back(&map);
-      map.registry = this;
-      map.registry_index = container.size()-1;
+    /// Attachs the observer into the registry. If the observer has been still attached
+    /// into an other registry it is detached from that automaticly.
+    void attach(ObserverBase& observer) {
+      if (observer.registry) {
+	observer.registry->detach(observer);
+      }
+      container.push_back(&observer);
+      observer.registry = this;
+      observer.registry_index = container.size()-1;
     } 
 
-    /// Detachs a map from the \e MapRegistry.
+    /// Detachs a observer from the \e AlterationObserverRegistry.
 	
     /**
-     * Detachs a map from the \e MapRegistry.
+     * Detachs a observer from the \e AlterationObserverRegistry.
      */
-    void detach(MapBase& map) {
-      container.back()->registry_index = map.registry_index; 
-      container[map.registry_index] = container.back();
+    void detach(ObserverBase& base) {
+      container.back()->registry_index = base.registry_index; 
+      container[base.registry_index] = container.back();
       container.pop_back();
-      map.registry = 0;
-      map.graph = 0;
+      base.registry = 0;
+      base.graph = 0;
     }
 	
-    /// Notify all the registered maps about a Key added.
+    /// Notify all the registered observers about a Key added.
 		
     /**
-     * Notify all the registered maps about a Key added.
+     * Notify all the registered observers about a Key added.
      * This member should be called whenever a node or edge
      * is added to the graph.
      */
-    void add(const KeyType& key) {
+    void add(const Item& key) {
       typename Container::iterator it;
       for (it = container.begin(); it != container.end(); ++it) {
 	(*it)->add(key);
       }
     }	
 
-    /// Notify all the registered maps about a Key erased.
+    /// Notify all the registered observers about a Key erased.
 		
     /**
-     * Notify all the registered maps about a Key erased.
+     * Notify all the registered observers about a Key erased.
      * This member should be called whenever a node or edge
      * is erased from the graph.
      */ 
-    void erase(const KeyType& key) {
+    void erase(const Item& key) {
       typename Container::iterator it;
       for (it = container.begin(); it != container.end(); ++it) {
 	(*it)->erase(key);
@@ -337,10 +312,10 @@
     }
 
 
-    /// Notify all the registered maps about all the Keys are erased.
+    /// Notify all the registered observers about all the Keys are erased.
 
     /**
-     * Notify all the registered maps about the map should be cleared.
+     * Notify all the registered observers about the observer should be cleared.
      * This member should be called whenever all of the nodes or edges
      * are erased from the graph.
      */ 

Modified: hugo/branches/graph_factory/src/lemon/vector_map.h
==============================================================================
--- hugo/branches/graph_factory/src/lemon/vector_map.h	(original)
+++ hugo/branches/graph_factory/src/lemon/vector_map.h	Thu Oct 14 17:23:21 2004
@@ -18,6 +18,7 @@
 #define LEMON_VECTOR_MAP_H
 
 #include <vector>
+#include <algorithm>
 
 ///\ingroup graphmaps
 ///\file
@@ -28,33 +29,33 @@
   /// \addtogroup graphmaps
   /// @{
   
-  /** The VectorMap template class is graph map structure what
-   *  automatically updates the map when a key is added to or erased from
-   *  the map. This map factory uses the allocators to implement 
-   *  the container functionality. This map factory
-   *  uses the std::vector to implement the container function.
-   *
-   *  \param MapRegistry The MapRegistry that the maps will belong to.
-   *  \param Value The value type of the map.
-   * 
-   *  \author Balazs Dezso
-   */
-	
-  template <typename MapRegistry, typename IdMap, typename Value>
-  class VectorMap : public MapRegistry::MapBase {
+  /// The VectorMap template class is graph map structure what
+  /// automatically updates the map when a key is added to or erased from
+  /// the map. This map factory uses the allocators to implement 
+  /// the container functionality. This map factory
+  /// uses the std::vector to implement the container function.
+  ///
+  /// \param Registry The AlterationObserverRegistry that will notify this map.
+  /// \param IdMap The IdMap type of the graph items.
+  /// \param Value The value type of the map.
+  /// 
+  /// \author Balazs Dezso
+  	
+  template <typename Registry, typename IdMap, typename Value>
+  class VectorMap : public Registry::ObserverBase {
   public:
 		
     /// The graph type of the maps. 
-    typedef typename MapRegistry::Graph Graph;
+    typedef typename Registry::Graph Graph;
     /// The key type of the maps.
-    typedef typename MapRegistry::KeyType KeyType;
+    typedef typename Registry::ItemType KeyType;
     /// The iterator to iterate on the keys.
-    typedef typename MapRegistry::KeyIt KeyIt;
+    typedef typename Registry::ItemIt KeyIt;
 
     /// The map type.
     typedef VectorMap Map;
-    /// The MapBase of the Map which implements the core regisitry function.
-    typedef typename MapRegistry::MapBase MapBase;
+    /// The ObserverBase of the Map which implements the core regisitry function.
+    typedef typename Registry::ObserverBase ObserverBase;
 
   private:
 		
@@ -63,7 +64,6 @@
 
   public:
 
-
     /// The value type of the map.
     typedef Value ValueType;
     /// The reference type of the map;
@@ -78,50 +78,59 @@
     /// The pointer type of the map;
     typedef typename Container::const_pointer ConstPointerType;
 
-    /// Constructor to attach the new map into a registry.
+    /// Constructor to attach the new map into the registry.
 
-    /** Constructor to attach the new map into a registry.
-     *  It adds all the nodes or edges of the graph to the map.
-     */
-    VectorMap(const Graph& g, MapRegistry& r) 
-      : MapBase(g, r), container(IdMap(g).maxId()+1) {}
+    /// It construates a map and attachs it into the registry.
+    /// It adds all the items of the graph to the map.
+     
+    VectorMap(const Graph& g, Registry& r) 
+      : ObserverBase(g, r) {}
 
     /// Constructor uses given value to initialize the map. 
 
-    /** Constructor uses given value to initialize the map. 
-     *  It adds all the nodes or edges of the graph to the map.
-     */
-    VectorMap(const Graph& g, MapRegistry& r, const Value& v) 
-      : MapBase(g, r), container(IdMap(g).maxId()+1) {}
+    /// It construates a map uses a given value to initialize the map. 
+    /// It adds all the items of the graph to the map.
+     
+    VectorMap(const Graph& g, Registry& r, const Value& v) 
+      : ObserverBase(g, r) { 
+      std::fill(container.begin(), container.end(), v);
+    }
 
     /// The subcript operator.
 
-    /**
-     * The subscript operator. The map can be subscripted by the
-     * actual keys of the graph. 
-     */
+    /// The subscript operator. The map can be subscripted by the
+    /// actual items of the graph. 
+     
     ReferenceType operator[](const KeyType& key) {
-      return container[IdMap(*MapBase::getGraph())[key]];
+      return container[IdMap(*ObserverBase::getGraph())[key]];
     } 
 		
     /// The const subcript operator.
 
-    /**
-     * The const subscript operator. The map can be subscripted by the
-     * actual keys of the graph. 
-     */
+    /// The const subscript operator. The map can be subscripted by the
+    /// actual items of the graph. 
+     
     ConstReferenceType operator[](const KeyType& key) const {
-      return container[IdMap(*MapBase::getGraph())[key]];
+      return container[IdMap(*ObserverBase::getGraph())[key]];
+    }
+
+
+    /// The setter function of the map.
+
+    /// It the same as operator[](key) = value expression.
+    ///
+     
+    void set(const KeyType& key, const ValueType& value) const {
+      container[IdMap(*ObserverBase::getGraph())[key]] = value;
     }
 
     /// Adds a new key to the map.
 		
-    /** Adds a new key to the map. It called by the map registry
-     *  and it overrides the \ref MapRegistry::MapBase MapBase's
-     *  add() member function.
-     */
+    /// It adds a new key to the map. It called by the observer registry
+    /// and it overrides the add() member function of the observer base.
+     
     void add(const KeyType& key) {
-      int id =  IdMap(*MapBase::getGraph())[key];
+      int id =  IdMap(*ObserverBase::getGraph())[key];
       if (id >= (int)container.size()) {
 	container.resize(id + 1);
       }
@@ -129,19 +138,22 @@
 
     /// Erases a key from the map.
 		
-    /** Erase a key from the map. It called by the map registry 
-     *  and it overrides the \ref MapRegistry::MapBase MapBase's
-     *  erase() member function.
-     */
+    /// Erase a key from the map. It called by the observer registry
+    /// and it overrides the erase() member function of the observer base.     
     void erase(const KeyType& key) {}
 
-    /// Makes empty the map.
+    /// Buildes the map.
+		
+    /// It buildes the map. It called by the observer registry
+    /// and it overrides the build() member function of the observer base.
+    void build() { 
+      container.resize(IdMap(g).maxId()+1);
+    }
 
-    /** Makes empty the map. It called by the map registry 
-     *  and it overrides the \ref MapRegistry::MapBase MapBase's
-     *  clear() member function.
-     */
+    /// Clear the map.
 
+    /// It erase all items from the map. It called by the observer registry
+    /// and it overrides the clear() member function of the observer base.     
     void clear() { 
       container.clear();
     }



More information about the Lemon-commits mailing list