lemon/bits/vector_map.h
changeset 1708 8fea0e60f855
parent 1669 66ae78d29f1e
child 1719 674182524bd9
equal deleted inserted replaced
2:119602ea1cb2 3:c51ea1f940b2
    42   /// the map. This map factory uses the allocators to implement 
    42   /// the map. This map factory uses the allocators to implement 
    43   /// the container functionality. This map factory
    43   /// the container functionality. This map factory
    44   /// uses the std::vector to implement the container function.
    44   /// uses the std::vector to implement the container function.
    45   ///
    45   ///
    46   /// \param Registry The AlterationNotifier that will notify this map.
    46   /// \param Registry The AlterationNotifier that will notify this map.
    47   /// \param IdMap The IdMap type of the graph items.
    47   /// \param Item The item type of the graph items.
    48   /// \param Value The value type of the map.
    48   /// \param Value The value type of the map.
    49   /// 
    49   /// 
    50   /// \author Balazs Dezso
    50   /// \author Balazs Dezso
    51   	
    51   	
    52 
       
    53   template <
    52   template <
    54     typename _Graph, 
    53     typename _Graph, 
    55     typename _Item,    
    54     typename _Item,    
    56     typename _Value
    55     typename _Value
    57     >
    56     >
    58   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
    57   class VectorMap : public AlterationNotifier<_Item>::ObserverBase {
    59   public:
    58   public:
       
    59 
       
    60     typedef True AdaptibleTag;
    60 		
    61 		
    61     /// The graph type of the map. 
    62     /// The graph type of the map. 
    62     typedef _Graph Graph;
    63     typedef _Graph Graph;
    63     /// The key type of the map.
    64     /// The key type of the map.
    64     typedef _Item Key;
    65     typedef _Item Key;
    91     /// The pointer type of the map;
    92     /// The pointer type of the map;
    92     typedef typename Container::const_pointer ConstPointer;
    93     typedef typename Container::const_pointer ConstPointer;
    93 
    94 
    94     typedef True FullTypeTag;
    95     typedef True FullTypeTag;
    95 
    96 
    96     /// Constructor to attach the new map into the registry.
    97     /// \brief Constructor to attach the new map into the registry.
    97 
    98     ///
    98     /// It construates a map and attachs it into the registry.
    99     /// It constructs a map and attachs it into the registry.
    99     /// It adds all the items of the graph to the map.
   100     /// It adds all the items of the graph to the map.
   100      
       
   101     VectorMap(const Graph& _g) : graph(&_g) {
   101     VectorMap(const Graph& _g) : graph(&_g) {
   102       attach(_g.getNotifier(_Item()));
   102       attach(_g.getNotifier(_Item()));
   103       build();
   103       build();
   104     }
   104     }
   105 
   105 
   106     /// Constructor uses given value to initialize the map. 
   106     /// \brief Constructor uses given value to initialize the map. 
   107 
   107     ///
   108     /// It construates a map uses a given value to initialize the map. 
   108     /// It constructs a map uses a given value to initialize the map. 
   109     /// It adds all the items of the graph to the map.
   109     /// It adds all the items of the graph to the map.
   110      
       
   111     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
   110     VectorMap(const Graph& _g, const Value& _v) : graph(&_g) { 
   112       attach(_g.getNotifier(_Item()));
   111       attach(_g.getNotifier(_Item()));
   113       container.resize(graph->maxId(_Item()) + 1, _v);
   112       container.resize(graph->maxId(_Item()) + 1, _v);
   114     }
   113     }
   115 
   114 
       
   115     /// \brief Copy constructor
       
   116     ///
       
   117     /// Copy constructor.
   116     VectorMap(const VectorMap& _copy) 
   118     VectorMap(const VectorMap& _copy) 
   117       : Parent(), graph(_copy.getGraph()) {
   119       : Parent(), graph(_copy.getGraph()) {
   118       if (_copy.attached()) {
   120       if (_copy.attached()) {
   119 	attach(*_copy.getRegistry());
   121 	attach(*_copy.getRegistry());
   120 	container = _copy.container;
   122 	container = _copy.container;
   121       }
   123       }
   122     }
   124     }
   123 
   125 
       
   126     /// \brief Destrcutor
       
   127     ///
       
   128     /// Destructor.
   124     virtual ~VectorMap() {
   129     virtual ~VectorMap() {
   125       if (attached()) {
   130       if (attached()) {
   126 	detach();
   131 	detach();
   127       }
   132       }
   128     }
   133     }
   142       return graph;
   147       return graph;
   143     }
   148     }
   144 
   149 
   145   public:
   150   public:
   146 
   151 
   147     /// The subcript operator.
   152     /// \brief The subcript operator.
   148 
   153     ///
   149     /// The subscript operator. The map can be subscripted by the
   154     /// The subscript operator. The map can be subscripted by the
   150     /// actual items of the graph. 
   155     /// actual items of the graph.      
   151      
       
   152     Reference operator[](const Key& key) {
   156     Reference operator[](const Key& key) {
   153       return container[graph->id(key)];
   157       return container[graph->id(key)];
   154     } 
   158     } 
   155 		
   159 		
   156     /// The const subcript operator.
   160     /// \brief The const subcript operator.
   157 
   161     ///
   158     /// The const subscript operator. The map can be subscripted by the
   162     /// The const subscript operator. The map can be subscripted by the
   159     /// actual items of the graph. 
   163     /// actual items of the graph. 
   160      
       
   161     ConstReference operator[](const Key& key) const {
   164     ConstReference operator[](const Key& key) const {
   162       return container[graph->id(key)];
   165       return container[graph->id(key)];
   163     }
   166     }
   164 
   167 
   165 
   168 
   166     /// The setter function of the map.
   169     /// \brief The setter function of the map.
   167 
   170     ///
   168     /// It the same as operator[](key) = value expression.
   171     /// It the same as operator[](key) = value expression.
   169     ///
       
   170     void set(const Key& key, const Value& value) {
   172     void set(const Key& key, const Value& value) {
   171       (*this)[key] = value;
   173       (*this)[key] = value;
   172     }
   174     }
   173 
   175 
   174   protected:
   176   protected:
   175 
   177 
   176     /// \brief Adds a new key to the map.
   178     /// \brief Adds a new key to the map.
   177     ///		
   179     ///		
   178     /// It adds a new key to the map. It called by the observer registry
   180     /// It adds a new key to the map. It called by the observer registry
   179     /// and it overrides the add() member function of the observer base.
   181     /// and it overrides the add() member function of the observer base.     
   180      
   182     virtual void add(const Key& key) {
   181     void add(const Key& key) {
       
   182       int id = graph->id(key);
   183       int id = graph->id(key);
   183       if (id >= (int)container.size()) {
   184       if (id >= (int)container.size()) {
   184 	container.resize(id + 1);
   185 	container.resize(id + 1);
   185       }
   186       }
   186     }
   187     }
   187 
   188 
   188     /// Erases a key from the map.
   189     /// \brief Erase a key from the map.
   189 		
   190     ///
   190     /// Erase a key from the map. It called by the observer registry
   191     /// Erase a key from the map. It called by the observer registry
   191     /// and it overrides the erase() member function of the observer base.     
   192     /// and it overrides the erase() member function of the observer base.     
   192     void erase(const Key&) {}
   193     virtual void erase(const Key&) {}
   193 
   194 
   194     /// Buildes the map.
   195     /// \brief Buildes the map.
   195 		
   196     ///	
   196     /// It buildes the map. It called by the observer registry
   197     /// It buildes the map. It called by the observer registry
   197     /// and it overrides the build() member function of the observer base.
   198     /// and it overrides the build() member function of the observer base.
   198 
   199     virtual void build() { 
   199     void build() { 
       
   200       container.resize(graph->maxId(_Item()) + 1);
   200       container.resize(graph->maxId(_Item()) + 1);
   201     }
   201     }
   202 
   202 
   203     /// Clear the map.
   203     /// \brief Clear the map.
   204 
   204     ///
   205     /// It erase all items from the map. It called by the observer registry
   205     /// It erase all items from the map. It called by the observer registry
   206     /// and it overrides the clear() member function of the observer base.     
   206     /// and it overrides the clear() member function of the observer base.     
   207     void clear() { 
   207     virtual void clear() { 
   208       container.clear();
   208       container.clear();
   209     }
   209     }
   210     
   210     
   211   private:
   211   private:
   212 		
   212