00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00054 #ifndef __itk_hash_map_h
00055 #define __itk_hash_map_h
00056
00057 #if defined(_MSC_VER)
00058 #pragma warning ( disable : 4786 )
00059 #endif
00060
00061 #if (defined(__GNUC__) && (((__GNUC__==3) && (__GNUC_MINOR__>=1) || (__GNUC__>3) ) || ( (__GNUC__==4) && defined(__INTEL_COMPILER) ) )) || (defined(__IBMCPP__) && __IBMCPP__ >= 600)
00062
00063
00064
00065
00066
00067
00068
00069 #include <ext/hash_map>
00070
00071
00072 namespace itk
00073 {
00074 using __gnu_cxx::hash;
00075 using __gnu_cxx::hash_map;
00076 using __gnu_cxx::hash_multimap;
00077 }
00078
00079 #else
00080
00081 #include "itk_hashtable.h"
00082 #include "itk_alloc.h"
00083
00084
00085
00086 #if defined(__MWERKS__)
00087 #include "vcl_functional.h"
00088 #endif
00089
00090 #include "vcl_compiler.h"
00091
00092
00093 namespace itk
00094 {
00095
00096 # define VCL_IMPORT_CONTAINER_TYPEDEFS(super) \
00097 typedef typename super::value_type value_type; \
00098 typedef typename super::reference reference; \
00099 typedef typename super::size_type size_type; \
00100 typedef typename super::const_reference const_reference; \
00101 typedef typename super::difference_type difference_type;
00102
00103 # define VCL_IMPORT_ITERATORS(super) \
00104 typedef typename super::iterator iterator; \
00105 typedef typename super::const_iterator const_iterator;
00106
00107 # define VCL_IMPORT_REVERSE_ITERATORS(super) \
00108 typedef typename super::const_reverse_iterator const_reverse_iterator; \
00109 typedef typename super::reverse_iterator reverse_iterator;
00110
00111 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00112 class hash_map;
00113 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00114 class hash_multimap;
00115
00116 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00117 bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>&,
00118 const hash_map<Key, T, HashFcn, EqualKey, Alloc>&);
00119 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00120 bool operator==(const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>&,
00121 const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>&);
00122
00127 template <class Key, class T,
00128 VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00129 VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00130 VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00131 class hash_map
00132 {
00133 private:
00134 typedef std::select1st<std::pair<const Key, T> > sel1st;
00135 typedef hashtable<std::pair<const Key, T>, Key, HashFcn, sel1st, EqualKey, Alloc> ht;
00136 typedef hash_map<Key, T, HashFcn, EqualKey, Alloc> self;
00137 public:
00138 VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00139 VCL_IMPORT_ITERATORS(ht)
00140 typedef typename ht::key_type key_type;
00141 typedef typename ht::hasher hasher;
00142 typedef typename ht::key_equal key_equal;
00143 typedef T data_type;
00144 typedef typename ht::pointer pointer;
00145 typedef typename ht::const_pointer const_pointer;
00146 private:
00147 ht rep;
00148
00149 public:
00150 hasher hash_funct() const { return rep.hash_funct(); }
00151 key_equal key_eq() const { return rep.key_eq(); }
00152
00153 public:
00154 hash_map() : rep(100, hasher(), key_equal()) {}
00155 hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
00156 hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00157 hash_map(size_type n, const hasher& hf, const key_equal& eql)
00158 : rep(n, hf, eql) {}
00159
00160 hash_map(const value_type* f, const value_type* l)
00161 : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00162 hash_map(const value_type* f, const value_type* l, size_type n)
00163 : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00164 hash_map(const value_type* f, const value_type* l, size_type n,
00165 const hasher& hf)
00166 : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00167 hash_map(const value_type* f, const value_type* l, size_type n,
00168 const hasher& hf, const key_equal& eql)
00169 : rep(n, hf, eql) { rep.insert_unique(f, l); }
00170
00171 hash_map(const_iterator f, const_iterator l)
00172 : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00173 hash_map(const_iterator f, const_iterator l, size_type n)
00174 : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00175 hash_map(const_iterator f, const_iterator l, size_type n,
00176 const hasher& hf)
00177 : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00178 hash_map(const_iterator f, const_iterator l, size_type n,
00179 const hasher& hf, const key_equal& eql)
00180 : rep(n, hf, eql) { rep.insert_unique(f, l); }
00181
00182 public:
00183 size_type size() const { return rep.size(); }
00184 size_type max_size() const { return rep.max_size(); }
00185 bool empty() const { return rep.empty(); }
00186 void swap(self& hs) { rep.swap(hs.rep); }
00187
00188 friend bool operator==ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(self)(const self &, const self &);
00189
00190 iterator begin() { return rep.begin(); }
00191 iterator end() { return rep.end(); }
00192 const_iterator begin() const { return rep.begin(); }
00193 const_iterator end() const { return rep.end(); }
00194
00195 public:
00196 std::pair<iterator, bool> insert(const value_type& obj)
00197 { return rep.insert_unique(obj); }
00198 void insert(const value_type* f, const value_type* l) { rep.insert_unique(f,l); }
00199 void insert(const_iterator f, const_iterator l) { rep.insert_unique(f, l); }
00200 std::pair<iterator, bool> insert_noresize(const value_type& obj)
00201 { return rep.insert_unique_noresize(obj); }
00202
00203 iterator find(const key_type& key) { return rep.find(key); }
00204 const_iterator find(const key_type& key) const { return rep.find(key); }
00205
00206 T& operator[](const key_type& key)
00207 {
00208 value_type val(key, T());
00209 return rep.find_or_insert(val).second;
00210 }
00211
00212 size_type count(const key_type& key) const { return rep.count(key); }
00213
00214 std::pair<iterator, iterator> equal_range(const key_type& key)
00215 { return rep.equal_range(key); }
00216 std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00217 { return rep.equal_range(key); }
00218
00219 size_type erase(const key_type& key) {return rep.erase(key); }
00220 void erase(iterator it) { rep.erase(it); }
00221 void erase(iterator f, iterator l) { rep.erase(f, l); }
00222 void clear() { rep.clear(); }
00223
00224 public:
00225 void resize(size_type hint) { rep.resize(hint); }
00226 size_type bucket_count() const { return rep.bucket_count(); }
00227 size_type max_bucket_count() const { return rep.max_bucket_count(); }
00228 size_type elems_in_bucket(size_type n) const
00229 { return rep.elems_in_bucket(n); }
00230 };
00231
00232
00233 template <class Key, class T, VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00234 VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00235 VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00236 class hash_multimap
00237 {
00238 private:
00239 typedef hashtable<std::pair<const Key, T>, Key, HashFcn,
00240 std::select1st<std::pair<const Key, T> >, EqualKey, Alloc> ht;
00241 typedef hash_multimap<Key, T, HashFcn, EqualKey, Alloc> self;
00242 public:
00243 VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00244 VCL_IMPORT_ITERATORS(ht)
00245 typedef typename ht::key_type key_type;
00246 typedef typename ht::hasher hasher;
00247 typedef typename ht::key_equal key_equal;
00248 typedef T data_type;
00249 typedef typename ht::pointer pointer;
00250 typedef typename ht::const_pointer const_pointer;
00251
00252 hasher hash_funct() const { return rep.hash_funct(); }
00253 key_equal key_eq() const { return rep.key_eq(); }
00254 private:
00255 ht rep;
00256
00257 public:
00258 hash_multimap() : rep(100, hasher(), key_equal()) {}
00259 hash_multimap(size_type n) : rep(n, hasher(), key_equal()) {}
00260 hash_multimap(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00261 hash_multimap(size_type n, const hasher& hf, const key_equal& eql)
00262 : rep(n, hf, eql) {}
00263
00264 hash_multimap(const value_type* f, const value_type* l)
00265 : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00266 hash_multimap(const value_type* f, const value_type* l, size_type n)
00267 : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00268 hash_multimap(const value_type* f, const value_type* l, size_type n,
00269 const hasher& hf)
00270 : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00271 hash_multimap(const value_type* f, const value_type* l, size_type n,
00272 const hasher& hf, const key_equal& eql)
00273 : rep(n, hf, eql) { rep.insert_equal(f, l); }
00274
00275 hash_multimap(const_iterator f, const_iterator l)
00276 : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00277 hash_multimap(const_iterator f, const_iterator l, size_type n)
00278 : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00279 hash_multimap(const_iterator f, const_iterator l, size_type n,
00280 const hasher& hf)
00281 : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00282 hash_multimap(const_iterator f, const_iterator l, size_type n,
00283 const hasher& hf, const key_equal& eql)
00284 : rep(n, hf, eql) { rep.insert_equal(f, l); }
00285
00286 public:
00287 size_type size() const { return rep.size(); }
00288 size_type max_size() const { return rep.max_size(); }
00289 bool empty() const { return rep.empty(); }
00290 void swap(self& hs) { rep.swap(hs.rep); }
00291
00292 friend bool operator==ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(self)(const self &, const self &);
00293
00294 iterator begin() { return rep.begin(); }
00295 iterator end() { return rep.end(); }
00296 const_iterator begin() const { return rep.begin(); }
00297 const_iterator end() const { return rep.end(); }
00298
00299 public:
00300 iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
00301 void insert(const value_type* f, const value_type* l) { rep.insert_equal(f,l); }
00302 void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
00303 iterator insert_noresize(const value_type& obj)
00304 { return rep.insert_equal_noresize(obj); }
00305
00306 iterator find(const key_type& key) { return rep.find(key); }
00307 const_iterator find(const key_type& key) const { return rep.find(key); }
00308
00309 size_type count(const key_type& key) const { return rep.count(key); }
00310
00311 std::pair<iterator, iterator> equal_range(const key_type& key)
00312 { return rep.equal_range(key); }
00313 std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00314 { return rep.equal_range(key); }
00315
00316 size_type erase(const key_type& key) {return rep.erase(key); }
00317 void erase(iterator it) { rep.erase(it); }
00318 void erase(iterator f, iterator l) { rep.erase(f, l); }
00319 void clear() { rep.clear(); }
00320
00321 public:
00322 void resize(size_type hint) { rep.resize(hint); }
00323 size_type bucket_count() const { return rep.bucket_count(); }
00324 size_type max_bucket_count() const { return rep.max_bucket_count(); }
00325 size_type elems_in_bucket(size_type n) const
00326 { return rep.elems_in_bucket(n); }
00327 };
00328
00331 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00332 bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00333 const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00334 {
00335 return hm1.rep == hm2.rep;
00336 }
00337
00340 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00341 bool operator==(const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00342 const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00343 {
00344 return hm1.rep == hm2.rep;
00345 }
00346
00347
00348 #define HASH_MAP_INSTANTIATE \
00349 extern "please include emulation/hash_map.txx instead"
00350
00351 }
00352
00353 #endif
00354
00355 #endif // itk_emulation_hash_map_h
00356