hash.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: hash.h
  3. // Purpose: interface of wxHashTable
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxHashTable
  9. @deprecated
  10. Please note that this class is retained for backward compatibility
  11. reasons; you should use wxHashMap.
  12. This class provides hash table functionality for wxWidgets, and for an
  13. application if it wishes. Data can be hashed on an integer or string key.
  14. Example:
  15. @code
  16. wxHashTable table(wxKEY_STRING);
  17. wxPoint *point = new wxPoint(100, 200);
  18. table.Put("point 1", point);
  19. ....
  20. wxPoint *found_point = (wxPoint *)table.Get("point 1");
  21. @endcode
  22. A hash table is implemented as an array of pointers to lists.
  23. When no data has been stored, the hash table takes only a little more space
  24. than this array (default size is 1000). When a data item is added, an integer
  25. is constructed from the integer or string key that is within the bounds of the array.
  26. If the array element is @NULL, a new (keyed) list is created for the element.
  27. Then the data object is appended to the list, storing the key in case other
  28. data objects need to be stored in the list also (when a 'collision' occurs).
  29. Retrieval involves recalculating the array index from the key, and searching
  30. along the keyed list for the data object whose stored key matches the passed key.
  31. Obviously this is quicker when there are fewer collisions, so hashing will
  32. become inefficient if the number of items to be stored greatly exceeds the
  33. size of the hash table.
  34. @library{wxbase}
  35. @category{containers}
  36. @see wxList
  37. */
  38. class wxHashTable : public wxObject
  39. {
  40. public:
  41. /**
  42. Constructor. @a key_type is one of wxKEY_INTEGER, or wxKEY_STRING,
  43. and indicates what sort of keying is required. @a size is optional.
  44. */
  45. wxHashTable(wxKeyType key_type = wxKEY_INTEGER, size_t size = 1000);
  46. /**
  47. Destroys the hash table.
  48. */
  49. virtual ~wxHashTable();
  50. /**
  51. The counterpart of Next(). If the application wishes to iterate
  52. through all the data in the hash table, it can call BeginFind() and
  53. then loop on Next().
  54. */
  55. void BeginFind();
  56. /**
  57. Clears the hash table of all nodes (but as usual, doesn't delete user data).
  58. */
  59. void Clear();
  60. //@{
  61. /**
  62. Deletes entry in hash table and returns the user's data (if found).
  63. */
  64. wxObject* Delete(long key);
  65. wxObject* Delete(const wxString& key);
  66. //@}
  67. /**
  68. If set to @true data stored in hash table will be deleted when hash table
  69. object is destroyed.
  70. */
  71. void DeleteContents(bool flag);
  72. //@{
  73. /**
  74. Gets data from the hash table, using an integer or string key
  75. (depending on which has table constructor was used).
  76. */
  77. wxObject* Get(long key);
  78. wxObject* Get(const char* key);
  79. //@}
  80. /**
  81. Returns the number of elements in the hash table.
  82. */
  83. size_t GetCount() const;
  84. /**
  85. Makes an integer key out of a string. An application may wish to make a key
  86. explicitly (for instance when combining two data values to form a key).
  87. */
  88. static long MakeKey(const wxString& string);
  89. /**
  90. If the application wishes to iterate through all the data in the hash
  91. table, it can call BeginFind() and then loop on Next(). This function
  92. returns a @b wxHashTable::Node pointer (or @NULL if there are no more nodes).
  93. The return value is functionally equivalent to @b wxNode but might not be
  94. implemented as a @b wxNode. The user will probably only wish to use the
  95. wxNode::GetData() method to retrieve the data; the node may also be deleted.
  96. */
  97. wxHashTable::Node* Next();
  98. //@{
  99. /**
  100. Inserts data into the hash table, using an integer or string key (depending on
  101. which has table constructor was used).
  102. The key string is copied and stored by the hash table implementation.
  103. */
  104. void Put(long key, wxObject* object);
  105. void Put(const char* key, wxObject* object);
  106. //@}
  107. };