hash.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (c) 2000, 2010, Oracle and/or its affiliates.
  2. Copyright (c) 2011, 2013, Monty Program Ab.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. /* Dynamic hashing of record with different key-length */
  14. #ifndef _hash_h
  15. #define _hash_h
  16. #include "my_global.h" /* uchar */
  17. #include "my_sys.h" /* DYNAMIC_ARRAY */
  18. /*
  19. This forward declaration is used from C files where the real
  20. definition is included before. Since C does not allow repeated
  21. typedef declarations, even when identical, the definition may not be
  22. repeated.
  23. */
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /*
  28. Overhead to store an element in hash
  29. Can be used to approximate memory consumption for a hash
  30. */
  31. #define HASH_OVERHEAD (sizeof(char*)*2)
  32. /* flags for hash_init */
  33. #define HASH_UNIQUE 1 /* hash_insert fails on duplicate key */
  34. #define HASH_THREAD_SPECIFIC 2 /* Mark allocated memory THREAD_SPECIFIC */
  35. typedef uint my_hash_value_type;
  36. typedef uchar *(*my_hash_get_key)(const uchar *,size_t*,my_bool);
  37. typedef my_hash_value_type (*my_hash_function)(CHARSET_INFO *,
  38. const uchar *, size_t);
  39. typedef void (*my_hash_free_key)(void *);
  40. typedef my_bool (*my_hash_walk_action)(void *,void *);
  41. typedef struct st_hash {
  42. size_t key_offset,key_length; /* Length of key if const length */
  43. size_t blength;
  44. ulong records;
  45. uint flags;
  46. DYNAMIC_ARRAY array; /* Place for hash_keys */
  47. my_hash_get_key get_key;
  48. my_hash_function hash_function;
  49. void (*free)(void *);
  50. CHARSET_INFO *charset;
  51. } HASH;
  52. /* A search iterator state */
  53. typedef uint HASH_SEARCH_STATE;
  54. #define my_hash_init(A,B,C,D,E,F,G,H) my_hash_init2(A,0,B,C,D,E,F,0,G,H)
  55. my_bool my_hash_init2(HASH *hash, uint growth_size, CHARSET_INFO *charset,
  56. ulong default_array_elements, size_t key_offset,
  57. size_t key_length, my_hash_get_key get_key,
  58. my_hash_function hash_function,
  59. void (*free_element)(void*),
  60. uint flags);
  61. void my_hash_free(HASH *tree);
  62. void my_hash_reset(HASH *hash);
  63. uchar *my_hash_element(HASH *hash, ulong idx);
  64. uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
  65. uchar *my_hash_search_using_hash_value(const HASH *info,
  66. my_hash_value_type hash_value,
  67. const uchar *key, size_t length);
  68. my_hash_value_type my_hash_sort(CHARSET_INFO *cs,
  69. const uchar *key, size_t length);
  70. #define my_calc_hash(A, B, C) my_hash_sort((A)->charset, B, C)
  71. uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
  72. HASH_SEARCH_STATE *state);
  73. uchar *my_hash_first_from_hash_value(const HASH *info,
  74. my_hash_value_type hash_value,
  75. const uchar *key,
  76. size_t length,
  77. HASH_SEARCH_STATE *state);
  78. uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
  79. HASH_SEARCH_STATE *state);
  80. my_bool my_hash_insert(HASH *info, const uchar *data);
  81. my_bool my_hash_delete(HASH *hash, uchar *record);
  82. my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key,
  83. size_t old_key_length);
  84. void my_hash_replace(HASH *hash, HASH_SEARCH_STATE *state, uchar *new_row);
  85. my_bool my_hash_check(HASH *hash); /* Only in debug library */
  86. my_bool my_hash_iterate(HASH *hash, my_hash_walk_action action, void *argument);
  87. #define my_hash_clear(H) bzero((char*) (H), sizeof(*(H)))
  88. #define my_hash_inited(H) ((H)->blength != 0)
  89. #define my_hash_init_opt(A,B,C,D,E,F,G,H) \
  90. (!my_hash_inited(A) && my_hash_init(A,B,C,D,E,F,G,H))
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif