Back to index

salome-med  6.5.0
InterpKernelHashMap.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2001, 2002, 2004, 2005, 2006, 2009 Free Software Foundation, Inc.
00002 //
00003 // This file is part of the GNU ISO C++ Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the
00005 // terms of the GNU General Public License as published by the
00006 // Free Software Foundation; either version 3, or (at your option)
00007 // any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 
00014 // Under Section 7 of GPL version 3, you are granted additional
00015 // permissions described in the GCC Runtime Library Exception, version
00016 // 3.1, as published by the Free Software Foundation.
00017 
00018 // You should have received a copy of the GNU General Public License and
00019 // a copy of the GCC Runtime Library Exception along with this program;
00020 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00021 // <http://www.gnu.org/licenses/>.
00022 
00023 /*
00024  * Copyright (c) 1996
00025  * Silicon Graphics Computer Systems, Inc.
00026  *
00027  * Permission to use, copy, modify, distribute and sell this software
00028  * and its documentation for any purpose is hereby granted without fee,
00029  * provided that the above copyright notice appear in all copies and
00030  * that both that copyright notice and this permission notice appear
00031  * in supporting documentation.  Silicon Graphics makes no
00032  * representations about the suitability of this software for any
00033  * purpose.  It is provided "as is" without express or implied warranty.
00034  *
00035  *
00036  * Copyright (c) 1994
00037  * Hewlett-Packard Company
00038  *
00039  * Permission to use, copy, modify, distribute and sell this software
00040  * and its documentation for any purpose is hereby granted without fee,
00041  * provided that the above copyright notice appear in all copies and
00042  * that both that copyright notice and this permission notice appear
00043  * in supporting documentation.  Hewlett-Packard Company makes no
00044  * representations about the suitability of this software for any
00045  * purpose.  It is provided "as is" without express or implied warranty.
00046  *
00047  */
00048 #ifndef __INTERPKERNELHASHMAP__
00049 #define __INTERPKERNELHASHMAP__
00050 
00051 #include "InterpKernelStlExt.hxx"
00052 #include "InterpKernelHashTable.hxx"
00053 
00054 namespace INTERP_KERNEL
00055 {
00056   template<class _Key, class _Tp, class _HashFn = hash<_Key>,
00057            class _EqualKey = std::equal_to<_Key>, class _Alloc = std::allocator<_Tp> >
00058   class HashMap
00059   {
00060   private:
00061     typedef hashtable<std::pair<const _Key, _Tp>,_Key, _HashFn,
00062                       STLEXT::Select1st<std::pair<const _Key, _Tp> >,
00063                       _EqualKey, _Alloc> _Ht;
00064 
00065     _Ht _M_ht;
00066     
00067   public:
00068     typedef typename _Ht::key_type key_type;
00069     typedef _Tp data_type;
00070     typedef _Tp mapped_type;
00071     typedef typename _Ht::value_type value_type;
00072     typedef typename _Ht::hasher hasher;
00073     typedef typename _Ht::key_equal key_equal;
00074     
00075     typedef typename _Ht::size_type size_type;
00076     typedef typename _Ht::difference_type difference_type;
00077     typedef typename _Ht::pointer pointer;
00078     typedef typename _Ht::const_pointer const_pointer;
00079     typedef typename _Ht::reference reference;
00080     typedef typename _Ht::const_reference const_reference;
00081     
00082     typedef typename _Ht::iterator iterator;
00083     typedef typename _Ht::const_iterator const_iterator;
00084     
00085     typedef typename _Ht::allocator_type allocator_type;
00086       
00087     hasher hash_funct() const { return _M_ht.hash_funct(); }
00088 
00089     key_equal key_eq() const { return _M_ht.key_eq(); }
00090     
00091     allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00092 
00093     HashMap() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
00094   
00095     explicit HashMap(size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
00096 
00097     HashMap(size_type __n, const hasher& __hf) : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
00098 
00099     HashMap(size_type __n, const hasher& __hf, const key_equal& __eql,
00100             const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a) {}
00101     
00102     template<class _InputIterator>
00103     HashMap(_InputIterator __f, _InputIterator __l) : _M_ht(100, hasher(), key_equal(), allocator_type())
00104     { _M_ht.insert_unique(__f, __l); }
00105     
00106     template<class _InputIterator>
00107     HashMap(_InputIterator __f, _InputIterator __l, size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type())
00108     { _M_ht.insert_unique(__f, __l); }
00109 
00110     template<class _InputIterator>
00111     HashMap(_InputIterator __f, _InputIterator __l, size_type __n, const hasher& __hf)
00112       : _M_ht(__n, __hf, key_equal(), allocator_type())
00113     { _M_ht.insert_unique(__f, __l); }
00114     
00115     template<class _InputIterator>
00116     HashMap(_InputIterator __f, _InputIterator __l, size_type __n,
00117             const hasher& __hf, const key_equal& __eql,
00118             const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a)
00119     { _M_ht.insert_unique(__f, __l); }
00120     
00121     size_type size() const { return _M_ht.size(); }
00122     
00123     size_type max_size() const { return _M_ht.max_size(); }
00124     
00125     bool empty() const { return _M_ht.empty(); }
00126     
00127     void swap(HashMap& __hs) { _M_ht.swap(__hs._M_ht); }
00128     
00129     template<class _K1, class _T1, class _HF, class _EqK, class _Al>
00130     friend bool operator== (const HashMap<_K1, _T1, _HF, _EqK, _Al>&,
00131                             const HashMap<_K1, _T1, _HF, _EqK, _Al>&);
00132     
00133     iterator begin() { return _M_ht.begin(); }
00134     
00135     iterator end() { return _M_ht.end(); }
00136     
00137     const_iterator begin() const { return _M_ht.begin(); }
00138     
00139     const_iterator end() const { return _M_ht.end(); }
00140     
00141     std::pair<iterator, bool> insert(const value_type& __obj) { return _M_ht.insert_unique(__obj); }
00142     
00143     template<class _InputIterator>
00144     void insert(_InputIterator __f, _InputIterator __l) { _M_ht.insert_unique(__f, __l); }
00145     
00146     std::pair<iterator, bool>
00147     insert_noresize(const value_type& __obj) { return _M_ht.insert_unique_noresize(__obj); }
00148     
00149     iterator find(const key_type& __key) { return _M_ht.find(__key); }
00150     
00151     const_iterator find(const key_type& __key) const { return _M_ht.find(__key); }
00152     
00153     _Tp& operator[](const key_type& __key) { return _M_ht.find_or_insert(value_type(__key, _Tp())).second; }
00154     
00155     size_type count(const key_type& __key) const { return _M_ht.count(__key); }
00156     
00157     std::pair<iterator, iterator> equal_range(const key_type& __key) { return _M_ht.equal_range(__key); }
00158     
00159     std::pair<const_iterator, const_iterator> equal_range(const key_type& __key) const { return _M_ht.equal_range(__key); }
00160     
00161     size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
00162     
00163     void erase(iterator __it) { _M_ht.erase(__it); }
00164     
00165     void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
00166 
00167     void clear() { _M_ht.clear(); }
00168 
00169     void resize(size_type __hint) { _M_ht.resize(__hint); }
00170     
00171     size_type bucket_count() const { return _M_ht.bucket_count(); }
00172 
00173     size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00174     
00175     size_type elems_in_bucket(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00176   };
00177   
00178   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
00179   inline bool operator==(const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
00180                          const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
00181   { return __hm1._M_ht == __hm2._M_ht; }
00182   
00183   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
00184   inline bool operator!=(const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
00185                          const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
00186   { return !(__hm1 == __hm2); }
00187   
00188   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
00189   inline void swap(HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
00190                    HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
00191   { __hm1.swap(__hm2); }
00192 
00193   template<class _Key, class _Tp,
00194            class _HashFn = hash<_Key>,
00195            class _EqualKey = std::equal_to<_Key>,
00196            class _Alloc = std::allocator<_Tp> >
00197   class HashMultiMap
00198   {
00199   private:
00200     typedef hashtable<std::pair<const _Key, _Tp>, _Key, _HashFn,
00201                       STLEXT::Select1st<std::pair<const _Key, _Tp> >, _EqualKey, _Alloc>
00202     _Ht;
00203     _Ht _M_ht;
00204   public:
00205     typedef typename _Ht::key_type key_type;
00206     typedef _Tp data_type;
00207     typedef _Tp mapped_type;
00208     typedef typename _Ht::value_type value_type;
00209     typedef typename _Ht::hasher hasher;
00210     typedef typename _Ht::key_equal key_equal;
00211     
00212     typedef typename _Ht::size_type size_type;
00213     typedef typename _Ht::difference_type difference_type;
00214     typedef typename _Ht::pointer pointer;
00215     typedef typename _Ht::const_pointer const_pointer;
00216     typedef typename _Ht::reference reference;
00217     typedef typename _Ht::const_reference const_reference;
00218     
00219     typedef typename _Ht::iterator iterator;
00220     typedef typename _Ht::const_iterator const_iterator;
00221     
00222     typedef typename _Ht::allocator_type allocator_type;
00223   
00224     hasher hash_funct() const { return _M_ht.hash_funct(); }
00225     
00226     key_equal key_eq() const { return _M_ht.key_eq(); }
00227     
00228     allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00229     
00230     HashMultiMap() : _M_ht(100, hasher(), key_equal(), allocator_type()) { }
00231     
00232     explicit HashMultiMap(size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
00233     
00234     HashMultiMap(size_type __n, const hasher& __hf) : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
00235     
00236     HashMultiMap(size_type __n, const hasher& __hf, const key_equal& __eql,
00237                  const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a) {}
00238     
00239     template<class _InputIterator>
00240     HashMultiMap(_InputIterator __f, _InputIterator __l) : _M_ht(100, hasher(), key_equal(), allocator_type())
00241     { _M_ht.insert_equal(__f, __l); }
00242     
00243     template<class _InputIterator>
00244     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type())
00245     { _M_ht.insert_equal(__f, __l); }
00246     
00247     template<class _InputIterator>
00248     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n, const hasher& __hf)
00249       : _M_ht(__n, __hf, key_equal(), allocator_type())
00250     { _M_ht.insert_equal(__f, __l); }
00251     
00252     template<class _InputIterator>
00253     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n,
00254                  const hasher& __hf, const key_equal& __eql,
00255                  const allocator_type& __a = allocator_type())
00256       : _M_ht(__n, __hf, __eql, __a)
00257     { _M_ht.insert_equal(__f, __l); }
00258     
00259     size_type size() const { return _M_ht.size(); }
00260     
00261     size_type max_size() const { return _M_ht.max_size(); }
00262     
00263     bool empty() const { return _M_ht.empty(); }
00264     
00265     void swap(HashMultiMap& __hs) { _M_ht.swap(__hs._M_ht); }
00266     
00267     template<class _K1, class _T1, class _HF, class _EqK, class _Al>
00268     friend bool operator==(const HashMultiMap<_K1, _T1, _HF, _EqK, _Al>&,
00269                            const HashMultiMap<_K1, _T1, _HF, _EqK, _Al>&);
00270     
00271     iterator begin() { return _M_ht.begin(); }
00272     
00273     iterator end() { return _M_ht.end(); }
00274     
00275     const_iterator begin() const { return _M_ht.begin(); }
00276     
00277     const_iterator end() const { return _M_ht.end(); }
00278     
00279     iterator insert(const value_type& __obj) { return _M_ht.insert_equal(__obj); }
00280     
00281     template<class _InputIterator>
00282     void insert(_InputIterator __f, _InputIterator __l) { _M_ht.insert_equal(__f,__l); }
00283     
00284     iterator insert_noresize(const value_type& __obj) { return _M_ht.insert_equal_noresize(__obj); }
00285     
00286     iterator find(const key_type& __key) { return _M_ht.find(__key); }
00287     
00288     const_iterator find(const key_type& __key) const { return _M_ht.find(__key); }
00289     
00290     size_type count(const key_type& __key) const { return _M_ht.count(__key); }
00291     
00292     std::pair<iterator, iterator> equal_range(const key_type& __key) { return _M_ht.equal_range(__key); }
00293     
00294     std::pair<const_iterator, const_iterator> equal_range(const key_type& __key) const { return _M_ht.equal_range(__key); }
00295     
00296     size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
00297     
00298     void erase(iterator __it) { _M_ht.erase(__it); }
00299     
00300     void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
00301     
00302     void clear() { _M_ht.clear(); }
00303     
00304     void resize(size_type __hint) { _M_ht.resize(__hint); }
00305     
00306     size_type bucket_count() const { return _M_ht.bucket_count(); }
00307     
00308     size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00309     
00310     size_type elems_in_bucket(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00311   };
00312   
00313   template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
00314   inline bool operator==(const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
00315                          const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
00316   { return __hm1._M_ht == __hm2._M_ht; }
00317   
00318   template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
00319   inline bool operator!=(const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
00320                          const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
00321   { return !(__hm1 == __hm2); }
00322   
00323   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
00324   inline void swap(HashMultiMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
00325                    HashMultiMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
00326   { __hm1.swap(__hm2); }
00327   
00328 }
00329 
00330 namespace std
00331 {
00332   // Specialization of insert_iterator so that it will work for HashMap
00333   // and HashMultiMap.
00334   template<class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00335   class insert_iterator<INTERP_KERNEL::HashMap<_Key, _Tp, _HashFn, 
00336                                                _EqKey, _Alloc> >
00337   {
00338   protected:
00339     typedef INTERP_KERNEL::HashMap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
00340     _Container;
00341     _Container* container;
00342   public:
00343     typedef _Container          container_type;
00344     typedef output_iterator_tag iterator_category;
00345     typedef void                value_type;
00346     typedef void                difference_type;
00347     typedef void                pointer;
00348     typedef void                reference;
00349       
00350     insert_iterator(_Container& __x) : container(&__x) {}
00351     
00352     insert_iterator(_Container& __x, typename _Container::iterator) : container(&__x) {}
00353     
00354     insert_iterator<_Container>& operator=(const typename _Container::value_type& __value__)
00355     {
00356       container->insert(__value__);
00357       return *this;
00358     }
00359     
00360     insert_iterator<_Container>& operator*() { return *this; }
00361     
00362     insert_iterator<_Container>& operator++() { return *this; }
00363 
00364     insert_iterator<_Container>& operator++(int) { return *this; }
00365   };
00366 
00367   template<class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00368   class insert_iterator<INTERP_KERNEL::HashMultiMap<_Key, _Tp, _HashFn,
00369                                                     _EqKey, _Alloc> >
00370   {
00371   protected:
00372     typedef INTERP_KERNEL::HashMultiMap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
00373     _Container;
00374     _Container* container;
00375     typename _Container::iterator iter;
00376     
00377   public:
00378     typedef _Container          container_type;
00379     typedef output_iterator_tag iterator_category;
00380     typedef void                value_type;
00381     typedef void                difference_type;
00382     typedef void                pointer;
00383     typedef void                reference;
00384     
00385     insert_iterator(_Container& __x) : container(&__x) {}
00386 
00387     insert_iterator(_Container& __x, typename _Container::iterator) : container(&__x) {}
00388 
00389     insert_iterator<_Container>& operator=(const typename _Container::value_type& __value__)
00390     {
00391       container->insert(__value__);
00392       return *this;
00393     }
00394 
00395     insert_iterator<_Container>& operator*() { return *this; }
00396 
00397     insert_iterator<_Container>& operator++() { return *this; }
00398 
00399     insert_iterator<_Container>& operator++(int) { return *this; }
00400   };
00401 }
00402 
00403 #endif