00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(AFX_COMPACTMAP_H__BC32B669_4C12_4E9A_949B_833EFEBE2493__INCLUDED_)
00012 #define AFX_COMPACTMAP_H__BC32B669_4C12_4E9A_949B_833EFEBE2493__INCLUDED_
00013
00014 #if _MSC_VER > 1000
00015 #pragma once
00016 #endif // _MSC_VER > 1000
00017
00018
00019 #include "CompactColl.h"
00020 #include <map>
00021
00022
00023 template <class K, class V, class P,class POL, class A>
00024 struct CMapCT
00025 {
00026 typedef K key_type;
00027 typedef V mapped_type;
00028 typedef std::pair<const K,V> value_type;
00029 typedef P key_compare;
00030 struct value_compare
00031 : public key_compare
00032 {
00033 value_compare()
00034 { }
00035 value_compare(const key_compare& rhs)
00036 : key_compare(rhs)
00037 { }
00038 bool operator()(const value_type& lhs,const value_type& rhs) const
00039 {
00040 return key_compare::operator()(lhs.first,rhs.first);
00041 }
00042 bool operator()(const key_type& lhs,const key_type& rhs) const
00043 {
00044 return key_compare::operator()(lhs,rhs);
00045 }
00046 bool operator()(const key_type& lhs,const value_type& rhs) const
00047 {
00048 return key_compare::operator()(lhs,rhs.first);
00049 }
00050 bool operator()(const value_type& lhs,const key_type& rhs) const
00051 {
00052 return key_compare::operator()(lhs.first,rhs);
00053 }
00054 };
00055
00056 class AssignableValueType
00057 : public value_type
00058 {
00059 public:
00060 AssignableValueType(const key_type& k, const mapped_type& v)
00061 : value_type(k,v)
00062 {
00063 }
00064 AssignableValueType(const value_type& rhs)
00065 : value_type(rhs)
00066 {
00067 }
00068 AssignableValueType& operator=(const value_type& rhs)
00069 {
00070 const_cast<key_type&>(first) = rhs.first;
00071 second = rhs.second;
00072 return *this;
00073 }
00074 AssignableValueType& operator=(const AssignableValueType& rhs)
00075 {
00076 const_cast<key_type&>(first) = rhs.first;
00077 second = rhs.second;
00078 return *this;
00079 }
00080 };
00081
00082 typedef A allocator_type;
00083 typedef typename allocator_type::size_type size_type;
00084 typedef POL PolicyTagType;
00085 #if defined(_MSC_VER) && _MSC_VER <= 1200
00086 typedef std::allocator<AssignableValueType> AssignableAllocatorType;
00087 #else
00088 typedef typename allocator_type::template rebind<AssignableValueType>::other AssignableAllocatorType;
00089 #endif
00090 typedef std::map<K,V,P,A> DynSetType;
00091
00092 value_compare kc;
00093 allocator_type al;
00094 CMapCT(const key_compare& pred,
00095 const allocator_type& alloc)
00096 : kc(pred), al(alloc)
00097 { }
00098 CMapCT()
00099 { }
00100
00101 void swap(CMapCT& rhs)
00102 {
00103 std::swap(kc,rhs.kc);
00104 std::swap(al,rhs.al);
00105 }
00106 bool less(const key_type& lhs, const key_type& rhs) const
00107 {
00108 return kc(lhs,rhs);
00109 }
00110 void assign(value_type& lhs, const value_type& rhs) const
00111 {
00112
00113
00114 const_cast<key_type&>(lhs.first) = rhs.first;
00115 lhs.second = rhs.second;
00116 }
00117 const key_type& extractKey(const value_type& val) const
00118 {
00119 return val.first;
00120 }
00121 const key_type& extractKey(value_type& val) const
00122 {
00123 return val.first;
00124 }
00125 const key_compare& key_comp() const
00126 {
00127 return kc;
00128 }
00129 const value_compare& value_comp() const
00130 {
00131 return kc;
00132 }
00133 const allocator_type& get_allocator() const
00134 {
00135 return al;
00136 }
00137 };
00138
00139 #ifdef _MSC_VER
00140 namespace std
00141 {
00142 template<class T>
00143 struct MSCStdSwapInjector
00144 {
00145 friend void swap(T& lhs,T& rhs)
00146 {
00147 lhs.swap(rhs);
00148 }
00149 };
00150 }
00151 #endif
00152
00153 template<class K,class V,class P = std::less<K>,class POLICY = MaximizeSpeedPolicy,class A = std::allocator<std::pair<const K,V> > >
00154 class CompactMap
00155 : public CompactColl<CMapCT<K,V,P,POLICY,A> >
00156 #ifdef _MSC_VER
00157 , public std::MSCStdSwapInjector<CompactMap>
00158 #endif
00159 {
00160 typedef CMapCT<K,V,P,POLICY,A> CTType;
00161 typedef CompactColl<CTType> ImplType;
00162 public:
00163 typedef typename ImplType::key_compare key_compare;
00164 typedef typename ImplType::allocator_type allocator_type;
00165 typedef typename ImplType::key_type key_type;
00166 typedef V mapped_type;
00167
00168 explicit CompactMap(const key_compare& pred = key_compare(),
00169 const allocator_type& alloc = allocator_type())
00170 : ImplType(CMapCT<K,V,P,POLICY,A>(pred,alloc))
00171 {
00172 }
00173 mapped_type& operator[](const key_type& key)
00174 {
00175 return insert(value_type(key,mapped_type())).first->second;
00176 }
00177 };
00178
00179 template<class K,class V,class P,class POLICY,class A>
00180 inline bool operator==(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00181 {
00182 return (lhs.size() == rhs.size()
00183 && std::equal(lhs.begin(),lhs.end(),rhs.begin()));
00184 }
00185
00186 template<class K,class V,class P,class POLICY,class A>
00187 inline bool operator!=(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00188 {
00189 return ! (lhs == rhs);
00190 }
00191
00192 template<class K,class V,class P,class POLICY,class A>
00193 inline bool operator<(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00194 {
00195 return std::lexicographical_compare(lhs.begin(),lhs.end(),rhs.begin(),rhs.end());
00196 }
00197
00198 template<class K,class V,class P,class POLICY,class A>
00199 inline bool operator>(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00200 {
00201 return rhs < lhs;
00202 }
00203
00204 template<class K,class V,class P,class POLICY,class A>
00205 inline bool operator<=(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00206 {
00207 return ! (rhs < lhs);
00208 }
00209
00210 template<class K,class V,class P,class POLICY,class A>
00211 inline bool operator>=(const CompactMap<K,V,P,POLICY,A>& lhs, const CompactMap<K,V,P,POLICY,A>& rhs)
00212 {
00213 return ! (lhs < rhs);
00214 }
00215
00216 #ifndef _MSC_VER
00217 namespace std
00218 {
00219
00220 template<class K,class V,class P,class POLICY,class A>
00221 inline void swap(::CompactMap<K,V,P,POLICY,A>& lhs, ::CompactMap<K,V,P,POLICY,A>& rhs)
00222 {
00223 lhs.swap(rhs);
00224 }
00225 }
00226 #endif
00227
00228 #endif // !defined(AFX_COMPACTMAP_H__BC32B669_4C12_4E9A_949B_833EFEBE2493__INCLUDED_)
00229 ICY,class A>
00230 inline void swap(::CompactMap<K,V,P,POLICY,A>& lhs, ::CompactMap<K,V,P,POLICY,A>& rhs)
00231 {
00232 lhs.swap(rhs);
00233 }
00234 }
00235 #endif
00236
00237 #endif // !defined(AFX_COMPACTMAP_H__BC32B669_4C12_4E9A_949B_833EFEBE2493__INCLUDED_)