Add rotate functionality to ql module

This commit is contained in:
Yinan Zhang 2020-04-02 11:11:08 -07:00
parent 0dc95a882f
commit 1dd24ca6d2
2 changed files with 34 additions and 1 deletions

View File

@ -98,6 +98,16 @@ struct { \
ql_first(a_head_b) = (a_elm); \
} while (0)
/*
* An optimized version of:
* a_type *t = ql_first(a_head);
* ql_remove((a_head), t, a_field);
* ql_tail_insert((a_head), t, a_field);
*/
#define ql_rotate(a_head, a_field) do { \
ql_first(a_head) = qr_next(ql_first(a_head), a_field); \
} while (0)
#define ql_foreach(a_var, a_head, a_field) \
qr_foreach((a_var), ql_first(a_head), a_field)

View File

@ -258,6 +258,28 @@ TEST_BEGIN(test_ql_concat_split) {
}
TEST_END
TEST_BEGIN(test_ql_rotate) {
list_head_t head;
list_t entries[NENTRIES];
unsigned i;
ql_new(&head);
init_entries(entries, sizeof(entries)/sizeof(list_t));
for (i = 0; i < NENTRIES; i++) {
ql_tail_insert(&head, &entries[i], link);
}
char head_id = ql_first(&head)->id;
for (i = 0; i < NENTRIES; i++) {
assert_c_eq(ql_first(&head)->id, head_id, "");
ql_rotate(&head, link);
assert_c_eq(ql_last(&head, link)->id, head_id, "");
head_id++;
}
test_entries_list(&head, entries, NENTRIES);
}
TEST_END
int
main(void) {
return test(
@ -267,5 +289,6 @@ main(void) {
test_ql_head_insert,
test_ql_head_remove,
test_ql_insert,
test_ql_concat_split);
test_ql_concat_split,
test_ql_rotate);
}