lemon/bits/vector_map.h
changeset 218 0d6511647639
parent 157 2ccc1afc2c52
child 220 a5d8c039f218
equal deleted inserted replaced
2:8677d1b9390e 3:2f6e77bc9a44
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
    47   /// \tparam _Notifier The AlterationNotifier that will notify this map.
    47   /// \tparam _Notifier The AlterationNotifier that will notify this map.
    48   /// \tparam _Item The item type of the graph items.
    48   /// \tparam _Item The item type of the graph items.
    49   /// \tparam _Value The value type of the map.
    49   /// \tparam _Value The value type of the map.
    50   /// \todo Fix the doc: there is _Graph parameter instead of _Notifier.
    50   /// \todo Fix the doc: there is _Graph parameter instead of _Notifier.
    51   template <typename _Graph, typename _Item, typename _Value>
    51   template <typename _Graph, typename _Item, typename _Value>
    52   class VectorMap 
    52   class VectorMap
    53     : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
    53     : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
    54   private:
    54   private:
    55 		
    55 
    56     /// The container type of the map.
    56     /// The container type of the map.
    57     typedef std::vector<_Value> Container;	
    57     typedef std::vector<_Value> Container;
    58 
    58 
    59   public:
    59   public:
    60 
    60 
    61     /// The graph type of the map. 
    61     /// The graph type of the map.
    62     typedef _Graph Graph;
    62     typedef _Graph Graph;
    63     /// The item type of the map.
    63     /// The item type of the map.
    64     typedef _Item Item;
    64     typedef _Item Item;
    65     /// The reference map tag.
    65     /// The reference map tag.
    66     typedef True ReferenceMapTag;
    66     typedef True ReferenceMapTag;
    91     VectorMap(const Graph& graph) {
    91     VectorMap(const Graph& graph) {
    92       Parent::attach(graph.notifier(Item()));
    92       Parent::attach(graph.notifier(Item()));
    93       container.resize(Parent::notifier()->maxId() + 1);
    93       container.resize(Parent::notifier()->maxId() + 1);
    94     }
    94     }
    95 
    95 
    96     /// \brief Constructor uses given value to initialize the map. 
    96     /// \brief Constructor uses given value to initialize the map.
    97     ///
    97     ///
    98     /// It constructs a map uses a given value to initialize the map. 
    98     /// It constructs a map uses a given value to initialize the map.
    99     /// It adds all the items of the graph to the map.
    99     /// It adds all the items of the graph to the map.
   100     VectorMap(const Graph& graph, const Value& value) {
   100     VectorMap(const Graph& graph, const Value& value) {
   101       Parent::attach(graph.notifier(Item()));
   101       Parent::attach(graph.notifier(Item()));
   102       container.resize(Parent::notifier()->maxId() + 1, value);
   102       container.resize(Parent::notifier()->maxId() + 1, value);
   103     }
   103     }
   105     /// \brief Copy constructor
   105     /// \brief Copy constructor
   106     ///
   106     ///
   107     /// Copy constructor.
   107     /// Copy constructor.
   108     VectorMap(const VectorMap& _copy) : Parent() {
   108     VectorMap(const VectorMap& _copy) : Parent() {
   109       if (_copy.attached()) {
   109       if (_copy.attached()) {
   110 	Parent::attach(*_copy.notifier());
   110         Parent::attach(*_copy.notifier());
   111 	container = _copy.container;
   111         container = _copy.container;
   112       }
   112       }
   113     }
   113     }
   114 
   114 
   115     /// \brief Assign operator.
   115     /// \brief Assign operator.
   116     ///
   116     ///
   117     /// This operator assigns for each item in the map the
   117     /// This operator assigns for each item in the map the
   118     /// value mapped to the same item in the copied map.  
   118     /// value mapped to the same item in the copied map.
   119     /// The parameter map should be indiced with the same
   119     /// The parameter map should be indiced with the same
   120     /// itemset because this assign operator does not change
   120     /// itemset because this assign operator does not change
   121     /// the container of the map. 
   121     /// the container of the map.
   122     VectorMap& operator=(const VectorMap& cmap) {
   122     VectorMap& operator=(const VectorMap& cmap) {
   123       return operator=<VectorMap>(cmap);
   123       return operator=<VectorMap>(cmap);
   124     }
   124     }
   125 
   125 
   126 
   126 
   127     /// \brief Template assign operator.
   127     /// \brief Template assign operator.
   128     ///
   128     ///
   129     /// The given parameter should be conform to the ReadMap
   129     /// The given parameter should be conform to the ReadMap
   130     /// concecpt and could be indiced by the current item set of
   130     /// concecpt and could be indiced by the current item set of
   131     /// the NodeMap. In this case the value for each item
   131     /// the NodeMap. In this case the value for each item
   132     /// is assigned by the value of the given ReadMap. 
   132     /// is assigned by the value of the given ReadMap.
   133     template <typename CMap>
   133     template <typename CMap>
   134     VectorMap& operator=(const CMap& cmap) {
   134     VectorMap& operator=(const CMap& cmap) {
   135       checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
   135       checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
   136       const typename Parent::Notifier* nf = Parent::notifier();
   136       const typename Parent::Notifier* nf = Parent::notifier();
   137       Item it;
   137       Item it;
   138       for (nf->first(it); it != INVALID; nf->next(it)) {
   138       for (nf->first(it); it != INVALID; nf->next(it)) {
   139         set(it, cmap[it]);
   139         set(it, cmap[it]);
   140       }
   140       }
   141       return *this;
   141       return *this;
   142     }
   142     }
   143     
   143 
   144   public:
   144   public:
   145 
   145 
   146     /// \brief The subcript operator.
   146     /// \brief The subcript operator.
   147     ///
   147     ///
   148     /// The subscript operator. The map can be subscripted by the
   148     /// The subscript operator. The map can be subscripted by the
   149     /// actual items of the graph.      
   149     /// actual items of the graph.
   150     Reference operator[](const Key& key) {
   150     Reference operator[](const Key& key) {
   151       return container[Parent::notifier()->id(key)];
   151       return container[Parent::notifier()->id(key)];
   152     } 
   152     }
   153 		
   153 
   154     /// \brief The const subcript operator.
   154     /// \brief The const subcript operator.
   155     ///
   155     ///
   156     /// The const subscript operator. The map can be subscripted by the
   156     /// The const subscript operator. The map can be subscripted by the
   157     /// actual items of the graph. 
   157     /// actual items of the graph.
   158     ConstReference operator[](const Key& key) const {
   158     ConstReference operator[](const Key& key) const {
   159       return container[Parent::notifier()->id(key)];
   159       return container[Parent::notifier()->id(key)];
   160     }
   160     }
   161 
   161 
   162 
   162 
   168     }
   168     }
   169 
   169 
   170   protected:
   170   protected:
   171 
   171 
   172     /// \brief Adds a new key to the map.
   172     /// \brief Adds a new key to the map.
   173     ///		
   173     ///
   174     /// It adds a new key to the map. It called by the observer notifier
   174     /// It adds a new key to the map. It called by the observer notifier
   175     /// and it overrides the add() member function of the observer base.     
   175     /// and it overrides the add() member function of the observer base.
   176     virtual void add(const Key& key) {
   176     virtual void add(const Key& key) {
   177       int id = Parent::notifier()->id(key);
   177       int id = Parent::notifier()->id(key);
   178       if (id >= int(container.size())) {
   178       if (id >= int(container.size())) {
   179 	container.resize(id + 1);
   179         container.resize(id + 1);
   180       }
   180       }
   181     }
   181     }
   182 
   182 
   183     /// \brief Adds more new keys to the map.
   183     /// \brief Adds more new keys to the map.
   184     ///		
   184     ///
   185     /// It adds more new keys to the map. It called by the observer notifier
   185     /// It adds more new keys to the map. It called by the observer notifier
   186     /// and it overrides the add() member function of the observer base.     
   186     /// and it overrides the add() member function of the observer base.
   187     virtual void add(const std::vector<Key>& keys) {
   187     virtual void add(const std::vector<Key>& keys) {
   188       int max = container.size() - 1;
   188       int max = container.size() - 1;
   189       for (int i = 0; i < int(keys.size()); ++i) {
   189       for (int i = 0; i < int(keys.size()); ++i) {
   190         int id = Parent::notifier()->id(keys[i]);
   190         int id = Parent::notifier()->id(keys[i]);
   191         if (id >= max) {
   191         if (id >= max) {
   196     }
   196     }
   197 
   197 
   198     /// \brief Erase a key from the map.
   198     /// \brief Erase a key from the map.
   199     ///
   199     ///
   200     /// Erase a key from the map. It called by the observer notifier
   200     /// Erase a key from the map. It called by the observer notifier
   201     /// and it overrides the erase() member function of the observer base.     
   201     /// and it overrides the erase() member function of the observer base.
   202     virtual void erase(const Key& key) {
   202     virtual void erase(const Key& key) {
   203       container[Parent::notifier()->id(key)] = Value();
   203       container[Parent::notifier()->id(key)] = Value();
   204     }
   204     }
   205 
   205 
   206     /// \brief Erase more keys from the map.
   206     /// \brief Erase more keys from the map.
   207     ///
   207     ///
   208     /// Erase more keys from the map. It called by the observer notifier
   208     /// Erase more keys from the map. It called by the observer notifier
   209     /// and it overrides the erase() member function of the observer base.     
   209     /// and it overrides the erase() member function of the observer base.
   210     virtual void erase(const std::vector<Key>& keys) {
   210     virtual void erase(const std::vector<Key>& keys) {
   211       for (int i = 0; i < int(keys.size()); ++i) {
   211       for (int i = 0; i < int(keys.size()); ++i) {
   212 	container[Parent::notifier()->id(keys[i])] = Value();
   212         container[Parent::notifier()->id(keys[i])] = Value();
   213       }
   213       }
   214     }
   214     }
   215     
   215 
   216     /// \brief Buildes the map.
   216     /// \brief Buildes the map.
   217     ///	
   217     ///
   218     /// It buildes the map. It called by the observer notifier
   218     /// It buildes the map. It called by the observer notifier
   219     /// and it overrides the build() member function of the observer base.
   219     /// and it overrides the build() member function of the observer base.
   220     virtual void build() { 
   220     virtual void build() {
   221       int size = Parent::notifier()->maxId() + 1;
   221       int size = Parent::notifier()->maxId() + 1;
   222       container.reserve(size);
   222       container.reserve(size);
   223       container.resize(size);
   223       container.resize(size);
   224     }
   224     }
   225 
   225 
   226     /// \brief Clear the map.
   226     /// \brief Clear the map.
   227     ///
   227     ///
   228     /// It erase all items from the map. It called by the observer notifier
   228     /// It erase all items from the map. It called by the observer notifier
   229     /// and it overrides the clear() member function of the observer base.     
   229     /// and it overrides the clear() member function of the observer base.
   230     virtual void clear() { 
   230     virtual void clear() {
   231       container.clear();
   231       container.clear();
   232     }
   232     }
   233     
   233 
   234   private:
   234   private:
   235 		
   235 
   236     Container container;
   236     Container container;
   237 
   237 
   238   };
   238   };
   239 
   239 
   240 }
   240 }