From 15d3a22d5f9bbb0ec09f86bb02e676f9cff4da4e Mon Sep 17 00:00:00 2001 From: Nguyen Ba Ngoc Date: Fri, 13 Dec 2024 13:04:44 +0700 Subject: [PATCH] =?UTF-8?q?T=C3=A1i=20c=E1=BA=A5u=20tr=C3=BAc=20quy=20t?= =?UTF-8?q?=E1=BA=AFc=20=C4=91=E1=BA=B7t=20t=C3=AAn:=20thay=20ti=E1=BB=81n?= =?UTF-8?q?=20t=E1=BB=91=20b=E1=BA=B1ng=20t=C3=AAn=20c=E1=BA=A5u=20tr?= =?UTF-8?q?=C3=BAc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v3/hmap.h | 106 ++++++++++++++++---------------- v3/slist.h | 10 +-- v3/tests/hmap_demo_ut.c | 2 +- v3/tests/ivec_put_rem_demo_ut.c | 6 +- v3/tests/ivec_sort_ut.c | 4 +- v3/tests/ivec_ut.c | 4 +- v3/tests/slist_demo_ut.c | 2 +- v3/vec.h | 54 ++++++++-------- 8 files changed, 94 insertions(+), 94 deletions(-) diff --git a/v3/hmap.h b/v3/hmap.h index 619ae67..6a0c8ae 100644 --- a/v3/hmap.h +++ b/v3/hmap.h @@ -55,24 +55,24 @@ const int prime_mod [] = #define SET_BIT(bitmap, index) ((bitmap)[(index) / 8] |= 1U << ((index) % 8)) #define HMAP_DECL(pre, key_t, value_t) \ -struct pre##hmap_node; \ -struct pre##hmap; \ -struct pre##hmap *pre##hmap(int cap, unsigned (*ha)(), int (*eq)()); \ -struct pre##hmap_node *pre##hmap_put(struct pre##hmap *hm, key_t k, value_t v); \ -value_t *pre##hmap_get(struct pre##hmap *hm, key_t k); \ -struct pre##hmap_node *pre##hmap_rem(struct pre##hmap *hm, key_t k); \ -void pre##hmap_del(struct pre##hmap *hm); +struct hname##_node; \ +struct hname; \ +struct hname *hname(int cap, unsigned (*ha)(), int (*eq)()); \ +struct hname##_node *hname##_put(struct hname *hm, key_t k, value_t v); \ +value_t *hname##_get(struct hname *hm, key_t k); \ +struct hname##_node *hname##_rem(struct hname *hm, key_t k); \ +void hname##_del(struct hname *hm); -#define HMAP_IMPL(pre, key_t, value_t) \ -struct pre##hmap_node { \ +#define HMAP_IMPL(hname, key_t, value_t) \ +struct hname##_node { \ key_t key; \ value_t value; \ unsigned hash; \ enum hmap_node_state state; \ }; \ -struct pre##hmap { \ - struct pre##hmap_node *nodes; \ - struct pre##hmap_node *end; \ +struct hname { \ + struct hname##_node *nodes; \ + struct hname##_node *end; \ int mod; \ unsigned mask; \ int size; \ @@ -88,31 +88,31 @@ static int closest_shift(int n) { \ } \ return i; \ } \ -static void pre##hmap_set_shift(struct pre##hmap *hm, int shift) { \ +static void hname##_set_shift(struct hname *hm, int shift) { \ hm->cap = 1 << shift; \ hm->mod = prime_mod[shift]; \ hm->mask = hm->cap - 1; \ } \ -static void pre##hmap_set_shift_from_cap(struct pre##hmap *hm, int cap) { \ +static void hname##_set_shift_from_cap(struct hname *hm, int cap) { \ int shift = closest_shift(cap); \ if (shift < HASH_MIN_SHIFT) { \ shift = HASH_MIN_SHIFT; \ } \ - pre##hmap_set_shift(hm, shift); \ + hname##_set_shift(hm, shift); \ } \ -static void pre##hmap_setup(struct pre##hmap *hm, int shift) { \ +static void hname##_setup(struct hname *hm, int shift) { \ if (shift < HASH_MIN_SHIFT) { \ shift = HASH_MIN_SHIFT; \ } \ - pre##hmap_set_shift(hm, shift); \ - hm->nodes = calloc(hm->cap, sizeof(struct pre##hmap_node)); \ + hname##_set_shift(hm, shift); \ + hm->nodes = calloc(hm->cap, sizeof(struct hname##_node)); \ hm->end = hm->nodes + hm->cap; \ } \ -static void pre##hmap_realloc_arrays(struct pre##hmap *hm) { \ - hm->nodes = realloc(hm->nodes, hm->cap * sizeof(struct pre##hmap_node)); \ +static void hname##_realloc_arrays(struct hname *hm) { \ + hm->nodes = realloc(hm->nodes, hm->cap * sizeof(struct hname##_node)); \ hm->end = hm->nodes + hm->cap; \ } \ -static void relocate_map(struct pre##hmap *hm, unsigned ocap, \ +static void relocate_map(struct hname *hm, unsigned ocap, \ unsigned char *reallocated_flags) { \ for (int i = 0; i < ocap; ++i) { \ if (hm->nodes[i].state != USING) { \ @@ -122,7 +122,7 @@ static void relocate_map(struct pre##hmap *hm, unsigned ocap, \ if (GET_BIT(reallocated_flags, i)) { \ continue; \ } \ - struct pre##hmap_node n = hm->nodes[i]; \ + struct hname##_node n = hm->nodes[i]; \ hm->nodes[i].state = UNUSED; \ for (;;) { \ unsigned idx, step = 0; \ @@ -137,50 +137,50 @@ static void relocate_map(struct pre##hmap *hm, unsigned ocap, \ hm->nodes[idx] = n; \ break; \ } \ - struct pre##hmap_node tmp = hm->nodes[idx]; \ + struct hname##_node tmp = hm->nodes[idx]; \ hm->nodes[idx] = n; \ n = tmp; \ } \ } \ } \ -static void pre##hmap_realloc(struct pre##hmap *hm) { \ +static void hname##_realloc(struct hname *hm) { \ int ocap = hm->cap; \ - pre##hmap_set_shift_from_cap(hm, hm->size * 1.333); \ + hname##_set_shift_from_cap(hm, hm->size * 1.333); \ if (hm->cap > ocap) { \ - pre##hmap_realloc_arrays(hm); \ - memset(hm->nodes + ocap, 0, (hm->cap - ocap) * sizeof(struct pre##hmap_node)); \ + hname##_realloc_arrays(hm); \ + memset(hm->nodes + ocap, 0, (hm->cap - ocap) * sizeof(struct hname##_node)); \ } \ unsigned char *reallocated_flags = calloc((hm->cap + 7) / 8, sizeof(unsigned char)); \ relocate_map(hm, ocap, reallocated_flags); \ free(reallocated_flags); \ if (hm->cap < ocap) { \ - pre##hmap_realloc_arrays(hm); \ + hname##_realloc_arrays(hm); \ } \ hm->used = hm->size; \ } \ -static inline int pre##hmap_maybe_realloc(struct pre##hmap *hm) { \ +static inline int hname##_maybe_realloc(struct hname *hm) { \ unsigned used = hm->used, cap = hm->cap; \ if ((cap > hm->size * 4 && cap > 1 << HASH_MIN_SHIFT) || \ (cap <= used + (used/16))) { \ - pre##hmap_realloc(hm); \ + hname##_realloc(hm); \ return 1; \ } \ return 0; \ } \ -static struct pre##hmap_node *pre##hmap_rem_node(struct pre##hmap *hm, int idx) { \ - struct pre##hmap_node *n = hm->nodes + idx; \ +static struct hname##_node *hname##_rem_node(struct hname *hm, int idx) { \ + struct hname##_node *n = hm->nodes + idx; \ n->state = DELETED; \ hm->size--; \ return n; \ } \ -static inline int pre##hmap_lookup(struct pre##hmap *hm, key_t key, \ +static inline int hname##_lookup(struct hname *hm, key_t key, \ unsigned *hash_return) { \ unsigned lookup_hash = hm->ha(key); \ if (hash_return) { \ *hash_return = lookup_hash; \ } \ int idx = HASH2IDX(hm, lookup_hash); \ - struct pre##hmap_node *n = hm->nodes + idx; \ + struct hname##_node *n = hm->nodes + idx; \ int first_deleted = -1; \ int step = 0; \ while (n->state != UNUSED) { \ @@ -201,19 +201,19 @@ static inline int pre##hmap_lookup(struct pre##hmap *hm, key_t key, \ } \ return idx; \ } \ -struct pre##hmap *pre##hmap(int shift, unsigned (*ha)(), int (*eq)()) { \ - struct pre##hmap *hm = malloc(sizeof(struct pre##hmap)); \ +struct hname *hname(int shift, unsigned (*ha)(), int (*eq)()) { \ + struct hname *hm = malloc(sizeof(struct hname)); \ hm->size = 0; \ hm->used = 0; \ hm->ha = ha; \ hm->eq = eq; \ - pre##hmap_setup(hm, shift); \ + hname##_setup(hm, shift); \ return hm; \ } \ -struct pre##hmap_node *pre##hmap_put(struct pre##hmap *hm, key_t k, value_t v) { \ +struct hname##_node *hname##_put(struct hname *hm, key_t k, value_t v) { \ unsigned key_hash; \ - int idx = pre##hmap_lookup(hm, k, &key_hash); \ - struct pre##hmap_node *n = hm->nodes + idx; \ + int idx = hname##_lookup(hm, k, &key_hash); \ + struct hname##_node *n = hm->nodes + idx; \ if (n->state == USING) { \ return n; \ } \ @@ -225,42 +225,42 @@ struct pre##hmap_node *pre##hmap_put(struct pre##hmap *hm, key_t k, value_t v) { n->state = USING; \ if (new_usage) { \ ++hm->used; \ - pre##hmap_maybe_realloc(hm); \ + hname##_maybe_realloc(hm); \ } \ return NULL; \ } \ -value_t *pre##hmap_get(struct pre##hmap *hm, key_t k) { \ - int idx = pre##hmap_lookup(hm, k, NULL); \ - struct pre##hmap_node *n = hm->nodes + idx; \ +value_t *hname##_get(struct hname *hm, key_t k) { \ + int idx = hname##_lookup(hm, k, NULL); \ + struct hname##_node *n = hm->nodes + idx; \ if (n->state == USING) { \ return &n->value; \ } \ return NULL; \ } \ -struct pre##hmap_node *pre##hmap_rem(struct pre##hmap *hm, key_t key) { \ - int idx = pre##hmap_lookup(hm, key, NULL); \ +struct hname##_node *hname##_rem(struct hname *hm, key_t key) { \ + int idx = hname##_lookup(hm, key, NULL); \ if (hm->nodes[idx].state != USING) { \ return NULL; \ } \ - pre##hmap_rem_node(hm, idx); \ - if (pre##hmap_maybe_realloc(hm)) { \ - idx = pre##hmap_lookup(hm, key, NULL); \ + hname##_rem_node(hm, idx); \ + if (hname##_maybe_realloc(hm)) { \ + idx = hname##_lookup(hm, key, NULL); \ } \ return hm->nodes + idx; \ } \ -void pre##hmap_del(struct pre##hmap *hm) { \ +void hname##_del(struct hname *hm) { \ free(hm->nodes); \ free(hm); \ } \ -struct pre##hmap_node *pre##hmap_first(struct pre##hmap *hm) {\ - for (struct pre##hmap_node *iter = hm->nodes; iter < hm->end; ++iter) { \ +struct hname##_node *hname##_first(struct hname *hm) {\ + for (struct hname##_node *iter = hm->nodes; iter < hm->end; ++iter) { \ if (iter->state == USING) { \ return iter; \ } \ } \ return NULL; \ } \ -struct pre##hmap_node *pre##hmap_next(struct pre##hmap *hm, struct pre##hmap_node *n) { \ +struct hname##_node *hname##_next(struct hname *hm, struct hname##_node *n) { \ ++n; \ while (n < hm->end) { \ if (n->state == USING) { \ diff --git a/v3/slist.h b/v3/slist.h index 69ce111..f227876 100644 --- a/v3/slist.h +++ b/v3/slist.h @@ -3,7 +3,7 @@ #include -#define SDECL(sname, elem_t) \ +#define SLIST_DECL(sname, elem_t) \ struct sname##_node; \ struct sname; \ struct sname##_node *sname##_node(elem_t value); \ @@ -20,7 +20,7 @@ elem_t *sname##_peek(struct sname *list); \ struct sname *sname##_deque(struct sname *list); \ int sname##_empty(struct sname *list); -#define SIMPL(sname, elem_t) \ +#define SLIST_IMPL(sname, elem_t) \ struct sname##_node { \ elem_t value; \ struct sname##_node *next; \ @@ -112,8 +112,8 @@ int sname##_empty(struct sname *list) { \ return list->first == NULL || list->last == NULL; \ } -#define SDECL_IMPL(sname, elem_t) \ -SDECL(sname, elem_t) \ -SIMPL(sname, elem_t) +#define SLIST_DECL_IMPL(sname, elem_t) \ +SLIST_DECL(sname, elem_t) \ +SLIST_IMPL(sname, elem_t) #endif // SLIST_H_ \ No newline at end of file diff --git a/v3/tests/hmap_demo_ut.c b/v3/tests/hmap_demo_ut.c index d350af2..30de5a2 100644 --- a/v3/tests/hmap_demo_ut.c +++ b/v3/tests/hmap_demo_ut.c @@ -4,7 +4,7 @@ #include #include -HMAP_DECL_IMPL(si, char *, int) +HMAP_DECL_IMPL(sihmap, char *, int) const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; const int n = sizeof(letters) - 1; diff --git a/v3/tests/ivec_put_rem_demo_ut.c b/v3/tests/ivec_put_rem_demo_ut.c index 8f704b4..652c694 100644 --- a/v3/tests/ivec_put_rem_demo_ut.c +++ b/v3/tests/ivec_put_rem_demo_ut.c @@ -4,9 +4,9 @@ #include #include -VEC_DECL_IMPL(i, int) +VECT_DECL_IMPL(ivec, int) -void ivec_print(struct ivector *v) { +void ivec_print(struct ivec *v) { printf("sz: %d\ncap: %d\n", v->size, v->cap); for (int i = 0; i < v->size; ++i) { printf("%d\n", v->elems[i]); @@ -15,7 +15,7 @@ void ivec_print(struct ivector *v) { int main() { srand(time(NULL)); - struct ivector *v = ivector(0); + struct ivec *v = ivec(0); ivec_put(v, 1, 0); ivec_put(v, 2, 0); ivec_put(v, 3, 1); diff --git a/v3/tests/ivec_sort_ut.c b/v3/tests/ivec_sort_ut.c index e0f7fe6..9553005 100644 --- a/v3/tests/ivec_sort_ut.c +++ b/v3/tests/ivec_sort_ut.c @@ -5,7 +5,7 @@ #include #include -VEC_DECL_IMPL(i, int) +VECT_DECL_IMPL(ivec, int) int lte(const void *o1, const void *o2) { return *((const int *)o1) <= *((const int *)o2); @@ -13,7 +13,7 @@ int lte(const void *o1, const void *o2) { int main() { srand(time(NULL)); - struct ivector *v = ivector(0); + struct ivec *v = ivec(0); for (int i = 0; i < 10; ++i) { ivec_append(v, rand()); } diff --git a/v3/tests/ivec_ut.c b/v3/tests/ivec_ut.c index 31db164..d245f2c 100644 --- a/v3/tests/ivec_ut.c +++ b/v3/tests/ivec_ut.c @@ -2,10 +2,10 @@ #include -VEC_DECL_IMPL(i, int) +VECT_DECL_IMPL(ivec, int) int main() { - struct ivector *v = ivector(0); + struct ivec *v = ivec(0); for (int i = 0; i < 5; ++i) { ivec_append(v, i); } diff --git a/v3/tests/slist_demo_ut.c b/v3/tests/slist_demo_ut.c index 39b240d..82a3deb 100644 --- a/v3/tests/slist_demo_ut.c +++ b/v3/tests/slist_demo_ut.c @@ -2,7 +2,7 @@ #include -SDECL_IMPL(slist, int) +SLIST_DECL_IMPL(slist, int) void print_lst(struct slist *lst) { printf("size = %d\n", lst->size); diff --git a/v3/vec.h b/v3/vec.h index 5778f85..de0bd3f 100644 --- a/v3/vec.h +++ b/v3/vec.h @@ -5,49 +5,49 @@ #include /* (C) Nguyễn Bá Ngọc 2024 */ -#define VEC_DECL(pre, elem_t) \ +#define VECT_DECL(vname, elem_t) \ \ -struct pre##vector; \ -struct pre##vector *pre##vector(int n); \ -int pre##vec_reserve(struct pre##vector *v, int new_cap); \ -void pre##vec_append(struct pre##vector *v, elem_t elem); \ -elem_t pre##vec_rem(struct pre##vector *v, int idx); \ -void pre##vec_put(struct pre##vector *v, elem_t elem, int idx); \ -void pre##vec_resize(struct pre##vector *v, int new_sz); \ -void pre##vec_del(struct pre##vector *v); +struct vname; \ +struct vname *vname(int n); \ +int vname##_reserve(struct vname *v, int new_cap); \ +void vname##_append(struct vname *v, elem_t elem); \ +elem_t vname##_rem(struct vname *v, int idx); \ +void vname##_put(struct vname *v, elem_t elem, int idx); \ +void vname##_resize(struct vname *v, int new_sz); \ +void vname##_del(struct vname *v); -#define VEC_IMPL(pre, elem_t) \ -struct pre##vector { \ +#define VECT_IMPL(vname, elem_t) \ +struct vname { \ int size; \ int cap; \ elem_t *elems; \ }; \ -struct pre##vector *pre##vector(int n) { \ - struct pre##vector *v = malloc(sizeof(struct pre##vector)); \ +struct vname *vname(int n) { \ + struct vname *v = malloc(sizeof(struct vname)); \ v->size = n; \ v->cap = v->size > 0? v->size: 8; \ v->elems = calloc(v->cap, sizeof(elem_t)); \ return v; \ } \ -elem_t pre##vec_rem(struct pre##vector *v, int idx) { \ +elem_t vname##_rem(struct vname *v, int idx) { \ elem_t tmp = v->elems[idx]; \ for (int i = idx; i < v->size - 1; ++i) { \ v->elems[i] = v->elems[i + 1]; \ } \ --v->size; \ if (v->size > 8 && v->cap > v->size * 2) { \ - pre##vec_reserve(v, v->size * 1.33); \ + vname##_reserve(v, v->size * 1.33); \ } \ return tmp; \ } \ -void pre##vec_put(struct pre##vector *v, elem_t elem, int idx) { \ - pre##vec_append(v, elem); \ +void vname##_put(struct vname *v, elem_t elem, int idx) { \ + vname##_append(v, elem); \ for (int i = v->size; i > idx; --i) { \ v->elems[i] = v->elems[i - 1]; \ } \ v->elems[idx] = elem; \ } \ -int pre##vec_reserve(struct pre##vector *v, int newcap) { \ +int vname##_reserve(struct vname *v, int newcap) { \ if (newcap <= v->size) { \ return 1; \ } \ @@ -58,22 +58,22 @@ int pre##vec_reserve(struct pre##vector *v, int newcap) { \ v->cap = newcap; \ return 0; \ } \ -void pre##vec_append(struct pre##vector *v, elem_t value) { \ +void vname##_append(struct vname *v, elem_t value) { \ if (v->cap == 0) { \ - pre##vec_reserve(v, 16); \ + vname##_reserve(v, 16); \ } else if (v->size == v->cap) { \ - pre##vec_reserve(v, 2 * v->cap); \ + vname##_reserve(v, 2 * v->cap); \ } \ v->elems[v->size] = value; \ ++v->size; \ } \ -void pre##vec_resize(struct pre##vector *v, int newsize) { \ +void vname##_resize(struct vname *v, int newsize) { \ if (newsize > v->cap) { \ - pre##vec_reserve(v, newsize); \ + vname##_reserve(v, newsize); \ } \ v->size = newsize; \ } \ -void pre##vec_del(struct pre##vector *v) { \ +void vname##_del(struct vname *v) { \ if (!v) { \ return; \ } \ @@ -81,8 +81,8 @@ void pre##vec_del(struct pre##vector *v) { \ free(v); \ } -#define VEC_DECL_IMPL(pre, elem_t) \ -VEC_DECL(pre, elem_t) \ -VEC_IMPL(pre, elem_t) +#define VECT_DECL_IMPL(pre, elem_t) \ +VECT_DECL(pre, elem_t) \ +VECT_IMPL(pre, elem_t) #endif // VEC_H_ \ No newline at end of file