hashset.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: hashset.h
  3. // Purpose: interface of wxHashSet
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxHashSet
  9. This is a simple, type-safe, and reasonably efficient hash set class,
  10. whose interface is a subset of the interface of STL containers.
  11. The interface is similar to std::tr1::hash_set or std::set classes but
  12. notice that, unlike std::set, the contents of a hash set is not sorted.
  13. Example:
  14. @code
  15. class MyClass { ... };
  16. // same, with MyClass* keys (only uses pointer equality!)
  17. WX_DECLARE_HASH_SET( MyClass*, wxPointerHash, wxPointerEqual, MySet1 );
  18. // same, with int keys
  19. WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual, MySet2 );
  20. // declare a hash set with string keys
  21. WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, MySet3 );
  22. MySet1 h1;
  23. MySet2 h1;
  24. MySet3 h3;
  25. // store and retrieve values
  26. h1.insert( new MyClass( 1 ) );
  27. h3.insert( "foo" );
  28. h3.insert( "bar" );
  29. h3.insert( "baz" );
  30. int size = h3.size(); // now is three
  31. bool has_foo = h3.find( "foo" ) != h3.end();
  32. h3.insert( "bar" ); // still has size three
  33. // iterate over all the elements in the class
  34. MySet3::iterator it;
  35. for( it = h3.begin(); it != h3.end(); ++it )
  36. {
  37. wxString key = *it;
  38. // do something useful with key
  39. }
  40. @endcode
  41. @section hashset_declaringnew Declaring new hash set types
  42. @code
  43. WX_DECLARE_HASH_SET( KEY_T, // type of the keys
  44. HASH_T, // hasher
  45. KEY_EQ_T, // key equality predicate
  46. CLASSNAME); // name of the class
  47. @endcode
  48. The HASH_T and KEY_EQ_T are the types used for the hashing function and key
  49. comparison. wxWidgets provides three predefined hashing functions:
  50. wxIntegerHash for integer types ( int, long, short, and their unsigned counterparts ),
  51. wxStringHash for strings ( wxString, wxChar*, char* ), and wxPointerHash for
  52. any kind of pointer.
  53. Similarly three equality predicates: wxIntegerEqual, wxStringEqual, wxPointerEqual
  54. are provided. Using this you could declare a hash set using int values like this:
  55. @code
  56. WX_DECLARE_HASH_SET( int,
  57. wxIntegerHash,
  58. wxIntegerEqual,
  59. MySet );
  60. // using an user-defined class for keys
  61. class MyKey { ... };
  62. // hashing function
  63. class MyKeyHash
  64. {
  65. public:
  66. MyKeyHash() { }
  67. unsigned long operator()( const MyKey& k ) const
  68. {
  69. // compute the hash
  70. }
  71. MyKeyHash& operator=(const MyKeyHash&) { return *this; }
  72. };
  73. // comparison operator
  74. class MyKeyEqual
  75. {
  76. public:
  77. MyKeyEqual() { }
  78. bool operator()( const MyKey& a, const MyKey& b ) const
  79. {
  80. // compare for equality
  81. }
  82. MyKeyEqual& operator=(const MyKeyEqual&) { return *this; }
  83. };
  84. WX_DECLARE_HASH_SET( MyKey, // type of the keys
  85. MyKeyHash, // hasher
  86. MyKeyEqual, // key equality predicate
  87. CLASSNAME); // name of the class
  88. @endcode
  89. @section hashset_types Types
  90. In the documentation below you should replace wxHashSet with the name you
  91. used in the class declaration.
  92. - wxHashSet::key_type: Type of the hash keys
  93. - wxHashSet::mapped_type: Type of hash keys
  94. - wxHashSet::value_type: Type of hash keys
  95. - wxHashSet::iterator: Used to enumerate all the elements in a hash set;
  96. it is similar to a value_type*
  97. - wxHashSet::const_iterator: Used to enumerate all the elements in a constant
  98. hash set; it is similar to a const value_type*
  99. - wxHashSet::size_type: Used for sizes
  100. - wxHashSet::Insert_Result: The return value for insert()
  101. @section hashset_iter Iterators
  102. An iterator is similar to a pointer, and so you can use the usual pointer
  103. operations: ++it ( and it++ ) to move to the next element, *it to access the
  104. element pointed to, *it to access the value of the element pointed to.
  105. Hash sets provide forward only iterators, this means that you can't use --it,
  106. it + 3, it1 - it2.
  107. @library{wxbase}
  108. @category{containers}
  109. */
  110. class wxHashSet
  111. {
  112. public:
  113. /**
  114. The size parameter is just a hint, the table will resize automatically
  115. to preserve performance.
  116. */
  117. wxHashSet(size_type size = 10);
  118. /**
  119. Copy constructor.
  120. */
  121. wxHashSet(const wxHashSet& set);
  122. //@{
  123. /**
  124. Returns an iterator pointing at the first element of the hash set.
  125. Please remember that hash sets do not guarantee ordering.
  126. */
  127. const_iterator begin() const;
  128. iterator begin();
  129. //@}
  130. /**
  131. Removes all elements from the hash set.
  132. */
  133. void clear();
  134. /**
  135. Counts the number of elements with the given key present in the set.
  136. This function returns only 0 or 1.
  137. */
  138. size_type count(const key_type& key) const;
  139. /**
  140. Returns @true if the hash set does not contain any elements, @false otherwise.
  141. */
  142. bool empty() const;
  143. //@{
  144. /**
  145. Returns an iterator pointing at the one-after-the-last element of the hash set.
  146. Please remember that hash sets do not guarantee ordering.
  147. */
  148. const_iterator end() const;
  149. iterator end();
  150. //@}
  151. /**
  152. Erases the element with the given key, and returns the number of elements
  153. erased (either 0 or 1).
  154. */
  155. size_type erase(const key_type& key);
  156. //@{
  157. /**
  158. Erases the element pointed to by the iterator. After the deletion
  159. the iterator is no longer valid and must not be used.
  160. */
  161. void erase(iterator it);
  162. void erase(const_iterator it);
  163. //@}
  164. //@{
  165. /**
  166. If an element with the given key is present, the functions returns
  167. an iterator pointing at that element, otherwise an invalid iterator
  168. is returned.
  169. i.e.
  170. @code
  171. hashset.find( non_existent_key ) == hashset.end()
  172. @endcode
  173. */
  174. iterator find(const key_type& key) const;
  175. const_iterator find(const key_type& key) const;
  176. //@}
  177. /**
  178. Inserts the given value in the hash set.
  179. The return value is equivalent to a
  180. @code std::pair<wxHashMap::iterator, bool> @endcode
  181. The iterator points to the inserted element, the boolean value is @true
  182. if @a v was actually inserted.
  183. */
  184. Insert_Result insert(const value_type& v);
  185. /**
  186. Returns the number of elements in the set.
  187. */
  188. size_type size() const;
  189. };