pycore_hashtable.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef Py_INTERNAL_HASHTABLE_H
  2. #define Py_INTERNAL_HASHTABLE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. /* Single linked list */
  10. typedef struct _Py_slist_item_s {
  11. struct _Py_slist_item_s *next;
  12. } _Py_slist_item_t;
  13. typedef struct {
  14. _Py_slist_item_t *head;
  15. } _Py_slist_t;
  16. #define _Py_SLIST_ITEM_NEXT(ITEM) _Py_RVALUE(((_Py_slist_item_t *)(ITEM))->next)
  17. #define _Py_SLIST_HEAD(SLIST) _Py_RVALUE(((_Py_slist_t *)(SLIST))->head)
  18. /* _Py_hashtable: table entry */
  19. typedef struct {
  20. /* used by _Py_hashtable_t.buckets to link entries */
  21. _Py_slist_item_t _Py_slist_item;
  22. Py_uhash_t key_hash;
  23. void *key;
  24. void *value;
  25. } _Py_hashtable_entry_t;
  26. /* _Py_hashtable: prototypes */
  27. /* Forward declaration */
  28. struct _Py_hashtable_t;
  29. typedef struct _Py_hashtable_t _Py_hashtable_t;
  30. typedef Py_uhash_t (*_Py_hashtable_hash_func) (const void *key);
  31. typedef int (*_Py_hashtable_compare_func) (const void *key1, const void *key2);
  32. typedef void (*_Py_hashtable_destroy_func) (void *key);
  33. typedef _Py_hashtable_entry_t* (*_Py_hashtable_get_entry_func)(_Py_hashtable_t *ht,
  34. const void *key);
  35. typedef struct {
  36. // Allocate a memory block
  37. void* (*malloc) (size_t size);
  38. // Release a memory block
  39. void (*free) (void *ptr);
  40. } _Py_hashtable_allocator_t;
  41. /* _Py_hashtable: table */
  42. struct _Py_hashtable_t {
  43. size_t nentries; // Total number of entries in the table
  44. size_t nbuckets;
  45. _Py_slist_t *buckets;
  46. _Py_hashtable_get_entry_func get_entry_func;
  47. _Py_hashtable_hash_func hash_func;
  48. _Py_hashtable_compare_func compare_func;
  49. _Py_hashtable_destroy_func key_destroy_func;
  50. _Py_hashtable_destroy_func value_destroy_func;
  51. _Py_hashtable_allocator_t alloc;
  52. };
  53. /* Hash a pointer (void*) */
  54. PyAPI_FUNC(Py_uhash_t) _Py_hashtable_hash_ptr(const void *key);
  55. /* Comparison using memcmp() */
  56. PyAPI_FUNC(int) _Py_hashtable_compare_direct(
  57. const void *key1,
  58. const void *key2);
  59. PyAPI_FUNC(_Py_hashtable_t *) _Py_hashtable_new(
  60. _Py_hashtable_hash_func hash_func,
  61. _Py_hashtable_compare_func compare_func);
  62. PyAPI_FUNC(_Py_hashtable_t *) _Py_hashtable_new_full(
  63. _Py_hashtable_hash_func hash_func,
  64. _Py_hashtable_compare_func compare_func,
  65. _Py_hashtable_destroy_func key_destroy_func,
  66. _Py_hashtable_destroy_func value_destroy_func,
  67. _Py_hashtable_allocator_t *allocator);
  68. PyAPI_FUNC(void) _Py_hashtable_destroy(_Py_hashtable_t *ht);
  69. PyAPI_FUNC(void) _Py_hashtable_clear(_Py_hashtable_t *ht);
  70. typedef int (*_Py_hashtable_foreach_func) (_Py_hashtable_t *ht,
  71. const void *key, const void *value,
  72. void *user_data);
  73. /* Call func() on each entry of the hashtable.
  74. Iteration stops if func() result is non-zero, in this case it's the result
  75. of the call. Otherwise, the function returns 0. */
  76. PyAPI_FUNC(int) _Py_hashtable_foreach(
  77. _Py_hashtable_t *ht,
  78. _Py_hashtable_foreach_func func,
  79. void *user_data);
  80. PyAPI_FUNC(size_t) _Py_hashtable_size(const _Py_hashtable_t *ht);
  81. PyAPI_FUNC(size_t) _Py_hashtable_len(const _Py_hashtable_t *ht);
  82. /* Add a new entry to the hash. The key must not be present in the hash table.
  83. Return 0 on success, -1 on memory error. */
  84. PyAPI_FUNC(int) _Py_hashtable_set(
  85. _Py_hashtable_t *ht,
  86. const void *key,
  87. void *value);
  88. /* Get an entry.
  89. Return NULL if the key does not exist. */
  90. static inline _Py_hashtable_entry_t *
  91. _Py_hashtable_get_entry(_Py_hashtable_t *ht, const void *key)
  92. {
  93. return ht->get_entry_func(ht, key);
  94. }
  95. /* Get value from an entry.
  96. Return NULL if the entry is not found.
  97. Use _Py_hashtable_get_entry() to distinguish entry value equal to NULL
  98. and entry not found. */
  99. PyAPI_FUNC(void*) _Py_hashtable_get(_Py_hashtable_t *ht, const void *key);
  100. /* Remove a key and its associated value without calling key and value destroy
  101. functions.
  102. Return the removed value if the key was found.
  103. Return NULL if the key was not found. */
  104. PyAPI_FUNC(void*) _Py_hashtable_steal(
  105. _Py_hashtable_t *ht,
  106. const void *key);
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif /* !Py_INTERNAL_HASHTABLE_H */