00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038 #ifndef BLOCXX_MAP_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_MAP_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/COWReference.hpp"
00042 #include <map>
00043 #include <functional>
00044
00045
00046 namespace BLOCXX_NAMESPACE
00047 {
00048
00049
00050
00051 template<class Key, class T, class Compare > class Map;
00052
00053 template<class Key, class T, class Compare>
00054 inline bool operator==(const Map<Key, T, Compare>& x,
00055 const Map<Key, T, Compare>& y);
00056
00057 template<class Key, class T, class Compare>
00058 inline bool operator<(const Map<Key, T, Compare>& x,
00059 const Map<Key, T, Compare>& y);
00060
00061
00062 template<class Key, class T, class Compare = std::less<Key> > class Map
00063 {
00064 typedef std::map<Key, T, Compare > M;
00065 COWReference<M> m_impl;
00066 public:
00067 typedef typename M::key_type key_type;
00068 typedef typename M::mapped_type mapped_type;
00069 typedef typename M::value_type value_type;
00070 typedef typename M::key_compare key_compare;
00071 typedef typename M::value_compare value_compare;
00072 typedef typename M::pointer pointer;
00073 typedef typename M::const_pointer const_pointer;
00074 typedef typename M::reference reference;
00075 typedef typename M::const_reference const_reference;
00076 typedef typename M::iterator iterator;
00077 typedef typename M::const_iterator const_iterator;
00078 typedef typename M::reverse_iterator reverse_iterator;
00079 typedef typename M::const_reverse_iterator const_reverse_iterator;
00080 typedef typename M::size_type size_type;
00081 typedef typename M::difference_type difference_type;
00082 Map() : m_impl(new M) { }
00083 explicit Map(M* toWrap) : m_impl(toWrap)
00084 { }
00085 explicit Map(const Compare& comp)
00086 : m_impl(new M(comp)) { }
00087 template <class InputIterator>
00088 Map(InputIterator first, InputIterator last) :
00089 m_impl(new M(first, last))
00090 {
00091 }
00092 template <class InputIterator>
00093 Map(InputIterator first, InputIterator last, const Compare& comp) :
00094 m_impl(new M(first, last, comp))
00095 {
00096 }
00097 M* getImpl()
00098 {
00099 return &*m_impl;
00100 }
00101 key_compare key_comp() const
00102 {
00103 return m_impl->key_comp();
00104 }
00105 value_compare value_comp() const
00106 {
00107 return m_impl->value_comp();
00108 }
00109 iterator begin()
00110 {
00111 return m_impl->begin();
00112 }
00113 const_iterator begin() const
00114 {
00115 return m_impl->begin();
00116 }
00117 iterator end()
00118 {
00119 return m_impl->end();
00120 }
00121 const_iterator end() const
00122 {
00123 return m_impl->end();
00124 }
00125 reverse_iterator rbegin()
00126 {
00127 return m_impl->rbegin();
00128 }
00129 const_reverse_iterator rbegin() const
00130 {
00131 return m_impl->rbegin();
00132 }
00133 reverse_iterator rend()
00134 {
00135 return m_impl->rend();
00136 }
00137 const_reverse_iterator rend() const
00138 {
00139 return m_impl->rend();
00140 }
00141 bool empty() const
00142 {
00143 return m_impl->empty();
00144 }
00145 size_type size() const
00146 {
00147 return m_impl->size();
00148 }
00149 size_type max_size() const
00150 {
00151 return m_impl->max_size();
00152 }
00153 T& operator[](const key_type& k)
00154 {
00155 return m_impl->operator[](k);
00156 }
00157 void swap(Map<Key, T, Compare>& x)
00158 {
00159 m_impl.swap(x.m_impl);
00160 }
00161 std::pair<iterator, bool> insert(const value_type& x)
00162 {
00163 return m_impl->insert(x);
00164 }
00165 iterator insert(iterator position, const value_type& x)
00166 {
00167 return m_impl->insert(position, x);
00168 }
00169 template <class InputIterator>
00170 void insert(InputIterator first, InputIterator last)
00171 {
00172 m_impl->insert(first, last);
00173 }
00174 void erase(iterator position)
00175 {
00176 m_impl->erase(position);
00177 }
00178 size_type erase(const key_type& x)
00179 {
00180 return m_impl->erase(x);
00181 }
00182 void erase(iterator first, iterator last)
00183 {
00184 m_impl->erase(first, last);
00185 }
00186 void clear()
00187 {
00188 m_impl->clear();
00189 }
00190 iterator find(const key_type& x)
00191 {
00192 return m_impl->find(x);
00193 }
00194 const_iterator find(const key_type& x) const
00195 {
00196 return m_impl->find(x);
00197 }
00198 size_type count(const key_type& x) const
00199 {
00200 return m_impl->count(x);
00201 }
00202 iterator lower_bound(const key_type& x)
00203 {
00204 return m_impl->lower_bound(x);
00205 }
00206 const_iterator lower_bound(const key_type& x) const
00207 {
00208 return m_impl->lower_bound(x);
00209 }
00210 iterator upper_bound(const key_type& x)
00211 {
00212 return m_impl->upper_bound(x);
00213 }
00214 const_iterator upper_bound(const key_type& x) const
00215 {
00216 return m_impl->upper_bound(x);
00217 }
00218 std::pair<iterator, iterator> equal_range(const key_type& x)
00219 {
00220 return m_impl->equal_range(x);
00221 }
00222 std::pair<const_iterator, const_iterator>
00223 equal_range(const key_type& x) const
00224 {
00225 return m_impl->equal_range(x);
00226 }
00227 friend bool operator== <>(const Map<Key, T, Compare>& x,
00228 const Map<Key, T, Compare>& y);
00229 friend bool operator< <>(const Map<Key, T, Compare>& x,
00230 const Map<Key, T, Compare>& y);
00231 };
00232 template <class Key, class T, class Compare>
00233 std::map<Key, T, Compare>* COWReferenceClone(std::map<Key, T, Compare>* obj)
00234 {
00235 return new std::map<Key, T, Compare>(*obj);
00236 }
00237 template<class Key, class T, class Compare>
00238 inline bool operator==(const Map<Key, T, Compare>& x,
00239 const Map<Key, T, Compare>& y)
00240 {
00241 return *x.m_impl == *y.m_impl;
00242 }
00243 template<class Key, class T, class Compare>
00244 inline bool operator<(const Map<Key, T, Compare>& x,
00245 const Map<Key, T, Compare>& y)
00246 {
00247 return *x.m_impl < *y.m_impl;
00248 }
00249 template <class Key, class T, class Compare>
00250 inline void swap(Map<Key, T, Compare>& x,
00251 Map<Key, T, Compare>& y)
00252 {
00253 x.swap(y);
00254 }
00255
00256 }
00257
00258 #endif