NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
HashSet.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #ifndef WTF_HashSet_h
22 #define WTF_HashSet_h
23 
24 #include "FastAllocBase.h"
25 #include "HashTable.h"
26 
27 namespace WTF {
28 
29  template<typename Value, typename HashFunctions, typename Traits> class HashSet;
30  template<typename Value, typename HashFunctions, typename Traits>
32  template<typename Value, typename HashFunctions, typename Traits>
34 
35  template<typename T> struct IdentityExtractor;
36 
37  template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash,
38  typename TraitsArg = HashTraits<ValueArg> > class HashSet {
39  WTF_MAKE_FAST_ALLOCATED;
40  private:
41  typedef HashArg HashFunctions;
42  typedef TraitsArg ValueTraits;
43 
44  public:
45  typedef typename ValueTraits::TraitType ValueType;
46 
47  private:
48  typedef HashTable<ValueType, ValueType, IdentityExtractor<ValueType>,
49  HashFunctions, ValueTraits, ValueTraits> HashTableType;
50 
51  public:
52  typedef HashTableConstIteratorAdapter<HashTableType, ValueType> iterator;
53  typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
54 
55  void swap(HashSet&);
56 
57  int size() const;
58  int capacity() const;
59  bool isEmpty() const;
60 
61  iterator begin() const;
62  iterator end() const;
63 
64  iterator find(const ValueType&) const;
65  bool contains(const ValueType&) const;
66 
67  // An alternate version of find() that finds the object by hashing and comparing
68  // with some other type, to avoid the cost of type conversion. HashTranslator
69  // must have the following function members:
70  // static unsigned hash(const T&);
71  // static bool equal(const ValueType&, const T&);
72  template<typename T, typename HashTranslator> iterator find(const T&) const;
73  template<typename T, typename HashTranslator> bool contains(const T&) const;
74 
75  // The return value is a pair of an interator to the new value's location,
76  // and a bool that is true if an new entry was added.
77  pair<iterator, bool> add(const ValueType&);
78 
79  // An alternate version of add() that finds the object by hashing and comparing
80  // with some other type, to avoid the cost of type conversion if the object is already
81  // in the table. HashTranslator must have the following function members:
82  // static unsigned hash(const T&);
83  // static bool equal(const ValueType&, const T&);
84  // static translate(ValueType&, const T&, unsigned hashCode);
85  template<typename T, typename HashTranslator> pair<iterator, bool> add(const T&);
86 
87  void remove(const ValueType&);
88  void remove(iterator);
89  void clear();
90 
91  private:
92  friend void deleteAllValues<>(const HashSet&);
93  friend void fastDeleteAllValues<>(const HashSet&);
94 
95  HashTableType m_impl;
96  };
97 
98  template<typename T> struct IdentityExtractor {
99  static const T& extract(const T& t) { return t; }
100  };
101 
102  template<typename ValueType, typename ValueTraits, typename T, typename Translator>
104  static unsigned hash(const T& key) { return Translator::hash(key); }
105  static bool equal(const ValueType& a, const T& b) { return Translator::equal(a, b); }
106  static void translate(ValueType& location, const T& key, const T&, unsigned hashCode)
107  {
108  Translator::translate(location, key, hashCode);
109  }
110  };
111 
112  template<typename T, typename U, typename V>
113  inline void HashSet<T, U, V>::swap(HashSet& other)
114  {
115  m_impl.swap(other.m_impl);
116  }
117 
118  template<typename T, typename U, typename V>
119  inline int HashSet<T, U, V>::size() const
120  {
121  return m_impl.size();
122  }
123 
124  template<typename T, typename U, typename V>
125  inline int HashSet<T, U, V>::capacity() const
126  {
127  return m_impl.capacity();
128  }
129 
130  template<typename T, typename U, typename V>
131  inline bool HashSet<T, U, V>::isEmpty() const
132  {
133  return m_impl.isEmpty();
134  }
135 
136  template<typename T, typename U, typename V>
138  {
139  return m_impl.begin();
140  }
141 
142  template<typename T, typename U, typename V>
144  {
145  return m_impl.end();
146  }
147 
148  template<typename T, typename U, typename V>
150  {
151  return m_impl.find(value);
152  }
153 
154  template<typename T, typename U, typename V>
155  inline bool HashSet<T, U, V>::contains(const ValueType& value) const
156  {
157  return m_impl.contains(value);
158  }
159 
160  template<typename Value, typename HashFunctions, typename Traits>
161  template<typename T, typename HashTranslator>
164  {
166  return m_impl.template find<T, Adapter>(value);
167  }
168 
169  template<typename Value, typename HashFunctions, typename Traits>
170  template<typename T, typename HashTranslator>
172  {
174  return m_impl.template contains<T, Adapter>(value);
175  }
176 
177  /*
178  template<typename T, typename U, typename V>
179  inline pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType& value)
180  {
181  return m_impl.add(value);
182  }
183  */
184  // fix
185  template<typename T, typename U, typename V>
186  inline pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType& value)
187  {
188  typedef typename HashSet<T, U, V>::iterator iter_type;
189  auto& temp = m_impl.add(value);
190  return make_pair((iter_type)temp.first, temp.second);
191  }
192 
193  /*
194  template<typename Value, typename HashFunctions, typename Traits>
195  template<typename T, typename HashTranslator>
196  inline pair<typename HashSet<Value, HashFunctions, Traits>::iterator, bool>
197  HashSet<Value, HashFunctions, Traits>::add(const T& value)
198  {
199  typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, HashTranslator> Adapter;
200  return m_impl.template addPassingHashCode<T, T, Adapter>(value, value);
201  }
202  */
203  // fix
204  template<typename Value, typename HashFunctions, typename Traits>
205  template<typename T, typename HashTranslator>
206  inline pair<typename HashSet<Value, HashFunctions, Traits>::iterator, bool>
208  {
210  typedef typename HashSet<Value, HashFunctions, Traits>::iterator iter_type;
211  auto& temp = m_impl.template addPassingHashCode<T, T, Adapter>(value, value);
212  return make_pair((iter_type)temp.first, temp.second);
213  }
214 
215  template<typename T, typename U, typename V>
217  {
218  if (it.m_impl == m_impl.end())
219  return;
220  m_impl.internalCheckTableConsistency();
221  m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
222  }
223 
224  template<typename T, typename U, typename V>
226  {
227  remove(find(value));
228  }
229 
230  template<typename T, typename U, typename V>
232  {
233  m_impl.clear();
234  }
235 
236  template<typename ValueType, typename HashTableType>
237  void deleteAllValues(HashTableType& collection)
238  {
239  typedef typename HashTableType::const_iterator iterator;
240  iterator end = collection.end();
241  for (iterator it = collection.begin(); it != end; ++it)
242  delete *it;
243  }
244 
245  template<typename T, typename U, typename V>
246  inline void deleteAllValues(const HashSet<T, U, V>& collection)
247  {
248  deleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
249  }
250 
251  template<typename ValueType, typename HashTableType>
252  void fastDeleteAllValues(HashTableType& collection)
253  {
254  typedef typename HashTableType::const_iterator iterator;
255  iterator end = collection.end();
256  for (iterator it = collection.begin(); it != end; ++it)
257  fastDelete(*it);
258  }
259 
260  template<typename T, typename U, typename V>
261  inline void fastDeleteAllValues(const HashSet<T, U, V>& collection)
262  {
263  fastDeleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
264  }
265 
266  template<typename T, typename U, typename V, typename W>
267  inline void copyToVector(const HashSet<T, U, V>& collection, W& vector)
268  {
269  typedef typename HashSet<T, U, V>::const_iterator iterator;
270 
271  vector.resize(collection.size());
272 
273  iterator it = collection.begin();
274  iterator end = collection.end();
275  for (unsigned i = 0; it != end; ++it, ++i)
276  vector[i] = *it;
277  }
278 
279 } // namespace WTF
280 
281 using WTF::HashSet;
282 
283 #endif /* WTF_HashSet_h */
void deleteAllValues(const HashSet< Value, HashFunctions, Traits > &)
HashTableConstIteratorAdapter< HashTableType, ValueType > iterator
Definition: HashSet.h:52
static unsigned hash(const T &key)
Definition: HashSet.h:104
pair< iterator, bool > add(const ValueType &)
Definition: HashSet.h:186
ValueTraits::TraitType ValueType
Definition: HashSet.h:45
HashTableConstIteratorAdapter< HashTableType, ValueType > const_iterator
Definition: HashSet.h:53
GLint location
Definition: glew.h:1819
int capacity() const
Definition: HashSet.h:125
Definition: HashSet.h:103
GLdouble GLdouble t
Definition: glew.h:1382
void copyToVector(const HashSet< T, U, V > &collection, W &vector)
Definition: HashSet.h:267
GLdouble GLdouble GLdouble b
Definition: glew.h:7885
GLuint GLuint end
Definition: glew.h:1237
bool contains(const ValueType &) const
Definition: HashSet.h:155
iterator find(const ValueType &) const
Definition: HashSet.h:149
GLsizei const GLfloat * value
Definition: glew.h:1833
void swap(HashSet &)
Definition: HashSet.h:113
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8272
int size() const
Definition: HashSet.h:119
void clear()
Definition: HashSet.h:231
void remove(const ValueType &)
Definition: HashSet.h:225
static bool equal(const ValueType &a, const T &b)
Definition: HashSet.h:105
iterator begin() const
Definition: HashSet.h:137
void fastDeleteAllValues(const HashSet< Value, HashFunctions, Traits > &)
static const T & extract(const T &t)
Definition: HashSet.h:99
Definition: HashSet.h:29
Definition: HashSet.h:27
iterator end() const
Definition: HashSet.h:143
Definition: HashSet.h:35
bool isEmpty() const
Definition: HashSet.h:131
static void translate(ValueType &location, const T &key, const T &, unsigned hashCode)
Definition: HashSet.h:106