lemon/maps.h
changeset 2514 57143c09dc20
parent 2489 48dddc283cfc
child 2553 bfced05fa852
equal deleted inserted replaced
30:370a65be8358 31:7129004e7514
    19 #ifndef LEMON_MAPS_H
    19 #ifndef LEMON_MAPS_H
    20 #define LEMON_MAPS_H
    20 #define LEMON_MAPS_H
    21 
    21 
    22 #include <iterator>
    22 #include <iterator>
    23 #include <functional>
    23 #include <functional>
       
    24 #include <vector>
    24 
    25 
    25 #include <lemon/bits/utility.h>
    26 #include <lemon/bits/utility.h>
    26 #include <lemon/bits/traits.h>
    27 #include <lemon/bits/traits.h>
    27 
    28 
    28 ///\file
    29 ///\file
   237 
   238 
   238     template <typename T1, typename C1 = std::less<T1> >
   239     template <typename T1, typename C1 = std::less<T1> >
   239     struct rebind {
   240     struct rebind {
   240       typedef StdMap<Key, T1, C1> other;
   241       typedef StdMap<Key, T1, C1> other;
   241     };
   242     };
       
   243   };
       
   244 
       
   245   /// \brief Map for storing values for the range \c [0..size-1] range keys
       
   246   ///
       
   247   /// The current map has the \c [0..size-1] keyset and the values
       
   248   /// are stored in a \c std::vector<T>  container. It can be used with
       
   249   /// some data structures, for example \c UnionFind, \c BinHeap, when 
       
   250   /// the used items are small integer numbers.
       
   251   template <typename T>
       
   252   class IntegerMap {
       
   253 
       
   254     template <typename T1>
       
   255     friend class IntegerMap;
       
   256 
       
   257   public:
       
   258 
       
   259     typedef True ReferenceMapTag;
       
   260     ///\e
       
   261     typedef int Key;
       
   262     ///\e
       
   263     typedef T Value;
       
   264     ///\e
       
   265     typedef T& Reference;
       
   266     ///\e
       
   267     typedef const T& ConstReference;
       
   268 
       
   269   private:
       
   270     
       
   271     typedef std::vector<T> Vector;
       
   272     Vector _vector;
       
   273 
       
   274   public:
       
   275 
       
   276     /// Constructor with specified default value
       
   277     IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
       
   278 
       
   279     /// \brief Constructs the map from an appropriate std::vector.
       
   280     template <typename T1>
       
   281     IntegerMap(const std::vector<T1>& vector) 
       
   282       : _vector(vector.begin(), vector.end()) {}
       
   283     
       
   284     /// \brief Constructs a map from an other IntegerMap.
       
   285     template <typename T1>
       
   286     IntegerMap(const IntegerMap<T1> &c) 
       
   287       : _vector(c._vector.begin(), c._vector.end()) {}
       
   288 
       
   289     /// \brief Resize the container
       
   290     void resize(int size, const T& value = T()) {
       
   291       _vector.resize(size, value);
       
   292     }
       
   293 
       
   294   private:
       
   295 
       
   296     IntegerMap& operator=(const IntegerMap&);
       
   297 
       
   298   public:
       
   299 
       
   300     ///\e
       
   301     Reference operator[](Key k) {
       
   302       return _vector[k];
       
   303     }
       
   304 
       
   305     /// \e 
       
   306     ConstReference operator[](Key k) const {
       
   307       return _vector[k];
       
   308     }
       
   309 
       
   310     /// \e 
       
   311     void set(const Key &k, const T& t) {
       
   312       _vector[k] = t;
       
   313     }
       
   314 
   242   };
   315   };
   243 
   316 
   244   /// @}
   317   /// @}
   245 
   318 
   246   /// \addtogroup map_adaptors
   319   /// \addtogroup map_adaptors