maps.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_MAPS_H 00018 #define LEMON_MAPS_H 00019 00025 00026 #include <map> 00027 00028 namespace lemon { 00029 00031 00034 template<typename K, typename T> 00035 class MapBase 00036 { 00037 public: 00039 typedef K KeyType; 00041 typedef T ValueType; 00042 }; 00043 00045 00049 template<typename K, typename T> 00050 class NullMap : public MapBase<K,T> 00051 { 00052 public: 00053 00055 T operator[](const K&) const { return T(); } 00057 void set(const K&, const T&) {} 00058 }; 00059 00060 00062 00066 template<typename K, typename T> 00067 class ConstMap : public MapBase<K,T> 00068 { 00069 T v; 00070 public: 00071 00073 00076 ConstMap() {} 00078 00081 ConstMap(const T &_v) : v(_v) {} 00082 00083 T operator[](const K&) const { return v; } 00084 void set(const K&, const T&) {} 00085 00086 template<typename T1> 00087 struct rebind { 00088 typedef ConstMap<K,T1> other; 00089 }; 00090 00091 template<typename T1> 00092 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} 00093 }; 00094 00095 //to document later 00096 template<typename T, T v> 00097 struct Const { }; 00098 //to document later 00099 template<typename K, typename V, V v> 00100 class ConstMap<K, Const<V, v> > : public MapBase<K, V> 00101 { 00102 public: 00103 ConstMap() { } 00104 V operator[](const K&) const { return v; } 00105 void set(const K&, const V&) { } 00106 }; 00107 //to document later 00108 typedef Const<bool, true> True; 00109 typedef Const<bool, false> False; 00110 00112 00117 template <typename Key, typename T, typename Compare = std::less<Key> > 00118 class StdMap : public std::map<Key,T,Compare> { 00119 typedef std::map<Key,T,Compare> parent; 00120 T v; 00121 typedef typename parent::value_type PairType; 00122 00123 public: 00124 typedef Key KeyType; 00125 typedef T ValueType; 00126 typedef T& ReferenceType; 00127 typedef const T& ConstReferenceType; 00128 00129 00130 StdMap() : v() {} 00132 StdMap(const T& _v) : v(_v) {} 00133 00137 StdMap(const parent &m) : parent(m) {} 00142 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} 00143 00144 template<typename T1, typename Comp1> 00145 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 00146 //FIXME; 00147 } 00148 00149 ReferenceType operator[](const Key &k) { 00150 return insert(PairType(k,v)).first -> second; 00151 } 00152 ConstReferenceType operator[](const Key &k) const { 00153 typename parent::iterator i = lower_bound(k); 00154 if (i == parent::end() || parent::key_comp()(k, (*i).first)) 00155 return v; 00156 return (*i).second; 00157 } 00158 void set(const Key &k, const T &t) { 00159 parent::operator[](k) = t; 00160 } 00161 00167 T setDefault(const T &_v) { T old=v; v=_v; return old; } 00168 00169 template<typename T1> 00170 struct rebind { 00171 typedef StdMap<Key,T1,Compare> other; 00172 }; 00173 }; 00174 00175 } 00176 #endif // LEMON_MAPS_H

Generated on Thu Sep 30 12:18:33 2004 for LEMON by doxygen 1.3.8