Skip to content

Commit e691fb3

Browse files
updated khashp; no performance changes
1 parent de00f80 commit e691fb3

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

_python/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ EXE=run-test
33
all:$(EXE)
44

55
run-test:test.c ../common.c
6-
$(CC) -O3 -Wall `python3.12-config --includes` `python3.12-config --lib` $< -o $@
6+
$(CC) -O3 -Wall `python3.12-config --includes` `python3.12-config --libs` $< -o $@
77

88
clean:
99
rm -fr $(EXE)

ctl/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static inline aux aux_copy(aux *self) { return *self; }
1313
#define T aux
1414
#include "ctl/unordered_map.h"
1515

16-
// https://github.com/ludocode/pottery
16+
// https://github.com/rurban/ctl
1717
// 741bf5f, cloned on 2025-03-17
1818

1919
void test_int(uint32_t N, uint32_t n0, int32_t is_del, uint32_t x0, uint32_t n_cp, udb_checkpoint_t *cp)

khashp/khashp.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#include <string.h>
33
#include "khashp.h"
44

5+
#ifdef KHASHP_STATIC
6+
#define KHP_SCOPE static inline
7+
#else
8+
#define KHP_SCOPE
9+
#endif
10+
511
#define kh_max_count(cap) (((cap)>>1) + ((cap)>>2)) /* default load factor: 75% */
612

713
#define MALLOC(type, cnt) ((type*)malloc((cnt) * sizeof(type)))
@@ -31,7 +37,7 @@ static int khp_key_eq0(const void *key1, const void *key2, uint32_t key_len) //
3137
return memcmp(key1, key2, key_len) == 0;
3238
}
3339

34-
khashp_t *khp_init(uint32_t key_len, uint32_t val_len, khp_hash_fn_t fn, khp_key_eq_t eq)
40+
KHP_SCOPE khashp_t *khp_init(uint32_t key_len, uint32_t val_len, khp_hash_fn_t fn, khp_key_eq_t eq)
3541
{
3642
khashp_t *h = CALLOC(khashp_t, 1);
3743
h->key_len = key_len, h->val_len = val_len;
@@ -40,21 +46,21 @@ khashp_t *khp_init(uint32_t key_len, uint32_t val_len, khp_hash_fn_t fn, khp_key
4046
return h;
4147
}
4248

43-
void khp_destroy(khashp_t *h)
49+
KHP_SCOPE void khp_destroy(khashp_t *h)
4450
{
4551
if (h == 0) return;
4652
free(h->b); free(h);
4753
}
4854

49-
void khp_clear(khashp_t *h)
55+
KHP_SCOPE void khp_clear(khashp_t *h)
5056
{
5157
if (h == 0 || h->used == 0) return;
5258
khint_t n_buckets = (khint_t)1U << h->bits;
5359
memset(h->used, 0, __kh_fsize(n_buckets) * sizeof(uint32_t));
5460
h->count = 0;
5561
}
5662

57-
khint_t khp_get(const khashp_t *h, const void *key)
63+
KHP_SCOPE khint_t khp_get(const khashp_t *h, const void *key)
5864
{
5965
khint_t i, last, n_buckets, mask, hash;
6066
if (h->b == 0) return 0;
@@ -69,7 +75,7 @@ khint_t khp_get(const khashp_t *h, const void *key)
6975
return !__kh_used(h->used, i)? n_buckets : i;
7076
}
7177

72-
int khp_resize(khashp_t *h, khint_t new_n_buckets)
78+
KHP_SCOPE int khp_resize(khashp_t *h, khint_t new_n_buckets)
7379
{
7480
uint32_t *new_used = 0;
7581
uint8_t *tmp;
@@ -120,7 +126,7 @@ int khp_resize(khashp_t *h, khint_t new_n_buckets)
120126
return 0;
121127
}
122128

123-
khint_t khp_put(khashp_t *h, const void *key, int *absent)
129+
KHP_SCOPE khint_t khp_put(khashp_t *h, const void *key, int *absent)
124130
{
125131
khint_t n_buckets, i, last, mask, hash;
126132
n_buckets = h->b? (khint_t)1U<<h->bits : 0U;
@@ -146,7 +152,7 @@ khint_t khp_put(khashp_t *h, const void *key, int *absent)
146152
return i;
147153
}
148154

149-
int khp_del(khashp_t *h, khint_t i)
155+
KHP_SCOPE int khp_del(khashp_t *h, khint_t i)
150156
{
151157
khint_t j = i, k, mask, n_buckets;
152158
if (h->b == 0) return 0;
@@ -164,19 +170,19 @@ int khp_del(khashp_t *h, khint_t i)
164170
return 1;
165171
}
166172

167-
void khp_get_val(const khashp_t *h, khint_t i, void *v)
173+
KHP_SCOPE void khp_get_val(const khashp_t *h, khint_t i, void *v)
168174
{
169175
uint8_t *p = (uint8_t*)khp_get_bucket(h, i) + h->key_len;
170176
if (h->val_len > 0) memcpy(v, p, h->val_len);
171177
}
172178

173-
void khp_set_val(const khashp_t *h, khint_t i, const void *v)
179+
KHP_SCOPE void khp_set_val(const khashp_t *h, khint_t i, const void *v)
174180
{
175181
uint8_t *p = (uint8_t*)khp_get_bucket(h, i) + h->key_len;
176182
if (h->val_len > 0) memcpy(p, v, h->val_len);
177183
}
178184

179-
void khp_get_key(const khashp_t *h, khint_t i, void *p)
185+
KHP_SCOPE void khp_get_key(const khashp_t *h, khint_t i, void *p)
180186
{
181187
memcpy(p, khp_get_bucket(h, i), h->key_len);
182188
}
@@ -203,14 +209,14 @@ static int kh_str_key_eq(const void *s1, const void *s2, uint32_t key_len)
203209
return strcmp(p1, p2) == 0;
204210
}
205211

206-
khashp_t *khp_str_init(uint32_t val_len, int dup)
212+
KHP_SCOPE khashp_t *khp_str_init(uint32_t val_len, int dup)
207213
{
208214
khashp_t *h = khp_init(sizeof(void*), val_len, khp_str_hash_fn, kh_str_key_eq);
209215
h->dup = !!dup;
210216
return h;
211217
}
212218

213-
void khp_str_destroy(khashp_t *h)
219+
KHP_SCOPE void khp_str_destroy(khashp_t *h)
214220
{
215221
if (h->dup) {
216222
khint_t k;
@@ -223,12 +229,12 @@ void khp_str_destroy(khashp_t *h)
223229
khp_destroy(h);
224230
}
225231

226-
khint_t khp_str_get(const khashp_t *h, const char *key)
232+
KHP_SCOPE khint_t khp_str_get(const khashp_t *h, const char *key)
227233
{
228234
return khp_get(h, &key);
229235
}
230236

231-
khint_t khp_str_put(khashp_t *h, const char *key, int *absent)
237+
KHP_SCOPE khint_t khp_str_put(khashp_t *h, const char *key, int *absent)
232238
{
233239
khint_t k = khp_put(h, &key, absent);
234240
if (*absent) {
@@ -244,7 +250,7 @@ khint_t khp_str_put(khashp_t *h, const char *key, int *absent)
244250
return k;
245251
}
246252

247-
int khp_str_del(khashp_t *h, khint_t i)
253+
KHP_SCOPE int khp_str_del(khashp_t *h, khint_t i)
248254
{
249255
if (h->b == 0) return 0;
250256
if (h->dup) {

khashp/khashp.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __AC_KHASHP_H
22
#define __AC_KHASHP_H
33

4-
#define AC_VERSION_KHASHP_H "r34"
4+
#define AC_VERSION_KHASHP_H "r35"
55

66
#include <stddef.h>
77
#include <stdint.h>
@@ -25,6 +25,8 @@ typedef struct {
2525
extern "C" {
2626
#endif
2727

28+
#ifndef KHASHP_STATIC
29+
2830
/**
2931
* Initialize a hash table
3032
*
@@ -95,21 +97,6 @@ void khp_get_val(const khashp_t *h, khint_t i, void *v);
9597
/** Set a value */
9698
void khp_set_val(const khashp_t *h, khint_t i, const void *v);
9799

98-
/** Get the capacity of a hash table */
99-
static inline khint_t khp_capacity(const khashp_t *h) { return h->b? 1U<<h->bits : 0U; }
100-
101-
/** End "iterator" */
102-
static inline khint_t khp_end(const khashp_t *h) { return khp_capacity(h); }
103-
104-
/** Get the number of elements in a hash table */
105-
static inline khint_t khp_size(const khashp_t *h) { return h->count; }
106-
107-
/** Test whether a bucket is occupied */
108-
static inline int khp_exist(const khashp_t *h, khint_t x) { return h->used[x>>5] >> (x&0x1fU) & 1U; }
109-
110-
/** Iterate over a hash table */
111-
#define khp_foreach(h, x) for ((x) = 0; (x) != khp_end(h); ++(x)) if (khp_exist((h), (x)))
112-
113100
/**
114101
* Initialize a hash table with string keys
115102
*
@@ -138,6 +125,23 @@ khint_t khp_str_put(khashp_t *h, const char *key, int *absent);
138125
/** Delete an element from a string hash table */
139126
int khp_str_del(khashp_t *h, khint_t i);
140127

128+
#endif // ~KHASHP_STATIC
129+
130+
/** Get the capacity of a hash table */
131+
static inline khint_t khp_capacity(const khashp_t *h) { return h->b? 1U<<h->bits : 0U; }
132+
133+
/** End "iterator" */
134+
static inline khint_t khp_end(const khashp_t *h) { return khp_capacity(h); }
135+
136+
/** Get the number of elements in a hash table */
137+
static inline khint_t khp_size(const khashp_t *h) { return h->count; }
138+
139+
/** Test whether a bucket is occupied */
140+
static inline int khp_exist(const khashp_t *h, khint_t x) { return h->used[x>>5] >> (x&0x1fU) & 1U; }
141+
142+
/** Iterate over a hash table */
143+
#define khp_foreach(h, x) for ((x) = 0; (x) != khp_end(h); ++(x)) if (khp_exist((h), (x)))
144+
141145
static inline void *khp_get_bucket(const khashp_t *h, khint_t i)
142146
{
143147
return &h->b[(h->key_len + h->val_len) * i];

0 commit comments

Comments
 (0)