Orfeo Toolbox  3.16
itk_hash_map.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itk_hash_map.h,v $
5  Language: C++
6  Date: $Date: 2009-02-05 19:05:01 $
7  Version: $Revision: 1.21 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
54 #ifndef __itk_hash_map_h
55 #define __itk_hash_map_h
56 
57 #if defined(_MSC_VER)
58 #pragma warning ( disable : 4786 )
59 #endif
60 
61 #if (defined(__GNUC__) && (((__GNUC__==3) && (__GNUC_MINOR__>=1) || (__GNUC__>3) ) || ( (__GNUC__==4) && defined(__INTEL_COMPILER) ) )) || (defined(__IBMCPP__) && __IBMCPP__ >= 600)
62 // Use this hash_map for GNU_C versions >= 3.1, IBMCPP >=600, or Intel compilers with GCCv4
63 
64 // #if (defined(__GNUC__) && ((__GNUC__==4) && (__GNUC_MINOR__>=3) ) )
65 // in gcc 4.3 the header <ext/hash_map> was deprecated and replaced with <unordered_map>.
66 // note that this is still experimental in gcc 4.3
67 // #include <tr1/unordered_map>
68 // #else
69 #include <ext/hash_map>
70 // #endif
71 
72 namespace itk
73 {
74  using __gnu_cxx::hash;
75  using __gnu_cxx::hash_map;
76  using __gnu_cxx::hash_multimap;
77 }
78 
79 #else
80 
81 #include "itk_hashtable.h"
82 #include "itk_alloc.h"
83 
84 
85 // The following is required for CodeWarrior
86 #if defined(__MWERKS__)
87 #include "vcl_functional.h"
88 #endif
89 
90 #include "vcl_compiler.h"
91 
92 
93 namespace itk
94 {
95 
96 # define VCL_IMPORT_CONTAINER_TYPEDEFS(super) \
97  typedef typename super::value_type value_type; \
98  typedef typename super::reference reference; \
99  typedef typename super::size_type size_type; \
100  typedef typename super::const_reference const_reference; \
101  typedef typename super::difference_type difference_type;
102 
103 # define VCL_IMPORT_ITERATORS(super) \
104  typedef typename super::iterator iterator; \
105  typedef typename super::const_iterator const_iterator;
106 
107 # define VCL_IMPORT_REVERSE_ITERATORS(super) \
108  typedef typename super::const_reverse_iterator const_reverse_iterator; \
109  typedef typename super::reverse_iterator reverse_iterator;
110 
111 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
112 class hash_map;
113 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
114 class hash_multimap;
115 
116 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
117 bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>&,
118  const hash_map<Key, T, HashFcn, EqualKey, Alloc>&);
119 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
120 bool operator==(const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>&,
121  const hash_multimap<Key, T, HashFcn, EqualKey, Alloc>&);
122 
127 template <class Key, class T,
128  VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
129  VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
130  VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
131 class hash_map
132 {
133 private:
134  typedef std::select1st<std::pair<const Key, T> > sel1st;
135  typedef hashtable<std::pair<const Key, T>, Key, HashFcn, sel1st, EqualKey, Alloc> ht;
137 public:
140  typedef typename ht::key_type key_type;
141  typedef typename ht::hasher hasher;
142  typedef typename ht::key_equal key_equal;
143  typedef T data_type;
144  typedef typename ht::pointer pointer;
145  typedef typename ht::const_pointer const_pointer;
146 private:
148 
149 public:
150  hasher hash_funct() const { return rep.hash_funct(); }
151  key_equal key_eq() const { return rep.key_eq(); }
152 
153 public:
154  hash_map() : rep(100, hasher(), key_equal()) {}
155  hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
156  hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
157  hash_map(size_type n, const hasher& hf, const key_equal& eql)
158  : rep(n, hf, eql) {}
159 
160  hash_map(const value_type* f, const value_type* l)
161  : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
162  hash_map(const value_type* f, const value_type* l, size_type n)
163  : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
164  hash_map(const value_type* f, const value_type* l, size_type n,
165  const hasher& hf)
166  : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
167  hash_map(const value_type* f, const value_type* l, size_type n,
168  const hasher& hf, const key_equal& eql)
169  : rep(n, hf, eql) { rep.insert_unique(f, l); }
170 
171  hash_map(const_iterator f, const_iterator l)
172  : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
173  hash_map(const_iterator f, const_iterator l, size_type n)
174  : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
175  hash_map(const_iterator f, const_iterator l, size_type n,
176  const hasher& hf)
177  : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
178  hash_map(const_iterator f, const_iterator l, size_type n,
179  const hasher& hf, const key_equal& eql)
180  : rep(n, hf, eql) { rep.insert_unique(f, l); }
181 
182 public:
183  size_type size() const { return rep.size(); }
184  size_type max_size() const { return rep.max_size(); }
185  bool empty() const { return rep.empty(); }
186  void swap(self& hs) { rep.swap(hs.rep); }
187 
188 #if _MSC_VER != 1600
189  friend bool operator==ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(self)(const self &, const self &);
190 #endif
191 
192  iterator begin() { return rep.begin(); }
193  iterator end() { return rep.end(); }
194  const_iterator begin() const { return rep.begin(); }
195  const_iterator end() const { return rep.end(); }
196 
197 public:
198  std::pair<iterator, bool> insert(const value_type& obj)
199  { return rep.insert_unique(obj); }
200  void insert(const value_type* f, const value_type* l) { rep.insert_unique(f,l); }
201  void insert(const_iterator f, const_iterator l) { rep.insert_unique(f, l); }
202  std::pair<iterator, bool> insert_noresize(const value_type& obj)
203  { return rep.insert_unique_noresize(obj); }
204 
205  iterator find(const key_type& key) { return rep.find(key); }
206  const_iterator find(const key_type& key) const { return rep.find(key); }
207 
208  T& operator[](const key_type& key)
209  {
210  value_type val(key, T());
211  return rep.find_or_insert(val).second;
212  }
213 
214  size_type count(const key_type& key) const { return rep.count(key); }
215 
216  std::pair<iterator, iterator> equal_range(const key_type& key)
217  { return rep.equal_range(key); }
218  std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
219  { return rep.equal_range(key); }
220 
221  size_type erase(const key_type& key) {return rep.erase(key); }
222  void erase(iterator it) { rep.erase(it); }
223  void erase(iterator f, iterator l) { rep.erase(f, l); }
224  void clear() { rep.clear(); }
225 
226 public:
227  void resize(size_type hint) { rep.resize(hint); }
228  size_type bucket_count() const { return rep.bucket_count(); }
229  size_type max_bucket_count() const { return rep.max_bucket_count(); }
230  size_type elems_in_bucket(size_type n) const
231  { return rep.elems_in_bucket(n); }
232 };
233 
234 
235 template <class Key, class T, VCL_DFL_TMPL_PARAM_STLDECL(HashFcn,hash<Key>),
236  VCL_DFL_TMPL_PARAM_STLDECL(EqualKey,std::equal_to<Key>),
237  VCL_DFL_TYPE_PARAM_STLDECL(Alloc,std::allocator<char> ) >
239 {
240 private:
241  typedef hashtable<std::pair<const Key, T>, Key, HashFcn,
242  std::select1st<std::pair<const Key, T> >, EqualKey, Alloc> ht;
244 public:
247  typedef typename ht::key_type key_type;
248  typedef typename ht::hasher hasher;
249  typedef typename ht::key_equal key_equal;
250  typedef T data_type;
251  typedef typename ht::pointer pointer;
252  typedef typename ht::const_pointer const_pointer;
253 
254  hasher hash_funct() const { return rep.hash_funct(); }
255  key_equal key_eq() const { return rep.key_eq(); }
256 private:
258 
259 public:
260  hash_multimap() : rep(100, hasher(), key_equal()) {}
261  hash_multimap(size_type n) : rep(n, hasher(), key_equal()) {}
262  hash_multimap(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
263  hash_multimap(size_type n, const hasher& hf, const key_equal& eql)
264  : rep(n, hf, eql) {}
265 
266  hash_multimap(const value_type* f, const value_type* l)
267  : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
268  hash_multimap(const value_type* f, const value_type* l, size_type n)
269  : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
270  hash_multimap(const value_type* f, const value_type* l, size_type n,
271  const hasher& hf)
272  : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
273  hash_multimap(const value_type* f, const value_type* l, size_type n,
274  const hasher& hf, const key_equal& eql)
275  : rep(n, hf, eql) { rep.insert_equal(f, l); }
276 
277  hash_multimap(const_iterator f, const_iterator l)
278  : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
279  hash_multimap(const_iterator f, const_iterator l, size_type n)
280  : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
281  hash_multimap(const_iterator f, const_iterator l, size_type n,
282  const hasher& hf)
283  : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
284  hash_multimap(const_iterator f, const_iterator l, size_type n,
285  const hasher& hf, const key_equal& eql)
286  : rep(n, hf, eql) { rep.insert_equal(f, l); }
287 
288 public:
289  size_type size() const { return rep.size(); }
290  size_type max_size() const { return rep.max_size(); }
291  bool empty() const { return rep.empty(); }
292  void swap(self& hs) { rep.swap(hs.rep); }
293 
294 #if _MSC_VER != 1600
295  friend bool operator==ITK_FRIEND_TEMPLATE_FUNCTION_ARGUMENT(self)(const self &, const self &);
296 #endif
297 
298  iterator begin() { return rep.begin(); }
299  iterator end() { return rep.end(); }
300  const_iterator begin() const { return rep.begin(); }
301  const_iterator end() const { return rep.end(); }
302 
303 public:
304  iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
305  void insert(const value_type* f, const value_type* l) { rep.insert_equal(f,l); }
306  void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
307  iterator insert_noresize(const value_type& obj)
308  { return rep.insert_equal_noresize(obj); }
309 
310  iterator find(const key_type& key) { return rep.find(key); }
311  const_iterator find(const key_type& key) const { return rep.find(key); }
312 
313  size_type count(const key_type& key) const { return rep.count(key); }
314 
315  std::pair<iterator, iterator> equal_range(const key_type& key)
316  { return rep.equal_range(key); }
317  std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
318  { return rep.equal_range(key); }
319 
320  size_type erase(const key_type& key) {return rep.erase(key); }
321  void erase(iterator it) { rep.erase(it); }
322  void erase(iterator f, iterator l) { rep.erase(f, l); }
323  void clear() { rep.clear(); }
324 
325 public:
326  void resize(size_type hint) { rep.resize(hint); }
327  size_type bucket_count() const { return rep.bucket_count(); }
328  size_type max_bucket_count() const { return rep.max_bucket_count(); }
329  size_type elems_in_bucket(size_type n) const
330  { return rep.elems_in_bucket(n); }
331 };
332 
335 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
338 {
339  return hm1.rep == hm2.rep;
340 }
341 
344 template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
347 {
348  return hm1.rep == hm2.rep;
349 }
350 
351 
352 #define HASH_MAP_INSTANTIATE \
353 extern "please include emulation/hash_map.txx instead"
354 
355 } // end namespace itk
356 
357 #endif
358 
359 #endif // itk_emulation_hash_map_h

Generated at Sat Feb 2 2013 23:22:54 for Orfeo Toolbox with doxygen 1.8.1.1