Better structure ql module

This commit is contained in:
Yinan Zhang 2020-04-03 15:05:20 -07:00
parent 4b66297ea0
commit ce17af4221

View File

@ -14,24 +14,25 @@ struct { \
#define ql_elm(a_type) qr(a_type) #define ql_elm(a_type) qr(a_type)
/* List functions. */ /* List functions. */
#define ql_first(a_head) ((a_head)->qlh_first)
#define ql_new(a_head) do { \ #define ql_new(a_head) do { \
(a_head)->qlh_first = NULL; \ ql_first(a_head) = NULL; \
} while (0) } while (0)
#define ql_clear(a_head) ql_new(a_head)
#define ql_move(a_head_dest, a_head_src) do { \ #define ql_move(a_head_dest, a_head_src) do { \
(a_head_dest)->qlh_first = (a_head_src)->qlh_first; \ ql_first(a_head_dest) = ql_first(a_head_src); \
(a_head_src)->qlh_first = NULL; \ ql_clear(a_head_src); \
} while (0) } while (0)
#define ql_empty(a_head) ((a_head)->qlh_first == NULL) #define ql_empty(a_head) (ql_first(a_head) == NULL)
#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field) #define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
#define ql_first(a_head) ((a_head)->qlh_first)
#define ql_last(a_head, a_field) \ #define ql_last(a_head, a_field) \
((ql_first(a_head) != NULL) \ (ql_empty(a_head) ? NULL : qr_prev(ql_first(a_head), a_field))
? qr_prev(ql_first(a_head), a_field) : NULL)
#define ql_next(a_head, a_elm, a_field) \ #define ql_next(a_head, a_elm, a_field) \
((ql_last(a_head, a_field) != (a_elm)) \ ((ql_last(a_head, a_field) != (a_elm)) \
@ -52,27 +53,27 @@ struct { \
qr_after_insert((a_qlelm), (a_elm), a_field) qr_after_insert((a_qlelm), (a_elm), a_field)
#define ql_head_insert(a_head, a_elm, a_field) do { \ #define ql_head_insert(a_head, a_elm, a_field) do { \
if (ql_first(a_head) != NULL) { \ if (!ql_empty(a_head)) { \
qr_before_insert(ql_first(a_head), (a_elm), a_field); \ qr_before_insert(ql_first(a_head), (a_elm), a_field); \
} \ } \
ql_first(a_head) = (a_elm); \ ql_first(a_head) = (a_elm); \
} while (0) } while (0)
#define ql_tail_insert(a_head, a_elm, a_field) do { \ #define ql_tail_insert(a_head, a_elm, a_field) do { \
if (ql_first(a_head) != NULL) { \ if (!ql_empty(a_head)) { \
qr_before_insert(ql_first(a_head), (a_elm), a_field); \ qr_before_insert(ql_first(a_head), (a_elm), a_field); \
} \ } \
ql_first(a_head) = qr_next((a_elm), a_field); \ ql_first(a_head) = qr_next((a_elm), a_field); \
} while (0) } while (0)
#define ql_concat(a_head_a, a_head_b, a_field) do { \ #define ql_concat(a_head_a, a_head_b, a_field) do { \
if (ql_first(a_head_a) == NULL) { \ if (ql_empty(a_head_a)) { \
ql_first(a_head_a) = ql_first(a_head_b); \ ql_move(a_head_a, a_head_b); \
} else if (ql_first(a_head_b) != NULL) { \ } else if (!ql_empty(a_head_b)) { \
qr_meld(ql_first(a_head_a), ql_first(a_head_b), \ qr_meld(ql_first(a_head_a), ql_first(a_head_b), \
a_field); \ a_field); \
ql_clear(a_head_b); \
} \ } \
ql_first(a_head_b) = NULL; \
} while (0) } while (0)
#define ql_remove(a_head, a_elm, a_field) do { \ #define ql_remove(a_head, a_elm, a_field) do { \
@ -82,7 +83,7 @@ struct { \
if (ql_first(a_head) != (a_elm)) { \ if (ql_first(a_head) != (a_elm)) { \
qr_remove((a_elm), a_field); \ qr_remove((a_elm), a_field); \
} else { \ } else { \
ql_first(a_head) = NULL; \ ql_clear(a_head); \
} \ } \
} while (0) } while (0)
@ -98,11 +99,11 @@ struct { \
#define ql_split(a_head_a, a_elm, a_head_b, a_field) do { \ #define ql_split(a_head_a, a_elm, a_head_b, a_field) do { \
if (ql_first(a_head_a) == (a_elm)) { \ if (ql_first(a_head_a) == (a_elm)) { \
ql_first(a_head_a) = NULL; \ ql_move(a_head_b, a_head_a); \
} else { \ } else { \
qr_split(ql_first(a_head_a), (a_elm), a_field); \ qr_split(ql_first(a_head_a), (a_elm), a_field); \
ql_first(a_head_b) = (a_elm); \
} \ } \
ql_first(a_head_b) = (a_elm); \
} while (0) } while (0)
/* /*