• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

WTF

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

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal