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: 2002/07/17 17:35:54 $
00007   Version:   $Revision: 1.5 $
00008 
00009   Copyright (c) 2002 Insight 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))
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 namespace itk
00078 {
00079 
00080 #  define VCL_IMPORT_CONTAINER_TYPEDEFS(super)                            \
00081     typedef typename super::value_type value_type;                               \
00082     typedef typename super::reference reference;                                 \
00083     typedef typename super::size_type size_type;                                 \
00084     typedef typename super::const_reference const_reference;                     \
00085     typedef typename super::difference_type difference_type;
00086 
00087 #  define VCL_IMPORT_ITERATORS(super)                                              \
00088     typedef typename super::iterator iterator;                                   \
00089     typedef typename super::const_iterator const_iterator; 
00090 
00091 #  define VCL_IMPORT_REVERSE_ITERATORS(super)                                      \
00092     typedef typename super::const_reverse_iterator  const_reverse_iterator;      \
00093     typedef typename super::reverse_iterator reverse_iterator;
00094 
00095 
00099 template <class Key, class T,
00100           VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00101           VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00102           VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00103 class hash_map
00104 {
00105 private:
00106   typedef std::select1st<std::pair<const Key, T> > sel1st;
00107   typedef hashtable<std::pair<const Key, T>, Key, HashFcn, sel1st, EqualKey, Alloc> ht;
00108   typedef hash_map<Key, T, HashFcn, EqualKey, Alloc> self;
00109 public:
00110   VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00111   VCL_IMPORT_ITERATORS(ht)
00112   typedef typename ht::key_type key_type;
00113   typedef typename ht::hasher hasher;
00114   typedef typename ht::key_equal key_equal;
00115   typedef T data_type;
00116   typedef typename ht::pointer pointer;
00117   typedef typename ht::const_pointer const_pointer;
00118 private:
00119   ht rep;
00120 
00121 public:
00122   hasher hash_funct() const { return rep.hash_funct(); }
00123   key_equal key_eq() const { return rep.key_eq(); }
00124 
00125 public:
00126   hash_map() : rep(100, hasher(), key_equal()) {}
00127   hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
00128   hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00129   hash_map(size_type n, const hasher& hf, const key_equal& eql)
00130     : rep(n, hf, eql) {}
00131 
00132   hash_map(const value_type* f, const value_type* l)
00133     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00134   hash_map(const value_type* f, const value_type* l, size_type n)
00135     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00136   hash_map(const value_type* f, const value_type* l, size_type n,
00137            const hasher& hf)
00138     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00139   hash_map(const value_type* f, const value_type* l, size_type n,
00140            const hasher& hf, const key_equal& eql)
00141     : rep(n, hf, eql) { rep.insert_unique(f, l); }
00142 
00143   hash_map(const_iterator f, const_iterator l)
00144     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
00145   hash_map(const_iterator f, const_iterator l, size_type n)
00146     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
00147   hash_map(const_iterator f, const_iterator l, size_type n,
00148            const hasher& hf)
00149     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
00150   hash_map(const_iterator f, const_iterator l, size_type n,
00151            const hasher& hf, const key_equal& eql)
00152     : rep(n, hf, eql) { rep.insert_unique(f, l); }
00153 
00154 public:
00155   size_type size() const { return rep.size(); }
00156   size_type max_size() const { return rep.max_size(); }
00157   bool empty() const { return rep.empty(); }
00158   void swap(self& hs) { rep.swap(hs.rep); }
00159   friend bool operator==VCL_NULL_TMPL_ARGS(const hash_map<Key,T,HashFcn,EqualKey,Alloc>&,
00160                          const hash_map<Key,T,HashFcn,EqualKey,Alloc>&);
00161 
00162   iterator begin() { return rep.begin(); }
00163   iterator end() { return rep.end(); }
00164   const_iterator begin() const { return rep.begin(); }
00165   const_iterator end() const { return rep.end(); }
00166 
00167 public:
00168   std::pair<iterator, bool> insert(const value_type& obj)
00169     { return rep.insert_unique(obj); }
00170   void insert(const value_type* f, const value_type* l) { rep.insert_unique(f,l); }
00171   void insert(const_iterator f, const_iterator l) { rep.insert_unique(f, l); }
00172   std::pair<iterator, bool> insert_noresize(const value_type& obj)
00173     { return rep.insert_unique_noresize(obj); }    
00174 
00175   iterator find(const key_type& key) { return rep.find(key); }
00176   const_iterator find(const key_type& key) const { return rep.find(key); }
00177 
00178   T& operator[](const key_type& key)
00179   {
00180       value_type val(key, T());
00181       return rep.find_or_insert(val).second;
00182   }
00183 
00184   size_type count(const key_type& key) const { return rep.count(key); }
00185   
00186   std::pair<iterator, iterator> equal_range(const key_type& key)
00187     { return rep.equal_range(key); }
00188   std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00189     { return rep.equal_range(key); }
00190 
00191   size_type erase(const key_type& key) {return rep.erase(key); }
00192   void erase(iterator it) { rep.erase(it); }
00193   void erase(iterator f, iterator l) { rep.erase(f, l); }
00194   void clear() { rep.clear(); }
00195 
00196 public:
00197   void resize(size_type hint) { rep.resize(hint); }
00198   size_type bucket_count() const { return rep.bucket_count(); }
00199   size_type max_bucket_count() const { return rep.max_bucket_count(); }
00200   size_type elems_in_bucket(size_type n) const
00201     { return rep.elems_in_bucket(n); }
00202 };
00203 
00204 
00205 template <class Key, class T, VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
00206           VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
00207           VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
00208 class hash_multimap
00209 {
00210 private:
00211   typedef hashtable<std::pair<const Key, T>, Key, HashFcn,
00212       std::select1st<std::pair<const Key, T> >, EqualKey, Alloc> ht;
00213   typedef hash_multimap<Key, T, HashFcn, EqualKey, Alloc> self;
00214 public:
00215   VCL_IMPORT_CONTAINER_TYPEDEFS(ht)
00216   VCL_IMPORT_ITERATORS(ht)
00217   typedef typename ht::key_type key_type;
00218   typedef typename ht::hasher hasher;
00219   typedef typename ht::key_equal key_equal;
00220   typedef T data_type;
00221   typedef typename ht::pointer pointer;
00222   typedef typename ht::const_pointer const_pointer;
00223 
00224   hasher hash_funct() const { return rep.hash_funct(); }
00225   key_equal key_eq() const { return rep.key_eq(); }
00226 private:
00227   ht rep;
00228 
00229 public:
00230   hash_multimap() : rep(100, hasher(), key_equal()) {}
00231   hash_multimap(size_type n) : rep(n, hasher(), key_equal()) {}
00232   hash_multimap(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
00233   hash_multimap(size_type n, const hasher& hf, const key_equal& eql)
00234     : rep(n, hf, eql) {}
00235 
00236   hash_multimap(const value_type* f, const value_type* l)
00237     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00238   hash_multimap(const value_type* f, const value_type* l, size_type n)
00239     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00240   hash_multimap(const value_type* f, const value_type* l, size_type n,
00241            const hasher& hf)
00242     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00243   hash_multimap(const value_type* f, const value_type* l, size_type n,
00244            const hasher& hf, const key_equal& eql)
00245     : rep(n, hf, eql) { rep.insert_equal(f, l); }
00246 
00247   hash_multimap(const_iterator f, const_iterator l)
00248     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
00249   hash_multimap(const_iterator f, const_iterator l, size_type n)
00250     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
00251   hash_multimap(const_iterator f, const_iterator l, size_type n,
00252            const hasher& hf)
00253     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
00254   hash_multimap(const_iterator f, const_iterator l, size_type n,
00255            const hasher& hf, const key_equal& eql)
00256     : rep(n, hf, eql) { rep.insert_equal(f, l); }
00257 
00258 public:
00259   size_type size() const { return rep.size(); }
00260   size_type max_size() const { return rep.max_size(); }
00261   bool empty() const { return rep.empty(); }
00262   void swap(self& hs) { rep.swap(hs.rep); }
00263   friend bool operator==VCL_NULL_TMPL_ARGS(const hash_multimap<Key,T,HashFcn,EqualKey,Alloc>&,
00264                          const hash_multimap<Key,T,HashFcn,EqualKey,Alloc>&);
00265 
00266   iterator begin() { return rep.begin(); }
00267   iterator end() { return rep.end(); }
00268   const_iterator begin() const { return rep.begin(); }
00269   const_iterator end() const { return rep.end(); }
00270 
00271 public:
00272   iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
00273   void insert(const value_type* f, const value_type* l) { rep.insert_equal(f,l); }
00274   void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
00275   iterator insert_noresize(const value_type& obj)
00276     { return rep.insert_equal_noresize(obj); }    
00277 
00278   iterator find(const key_type& key) { return rep.find(key); }
00279   const_iterator find(const key_type& key) const { return rep.find(key); }
00280 
00281   size_type count(const key_type& key) const { return rep.count(key); }
00282   
00283   std::pair<iterator, iterator> equal_range(const key_type& key)
00284     { return rep.equal_range(key); }
00285   std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
00286     { return rep.equal_range(key); }
00287 
00288   size_type erase(const key_type& key) {return rep.erase(key); }
00289   void erase(iterator it) { rep.erase(it); }
00290   void erase(iterator f, iterator l) { rep.erase(f, l); }
00291   void clear() { rep.clear(); }
00292 
00293 public:
00294   void resize(size_type hint) { rep.resize(hint); }
00295   size_type bucket_count() const { return rep.bucket_count(); }
00296   size_type max_bucket_count() const { return rep.max_bucket_count(); }
00297   size_type elems_in_bucket(size_type n) const
00298     { return rep.elems_in_bucket(n); }
00299 };
00300 
00301 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00302 inline bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00303                        const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00304 {
00305   return hm1.rep == hm2.rep;
00306 }
00307 
00308 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
00309 inline bool operator==(const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm1,
00310                        const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>& hm2)
00311 {
00312   return hm1.rep == hm2.rep;
00313 }
00314 
00315 
00316 #define HASH_MAP_INSTANTIATE \
00317 extern "please include emulation/hash_map.txx instead"
00318 
00319 } // end namespace itk
00320 
00321 #endif
00322 
00323 #endif // itk_emulation_hash_map_h

Generated at Wed Mar 12 01:12:45 2003 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000