Miscellaneous maps, early version
authorklao
Sat, 03 Apr 2004 20:33:05 +0000
changeset 286d3c4d99860a9
parent 285 0bc5f7f66bfa
child 287 5f42cb5cc1bf
Miscellaneous maps, early version
skeletons/maps.h: minor bug
src/include/maps.h
src/include/skeletons/maps.h
src/work/klao/map_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/include/maps.h	Sat Apr 03 20:33:05 2004 +0000
     1.3 @@ -0,0 +1,122 @@
     1.4 +// -*- C++ -*- //
     1.5 +#ifndef HUGO_MAPS_H
     1.6 +#define HUGO_MAPS_H
     1.7 +
     1.8 +///\file
     1.9 +///\brief Miscellaneous property maps
    1.10 +///
    1.11 +///\todo This file has the same name as the concept file in skeletons,
    1.12 +/// and this is not easily detectable in docs...
    1.13 +
    1.14 +#include <map>
    1.15 +
    1.16 +namespace hugo {
    1.17 +
    1.18 +  /// Null map. (aka DoNothingMap)
    1.19 +
    1.20 +  /// If you have to provide a map only for its type definitions,
    1.21 +  /// or if you have to provide a writable map, but will not use the
    1.22 +  /// data written to it...
    1.23 +  template<typename K, typename T>
    1.24 +  class NullMap
    1.25 +  {
    1.26 +  public:
    1.27 +    typedef K KeyType;
    1.28 +    typedef T ValueType;
    1.29 +
    1.30 +    T operator[](const K&) const { return T(); }
    1.31 +    void set(const K&, const T&) {}
    1.32 +  };
    1.33 +
    1.34 +
    1.35 +  /// Constant map.
    1.36 +
    1.37 +  /// This is a readable map which assignes a specified value to each key.
    1.38 +  /// In other aspects it is equivalent to the \ref NullMap
    1.39 +  template<typename K, typename T>
    1.40 +  class ConstMap
    1.41 +  {
    1.42 +    T v;
    1.43 +  public:
    1.44 +    typedef K KeyType;
    1.45 +    typedef T ValueType;
    1.46 +
    1.47 +    ConstMap() {}
    1.48 +    ConstMap(const T &_v) : v(_v) {}
    1.49 +
    1.50 +    T operator[](const K&) const { return v; }
    1.51 +    void set(const K&, const T&) {}
    1.52 +
    1.53 +    template<typename T1>
    1.54 +    struct rebind {
    1.55 +      typedef ConstMap<K,T1> other;
    1.56 +    };
    1.57 +
    1.58 +    template<typename T1>
    1.59 +    ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
    1.60 +  };
    1.61 +
    1.62 +
    1.63 +
    1.64 +  /// \c std::map wrapper
    1.65 +
    1.66 +  /// This is essentially a wrapper for \c std::map. With addition that
    1.67 +  /// you can specify a default value different from \c ValueType() .
    1.68 +  ///
    1.69 +  /// \todo Provide allocator parameter...
    1.70 +  template <typename Key, typename T, typename Compare = std::less<Key> >
    1.71 +  class StdMap : public std::map<Key,T,Compare> {
    1.72 +    typedef std::map<Key,T,Compare> parent;
    1.73 +    T v;
    1.74 +    typedef typename parent::value_type PairType;
    1.75 +
    1.76 +  public:
    1.77 +    typedef Key KeyType;
    1.78 +    typedef T ValueType;
    1.79 +    typedef T& ReferenceType;
    1.80 +    typedef const T& ConstReferenceType;
    1.81 +
    1.82 +
    1.83 +    StdMap() {}
    1.84 +    /// Constructor with specified default value
    1.85 +    StdMap(const T& _v) : v(_v) {}
    1.86 +
    1.87 +    /// \brief Constructs the map from an appropriate std::map.
    1.88 +    ///
    1.89 +    /// \warning Inefficient: copies the content of \c m !
    1.90 +    StdMap(const parent &m) : parent(m) {}
    1.91 +    /// \brief Constructs the map from an appropriate std::map, and explicitly
    1.92 +    /// specifies a default value.
    1.93 +    ///
    1.94 +    /// \warning Inefficient: copies the content of \c m !
    1.95 +    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
    1.96 +    
    1.97 +    template<typename T1, typename Comp1>
    1.98 +    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { FIXME; }
    1.99 +
   1.100 +    ReferenceType operator[](const Key &k) {
   1.101 +      return (  *( (insert(PairType(k,v))).first )  ).second;
   1.102 +    }
   1.103 +    ConstReferenceType operator[](const Key &k) const {
   1.104 +      typename parent::iterator i = lower_bound(__k);
   1.105 +      if (i == end() || key_comp()(k, (*i).first))
   1.106 +	return v;
   1.107 +      return (*i).second;
   1.108 +    }
   1.109 +    void set(const Key &k, const T &t) { parent::operator[](k)=t; }
   1.110 +
   1.111 +    /// Changes the default value of the map.
   1.112 +    /// \return Returns the previous default value.
   1.113 +    ///
   1.114 +    /// \warning The value of some keys (which has alredy been queried, but
   1.115 +    /// the value has been unchanged from the default) may change!
   1.116 +    T setDefault(const T &_v) { T old=v; v=_v; return old; }
   1.117 +
   1.118 +    template<typename T1>
   1.119 +    struct rebind {
   1.120 +      typedef StdMap<Key,T1,Compare> other;
   1.121 +    };
   1.122 +  };
   1.123 +  
   1.124 +}
   1.125 +#endif // HUGO_MAPS_H
     2.1 --- a/src/include/skeletons/maps.h	Sat Apr 03 18:42:39 2004 +0000
     2.2 +++ b/src/include/skeletons/maps.h	Sat Apr 03 20:33:05 2004 +0000
     2.3 @@ -78,7 +78,7 @@
     2.4        /// @brief Constructor that copies all keys from the other map and
     2.5        /// assigns to them a default value (optional)
     2.6        template<typename T1>
     2.7 -      ReadWritableMap(const ReadWritableMap<K,T1> &map, const T1 &v) {}
     2.8 +      ReadWritableMap(const ReadWritableMap<K,T1> &map, const ValueType &v) {}
     2.9  
    2.10        ReadWritableMap() {}
    2.11      };
    2.12 @@ -119,7 +119,7 @@
    2.13        /// @brief Constructor that copies all keys from the other map and
    2.14        /// assigns to them a default value (optional)
    2.15        template<typename T1>
    2.16 -      DereferableMap(const DereferableMap<K,T1> &map, const T1 &v) {}
    2.17 +      DereferableMap(const DereferableMap<K,T1> &map, const ValueType &v) {}
    2.18  
    2.19        DereferableMap() {}
    2.20      };
     3.1 --- a/src/work/klao/map_test.cc	Sat Apr 03 18:42:39 2004 +0000
     3.2 +++ b/src/work/klao/map_test.cc	Sat Apr 03 20:33:05 2004 +0000
     3.3 @@ -1,14 +1,23 @@
     3.4  #include <iostream>
     3.5  
     3.6 -#include <skeletons/maps.h>
     3.7 +#include <maps.h>
     3.8  
     3.9  using namespace std;
    3.10 -using namespace hugo::skeleton;
    3.11 +using namespace hugo;
    3.12  
    3.13  int main()
    3.14  {
    3.15 -  ReadableMap<int, char> a;
    3.16 +  ConstMap<int, char> a('#');
    3.17  
    3.18 -  cout << sizeof a << endl;
    3.19 +  cout << "sizeof ConstMap<int,char> = " << sizeof a << endl;
    3.20 +  cout << "a[5] = " << a[5] << endl;
    3.21  
    3.22 +  StdMap<int, char> b('$');
    3.23 +  cout << "sizeof ConstMap<int,char> = " << sizeof b << endl;
    3.24 +  cout << "sizeof std::map<int,char> = "
    3.25 +       << sizeof(std::map<int,char>) << endl;
    3.26 +  cout << "b[5] = " << b[5] << endl;
    3.27 +
    3.28 +  b[5]='l';
    3.29 +  cout << "b[5] = " << b[5] << endl;
    3.30  }