Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itk_hash_map.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itk_hash_map.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/04/28 16:02:41 $
00007   Version:   $Revision: 1.14 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 /*
00018  * Copyright (c) 1996
00019  * Silicon Graphics Computer Systems, Inc.
00020  *
00021  * Permission to use, copy, modify, distribute and sell this software
00022  * and its documentation for any purpose is hereby granted without fee,
00023  * provided that the above copyright notice appear in all copies and
00024  * that both that copyright notice and this permission notice appear
00025  * in supporting documentation.  Silicon Graphics makes no
00026  * representations about the suitability of this software for any
00027  * purpose.  It is provided "as is" without express or implied warranty.
00028  *
00029  *
00030  * Copyright (c) 1994
00031  * Hewlett-Packard Company
00032  *
00033  * Permission to use, copy, modify, distribute and sell this software
00034  * and its documentation for any purpose is hereby granted without fee,
00035  * provided that the above copyright notice appear in all copies and
00036  * that both that copyright notice and this permission notice appear
00037  * in supporting documentation.  Hewlett-Packard Company makes no
00038  * representations about the suitability of this software for any
00039  * purpose.  It is provided "as is" without express or implied warranty.
00040  *
00041  * Copyright (c) 1997
00042  * Moscow Center for SPARC Technology
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Moscow Center for SPARC Technology makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  *
00052  */
00053 
00054 #ifndef itk_emulation_hash_map_h
00055 #define itk_emulation_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)) && !defined(__INTEL_COMPILER)) || (defined(__IBMCPP__) && __IBMCPP__ >= 600)
00062 #include <ext/hash_map>
00063 
00064 namespace itk
00065 {
00066   using __gnu_cxx::hash;
00067   using __gnu_cxx::hash_map;
00068   using __gnu_cxx::hash_multimap;
00069 }
00070 
00071 #else
00072 
00073 #include "itk_hashtable.h"
00074 #include "itk_alloc.h"
00075 
00076 
00077 // The following is required for CodeWarrior
00078 #if defined(__MWERKS__)
00079 #include "vcl_functional.h"
00080 #endif
00081 
00082 #include "vcl_compiler.h"
00083 
00084 
00085 namespace itk
00086 {
00087 
00088 #  define VCL_IMPORT_CONTAINER_TYPEDEFS(super)                            \
00089     typedef typename super::value_type value_type;                               \
00090     typedef typename super::reference reference;                                 \
00091     typedef typename super::size_type size_type;                                 \
00092     typedef typename super::const_reference const_reference;                     \
00093     typedef typename super::difference_type difference_type;
00094 
00095 #  define VCL_IMPORT_ITERATORS(super)                                              \
00096     typedef typename super::iterator iterator;                                   \
00097     typedef typename super::const_iterator const_iterator; 
00098 
00099 #  define VCL_IMPORT_REVERSE_ITERATORS(super)                                      \
00100     typedef typename super::const_reverse_iterator  const_reverse_iterator;      \
00101     typedef typename super::reverse_iterator reverse_iterator;
00102 
00103 
00107 template <class Key, class T,
00108           VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00109           VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00110           VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00111 class hash_map
00112 {
00113 private:
00114   typedef std::select1st<std::pair<const Key, T> > sel1st;
00115   typedef hashtable<std::pair<const Key, T>, Key, HashFcn, sel1st, EqualKey, Alloc> ht;
00116   typedef hash_map<Key, T, HashFcn, EqualKey, Alloc> self;
00117 public:
00118   VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00119   VCL_IMPORT_ITERATORS(ht)
00120   typedef typename ht::key_type key_type;
00121   typedef typename ht::hasher hasher;
00122   typedef typename ht::key_equal key_equal;
00123   typedef T data_type;
00124   typedef typename ht::pointer pointer;
00125   typedef typename ht::const_pointer const_pointer;
00126 private:
00127   ht rep;
00128 
00129 public:
00130   hasher hash_funct() const { return rep.hash_funct(); }
00131   key_equal key_eq() const { return rep.key_eq(); }
00132 
00133 public:
00134   hash_map() : rep(100, hasher(), key_equal()) {}
00135   hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
00136   hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00137   hash_map(size_type n, const hasher& hf, const key_equal& eql)
00138     : rep(n, hf, eql) {}
00139 
00140   hash_map(const value_type* f, const value_type* l)
00141     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00142   hash_map(const value_type* f, const value_type* l, size_type n)
00143     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00144   hash_map(const value_type* f, const value_type* l, size_type n,
00145            const hasher& hf)
00146     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00147   hash_map(const value_type* f, const value_type* l, size_type n,
00148            const hasher& hf, const key_equal& eql)
00149     : rep(n, hf, eql) { rep.insert_unique(f, l); }
00150 
00151   hash_map(const_iterator f, const_iterator l)
00152     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00153   hash_map(const_iterator f, const_iterator l, size_type n)
00154     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00155   hash_map(const_iterator f, const_iterator l, size_type n,
00156            const hasher& hf)
00157     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00158   hash_map(const_iterator f, const_iterator l, size_type n,
00159            const hasher& hf, const key_equal& eql)
00160     : rep(n, hf, eql) { rep.insert_unique(f, l); }
00161 
00162 public:
00163   size_type size() const { return rep.size(); }
00164   size_type max_size() const { return rep.max_size(); }
00165   bool empty() const { return rep.empty(); }
00166   void swap(self& hs) { rep.swap(hs.rep); }
00167 #ifndef __BORLANDC__
00168   friend bool operator==VCL_NULL_TMPL_ARGS(const hash_map<Key,T,HashFcn,EqualKey,Alloc>&,
00169                          const hash_map<Key,T,HashFcn,EqualKey,Alloc>&);
00170 #endif
00171   iterator begin() { return rep.begin(); }
00172   iterator end() { return rep.end(); }
00173   const_iterator begin() const { return rep.begin(); }
00174   const_iterator end() const { return rep.end(); }
00175 
00176 public:
00177   std::pair<iterator, bool> insert(const value_type& obj)
00178     { return rep.insert_unique(obj); }
00179   void insert(const value_type* f, const value_type* l) { rep.insert_unique(f,l); }
00180   void insert(const_iterator f, const_iterator l) { rep.insert_unique(f, l); }
00181   std::pair<iterator, bool> insert_noresize(const value_type& obj)
00182     { return rep.insert_unique_noresize(obj); }    
00183 
00184   iterator find(const key_type& key) { return rep.find(key); }
00185   const_iterator find(const key_type& key) const { return rep.find(key); }
00186 
00187   T& operator[](const key_type& key)
00188   {
00189       value_type val(key, T());
00190       return rep.find_or_insert(val).second;
00191   }
00192 
00193   size_type count(const key_type& key) const { return rep.count(key); }
00194   
00195   std::pair<iterator, iterator> equal_range(const key_type& key)
00196     { return rep.equal_range(key); }
00197   std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00198     { return rep.equal_range(key); }
00199 
00200   size_type erase(const key_type& key) {return rep.erase(key); }
00201   void erase(iterator it) { rep.erase(it); }
00202   void erase(iterator f, iterator l) { rep.erase(f, l); }
00203   void clear() { rep.clear(); }
00204 
00205 public:
00206   void resize(size_type hint) { rep.resize(hint); }
00207   size_type bucket_count() const { return rep.bucket_count(); }
00208   size_type max_bucket_count() const { return rep.max_bucket_count(); }
00209   size_type elems_in_bucket(size_type n) const
00210     { return rep.elems_in_bucket(n); }
00211 };
00212 
00213 
00214 template <class Key, class T, VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00215           VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00216           VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00217 class hash_multimap
00218 {
00219 private:
00220   typedef hashtable<std::pair<const Key, T>, Key, HashFcn,
00221       std::select1st<std::pair<const Key, T> >, EqualKey, Alloc> ht;
00222   typedef hash_multimap<Key, T, HashFcn, EqualKey, Alloc> self;
00223 public:
00224   VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00225   VCL_IMPORT_ITERATORS(ht)
00226   typedef typename ht::key_type key_type;
00227   typedef typename ht::hasher hasher;
00228   typedef typename ht::key_equal key_equal;
00229   typedef T data_type;
00230   typedef typename ht::pointer pointer;
00231   typedef typename ht::const_pointer const_pointer;
00232 
00233   hasher hash_funct() const { return rep.hash_funct(); }
00234   key_equal key_eq() const { return rep.key_eq(); }
00235 private:
00236   ht rep;
00237 
00238 public:
00239   hash_multimap() : rep(100, hasher(), key_equal()) {}
00240   hash_multimap(size_type n) : rep(n, hasher(), key_equal()) {}
00241   hash_multimap(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00242   hash_multimap(size_type n, const hasher& hf, const key_equal& eql)
00243     : rep(n, hf, eql) {}
00244 
00245   hash_multimap(const value_type* f, const value_type* l)
00246     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00247   hash_multimap(const value_type* f, const value_type* l, size_type n)
00248     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00249   hash_multimap(const value_type* f, const value_type* l, size_type n,
00250            const hasher& hf)
00251     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00252   hash_multimap(const value_type* f, const value_type* l, size_type n,
00253            const hasher& hf, const key_equal& eql)
00254     : rep(n, hf, eql) { rep.insert_equal(f, l); }
00255 
00256   hash_multimap(const_iterator f, const_iterator l)
00257     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00258   hash_multimap(const_iterator f, const_iterator l, size_type n)
00259     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00260   hash_multimap(const_iterator f, const_iterator l, size_type n,
00261            const hasher& hf)
00262     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00263   hash_multimap(const_iterator f, const_iterator l, size_type n,
00264            const hasher& hf, const key_equal& eql)
00265     : rep(n, hf, eql) { rep.insert_equal(f, l); }
00266 
00267 public:
00268   size_type size() const { return rep.size(); }
00269   size_type max_size() const { return rep.max_size(); }
00270   bool empty() const { return rep.empty(); }
00271   void swap(self& hs) { rep.swap(hs.rep); }
00272   friend bool operator==VCL_NULL_TMPL_ARGS(const hash_multimap<Key,T,HashFcn,EqualKey,Alloc>&,
00273                          const hash_multimap<Key,T,HashFcn,EqualKey,Alloc>&);
00274 
00275   iterator begin() { return rep.begin(); }
00276   iterator end() { return rep.end(); }
00277   const_iterator begin() const { return rep.begin(); }
00278   const_iterator end() const { return rep.end(); }
00279 
00280 public:
00281   iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
00282   void insert(const value_type* f, const value_type* l) { rep.insert_equal(f,l); }
00283   void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
00284   iterator insert_noresize(const value_type& obj)
00285     { return rep.insert_equal_noresize(obj); }    
00286 
00287   iterator find(const key_type& key) { return rep.find(key); }
00288   const_iterator find(const key_type& key) const { return rep.find(key); }
00289 
00290   size_type count(const key_type& key) const { return rep.count(key); }
00291   
00292   std::pair<iterator, iterator> equal_range(const key_type& key)
00293     { return rep.equal_range(key); }
00294   std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00295     { return rep.equal_range(key); }
00296 
00297   size_type erase(const key_type& key) {return rep.erase(key); }
00298   void erase(iterator it) { rep.erase(it); }
00299   void erase(iterator f, iterator l) { rep.erase(f, l); }
00300   void clear() { rep.clear(); }
00301 
00302 public:
00303   void resize(size_type hint) { rep.resize(hint); }
00304   size_type bucket_count() const { return rep.bucket_count(); }
00305   size_type max_bucket_count() const { return rep.max_bucket_count(); }
00306   size_type elems_in_bucket(size_type n) const
00307     { return rep.elems_in_bucket(n); }
00308 };
00309 
00310 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00311 inline bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00312                        const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00313 {
00314   return hm1.rep == hm2.rep;
00315 }
00316 
00317 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00318 inline bool operator==(const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00319                        const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00320 {
00321   return hm1.rep == hm2.rep;
00322 }
00323 
00324 
00325 #define HASH_MAP_INSTANTIATE \
00326 extern "please include emulation/hash_map.txx instead"
00327 
00328 } // end namespace itk
00329 
00330 #endif
00331 
00332 #endif // itk_emulation_hash_map_h

Generated at Wed May 24 22:44:54 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000