Skip to content

Commit

Permalink
Phong cách macro: Gạch chân tên được khai báo trong macro, sử dụng th…
Browse files Browse the repository at this point in the history
…am số như biểu thức cơ bản trong biểu thức trong macro
  • Loading branch information
bangoc committed Dec 29, 2021
1 parent c4b22c9 commit 9ed9efb
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 138 deletions.
26 changes: 13 additions & 13 deletions base/arr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#define ARR(pa) (*(pa))
#define arr_t(etyp) etyp **
#define elem(a, i) (ARR(a)[i])
#define elem_ref(a, i) (ARR(a) + i)
#define elem(a, i) (ARR(a)[(i)])
#define elem_ref(a, i) (ARR(a) + (i))

enum arr_attrib {
ARR_SZ = 0,
Expand Down Expand Up @@ -43,33 +43,33 @@ static inline void **arr_create_internal(long cap, long elem_sz) {
#define arr_free(a) \
do { \
free(ARR_BEG(a)); \
*a = NULL; \
*(a) = NULL; \
free(a); \
a = NULL; \
(a) = NULL; \
} while (0)

#define arr_set_capacity(a, newcap) \
do { \
long *_tmp = ARR_BEG(a); \
long _elem_sz = _tmp[ARR_ELEM_SZ]; \
_tmp = realloc(_tmp, ARR_ATT_MAX * sizeof(long) + newcap * _elem_sz); \
_tmp = realloc(_tmp, ARR_ATT_MAX * sizeof(long) + (newcap) * _elem_sz); \
if (_tmp) { \
if (_tmp[ARR_SZ] > newcap) { \
_tmp[ARR_SZ] = newcap; \
if (_tmp[ARR_SZ] > (newcap)) { \
_tmp[ARR_SZ] = (newcap); \
} \
_tmp[ARR_CAP] = newcap; \
(*((void**)a)) = (void*)(_tmp + ARR_ATT_MAX); \
_tmp[ARR_CAP] = (newcap); \
(*((void**)(a))) = (void*)(_tmp + ARR_ATT_MAX); \
} \
} while (0)

#define arr_set_size(a, newsize) \
do { \
long _c = arr_capacity(a); \
if (newsize > _c) { \
if ((newsize) > _c) { \
arr_set_capacity(a, newsize); \
} \
if (newsize <= arr_capacity(a)) { \
arr_size(a) = newsize; \
if ((newsize) <= arr_capacity(a)) { \
arr_size(a) = (newsize); \
} \
} while (0)

Expand All @@ -90,7 +90,7 @@ static inline void **arr_create_internal(long cap, long elem_sz) {
_tmp = ARR_BEG(a); \
} \
if (_tmp[ARR_SZ] < _tmp[ARR_CAP]) { \
(*a)[_tmp[ARR_SZ]] = elem; \
ARR(a)[_tmp[ARR_SZ]] = (elem); \
++(_tmp[ARR_SZ]); \
} \
} while (0)
Expand Down
4 changes: 2 additions & 2 deletions base/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct bn_node {
struct bn_node *top;
} *bn_node_t;

#define to_bn(n) ((bn_node_t)n)
#define to_bn(n) ((bn_node_t)(n))
#define bn_connect1(n1, link, n2) to_bn(n1)->link = to_bn(n2)
#define bn_connect2(n1, link1, n2, link2) bn_connect1(n1, link1, n2); \
bn_connect1(n2, link2, n1)
Expand Down Expand Up @@ -72,7 +72,7 @@ bn_node_t bn_prev_inorder(bn_node_t x);
npp(cur); \
}

#define bn_is_empty(t) (t->root == NULL_PTR)
#define bn_is_empty(t) ((t)->root == NULL_PTR)

#define bn_change_child(old_node, new_node, parent, t) \
do { \
Expand Down
28 changes: 14 additions & 14 deletions base/bns.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

#include "base/bn.h"

#define bns_child(n, order) (order < 0? n->left: n->right)
#define bns_child_ref(n, order) (order < 0? &n->left: &n->right)
#define bns_child(n, order) ((order) < 0? (n)->left: (n)->right)
#define bns_child_ref(n, order) ((order) < 0? &((n)->left): &((n)->right))

#define bns_insert_setup(loc, root, data, cmp, same, parent) \
do { \
bn_node_t x = root; \
bn_node_t _x = (root); \
int _order; \
while (x) { \
parent = x; \
_order = cmp(data, x); \
while (_x) { \
(parent) = _x; \
_order = cmp(data, _x); \
if (!_order) { \
same = x; \
(same) = _x; \
} \
x = bns_child(x, _order); \
_x = bns_child(_x, _order); \
} \
loc = parent? bns_child_ref(parent, _order): &root; \
(loc) = (parent)? bns_child_ref(parent, _order): &(root); \
} while (0)

#define bns_search_inline(o, t, u, cmp, ...) \
do { \
int _order; \
bn_node_t _x = t->root; \
bn_node_t _x = (t)->root; \
bn_node_t o = NULL_PTR; \
while (_x) { \
_order = cmp(u, _x); \
Expand All @@ -45,7 +45,7 @@
#define bns_search_gte_inline(o, t, u, cmp, ...) \
do {\
int _order; \
bn_node_t _x = t->root; \
bn_node_t _x = (t)->root; \
bn_node_t o = NULL_PTR; \
while (_x) { \
_order = cmp(u, _x); \
Expand All @@ -67,7 +67,7 @@
#define bns_search_lte_inline(o, t, u, cmp, ...) \
do { \
int _order; \
bn_node_t _x = t->root; \
bn_node_t _x = (t)->root; \
bn_node_t o = NULL_PTR; \
while (_x) { \
_order = cmp(u, _x); \
Expand Down Expand Up @@ -95,15 +95,15 @@ typedef struct bns_node_g {
gtype key;
} *bns_node_g_t;

#define to_bns_node_g(n) ((bns_node_g_t)n)
#define to_bns_node_g(n) ((bns_node_g_t)(n))
#define bns_node_g_key(n) (to_bns_node_g(n)->key)

typedef struct bns_tree_g {
struct bn_tree base;
bn_compare_t cmp;
} *bns_tree_g_t;

#define to_bns_tree_g(t) ((bns_tree_g_t)t)
#define to_bns_tree_g(t) ((bns_tree_g_t)(t))
#define bns_tree_g_cmp(t) (to_bns_tree_g(t)->cmp)

// Hỗ trợ giản lược cách viết bns_tree_g_cmp(t)(v1, v2)
Expand Down
4 changes: 2 additions & 2 deletions base/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <time.h>

#define container_of(ptr, type, member) \
((type *)((void*)ptr - offsetof(type, member)))
((type *)((void*)(ptr) - offsetof(type, member)))

#define New(TYPE, ...) TYPE ## _create( __VA_ARGS__ )

Expand All @@ -22,7 +22,7 @@

#define BENCH(NAME, ITER, ...) do { \
double _sum = 0, _start, _stop; \
for (int i = 0; i < ITER; ++i) { \
for (int _i = 0; _i < ITER; ++_i) { \
_start = clock(); \
{ __VA_ARGS__; } \
_stop = clock(); \
Expand Down
4 changes: 2 additions & 2 deletions base/dll.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ typedef struct dll_s {
#define dll_back(list) (to_dll(list)->back)

#define dll_traverse(cur, list) \
for (dln_t cur = list->front; cur != NULL; cur = cur->next)
for (dln_t cur = (list)->front; cur != NULL; cur = cur->next)

#define dll_rtraverse(cur, list) \
for (dln_t cur = list->back; cur != NULL; cur = cur->prev)
for (dln_t cur = (list)->back; cur != NULL; cur = cur->prev)

/* Giao diện khái quát */

Expand Down
6 changes: 3 additions & 3 deletions base/gtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ typedef union generic_type {

#define gtype_swap(v1, v2) \
do { \
gtype _tmp = v1; \
v1 = v2; \
v2 = _tmp; \
gtype _tmp = (v1); \
(v1) = (v2); \
(v2) = _tmp; \
} while (0)

typedef int (*gtype_cmp_t)(gtype, gtype);
Expand Down
26 changes: 13 additions & 13 deletions base/hashes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ extern "C" {
#define HASH_IS_DELETED(h_) ((h_) == DELETED_HASH_VALUE)
#define HASH_IS_REAL(h_) ((h_) >= MIN_HASH_VALUE)
#define HASH_IS_NOTREAL(h_) (!HASH_IS_REAL(h_))
#define MAX(a, b) (a > b? a: b)
#define MAX(a, b) ((a) > (b)? (a): (b))

typedef unsigned int uint;
typedef uint32_t uint32;
typedef uint (*gtype_hash_t) (gtype);

#define hashes_next_pkey_or_pvalue(m, c, kv, o) \
do { \
o = NULL; \
uint *_hashes = ARR(m->hashes); \
gtype *_arr = ARR(m->kv); \
int _idx = c? c - _arr + 1: 0; \
for (int i = _idx; i < m->size; ++i) { \
if (HASH_IS_REAL(_hashes[i])) { \
o = _arr + i; \
(o) = NULL; \
uint *_hashes = ARR((m)->hashes); \
gtype *_arr = ARR((m)->kv); \
int _idx = (c)? (c) - _arr + 1: 0; \
for (int _i = _idx; _i < (m)->size; ++_i) { \
if (HASH_IS_REAL(_hashes[_i])) { \
(o) = _arr + _i; \
break; \
} \
} \
} while (0)

extern const int prime_mod [];

#define get_status_bit(bitmap, index) ((bitmap[index/32] >> (index %32)) & 1)
#define set_status_bit(bitmap, index) (bitmap[index / 32] |= 1U << (index % 32))
#define get_status_bit(bitmap, index) (((bitmap)[(index)/32] >> ((index) %32)) & 1)
#define set_status_bit(bitmap, index) ((bitmap)[(index) / 32] |= 1U << ((index) % 32))

#define evict_key_or_value(a, i, v, ov) \
{ \
gtype _tmp = a[i]; \
a[i] = v; \
ov = _tmp; \
gtype _tmp = (a)[(i)]; \
(a)[(i)] = (v); \
(ov) = _tmp; \
}

// Một hàm băm khái quát
Expand Down
20 changes: 10 additions & 10 deletions base/rb.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ int rb_delete(bn_tree_t t, bn_node_t z);


// ========== Macro viết nhanh ===========
#define to_rb(n) ((rb_node_t)n)
#define rb_color(n) (n? to_rb(n)->color: RB_BLACK)
#define to_rb(n) ((rb_node_t)(n))
#define rb_color(n) ((n)? to_rb(n)->color: RB_BLACK)
#define rb_color_str(n) color_names[(int)rb_color(n)]
#define rb_set_color(n, new_color) to_rb(n)->color = new_color
#define rb_set_color(n, new_color) to_rb(n)->color = (new_color)
#define rb_is_red(node) (rb_color(node) == RB_RED)
#define rb_is_black(node) (rb_color(node) == RB_BLACK)
#define rb_set_black(node) rb_set_color(node, RB_BLACK)
Expand All @@ -64,15 +64,15 @@ int rb_delete(bn_tree_t t, bn_node_t z);
/* x là trục xoay */
#define bn_rotate(t, x, right, left) \
do { \
bn_node_t _y = x->right; \
x->right = _y->left; \
bn_node_t _y = (x)->right; \
(x)->right = _y->left; \
if (_y->left != NULL_PTR) { \
_y->left->top = x; \
_y->left->top = (x); \
} \
_y->top = x->top; \
bn_change_child(x, _y, x->top, t); \
_y->left = x; \
x->top = _y; \
_y->top = (x)->top; \
bn_change_child(x, _y, (x)->top, t); \
_y->left = (x); \
(x)->top = _y; \
} while (0)

#endif // RB_H_
18 changes: 9 additions & 9 deletions base/src/bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ bn_node_t bn_left_most(bn_node_t x) {
}
bn_node_t y;
#define bn_MOST(x, child, out) \
out = x; \
(out) = (x); \
do { \
while (out->child != NULL_PTR) { \
out = out->child; \
while ((out)->child != NULL_PTR) { \
(out) = (out)->child; \
} \
} while (0)
bn_MOST(x, left, y);
Expand All @@ -105,13 +105,13 @@ bn_node_t bn_next_inorder(bn_node_t x) {
bn_node_t y;
#define BNS_NEAREST(x, left, right, out) \
do { \
if (x->right != NULL_PTR) { \
out = bn_ ##left ##_most(x->right); \
if ((x)->right != NULL_PTR) { \
(out) = bn_ ##left ##_most((x)->right); \
} else { \
out = x->top; \
while (out != NULL_PTR && x == out->right) {\
x = out; \
out = out->top; \
(out) = (x)->top; \
while ((out) != NULL_PTR && (x) == (out)->right) {\
(x) = (out); \
(out) = (out)->top; \
} \
} \
} while (0)
Expand Down
6 changes: 3 additions & 3 deletions base/src/rb.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ void rb_insert_fixup(bn_tree_t t, bn_node_t n, bn_node_t p) {

if (p == p->top->left) {
#define IMPL_INSERT_FIXUP(left, right) \
bn_node_t u = p->top->right; \
if (rb_is_red(u)) { \
bn_node_t _u = p->top->right; \
if (rb_is_red(_u)) { \
/* GP gp <- n mới \
p u thành>>> P U \
->n <- có thể vi phạm tính chất 4 nếu gp->top là đỏ,\
n có thể là con trái hoặc con phải của p \
*/ \
rb_set_black(p); \
rb_set_black(u); \
rb_set_black(_u); \
rb_set_red(p->top); \
n = p->top; \
p = n->top; \
Expand Down
20 changes: 10 additions & 10 deletions gdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ typedef struct gdl_s {
gtype_free_t free_value;
} *gdl_t;

#define gdn_value(n) (*n)
#define gdn_next(n) (((gtype*)n + GDN_NEXT)->g)
#define gdn_prev(n) (((gtype*)n + GDN_PREV)->g)
#define gdn_value(n) (*(n))
#define gdn_next(n) (((gtype*)(n) + GDN_NEXT)->g)
#define gdn_prev(n) (((gtype*)(n) + GDN_PREV)->g)
#define gdn_create(nn, value) \
gtype *nn = malloc(GDN_ELEMENTS * sizeof(gtype)); \
gdn_value(nn) = value; \
gdn_value(nn) = (value); \
gdn_next(nn) = NULL; \
gdn_prev(nn) = NULL

#define gdl_front(list) (list->front)
#define gdl_back(list) (list->back)
#define gdl_front(list) ((list)->front)
#define gdl_back(list) ((list)->back)
#define gdl_is_empty(list) (gdl_front(list) == NULL && gdl_back(list) == NULL)
#define gdl_push_front(list, value) \
do { \
Expand Down Expand Up @@ -107,14 +107,14 @@ typedef struct gdl_s {

#define gdl_inserta(list, pos, value) \
do { \
if (!pos) { \
if (!(pos)) { \
gdl_push_back(list, value); \
break; \
} \
gdn_create(_nn, value); \
gtype *_tmp = gdn_next(pos); \
gdn_next(pos) = _nn; \
gdn_prev(_nn) = pos; \
gdn_prev(_nn) = (pos); \
gdn_next(_nn) = _tmp; \
if (_tmp) { \
gdn_prev(_tmp) = _nn; \
Expand All @@ -125,14 +125,14 @@ typedef struct gdl_s {

#define gdl_insertb(list, pos, value) \
do { \
if (!pos) { \
if (!(pos)) { \
gdl_push_front(list, value); \
break; \
} \
gdn_create(_nn, value); \
gtype *_tmp = gdn_prev(pos); \
gdn_prev(pos) = _nn; \
gdn_next(_nn) = pos; \
gdn_next(_nn) = (pos); \
gdn_prev(_nn) = _tmp; \
if (_tmp) { \
gdn_next(_tmp) = _nn; \
Expand Down
Loading

0 comments on commit 9ed9efb

Please sign in to comment.