# HG changeset patch # User klao # Date 1081024385 0 # Node ID d3c4d99860a9bec1195deeace01550314964d200 # Parent 0bc5f7f66bfa3d95b6e8c7213aa3a96dfb7972fb Miscellaneous maps, early version skeletons/maps.h: minor bug diff -r 0bc5f7f66bfa -r d3c4d99860a9 src/include/maps.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/include/maps.h Sat Apr 03 20:33:05 2004 +0000 @@ -0,0 +1,122 @@ +// -*- C++ -*- // +#ifndef HUGO_MAPS_H +#define HUGO_MAPS_H + +///\file +///\brief Miscellaneous property maps +/// +///\todo This file has the same name as the concept file in skeletons, +/// and this is not easily detectable in docs... + +#include + +namespace hugo { + + /// Null map. (aka DoNothingMap) + + /// If you have to provide a map only for its type definitions, + /// or if you have to provide a writable map, but will not use the + /// data written to it... + template + class NullMap + { + public: + typedef K KeyType; + typedef T ValueType; + + T operator[](const K&) const { return T(); } + void set(const K&, const T&) {} + }; + + + /// Constant map. + + /// This is a readable map which assignes a specified value to each key. + /// In other aspects it is equivalent to the \ref NullMap + template + class ConstMap + { + T v; + public: + typedef K KeyType; + typedef T ValueType; + + ConstMap() {} + ConstMap(const T &_v) : v(_v) {} + + T operator[](const K&) const { return v; } + void set(const K&, const T&) {} + + template + struct rebind { + typedef ConstMap other; + }; + + template + ConstMap(const ConstMap &, const T &_v) : v(_v) {} + }; + + + + /// \c std::map wrapper + + /// This is essentially a wrapper for \c std::map. With addition that + /// you can specify a default value different from \c ValueType() . + /// + /// \todo Provide allocator parameter... + template > + class StdMap : public std::map { + typedef std::map parent; + T v; + typedef typename parent::value_type PairType; + + public: + typedef Key KeyType; + typedef T ValueType; + typedef T& ReferenceType; + typedef const T& ConstReferenceType; + + + StdMap() {} + /// Constructor with specified default value + StdMap(const T& _v) : v(_v) {} + + /// \brief Constructs the map from an appropriate std::map. + /// + /// \warning Inefficient: copies the content of \c m ! + StdMap(const parent &m) : parent(m) {} + /// \brief Constructs the map from an appropriate std::map, and explicitly + /// specifies a default value. + /// + /// \warning Inefficient: copies the content of \c m ! + StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} + + template + StdMap(const StdMap &m, const T &_v) { FIXME; } + + ReferenceType operator[](const Key &k) { + return ( *( (insert(PairType(k,v))).first ) ).second; + } + ConstReferenceType operator[](const Key &k) const { + typename parent::iterator i = lower_bound(__k); + if (i == end() || key_comp()(k, (*i).first)) + return v; + return (*i).second; + } + void set(const Key &k, const T &t) { parent::operator[](k)=t; } + + /// Changes the default value of the map. + /// \return Returns the previous default value. + /// + /// \warning The value of some keys (which has alredy been queried, but + /// the value has been unchanged from the default) may change! + T setDefault(const T &_v) { T old=v; v=_v; return old; } + + template + struct rebind { + typedef StdMap other; + }; + }; + +} +#endif // HUGO_MAPS_H diff -r 0bc5f7f66bfa -r d3c4d99860a9 src/include/skeletons/maps.h --- a/src/include/skeletons/maps.h Sat Apr 03 18:42:39 2004 +0000 +++ b/src/include/skeletons/maps.h Sat Apr 03 20:33:05 2004 +0000 @@ -78,7 +78,7 @@ /// @brief Constructor that copies all keys from the other map and /// assigns to them a default value (optional) template - ReadWritableMap(const ReadWritableMap &map, const T1 &v) {} + ReadWritableMap(const ReadWritableMap &map, const ValueType &v) {} ReadWritableMap() {} }; @@ -119,7 +119,7 @@ /// @brief Constructor that copies all keys from the other map and /// assigns to them a default value (optional) template - DereferableMap(const DereferableMap &map, const T1 &v) {} + DereferableMap(const DereferableMap &map, const ValueType &v) {} DereferableMap() {} }; diff -r 0bc5f7f66bfa -r d3c4d99860a9 src/work/klao/map_test.cc --- a/src/work/klao/map_test.cc Sat Apr 03 18:42:39 2004 +0000 +++ b/src/work/klao/map_test.cc Sat Apr 03 20:33:05 2004 +0000 @@ -1,14 +1,23 @@ #include -#include +#include using namespace std; -using namespace hugo::skeleton; +using namespace hugo; int main() { - ReadableMap a; + ConstMap a('#'); - cout << sizeof a << endl; + cout << "sizeof ConstMap = " << sizeof a << endl; + cout << "a[5] = " << a[5] << endl; + StdMap b('$'); + cout << "sizeof ConstMap = " << sizeof b << endl; + cout << "sizeof std::map = " + << sizeof(std::map) << endl; + cout << "b[5] = " << b[5] << endl; + + b[5]='l'; + cout << "b[5] = " << b[5] << endl; }