[Lemon-commits] [lemon_svn] klao: r402 - in hugo/trunk/src: include include/skeletons work/klao
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:39:15 CET 2006
Author: klao
Date: Sat Apr 3 22:33:05 2004
New Revision: 402
Added:
hugo/trunk/src/include/maps.h
Modified:
hugo/trunk/src/include/skeletons/maps.h
hugo/trunk/src/work/klao/map_test.cc
Log:
Miscellaneous maps, early version
skeletons/maps.h: minor bug
Added: hugo/trunk/src/include/maps.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/include/maps.h Sat Apr 3 22:33:05 2004
@@ -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 <map>
+
+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<typename K, typename T>
+ 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<typename K, typename T>
+ 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<typename T1>
+ struct rebind {
+ typedef ConstMap<K,T1> other;
+ };
+
+ template<typename T1>
+ ConstMap(const ConstMap<K,T1> &, 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 <typename Key, typename T, typename Compare = std::less<Key> >
+ class StdMap : public std::map<Key,T,Compare> {
+ typedef std::map<Key,T,Compare> 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<typename T1, typename Comp1>
+ StdMap(const StdMap<Key,T1,Comp1> &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<typename T1>
+ struct rebind {
+ typedef StdMap<Key,T1,Compare> other;
+ };
+ };
+
+}
+#endif // HUGO_MAPS_H
Modified: hugo/trunk/src/include/skeletons/maps.h
==============================================================================
--- hugo/trunk/src/include/skeletons/maps.h (original)
+++ hugo/trunk/src/include/skeletons/maps.h Sat Apr 3 22:33:05 2004
@@ -78,7 +78,7 @@
/// @brief Constructor that copies all keys from the other map and
/// assigns to them a default value (optional)
template<typename T1>
- ReadWritableMap(const ReadWritableMap<K,T1> &map, const T1 &v) {}
+ ReadWritableMap(const ReadWritableMap<K,T1> &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<typename T1>
- DereferableMap(const DereferableMap<K,T1> &map, const T1 &v) {}
+ DereferableMap(const DereferableMap<K,T1> &map, const ValueType &v) {}
DereferableMap() {}
};
Modified: hugo/trunk/src/work/klao/map_test.cc
==============================================================================
--- hugo/trunk/src/work/klao/map_test.cc (original)
+++ hugo/trunk/src/work/klao/map_test.cc Sat Apr 3 22:33:05 2004
@@ -1,14 +1,23 @@
#include <iostream>
-#include <skeletons/maps.h>
+#include <maps.h>
using namespace std;
-using namespace hugo::skeleton;
+using namespace hugo;
int main()
{
- ReadableMap<int, char> a;
+ ConstMap<int, char> a('#');
- cout << sizeof a << endl;
+ cout << "sizeof ConstMap<int,char> = " << sizeof a << endl;
+ cout << "a[5] = " << a[5] << endl;
+ StdMap<int, char> b('$');
+ cout << "sizeof ConstMap<int,char> = " << sizeof b << endl;
+ cout << "sizeof std::map<int,char> = "
+ << sizeof(std::map<int,char>) << endl;
+ cout << "b[5] = " << b[5] << endl;
+
+ b[5]='l';
+ cout << "b[5] = " << b[5] << endl;
}
More information about the Lemon-commits
mailing list