hashmap.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: hashmap.h
  3. // Purpose: interface of wxHashMap
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxHashMap
  9. This is a simple, type-safe, and reasonably efficient hash map class,
  10. whose interface is a subset of the interface of STL containers.
  11. In particular, the interface is modelled after std::map, and the various,
  12. non-standard, std::hash_map (http://www.cppreference.com/wiki/stl/map/start).
  13. Example:
  14. @code
  15. class MyClass { ... };
  16. // declare a hash map with string keys and int values
  17. WX_DECLARE_STRING_HASH_MAP( int, MyHash5 );
  18. // same, with int keys and MyClass* values
  19. WX_DECLARE_HASH_MAP( int, MyClass*, wxIntegerHash, wxIntegerEqual, MyHash1 );
  20. // same, with wxString keys and int values
  21. WX_DECLARE_STRING_HASH_MAP( int, MyHash3 );
  22. // same, with wxString keys and values
  23. WX_DECLARE_STRING_HASH_MAP( wxString, MyHash2 );
  24. MyHash1 h1;
  25. MyHash2 h2;
  26. // store and retrieve values
  27. h1[1] = new MyClass( 1 );
  28. h1[10000000] = NULL;
  29. h1[50000] = new MyClass( 2 );
  30. h2["Bill"] = "ABC";
  31. wxString tmp = h2["Bill"];
  32. // since element with key "Joe" is not present, this will return
  33. // the default value, which is an empty string in the case of wxString
  34. MyClass tmp2 = h2["Joe"];
  35. // iterate over all the elements in the class
  36. MyHash2::iterator it;
  37. for( it = h2.begin(); it != h2.end(); ++it )
  38. {
  39. wxString key = it->first, value = it->second;
  40. // do something useful with key and value
  41. }
  42. @endcode
  43. @section hashmap_declaringnew Declaring new hash table types
  44. @code
  45. WX_DECLARE_STRING_HASH_MAP( VALUE_T, // type of the values
  46. CLASSNAME ); // name of the class
  47. @endcode
  48. Declares a hash map class named CLASSNAME, with wxString keys and VALUE_T values.
  49. @code
  50. WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, // type of the values
  51. CLASSNAME ); // name of the class
  52. @endcode
  53. Declares a hash map class named CLASSNAME, with void* keys and VALUE_T values.
  54. @code
  55. WX_DECLARE_HASH_MAP( KEY_T, // type of the keys
  56. VALUE_T, // type of the values
  57. HASH_T, // hasher
  58. KEY_EQ_T, // key equality predicate
  59. CLASSNAME); // name of the class
  60. @endcode
  61. The HASH_T and KEY_EQ_T are the types used for the hashing function and
  62. key comparison. wxWidgets provides three predefined hashing functions:
  63. @c wxIntegerHash for integer types ( int, long, short, and their unsigned counterparts ),
  64. @c wxStringHash for strings ( wxString, wxChar*, char* ), and @c wxPointerHash for
  65. any kind of pointer.
  66. Similarly three equality predicates: @c wxIntegerEqual, @c wxStringEqual,
  67. @c wxPointerEqual are provided.
  68. Using this you could declare a hash map mapping int values to wxString like this:
  69. @code
  70. WX_DECLARE_HASH_MAP( int,
  71. wxString,
  72. wxIntegerHash,
  73. wxIntegerEqual,
  74. MyHash );
  75. // using an user-defined class for keys
  76. class MyKey { ... };
  77. // hashing function
  78. class MyKeyHash
  79. {
  80. public:
  81. MyKeyHash() { }
  82. unsigned long operator()( const MyKey& k ) const
  83. {
  84. // compute the hash
  85. }
  86. MyKeyHash& operator=(const MyKeyHash&) { return *this; }
  87. };
  88. // comparison operator
  89. class MyKeyEqual
  90. {
  91. public:
  92. MyKeyEqual() { }
  93. bool operator()( const MyKey& a, const MyKey& b ) const
  94. {
  95. // compare for equality
  96. }
  97. MyKeyEqual& operator=(const MyKeyEqual&) { return *this; }
  98. };
  99. WX_DECLARE_HASH_MAP( MyKey, // type of the keys
  100. SOME_TYPE, // any type you like
  101. MyKeyHash, // hasher
  102. MyKeyEqual, // key equality predicate
  103. CLASSNAME); // name of the class
  104. @endcode
  105. @section hashmap_types Types
  106. In the documentation below you should replace wxHashMap with the name you used
  107. in the class declaration.
  108. - wxHashMap::key_type: Type of the hash keys.
  109. - wxHashMap::mapped_type: Type of the values stored in the hash map.
  110. - wxHashMap::value_type: Equivalent to struct { key_type first; mapped_type second }.
  111. - wxHashMap::iterator: Used to enumerate all the elements in a hash map;
  112. it is similar to a value_type*.
  113. - wxHashMap::const_iterator: Used to enumerate all the elements in a constant
  114. hash map; it is similar to a const value_type*.
  115. - wxHashMap::size_type: Used for sizes.
  116. - wxHashMap::Insert_Result: The return value for insert().
  117. @section hashmap_iter Iterators
  118. An iterator is similar to a pointer, and so you can use the usual pointer operations:
  119. ++it ( and it++ ) to move to the next element, *it to access the element pointed to,
  120. it->first ( it->second ) to access the key ( value ) of the element pointed to.
  121. Hash maps provide forward only iterators, this means that you can't use --it,
  122. it + 3, it1 - it2.
  123. @section hashmap_predef Predefined hashmap types
  124. wxWidgets defines the following hashmap types:
  125. - wxLongToLongHashMap (uses long both for keys and values)
  126. - wxStringToStringHashMap (uses wxString both for keys and values)
  127. @library{wxbase}
  128. @category{containers}
  129. */
  130. class wxHashMap
  131. {
  132. public:
  133. /**
  134. The size parameter is just a hint, the table will resize automatically
  135. to preserve performance.
  136. */
  137. wxHashMap(size_type size = 10);
  138. /**
  139. Copy constructor.
  140. */
  141. wxHashMap(const wxHashMap& map);
  142. //@{
  143. /**
  144. Returns an iterator pointing at the first element of the hash map.
  145. Please remember that hash maps do not guarantee ordering.
  146. */
  147. const_iterator begin() const;
  148. iterator begin();
  149. //@}
  150. /**
  151. Removes all elements from the hash map.
  152. */
  153. void clear();
  154. /**
  155. Counts the number of elements with the given key present in the map.
  156. This function returns only 0 or 1.
  157. */
  158. size_type count(const key_type& key) const;
  159. /**
  160. Returns @true if the hash map does not contain any elements, @false otherwise.
  161. */
  162. bool empty() const;
  163. //@{
  164. /**
  165. Returns an iterator pointing at the one-after-the-last element of the hash map.
  166. Please remember that hash maps do not guarantee ordering.
  167. */
  168. const_iterator end() const;
  169. iterator end();
  170. //@}
  171. //@{
  172. /**
  173. Erases the element with the given key, and returns the number of elements
  174. erased (either 0 or 1).
  175. */
  176. size_type erase(const key_type& key);
  177. /**
  178. Erases the element pointed to by the iterator. After the deletion
  179. the iterator is no longer valid and must not be used.
  180. */
  181. void erase(iterator it);
  182. void erase(const_iterator it);
  183. //@}
  184. //@{
  185. /**
  186. If an element with the given key is present, the functions returns an
  187. iterator pointing at that element, otherwise an invalid iterator is
  188. returned.
  189. @code
  190. hashmap.find( non_existent_key ) == hashmap.end()
  191. @endcode
  192. */
  193. iterator find(const key_type& key) const;
  194. const_iterator find(const key_type& key) const;
  195. //@}
  196. /**
  197. Inserts the given value in the hash map.
  198. The return value is equivalent to a
  199. @code std::pair<wxHashMap::iterator, bool> @endcode
  200. The iterator points to the inserted element, the boolean value is @true
  201. if @a v was actually inserted.
  202. */
  203. Insert_Result insert(const value_type& v);
  204. /**
  205. Use the key as an array subscript.
  206. The only difference is that if the given key is not present in the hash map,
  207. an element with the default @c value_type() is inserted in the table.
  208. */
  209. mapped_type operator[](const key_type& key);
  210. /**
  211. Returns the number of elements in the map.
  212. */
  213. size_type size() const;
  214. };