lemon/bits/vector_map.h
changeset 57 c1acf0018c0a
child 107 31a2e6d28f61
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/vector_map.h	Sun Jan 20 20:43:48 2008 +0100
     1.3 @@ -0,0 +1,243 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2007
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_BITS_VECTOR_MAP_H
    1.23 +#define LEMON_BITS_VECTOR_MAP_H
    1.24 +
    1.25 +#include <vector>
    1.26 +#include <algorithm>
    1.27 +
    1.28 +#include <lemon/bits/traits.h>
    1.29 +#include <lemon/bits/utility.h>
    1.30 +
    1.31 +#include <lemon/bits/alteration_notifier.h>
    1.32 +
    1.33 +#include <lemon/concept_check.h>
    1.34 +#include <lemon/concepts/maps.h>
    1.35 +
    1.36 +///\ingroup graphbits
    1.37 +///
    1.38 +///\file
    1.39 +///\brief Vector based graph maps.
    1.40 +namespace lemon {
    1.41 +
    1.42 +  /// \ingroup graphbits
    1.43 +  ///
    1.44 +  /// \brief Graph map based on the std::vector storage.
    1.45 +  ///
    1.46 +  /// The VectorMap template class is graph map structure what
    1.47 +  /// automatically updates the map when a key is added to or erased from
    1.48 +  /// the map. This map type uses the std::vector to store the values.
    1.49 +  ///
    1.50 +  /// \param Notifier The AlterationNotifier that will notify this map.
    1.51 +  /// \param Item The item type of the graph items.
    1.52 +  /// \param Value The value type of the map.
    1.53 +  /// 
    1.54 +  /// \author Balazs Dezso  	
    1.55 +  template <typename _Graph, typename _Item, typename _Value>
    1.56 +  class VectorMap 
    1.57 +    : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
    1.58 +  private:
    1.59 +		
    1.60 +    /// The container type of the map.
    1.61 +    typedef std::vector<_Value> Container;	
    1.62 +
    1.63 +  public:
    1.64 +
    1.65 +    /// The graph type of the map. 
    1.66 +    typedef _Graph Graph;
    1.67 +    /// The item type of the map.
    1.68 +    typedef _Item Item;
    1.69 +    /// The reference map tag.
    1.70 +    typedef True ReferenceMapTag;
    1.71 +
    1.72 +    /// The key type of the map.
    1.73 +    typedef _Item Key;
    1.74 +    /// The value type of the map.
    1.75 +    typedef _Value Value;
    1.76 +
    1.77 +    /// The notifier type.
    1.78 +    typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
    1.79 +
    1.80 +    /// The map type.
    1.81 +    typedef VectorMap Map;
    1.82 +    /// The base class of the map.
    1.83 +    typedef typename Notifier::ObserverBase Parent;
    1.84 +
    1.85 +    /// The reference type of the map;
    1.86 +    typedef typename Container::reference Reference;
    1.87 +    /// The const reference type of the map;
    1.88 +    typedef typename Container::const_reference ConstReference;
    1.89 +
    1.90 +
    1.91 +    /// \brief Constructor to attach the new map into the notifier.
    1.92 +    ///
    1.93 +    /// It constructs a map and attachs it into the notifier.
    1.94 +    /// It adds all the items of the graph to the map.
    1.95 +    VectorMap(const Graph& graph) {
    1.96 +      Parent::attach(graph.notifier(Item()));
    1.97 +      container.resize(Parent::notifier()->maxId() + 1);
    1.98 +    }
    1.99 +
   1.100 +    /// \brief Constructor uses given value to initialize the map. 
   1.101 +    ///
   1.102 +    /// It constructs a map uses a given value to initialize the map. 
   1.103 +    /// It adds all the items of the graph to the map.
   1.104 +    VectorMap(const Graph& graph, const Value& value) {
   1.105 +      Parent::attach(graph.notifier(Item()));
   1.106 +      container.resize(Parent::notifier()->maxId() + 1, value);
   1.107 +    }
   1.108 +
   1.109 +    /// \brief Copy constructor
   1.110 +    ///
   1.111 +    /// Copy constructor.
   1.112 +    VectorMap(const VectorMap& _copy) : Parent() {
   1.113 +      if (_copy.attached()) {
   1.114 +	Parent::attach(*_copy.notifier());
   1.115 +	container = _copy.container;
   1.116 +      }
   1.117 +    }
   1.118 +
   1.119 +    /// \brief Assign operator.
   1.120 +    ///
   1.121 +    /// This operator assigns for each item in the map the
   1.122 +    /// value mapped to the same item in the copied map.  
   1.123 +    /// The parameter map should be indiced with the same
   1.124 +    /// itemset because this assign operator does not change
   1.125 +    /// the container of the map. 
   1.126 +    VectorMap& operator=(const VectorMap& cmap) {
   1.127 +      return operator=<VectorMap>(cmap);
   1.128 +    }
   1.129 +
   1.130 +
   1.131 +    /// \brief Template assign operator.
   1.132 +    ///
   1.133 +    /// The given parameter should be conform to the ReadMap
   1.134 +    /// concecpt and could be indiced by the current item set of
   1.135 +    /// the NodeMap. In this case the value for each item
   1.136 +    /// is assigned by the value of the given ReadMap. 
   1.137 +    template <typename CMap>
   1.138 +    VectorMap& operator=(const CMap& cmap) {
   1.139 +      checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
   1.140 +      const typename Parent::Notifier* nf = Parent::notifier();
   1.141 +      Item it;
   1.142 +      for (nf->first(it); it != INVALID; nf->next(it)) {
   1.143 +        set(it, cmap[it]);
   1.144 +      }
   1.145 +      return *this;
   1.146 +    }
   1.147 +    
   1.148 +  public:
   1.149 +
   1.150 +    /// \brief The subcript operator.
   1.151 +    ///
   1.152 +    /// The subscript operator. The map can be subscripted by the
   1.153 +    /// actual items of the graph.      
   1.154 +    Reference operator[](const Key& key) {
   1.155 +      return container[Parent::notifier()->id(key)];
   1.156 +    } 
   1.157 +		
   1.158 +    /// \brief The const subcript operator.
   1.159 +    ///
   1.160 +    /// The const subscript operator. The map can be subscripted by the
   1.161 +    /// actual items of the graph. 
   1.162 +    ConstReference operator[](const Key& key) const {
   1.163 +      return container[Parent::notifier()->id(key)];
   1.164 +    }
   1.165 +
   1.166 +
   1.167 +    /// \brief The setter function of the map.
   1.168 +    ///
   1.169 +    /// It the same as operator[](key) = value expression.
   1.170 +    void set(const Key& key, const Value& value) {
   1.171 +      (*this)[key] = value;
   1.172 +    }
   1.173 +
   1.174 +  protected:
   1.175 +
   1.176 +    /// \brief Adds a new key to the map.
   1.177 +    ///		
   1.178 +    /// It adds a new key to the map. It called by the observer notifier
   1.179 +    /// and it overrides the add() member function of the observer base.     
   1.180 +    virtual void add(const Key& key) {
   1.181 +      int id = Parent::notifier()->id(key);
   1.182 +      if (id >= int(container.size())) {
   1.183 +	container.resize(id + 1);
   1.184 +      }
   1.185 +    }
   1.186 +
   1.187 +    /// \brief Adds more new keys to the map.
   1.188 +    ///		
   1.189 +    /// It adds more new keys to the map. It called by the observer notifier
   1.190 +    /// and it overrides the add() member function of the observer base.     
   1.191 +    virtual void add(const std::vector<Key>& keys) {
   1.192 +      int max = container.size() - 1;
   1.193 +      for (int i = 0; i < int(keys.size()); ++i) {
   1.194 +        int id = Parent::notifier()->id(keys[i]);
   1.195 +        if (id >= max) {
   1.196 +          max = id;
   1.197 +        }
   1.198 +      }
   1.199 +      container.resize(max + 1);
   1.200 +    }
   1.201 +
   1.202 +    /// \brief Erase a key from the map.
   1.203 +    ///
   1.204 +    /// Erase a key from the map. It called by the observer notifier
   1.205 +    /// and it overrides the erase() member function of the observer base.     
   1.206 +    virtual void erase(const Key& key) {
   1.207 +      container[Parent::notifier()->id(key)] = Value();
   1.208 +    }
   1.209 +
   1.210 +    /// \brief Erase more keys from the map.
   1.211 +    ///
   1.212 +    /// Erase more keys from the map. It called by the observer notifier
   1.213 +    /// and it overrides the erase() member function of the observer base.     
   1.214 +    virtual void erase(const std::vector<Key>& keys) {
   1.215 +      for (int i = 0; i < int(keys.size()); ++i) {
   1.216 +	container[Parent::notifier()->id(keys[i])] = Value();
   1.217 +      }
   1.218 +    }
   1.219 +    
   1.220 +    /// \brief Buildes the map.
   1.221 +    ///	
   1.222 +    /// It buildes the map. It called by the observer notifier
   1.223 +    /// and it overrides the build() member function of the observer base.
   1.224 +    virtual void build() { 
   1.225 +      int size = Parent::notifier()->maxId() + 1;
   1.226 +      container.reserve(size);
   1.227 +      container.resize(size);
   1.228 +    }
   1.229 +
   1.230 +    /// \brief Clear the map.
   1.231 +    ///
   1.232 +    /// It erase all items from the map. It called by the observer notifier
   1.233 +    /// and it overrides the clear() member function of the observer base.     
   1.234 +    virtual void clear() { 
   1.235 +      container.clear();
   1.236 +    }
   1.237 +    
   1.238 +  private:
   1.239 +		
   1.240 +    Container container;
   1.241 +
   1.242 +  };
   1.243 +
   1.244 +}
   1.245 +
   1.246 +#endif