@@ -31,20 +31,9 @@ typedef struct {
31
31
32
32
Py_uhash_t key_hash ;
33
33
void * key ;
34
- /* data (data_size bytes) follows */
34
+ void * value ;
35
35
} _Py_hashtable_entry_t ;
36
36
37
- #define _Py_HASHTABLE_ENTRY_PDATA (ENTRY ) \
38
- ((const void *)((char *)(ENTRY) \
39
- + sizeof(_Py_hashtable_entry_t)))
40
-
41
- #define _Py_HASHTABLE_ENTRY_READ_DATA (TABLE , ENTRY , DATA ) \
42
- do { \
43
- assert(sizeof(DATA) == (TABLE)->data_size); \
44
- memcpy(&(DATA), _Py_HASHTABLE_ENTRY_PDATA((ENTRY)), \
45
- sizeof(DATA)); \
46
- } while (0)
47
-
48
37
49
38
/* _Py_hashtable: prototypes */
50
39
@@ -55,12 +44,8 @@ typedef struct _Py_hashtable_t _Py_hashtable_t;
55
44
typedef Py_uhash_t (* _Py_hashtable_hash_func ) (const void * key );
56
45
typedef int (* _Py_hashtable_compare_func ) (const void * key1 , const void * key2 );
57
46
typedef void (* _Py_hashtable_destroy_func ) (void * key );
58
- typedef void (* _Py_hashtable_value_destroy_func ) (_Py_hashtable_t * ht ,
59
- _Py_hashtable_entry_t * entry );
60
47
typedef _Py_hashtable_entry_t * (* _Py_hashtable_get_entry_func )(_Py_hashtable_t * ht ,
61
48
const void * key );
62
- typedef int (* _Py_hashtable_get_func ) (_Py_hashtable_t * ht ,
63
- const void * key , void * data );
64
49
65
50
typedef struct {
66
51
/* allocate a memory block */
@@ -76,14 +61,12 @@ struct _Py_hashtable_t {
76
61
size_t num_buckets ;
77
62
size_t entries ; /* Total number of entries in the table. */
78
63
_Py_slist_t * buckets ;
79
- size_t data_size ;
80
64
81
- _Py_hashtable_get_func get_func ;
82
65
_Py_hashtable_get_entry_func get_entry_func ;
83
66
_Py_hashtable_hash_func hash_func ;
84
67
_Py_hashtable_compare_func compare_func ;
85
68
_Py_hashtable_destroy_func key_destroy_func ;
86
- _Py_hashtable_value_destroy_func value_destroy_func ;
69
+ _Py_hashtable_destroy_func value_destroy_func ;
87
70
_Py_hashtable_allocator_t alloc ;
88
71
};
89
72
@@ -96,95 +79,66 @@ PyAPI_FUNC(int) _Py_hashtable_compare_direct(
96
79
const void * key2 );
97
80
98
81
PyAPI_FUNC (_Py_hashtable_t * ) _Py_hashtable_new (
99
- size_t data_size ,
100
82
_Py_hashtable_hash_func hash_func ,
101
83
_Py_hashtable_compare_func compare_func );
102
84
103
85
PyAPI_FUNC (_Py_hashtable_t * ) _Py_hashtable_new_full (
104
- size_t data_size ,
105
- size_t init_size ,
106
86
_Py_hashtable_hash_func hash_func ,
107
87
_Py_hashtable_compare_func compare_func ,
108
88
_Py_hashtable_destroy_func key_destroy_func ,
109
- _Py_hashtable_value_destroy_func value_destroy_func ,
89
+ _Py_hashtable_destroy_func value_destroy_func ,
110
90
_Py_hashtable_allocator_t * allocator );
111
91
112
92
PyAPI_FUNC (void ) _Py_hashtable_destroy (_Py_hashtable_t * ht );
113
93
114
94
PyAPI_FUNC (void ) _Py_hashtable_clear (_Py_hashtable_t * ht );
115
95
116
96
typedef int (* _Py_hashtable_foreach_func ) (_Py_hashtable_t * ht ,
117
- _Py_hashtable_entry_t * entry ,
118
- void * arg );
97
+ const void * key , const void * value ,
98
+ void * user_data );
119
99
120
100
/* Call func() on each entry of the hashtable.
121
101
Iteration stops if func() result is non-zero, in this case it's the result
122
102
of the call. Otherwise, the function returns 0. */
123
103
PyAPI_FUNC (int ) _Py_hashtable_foreach (
124
104
_Py_hashtable_t * ht ,
125
105
_Py_hashtable_foreach_func func ,
126
- void * arg );
106
+ void * user_data );
127
107
128
- PyAPI_FUNC (size_t ) _Py_hashtable_size (_Py_hashtable_t * ht );
108
+ PyAPI_FUNC (size_t ) _Py_hashtable_size (const _Py_hashtable_t * ht );
129
109
130
110
/* Add a new entry to the hash. The key must not be present in the hash table.
131
- Return 0 on success, -1 on memory error.
132
-
133
- Don't call directly this function,
134
- but use _Py_HASHTABLE_SET() and _Py_HASHTABLE_SET_NODATA() macros */
111
+ Return 0 on success, -1 on memory error. */
135
112
PyAPI_FUNC (int ) _Py_hashtable_set (
136
113
_Py_hashtable_t * ht ,
137
114
const void * key ,
138
- size_t data_size ,
139
- const void * data );
140
-
141
- #define _Py_HASHTABLE_SET (TABLE , KEY , DATA ) \
142
- _Py_hashtable_set(TABLE, (KEY), sizeof(DATA), &(DATA))
143
-
144
- #define _Py_HASHTABLE_SET_NODATA (TABLE , KEY ) \
145
- _Py_hashtable_set(TABLE, (KEY), 0, NULL)
115
+ void * value );
146
116
147
117
148
118
/* Get an entry.
149
- Return NULL if the key does not exist.
150
-
151
- Don't call directly this function, but use _Py_HASHTABLE_GET_ENTRY()
152
- macro */
119
+ Return NULL if the key does not exist. */
153
120
static inline _Py_hashtable_entry_t *
154
121
_Py_hashtable_get_entry (_Py_hashtable_t * ht , const void * key )
155
122
{
156
123
return ht -> get_entry_func (ht , key );
157
124
}
158
125
159
- #define _Py_HASHTABLE_GET_ENTRY (TABLE , KEY ) \
160
- _Py_hashtable_get_entry(TABLE, (const void *)(KEY))
161
126
127
+ /* Get value from an entry.
128
+ Return NULL if the entry is not found.
162
129
163
- /* Get data from an entry. Copy entry data into data and return 1 if the entry
164
- exists, return 0 if the entry does not exist.
130
+ Use _Py_hashtable_get_entry() to distinguish entry value equal to NULL
131
+ and entry not found. */
132
+ extern void * _Py_hashtable_get (_Py_hashtable_t * ht , const void * key );
165
133
166
- Don't call directly this function, but use _Py_HASHTABLE_GET() macro */
167
- static inline int
168
- _Py_hashtable_get (_Py_hashtable_t * ht , const void * key ,
169
- size_t data_size , void * data )
170
- {
171
- assert (data_size == ht -> data_size );
172
- return ht -> get_func (ht , key , data );
173
- }
174
-
175
- #define _Py_HASHTABLE_GET (TABLE , KEY , DATA ) \
176
- _Py_hashtable_get(TABLE, (KEY), sizeof(DATA), &(DATA))
177
134
178
-
179
- /* Don't call directly this function, but use _Py_HASHTABLE_POP() macro */
180
- PyAPI_FUNC (int ) _Py_hashtable_pop (
135
+ // Remove a key and its associated value without calling key and value destroy
136
+ // functions.
137
+ // Return the removed value if the key was found.
138
+ // Return NULL if the key was not found.
139
+ PyAPI_FUNC (void * ) _Py_hashtable_steal (
181
140
_Py_hashtable_t * ht ,
182
- const void * key ,
183
- size_t data_size ,
184
- void * data );
185
-
186
- #define _Py_HASHTABLE_POP (TABLE , KEY , DATA ) \
187
- _Py_hashtable_pop(TABLE, (KEY), sizeof(DATA), &(DATA))
141
+ const void * key );
188
142
189
143
190
144
#ifdef __cplusplus
0 commit comments