This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hash_ptr.h
94 lines (80 loc) · 2.9 KB
/
hash_ptr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Oleg Grenrus <[email protected]> |
| See CREDITS for contributors |
| This defines hash_si_ptr. |
| It is like hash_si, but the key is always a non-zero zend_uintptr_t |
+----------------------------------------------------------------------+
*/
#ifndef HASH_PTR_H
#define HASH_PTR_H
#include <assert.h>
#ifdef PHP_WIN32
# include "ig_win32.h"
#else
# include <stdint.h> /* defines uint32_t etc */
#endif
#include <stddef.h>
#include "zend_types.h"
// NULL converted to an integer, on sane platforms.
#define HASH_PTR_KEY_INVALID 0
/** Key/value pair of hash_si_ptr.
* @author Oleg Grenrus <[email protected]>
* @see hash_si_ptr
*/
struct hash_si_ptr_pair
{
zend_uintptr_t key; /**< The key: The address of a pointer, casted to an int (won't be dereferenced). */
uint32_t value; /**< Value. */
};
/** Hash-array.
* Like c++ std::unordered_map<zend_uintptr_t, int32_t>, but does not allow HASH_PTR_KEY_INVALID as a key.
* Current implementation uses linear probing.
* @author Oleg Grenrus <[email protected]>
*/
struct hash_si_ptr {
size_t size; /**< Allocated size of array. */
size_t used; /**< Used size of array. */
struct hash_si_ptr_pair *data; /**< Pointer to array or pairs of data. */
};
/** Inits hash_si_ptr structure.
* @param h pointer to hash_si_ptr struct.
* @param size initial size of the hash array.
* @return 0 on success, 1 else.
*/
int hash_si_ptr_init(struct hash_si_ptr *h, size_t size);
/** Frees hash_si_ptr structure.
* Doesn't call free(h).
* @param h pointer to hash_si_ptr struct.
*/
void hash_si_ptr_deinit(struct hash_si_ptr *h);
/** Inserts value into hash_si_ptr.
* @param h Pointer to hash_si_ptr struct.
* @param key Pointer to key.
* @param key_len Key length.
* @param value Value.
* @return 0 on success, 1 or 2 else.
*/
int hash_si_ptr_insert (struct hash_si_ptr *h, const zend_uintptr_t key, uint32_t value);
/** Finds value from hash_si_ptr.
* Value returned thru value param.
* @param h Pointer to hash_si_ptr struct.
* @param key Pointer to key.
* @param key_len Key length.
* @param[out] value Found value.
* @return 0 if found, 1 if not.
*/
int hash_si_ptr_find (struct hash_si_ptr *h, const zend_uintptr_t key, uint32_t * value);
/** Returns size of hash_si_ptr.
* @param h Pointer to hash_si_ptr struct.
* @return Size of hash_si_ptr.
*/
size_t hash_si_ptr_size (struct hash_si_ptr *h);
/** Returns capacity of hash_si_ptr.
* @param h Pointer to hash_si_ptr struct.
* @return Capacity of hash_si_ptr.
*/
size_t hash_si_ptr_capacity (struct hash_si_ptr *h);
#endif /* HASH_PTR_H */