COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/maps.h @ 910:5a89cacf17f1

Last change on this file since 910:5a89cacf17f1 was 906:17f31d280385, checked in by Alpar Juttner, 20 years ago

Copyright header added.

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