src/lemon/maps.h
author alpar
Sun, 14 Nov 2004 13:15:46 +0000
changeset 991 e619a466ca5d
parent 977 48962802d168
child 1041 9d503ce002db
permissions -rw-r--r--
- Move template.h to doc/.
- Insert the license terms into the documentation.
- template.h now appears under the coding style documentation.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_MAPS_H
alpar@921
    18
#define LEMON_MAPS_H
klao@286
    19
klao@286
    20
///\file
klao@286
    21
///\brief Miscellaneous property maps
klao@286
    22
///
klao@959
    23
///\todo This file has the same name as the concept file in concept/,
klao@286
    24
/// and this is not easily detectable in docs...
klao@286
    25
klao@286
    26
#include <map>
klao@286
    27
alpar@921
    28
namespace lemon {
klao@286
    29
alpar@720
    30
  /// Base class of maps.
alpar@720
    31
alpar@805
    32
  /// Base class of maps.
alpar@805
    33
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
alpar@720
    34
  template<typename K, typename T>
alpar@720
    35
  class MapBase
alpar@720
    36
  {
alpar@720
    37
  public:
alpar@911
    38
    ///\e
alpar@987
    39
    typedef K Key;
alpar@911
    40
    ///\e
alpar@987
    41
    typedef T Value;
alpar@720
    42
  };
alpar@720
    43
alpar@805
    44
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    45
klao@286
    46
  /// If you have to provide a map only for its type definitions,
alpar@805
    47
  /// or if you have to provide a writable map, but
alpar@805
    48
  /// data written to it will sent to <tt>/dev/null</tt>...
klao@286
    49
  template<typename K, typename T>
alpar@720
    50
  class NullMap : public MapBase<K,T>
klao@286
    51
  {
klao@286
    52
  public:
klao@286
    53
alpar@805
    54
    /// Gives back a default constructed element.
klao@286
    55
    T operator[](const K&) const { return T(); }
alpar@805
    56
    /// Absorbs the value.
klao@286
    57
    void set(const K&, const T&) {}
klao@286
    58
  };
klao@286
    59
klao@286
    60
klao@286
    61
  /// Constant map.
klao@286
    62
alpar@805
    63
  /// This is a readable map which assigns a specified value to each key.
alpar@805
    64
  /// In other aspects it is equivalent to the \ref NullMap.
alpar@805
    65
  /// \todo set could be used to set the value.
klao@286
    66
  template<typename K, typename T>
alpar@720
    67
  class ConstMap : public MapBase<K,T>
klao@286
    68
  {
klao@286
    69
    T v;
klao@286
    70
  public:
klao@286
    71
alpar@805
    72
    /// Default constructor
alpar@805
    73
alpar@805
    74
    /// The value of the map will be uninitialized. 
alpar@805
    75
    /// (More exactly it will be default constructed.)
klao@286
    76
    ConstMap() {}
alpar@911
    77
    ///\e
alpar@805
    78
alpar@805
    79
    /// \param _v The initial value of the map.
alpar@911
    80
    ///
klao@286
    81
    ConstMap(const T &_v) : v(_v) {}
klao@286
    82
klao@286
    83
    T operator[](const K&) const { return v; }
klao@286
    84
    void set(const K&, const T&) {}
klao@286
    85
klao@286
    86
    template<typename T1>
klao@286
    87
    struct rebind {
klao@286
    88
      typedef ConstMap<K,T1> other;
klao@286
    89
    };
klao@286
    90
klao@286
    91
    template<typename T1>
klao@286
    92
    ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
klao@286
    93
  };
klao@286
    94
marci@890
    95
  //to document later
marci@890
    96
  template<typename T, T v>
marci@890
    97
  struct Const { };
marci@890
    98
  //to document later
marci@890
    99
  template<typename K, typename V, V v>
marci@890
   100
  class ConstMap<K, Const<V, v> > : public MapBase<K, V>
marci@890
   101
  {
marci@890
   102
  public:
marci@890
   103
    ConstMap() { }
marci@890
   104
    V operator[](const K&) const { return v; }
marci@890
   105
    void set(const K&, const V&) { }
marci@890
   106
  };
klao@286
   107
klao@286
   108
  /// \c std::map wrapper
klao@286
   109
klao@286
   110
  /// This is essentially a wrapper for \c std::map. With addition that
alpar@987
   111
  /// you can specify a default value different from \c Value() .
klao@286
   112
  ///
klao@286
   113
  /// \todo Provide allocator parameter...
alpar@987
   114
  template <typename K, typename T, typename Compare = std::less<K> >
alpar@987
   115
  class StdMap : public std::map<K,T,Compare> {
alpar@987
   116
    typedef std::map<K,T,Compare> parent;
klao@286
   117
    T v;
klao@286
   118
    typedef typename parent::value_type PairType;
klao@286
   119
klao@286
   120
  public:
alpar@987
   121
    typedef K Key;
alpar@987
   122
    typedef T Value;
alpar@987
   123
    typedef T& Reference;
alpar@987
   124
    typedef const T& ConstReference;
klao@286
   125
klao@286
   126
klao@345
   127
    StdMap() : v() {}
klao@286
   128
    /// Constructor with specified default value
klao@286
   129
    StdMap(const T& _v) : v(_v) {}
klao@286
   130
klao@286
   131
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   132
    ///
klao@286
   133
    /// \warning Inefficient: copies the content of \c m !
klao@286
   134
    StdMap(const parent &m) : parent(m) {}
klao@286
   135
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   136
    /// specifies a default value.
klao@286
   137
    ///
klao@286
   138
    /// \warning Inefficient: copies the content of \c m !
klao@286
   139
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   140
    
klao@286
   141
    template<typename T1, typename Comp1>
marci@389
   142
    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
marci@389
   143
      //FIXME; 
marci@389
   144
    }
klao@286
   145
alpar@987
   146
    Reference operator[](const Key &k) {
klao@346
   147
      return insert(PairType(k,v)).first -> second;
klao@286
   148
    }
alpar@987
   149
    ConstReference operator[](const Key &k) const {
marci@389
   150
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   151
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   152
	return v;
klao@286
   153
      return (*i).second;
klao@286
   154
    }
klao@345
   155
    void set(const Key &k, const T &t) {
klao@346
   156
      parent::operator[](k) = t;
klao@345
   157
    }
klao@286
   158
klao@286
   159
    /// Changes the default value of the map.
klao@286
   160
    /// \return Returns the previous default value.
klao@286
   161
    ///
alpar@805
   162
    /// \warning The value of some keys (which has already been queried, but
klao@286
   163
    /// the value has been unchanged from the default) may change!
klao@286
   164
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   165
klao@286
   166
    template<typename T1>
klao@286
   167
    struct rebind {
klao@286
   168
      typedef StdMap<Key,T1,Compare> other;
klao@286
   169
    };
klao@286
   170
  };
klao@286
   171
  
klao@286
   172
}
alpar@921
   173
#endif // LEMON_MAPS_H