Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / medtool / src / INTERP_KERNEL / Bases / InterpKernelHashMap.hxx
1 // Copyright (C) 2001-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // This library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 // GNU General Public License for more details.
24
25 // Under Section 7 of GPL version 3, you are granted additional
26 // permissions described in the GCC Runtime Library Exception, version
27 // 3.1, as published by the Free Software Foundation.
28
29 // You should have received a copy of the GNU General Public License and
30 // a copy of the GCC Runtime Library Exception along with this program;
31 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
32 // <http://www.gnu.org/licenses/>.
33
34 /*
35  * Copyright (c) 1996
36  * Silicon Graphics Computer Systems, Inc.
37  *
38  * Permission to use, copy, modify, distribute and sell this software
39  * and its documentation for any purpose is hereby granted without fee,
40  * provided that the above copyright notice appear in all copies and
41  * that both that copyright notice and this permission notice appear
42  * in supporting documentation.  Silicon Graphics makes no
43  * representations about the suitability of this software for any
44  * purpose.  It is provided "as is" without express or implied warranty.
45  *
46  *
47  * Copyright (c) 1994
48  * Hewlett-Packard Company
49  *
50  * Permission to use, copy, modify, distribute and sell this software
51  * and its documentation for any purpose is hereby granted without fee,
52  * provided that the above copyright notice appear in all copies and
53  * that both that copyright notice and this permission notice appear
54  * in supporting documentation.  Hewlett-Packard Company makes no
55  * representations about the suitability of this software for any
56  * purpose.  It is provided "as is" without express or implied warranty.
57  *
58  */
59 #ifndef __INTERPKERNELHASHMAP__
60 #define __INTERPKERNELHASHMAP__
61
62 #include "InterpKernelStlExt.hxx"
63 #include "InterpKernelHashTable.hxx"
64
65 namespace INTERP_KERNEL
66 {
67   template<class _Key, class _Tp, class _HashFn = hash<_Key>,
68            class _EqualKey = std::equal_to<_Key>, class _Alloc = std::allocator<_Tp> >
69   class HashMap
70   {
71   private:
72     typedef hashtable<std::pair<const _Key, _Tp>,_Key, _HashFn,
73                       STLEXT::Select1st<std::pair<const _Key, _Tp> >,
74                       _EqualKey, _Alloc> _Ht;
75
76     _Ht _M_ht;
77     
78   public:
79     typedef typename _Ht::key_type key_type;
80     typedef _Tp data_type;
81     typedef _Tp mapped_type;
82     typedef typename _Ht::value_type value_type;
83     typedef typename _Ht::hasher hasher;
84     typedef typename _Ht::key_equal key_equal;
85     
86     typedef typename _Ht::size_type size_type;
87     typedef typename _Ht::difference_type difference_type;
88     typedef typename _Ht::pointer pointer;
89     typedef typename _Ht::const_pointer const_pointer;
90     typedef typename _Ht::reference reference;
91     typedef typename _Ht::const_reference const_reference;
92     
93     typedef typename _Ht::iterator iterator;
94     typedef typename _Ht::const_iterator const_iterator;
95     
96     typedef typename _Ht::allocator_type allocator_type;
97       
98     hasher hash_funct() const { return _M_ht.hash_funct(); }
99
100     key_equal key_eq() const { return _M_ht.key_eq(); }
101     
102     allocator_type get_allocator() const { return _M_ht.get_allocator(); }
103
104     HashMap() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
105   
106     explicit HashMap(size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
107
108     HashMap(size_type __n, const hasher& __hf) : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
109
110     HashMap(size_type __n, const hasher& __hf, const key_equal& __eql,
111             const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a) {}
112     
113     template<class _InputIterator>
114     HashMap(_InputIterator __f, _InputIterator __l) : _M_ht(100, hasher(), key_equal(), allocator_type())
115     { _M_ht.insert_unique(__f, __l); }
116     
117     template<class _InputIterator>
118     HashMap(_InputIterator __f, _InputIterator __l, size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type())
119     { _M_ht.insert_unique(__f, __l); }
120
121     template<class _InputIterator>
122     HashMap(_InputIterator __f, _InputIterator __l, size_type __n, const hasher& __hf)
123       : _M_ht(__n, __hf, key_equal(), allocator_type())
124     { _M_ht.insert_unique(__f, __l); }
125     
126     template<class _InputIterator>
127     HashMap(_InputIterator __f, _InputIterator __l, size_type __n,
128             const hasher& __hf, const key_equal& __eql,
129             const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a)
130     { _M_ht.insert_unique(__f, __l); }
131     
132     size_type size() const { return _M_ht.size(); }
133     
134     size_type max_size() const { return _M_ht.max_size(); }
135     
136     bool empty() const { return _M_ht.empty(); }
137     
138     void swap(HashMap& __hs) { _M_ht.swap(__hs._M_ht); }
139     
140     template<class _K1, class _T1, class _HF, class _EqK, class _Al>
141     friend bool operator== (const HashMap<_K1, _T1, _HF, _EqK, _Al>&,
142                             const HashMap<_K1, _T1, _HF, _EqK, _Al>&);
143     
144     iterator begin() { return _M_ht.begin(); }
145     
146     iterator end() { return _M_ht.end(); }
147     
148     const_iterator begin() const { return _M_ht.begin(); }
149     
150     const_iterator end() const { return _M_ht.end(); }
151     
152     std::pair<iterator, bool> insert(const value_type& __obj) { return _M_ht.insert_unique(__obj); }
153     
154     template<class _InputIterator>
155     void insert(_InputIterator __f, _InputIterator __l) { _M_ht.insert_unique(__f, __l); }
156     
157     std::pair<iterator, bool>
158     insert_noresize(const value_type& __obj) { return _M_ht.insert_unique_noresize(__obj); }
159     
160     iterator find(const key_type& __key) { return _M_ht.find(__key); }
161     
162     const_iterator find(const key_type& __key) const { return _M_ht.find(__key); }
163     
164     _Tp& operator[](const key_type& __key) { return _M_ht.find_or_insert(value_type(__key, _Tp())).second; }
165     
166     size_type count(const key_type& __key) const { return _M_ht.count(__key); }
167     
168     std::pair<iterator, iterator> equal_range(const key_type& __key) { return _M_ht.equal_range(__key); }
169     
170     std::pair<const_iterator, const_iterator> equal_range(const key_type& __key) const { return _M_ht.equal_range(__key); }
171     
172     size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
173     
174     void erase(iterator __it) { _M_ht.erase(__it); }
175     
176     void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
177
178     void clear() { _M_ht.clear(); }
179
180     void resize(size_type __hint) { _M_ht.resize(__hint); }
181     
182     size_type bucket_count() const { return _M_ht.bucket_count(); }
183
184     size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
185     
186     size_type elems_in_bucket(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
187   };
188   
189   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
190   inline bool operator==(const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
191                          const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
192   { return __hm1._M_ht == __hm2._M_ht; }
193   
194   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
195   inline bool operator!=(const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
196                          const HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
197   { return !(__hm1 == __hm2); }
198   
199   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
200   inline void swap(HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
201                    HashMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
202   { __hm1.swap(__hm2); }
203
204   template<class _Key, class _Tp,
205            class _HashFn = hash<_Key>,
206            class _EqualKey = std::equal_to<_Key>,
207            class _Alloc = std::allocator<_Tp> >
208   class HashMultiMap
209   {
210   private:
211     typedef hashtable<std::pair<const _Key, _Tp>, _Key, _HashFn,
212                       STLEXT::Select1st<std::pair<const _Key, _Tp> >, _EqualKey, _Alloc>
213     _Ht;
214     _Ht _M_ht;
215   public:
216     typedef typename _Ht::key_type key_type;
217     typedef _Tp data_type;
218     typedef _Tp mapped_type;
219     typedef typename _Ht::value_type value_type;
220     typedef typename _Ht::hasher hasher;
221     typedef typename _Ht::key_equal key_equal;
222     
223     typedef typename _Ht::size_type size_type;
224     typedef typename _Ht::difference_type difference_type;
225     typedef typename _Ht::pointer pointer;
226     typedef typename _Ht::const_pointer const_pointer;
227     typedef typename _Ht::reference reference;
228     typedef typename _Ht::const_reference const_reference;
229     
230     typedef typename _Ht::iterator iterator;
231     typedef typename _Ht::const_iterator const_iterator;
232     
233     typedef typename _Ht::allocator_type allocator_type;
234   
235     hasher hash_funct() const { return _M_ht.hash_funct(); }
236     
237     key_equal key_eq() const { return _M_ht.key_eq(); }
238     
239     allocator_type get_allocator() const { return _M_ht.get_allocator(); }
240     
241     HashMultiMap() : _M_ht(100, hasher(), key_equal(), allocator_type()) { }
242     
243     explicit HashMultiMap(size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
244     
245     HashMultiMap(size_type __n, const hasher& __hf) : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
246     
247     HashMultiMap(size_type __n, const hasher& __hf, const key_equal& __eql,
248                  const allocator_type& __a = allocator_type()) : _M_ht(__n, __hf, __eql, __a) {}
249     
250     template<class _InputIterator>
251     HashMultiMap(_InputIterator __f, _InputIterator __l) : _M_ht(100, hasher(), key_equal(), allocator_type())
252     { _M_ht.insert_equal(__f, __l); }
253     
254     template<class _InputIterator>
255     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n) : _M_ht(__n, hasher(), key_equal(), allocator_type())
256     { _M_ht.insert_equal(__f, __l); }
257     
258     template<class _InputIterator>
259     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n, const hasher& __hf)
260       : _M_ht(__n, __hf, key_equal(), allocator_type())
261     { _M_ht.insert_equal(__f, __l); }
262     
263     template<class _InputIterator>
264     HashMultiMap(_InputIterator __f, _InputIterator __l, size_type __n,
265                  const hasher& __hf, const key_equal& __eql,
266                  const allocator_type& __a = allocator_type())
267       : _M_ht(__n, __hf, __eql, __a)
268     { _M_ht.insert_equal(__f, __l); }
269     
270     size_type size() const { return _M_ht.size(); }
271     
272     size_type max_size() const { return _M_ht.max_size(); }
273     
274     bool empty() const { return _M_ht.empty(); }
275     
276     void swap(HashMultiMap& __hs) { _M_ht.swap(__hs._M_ht); }
277     
278     template<class _K1, class _T1, class _HF, class _EqK, class _Al>
279     friend bool operator==(const HashMultiMap<_K1, _T1, _HF, _EqK, _Al>&,
280                            const HashMultiMap<_K1, _T1, _HF, _EqK, _Al>&);
281     
282     iterator begin() { return _M_ht.begin(); }
283     
284     iterator end() { return _M_ht.end(); }
285     
286     const_iterator begin() const { return _M_ht.begin(); }
287     
288     const_iterator end() const { return _M_ht.end(); }
289     
290     iterator insert(const value_type& __obj) { return _M_ht.insert_equal(__obj); }
291     
292     template<class _InputIterator>
293     void insert(_InputIterator __f, _InputIterator __l) { _M_ht.insert_equal(__f,__l); }
294     
295     iterator insert_noresize(const value_type& __obj) { return _M_ht.insert_equal_noresize(__obj); }
296     
297     iterator find(const key_type& __key) { return _M_ht.find(__key); }
298     
299     const_iterator find(const key_type& __key) const { return _M_ht.find(__key); }
300     
301     size_type count(const key_type& __key) const { return _M_ht.count(__key); }
302     
303     std::pair<iterator, iterator> equal_range(const key_type& __key) { return _M_ht.equal_range(__key); }
304     
305     std::pair<const_iterator, const_iterator> equal_range(const key_type& __key) const { return _M_ht.equal_range(__key); }
306     
307     size_type erase(const key_type& __key) { return _M_ht.erase(__key); }
308     
309     void erase(iterator __it) { _M_ht.erase(__it); }
310     
311     void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
312     
313     void clear() { _M_ht.clear(); }
314     
315     void resize(size_type __hint) { _M_ht.resize(__hint); }
316     
317     size_type bucket_count() const { return _M_ht.bucket_count(); }
318     
319     size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
320     
321     size_type elems_in_bucket(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
322   };
323   
324   template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
325   inline bool operator==(const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
326                          const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
327   { return __hm1._M_ht == __hm2._M_ht; }
328   
329   template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
330   inline bool operator!=(const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
331                          const HashMultiMap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
332   { return !(__hm1 == __hm2); }
333   
334   template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
335   inline void swap(HashMultiMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
336                    HashMultiMap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
337   { __hm1.swap(__hm2); }
338   
339 }
340
341 namespace std
342 {
343   // Specialization of insert_iterator so that it will work for HashMap
344   // and HashMultiMap.
345   template<class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
346   class insert_iterator<INTERP_KERNEL::HashMap<_Key, _Tp, _HashFn, 
347                                                _EqKey, _Alloc> >
348   {
349   protected:
350     typedef INTERP_KERNEL::HashMap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
351     _Container;
352     _Container* container;
353   public:
354     typedef _Container          container_type;
355     typedef output_iterator_tag iterator_category;
356     typedef void                value_type;
357     typedef void                difference_type;
358     typedef void                pointer;
359     typedef void                reference;
360       
361     insert_iterator(_Container& __x) : container(&__x) {}
362     
363     insert_iterator(_Container& __x, typename _Container::iterator) : container(&__x) {}
364     
365     insert_iterator<_Container>& operator=(const typename _Container::value_type& __value__)
366     {
367       container->insert(__value__);
368       return *this;
369     }
370     
371     insert_iterator<_Container>& operator*() { return *this; }
372     
373     insert_iterator<_Container>& operator++() { return *this; }
374
375     insert_iterator<_Container>& operator++(int) { return *this; }
376   };
377
378   template<class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
379   class insert_iterator<INTERP_KERNEL::HashMultiMap<_Key, _Tp, _HashFn,
380                                                     _EqKey, _Alloc> >
381   {
382   protected:
383     typedef INTERP_KERNEL::HashMultiMap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
384     _Container;
385     _Container* container;
386     typename _Container::iterator iter;
387     
388   public:
389     typedef _Container          container_type;
390     typedef output_iterator_tag iterator_category;
391     typedef void                value_type;
392     typedef void                difference_type;
393     typedef void                pointer;
394     typedef void                reference;
395     
396     insert_iterator(_Container& __x) : container(&__x) {}
397
398     insert_iterator(_Container& __x, typename _Container::iterator) : container(&__x) {}
399
400     insert_iterator<_Container>& operator=(const typename _Container::value_type& __value__)
401     {
402       container->insert(__value__);
403       return *this;
404     }
405
406     insert_iterator<_Container>& operator*() { return *this; }
407
408     insert_iterator<_Container>& operator++() { return *this; }
409
410     insert_iterator<_Container>& operator++(int) { return *this; }
411   };
412 }
413
414 #endif