[Lemon-commits] Peter Kovacs: Overall clean-up in maps.h

Lemon HG hg at lemon.cs.elte.hu
Sun Mar 16 08:35:43 CET 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/15968e25ca08
changeset: 80:15968e25ca08
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Sat Mar 15 21:07:24 2008 +0100
description:
	Overall clean-up in maps.h

	- Rename some map types:
	    * IntegerMap -> RangeMap
	    * StdMap -> SparseMap
	    * FunctorMap -> FunctorToMap
	    * MapFunctor -> MapToFunctor
	    * ForkWriteMap -> ForkMap
	    * SimpleMap -> WrapMap
	    * SimpleWriteMap -> WrapWriteMap
	- Remove the read-only ForkMap version.
	- Rename map-creator functions for the read-write arithmetic and
	logical maps.
	- Small fixes and improvements in the code.
	- Fix the typedefs of RangeMap to work correctly with bool type, too.
	- Rename template parameters, function parameters, and private members
	in many classes to be uniform and to avoid parameter names starting
	with underscore.
	- Use Key and Value types instead of K and V template parameters in
	public functions.
	- Extend the documentation with examples (e.g. for basic arithmetic
	and logical maps).
	- Many doc improvements.
	- Reorder the classes.
	- StoreBoolMap, BackInserterBoolMap, FrontInserterBoolMap,
	InserterBoolMap, FillBoolMap, SettingOrderBoolMap are almost
	unchanged, since they will be removed.
	- Also improve maps_test.cc to correctly check every map class, every
	constructor, and every creator function.

diffstat:

2 files changed, 1427 insertions(+), 1061 deletions(-)
lemon/maps.h      | 2226 ++++++++++++++++++++++++++++-------------------------
test/maps_test.cc |  262 +++++-

diffs (truncated from 3079 to 300 lines):

diff -r d73c2e8b25cb -r 15968e25ca08 lemon/maps.h
--- a/lemon/maps.h	Sat Mar 15 20:21:21 2008 +0100
+++ b/lemon/maps.h	Sat Mar 15 21:07:24 2008 +0100
@@ -24,12 +24,12 @@
 #include <vector>
 
 #include <lemon/bits/utility.h>
-// #include <lemon/bits/traits.h>
+#include <lemon/bits/traits.h>
 
 ///\file
 ///\ingroup maps
 ///\brief Miscellaneous property maps
-///
+
 #include <map>
 
 namespace lemon {
@@ -39,41 +39,46 @@ namespace lemon {
 
   /// Base class of maps.
 
-  /// Base class of maps.
-  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
-  template<typename K, typename T>
+  /// Base class of maps. It provides the necessary type definitions
+  /// required by the map %concepts.
+  template<typename K, typename V>
   class MapBase {
   public:
-    /// The key type of the map.
+    /// \biref The key type of the map.
     typedef K Key;
-    /// The value type of the map. (The type of objects associated with the keys).
-    typedef T Value;
+    /// \brief The value type of the map.
+    /// (The type of objects associated with the keys).
+    typedef V Value;
   };
+
 
   /// Null map. (a.k.a. DoNothingMap)
 
   /// This map can be used if you have to provide a map only for
-  /// its type definitions, or if you have to provide a writable map, 
-  /// but data written to it is not required (i.e. it will be sent to 
+  /// its type definitions, or if you have to provide a writable map,
+  /// but data written to it is not required (i.e. it will be sent to
   /// <tt>/dev/null</tt>).
-  template<typename K, typename T>
-  class NullMap : public MapBase<K, T> {
+  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
+  ///
+  /// \sa ConstMap
+  template<typename K, typename V>
+  class NullMap : public MapBase<K, V> {
   public:
-    typedef MapBase<K, T> Parent;
+    typedef MapBase<K, V> Parent;
     typedef typename Parent::Key Key;
     typedef typename Parent::Value Value;
-    
+
     /// Gives back a default constructed element.
-    T operator[](const K&) const { return T(); }
+    Value operator[](const Key&) const { return Value(); }
     /// Absorbs the value.
-    void set(const K&, const T&) {}
+    void set(const Key&, const Value&) {}
   };
 
-  ///Returns a \c NullMap class
+  /// Returns a \ref NullMap class
 
-  ///This function just returns a \c NullMap class.
-  ///\relates NullMap
-  template <typename K, typename V> 
+  /// This function just returns a \ref NullMap class.
+  /// \relates NullMap
+  template <typename K, typename V>
   NullMap<K, V> nullMap() {
     return NullMap<K, V>();
   }
@@ -81,62 +86,81 @@ namespace lemon {
 
   /// Constant map.
 
-  /// This is a \ref concepts::ReadMap "readable" map which assigns a 
+  /// This is a \ref concepts::ReadMap "readable" map which assigns a
   /// specified value to each key.
-  /// In other aspects it is equivalent to \c NullMap.
-  template<typename K, typename T>
-  class ConstMap : public MapBase<K, T> {
+  ///
+  /// In other aspects it is equivalent to \ref NullMap.
+  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
+  /// concept, but it absorbs the data written to it.
+  ///
+  /// The simplest way of using this map is through the constMap()
+  /// function.
+  ///
+  /// \sa NullMap
+  /// \sa IdentityMap
+  template<typename K, typename V>
+  class ConstMap : public MapBase<K, V> {
   private:
-    T v;
+    V _value;
   public:
-
-    typedef MapBase<K, T> Parent;
+    typedef MapBase<K, V> Parent;
     typedef typename Parent::Key Key;
     typedef typename Parent::Value Value;
 
     /// Default constructor
 
     /// Default constructor.
-    /// The value of the map will be uninitialized. 
-    /// (More exactly it will be default constructed.)
+    /// The value of the map will be default constructed.
     ConstMap() {}
-    
+
     /// Constructor with specified initial value
 
     /// Constructor with specified initial value.
-    /// \param _v is the initial value of the map.
-    ConstMap(const T &_v) : v(_v) {}
-    
-    ///\e
-    T operator[](const K&) const { return v; }
+    /// \param v is the initial value of the map.
+    ConstMap(const Value &v) : _value(v) {}
 
-    ///\e
-    void setAll(const T &t) {
-      v = t;
-    }    
+    /// Gives back the specified value.
+    Value operator[](const Key&) const { return _value; }
 
-    template<typename T1>
-    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
+    /// Absorbs the value.
+    void set(const Key&, const Value&) {}
+
+    /// Sets the value that is assigned to each key.
+    void setAll(const Value &v) {
+      _value = v;
+    }
+
+    template<typename V1>
+    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
   };
 
-  ///Returns a \c ConstMap class
+  /// Returns a \ref ConstMap class
 
-  ///This function just returns a \c ConstMap class.
-  ///\relates ConstMap
-  template<typename K, typename V> 
+  /// This function just returns a \ref ConstMap class.
+  /// \relates ConstMap
+  template<typename K, typename V>
   inline ConstMap<K, V> constMap(const V &v) {
     return ConstMap<K, V>(v);
   }
 
 
   template<typename T, T v>
-  struct Const { };
+  struct Const {};
 
   /// Constant map with inlined constant value.
 
-  /// This is a \ref concepts::ReadMap "readable" map which assigns a 
+  /// This is a \ref concepts::ReadMap "readable" map which assigns a
   /// specified value to each key.
-  /// In other aspects it is equivalent to \c NullMap.
+  ///
+  /// In other aspects it is equivalent to \ref NullMap.
+  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
+  /// concept, but it absorbs the data written to it.
+  ///
+  /// The simplest way of using this map is through the constMap()
+  /// function.
+  ///
+  /// \sa NullMap
+  /// \sa IdentityMap
   template<typename K, typename V, V v>
   class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
   public:
@@ -144,69 +168,230 @@ namespace lemon {
     typedef typename Parent::Key Key;
     typedef typename Parent::Value Value;
 
-    ConstMap() { }
-    ///\e
-    V operator[](const K&) const { return v; }
-    ///\e
-    void set(const K&, const V&) { }
+    /// Constructor.
+    ConstMap() {}
+
+    /// Gives back the specified value.
+    Value operator[](const Key&) const { return v; }
+
+    /// Absorbs the value.
+    void set(const Key&, const Value&) {}
   };
 
-  ///Returns a \c ConstMap class with inlined value
+  /// Returns a \ref ConstMap class with inlined constant value
 
-  ///This function just returns a \c ConstMap class with inlined value.
-  ///\relates ConstMap
-  template<typename K, typename V, V v> 
+  /// This function just returns a \ref ConstMap class with inlined
+  /// constant value.
+  /// \relates ConstMap
+  template<typename K, typename V, V v>
   inline ConstMap<K, Const<V, v> > constMap() {
     return ConstMap<K, Const<V, v> >();
   }
 
-  ///Map based on \c std::map
 
-  ///This is essentially a wrapper for \c std::map with addition that
-  ///you can specify a default value different from \c Value().
-  ///It meets the \ref concepts::ReferenceMap "ReferenceMap" concept.
-  template <typename K, typename T, typename Compare = std::less<K> >
-  class StdMap : public MapBase<K, T> {
-    template <typename K1, typename T1, typename C1>
-    friend class StdMap;
+  /// \brief Identity map.
+  ///
+  /// This map gives back the given key as value without any
+  /// modification.
+  ///
+  /// \sa ConstMap
+  template <typename T>
+  class IdentityMap : public MapBase<T, T> {
+  public:
+    typedef MapBase<T, T> Parent;
+    typedef typename Parent::Key Key;
+    typedef typename Parent::Value Value;
+
+    /// Gives back the given value without any modification.
+    const T& operator[](const T& t) const {
+      return t;
+    }
+  };
+
+  /// Returns an \ref IdentityMap class
+
+  /// This function just returns an \ref IdentityMap class.
+  /// \relates IdentityMap
+  template<typename T>
+  inline IdentityMap<T> identityMap() {
+    return IdentityMap<T>();
+  }
+
+
+  /// \brief Map for storing values for integer keys from the range
+  /// <tt>[0..size-1]</tt>.
+  ///
+  /// This map is essentially a wrapper for \c std::vector. It assigns
+  /// values to integer keys from the range <tt>[0..size-1]</tt>.
+  /// It can be used with some data structures, for example
+  /// \ref UnionFind, \ref BinHeap, when the used items are small
+  /// integers. This map conforms the \ref concepts::ReferenceMap
+  /// "ReferenceMap" concept.
+  ///
+  /// The simplest way of using this map is through the rangeMap()
+  /// function.
+  template <typename V>
+  class RangeMap : public MapBase<int, V> {
+    template <typename V1>
+    friend class RangeMap;
+  private:
+
+    typedef std::vector<V> Vector;
+    Vector _vector;
+
   public:
 
-    typedef MapBase<K, T> Parent;
-    ///Key type
+    typedef MapBase<int, V> Parent;
+    /// Key type
     typedef typename Parent::Key Key;
-    ///Value type
+    /// Value type
     typedef typename Parent::Value Value;
-    ///Reference Type
-    typedef T& Reference;
-    ///Const reference type
-    typedef const T& ConstReference;
+    /// Reference type



More information about the Lemon-commits mailing list