php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Alexander Borisov
3 *
4 * Author: Alexander Borisov <borisov@lexbor.com>
5 */
6
7#ifndef LEXBOR_HASH_H
8#define LEXBOR_HASH_H
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#include "lexbor/core/dobject.h"
15#include "lexbor/core/mraw.h"
16
17
18#define LEXBOR_HASH_SHORT_SIZE 16
19#define LEXBOR_HASH_TABLE_MIN_SIZE 32
20
21
24
25#ifndef LEXBOR_HASH_EXTERN
29
33#endif
34
35/*
36 * FIXME:
37 * It is necessary to add the rebuild of a hash table
38 * and optimize collisions.
39 */
40
43
44typedef uint32_t
45(*lexbor_hash_id_f)(const lxb_char_t *key, size_t size);
46
49 const lxb_char_t *key, size_t size);
50
51typedef bool
52(*lexbor_hash_cmp_f)(const lxb_char_t *first,
53 const lxb_char_t *second, size_t size);
54
65
75
77 lexbor_hash_id_f hash; /* For generate a hash id. */
78 lexbor_hash_cmp_f cmp; /* For compare key. */
79 lexbor_hash_copy_f copy; /* For copy key. */
80};
81
83 lexbor_hash_id_f hash; /* For generate a hash id. */
84 lexbor_hash_cmp_f cmp; /* For compare key. */
85};
86
87
90
92lexbor_hash_init(lexbor_hash_t *hash, size_t table_size, size_t struct_size);
93
94LXB_API void
96
98lexbor_hash_destroy(lexbor_hash_t *hash, bool destroy_obj);
99
100
101LXB_API void *
103 const lxb_char_t *key, size_t length);
104
105LXB_API void *
107 const lexbor_hash_search_t *search,
108 const lxb_char_t *key, size_t length);
109
110LXB_API void
112 const lxb_char_t *key, size_t length);
113
114LXB_API void *
116 const lxb_char_t *key, size_t length);
117
118LXB_API void
120 const lxb_char_t *key, size_t length,
121 const lexbor_hash_cmp_f cmp_func);
122
123LXB_API void *
125 const lxb_char_t *key, size_t length,
126 const lexbor_hash_cmp_f cmp_func);
127
128
129LXB_API uint32_t
130lexbor_hash_make_id(const lxb_char_t *key, size_t length);
131
132LXB_API uint32_t
133lexbor_hash_make_id_lower(const lxb_char_t *key, size_t length);
134
135LXB_API uint32_t
136lexbor_hash_make_id_upper(const lxb_char_t *key, size_t length);
137
140 const lxb_char_t *key, size_t length);
141
144 const lxb_char_t *key, size_t length);
145
148 const lxb_char_t *key, size_t length);
149
150
151/*
152 * Inline functions
153 */
156{
157 return hash->mraw;
158}
159
162{
163 if (entry->length <= LEXBOR_HASH_SHORT_SIZE) {
164 return (lxb_char_t *) entry->u.short_str;
165 }
166
167 return entry->u.long_str;
168}
169
172 lxb_char_t *data, size_t length)
173{
174 entry->length = length;
175
176 if (length <= LEXBOR_HASH_SHORT_SIZE) {
177 memcpy(entry->u.short_str, data, length);
178 return (lxb_char_t *) entry->u.short_str;
179 }
180
181 entry->u.long_str = data;
182 return entry->u.long_str;
183}
184
185lxb_inline void
187{
188 if (entry->length > LEXBOR_HASH_SHORT_SIZE) {
189 lexbor_mraw_free(hash->mraw, entry->u.long_str);
190 }
191
192 entry->length = 0;
193}
194
200
206
207lxb_inline size_t
212
213
214#ifdef __cplusplus
215} /* extern "C" */
216#endif
217
218#endif /* LEXBOR_HASH_H */
uint32_t u
Definition cdf.c:78
#define LXB_API
Definition def.h:48
#define LXB_EXTERN
Definition def.h:55
void * lexbor_dobject_calloc(lexbor_dobject_t *dobject)
Definition dobject.c:123
void * lexbor_dobject_free(lexbor_dobject_t *dobject, void *data)
Definition dobject.c:135
lxb_inline size_t lexbor_dobject_allocated(lexbor_dobject_t *dobject)
Definition dobject.h:65
LXB_API const lexbor_hash_search_t * lexbor_hash_search_lower
Definition hash.c:66
LXB_API const lexbor_hash_search_t * lexbor_hash_search_raw
Definition hash.c:63
LXB_API const lexbor_hash_insert_t * lexbor_hash_insert_lower
Definition hash.c:41
LXB_API const lexbor_hash_search_t * lexbor_hash_search_upper
Definition hash.c:69
LXB_API const lexbor_hash_insert_t * lexbor_hash_insert_raw
Definition hash.c:38
LXB_API const lexbor_hash_insert_t * lexbor_hash_insert_upper
Definition hash.c:44
new_type size
Definition ffi.c:4365
memcpy(ptr1, ptr2, size)
lxb_inline lexbor_mraw_t * lexbor_hash_mraw(const lexbor_hash_t *hash)
Definition hash.h:155
LXB_API lexbor_hash_t * lexbor_hash_create(void)
Definition hash.c:114
LXB_API uint32_t lexbor_hash_make_id_upper(const lxb_char_t *key, size_t length)
Definition hash.c:381
lxb_status_t(* lexbor_hash_copy_f)(lexbor_hash_t *hash, lexbor_hash_entry_t *entry, const lxb_char_t *key, size_t size)
Definition hash.h:48
LXB_API void * lexbor_hash_search_by_hash_id(lexbor_hash_t *hash, uint32_t hash_id, const lxb_char_t *key, size_t length, const lexbor_hash_cmp_f cmp_func)
Definition hash.c:320
struct lexbor_hash_search lexbor_hash_search_t
Definition hash.h:22
LXB_API lexbor_hash_t * lexbor_hash_destroy(lexbor_hash_t *hash, bool destroy_obj)
Definition hash.c:168
lxb_inline lexbor_hash_entry_t * lexbor_hash_entry_destroy(lexbor_hash_t *hash, lexbor_hash_entry_t *entry)
Definition hash.h:202
LXB_API uint32_t lexbor_hash_make_id_lower(const lxb_char_t *key, size_t length)
Definition hash.c:362
bool(* lexbor_hash_cmp_f)(const lxb_char_t *first, const lxb_char_t *second, size_t size)
Definition hash.h:52
uint32_t(* lexbor_hash_id_f)(const lxb_char_t *key, size_t size)
Definition hash.h:45
LXB_API void lexbor_hash_clean(lexbor_hash_t *hash)
Definition hash.c:160
LXB_API void * lexbor_hash_search(lexbor_hash_t *hash, const lexbor_hash_search_t *search, const lxb_char_t *key, size_t length)
Definition hash.c:274
LXB_API lxb_status_t lexbor_hash_copy(lexbor_hash_t *hash, lexbor_hash_entry_t *entry, const lxb_char_t *key, size_t length)
Definition hash.c:400
lxb_inline lxb_char_t * lexbor_hash_entry_str(const lexbor_hash_entry_t *entry)
Definition hash.h:161
LXB_API uint32_t lexbor_hash_make_id(const lxb_char_t *key, size_t length)
Definition hash.c:343
struct lexbor_hash lexbor_hash_t
Definition hash.h:41
LXB_API void * lexbor_hash_insert(lexbor_hash_t *hash, const lexbor_hash_insert_t *insert, const lxb_char_t *key, size_t length)
Definition hash.c:186
lxb_inline size_t lexbor_hash_entries_count(lexbor_hash_t *hash)
Definition hash.h:208
LXB_API lxb_status_t lexbor_hash_copy_lower(lexbor_hash_t *hash, lexbor_hash_entry_t *entry, const lxb_char_t *key, size_t length)
Definition hash.c:425
lxb_inline void lexbor_hash_entry_str_free(lexbor_hash_t *hash, lexbor_hash_entry_t *entry)
Definition hash.h:186
lxb_inline lexbor_hash_entry_t * lexbor_hash_entry_create(lexbor_hash_t *hash)
Definition hash.h:196
LXB_API void lexbor_hash_remove_by_hash_id(lexbor_hash_t *hash, uint32_t hash_id, const lxb_char_t *key, size_t length, const lexbor_hash_cmp_f cmp_func)
Definition hash.c:282
lxb_inline lxb_char_t * lexbor_hash_entry_str_set(lexbor_hash_entry_t *entry, lxb_char_t *data, size_t length)
Definition hash.h:171
LXB_API void lexbor_hash_remove(lexbor_hash_t *hash, const lexbor_hash_search_t *search, const lxb_char_t *key, size_t length)
Definition hash.c:266
struct lexbor_hash_entry lexbor_hash_entry_t
Definition hash.h:42
struct lexbor_hash_insert lexbor_hash_insert_t
Definition hash.h:23
LXB_API lxb_status_t lexbor_hash_init(lexbor_hash_t *hash, size_t table_size, size_t struct_size)
Definition hash.c:120
LXB_API void * lexbor_hash_insert_by_entry(lexbor_hash_t *hash, lexbor_hash_entry_t *entry, const lexbor_hash_search_t *search, const lxb_char_t *key, size_t length)
Definition hash.c:226
#define LEXBOR_HASH_SHORT_SIZE
Definition hash.h:18
LXB_API lxb_status_t lexbor_hash_copy_upper(lexbor_hash_t *hash, lexbor_hash_entry_t *entry, const lxb_char_t *key, size_t length)
Definition hash.c:452
hash(string $algo, string $data, bool $binary=false, array $options=[])
Definition hash.stub.php:12
void * lexbor_mraw_free(lexbor_mraw_t *mraw, void *data)
Definition mraw.c:392
unsigned char key[REFLECTION_KEY_LEN]
zend_constant * data
Definition hash.h:55
size_t length
Definition hash.h:61
union lexbor_hash_entry::@035143052233045245065130050150326123154202375277 u
lxb_char_t short_str[LEXBOR_HASH_SHORT_SIZE+1]
Definition hash.h:58
lexbor_hash_entry_t * next
Definition hash.h:63
lxb_char_t * long_str
Definition hash.h:57
lexbor_hash_copy_f copy
Definition hash.h:79
lexbor_hash_cmp_f cmp
Definition hash.h:78
lexbor_hash_id_f hash
Definition hash.h:77
lexbor_hash_cmp_f cmp
Definition hash.h:84
lexbor_hash_id_f hash
Definition hash.h:83
lexbor_mraw_t * mraw
Definition hash.h:68
lexbor_dobject_t * entries
Definition hash.h:67
size_t struct_size
Definition hash.h:73
lexbor_hash_entry_t ** table
Definition hash.h:70
size_t table_size
Definition hash.h:71
unsigned int lxb_status_t
Definition types.h:28
#define lxb_inline
Definition types.h:21
unsigned char lxb_char_t
Definition types.h:27