Skip to content

Commit

Permalink
rlist: add cut and foreach_safe_reverse helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
locker committed Jul 29, 2019
1 parent 0c87e6f commit 31cd36d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
28 changes: 28 additions & 0 deletions small/rlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ rlist_splice_tail(struct rlist *head1, struct rlist *head2)
}
}

/**
* move the initial part of list head2, up to but excluding item,
* to list head1; the old content of head1 is discarded
*/
static inline void
rlist_cut_before(struct rlist *head1, struct rlist *head2, struct rlist *item)
{
if (head1->next == item) {
rlist_create(head1);
return;
}
head1->next = head2->next;
head1->next->prev = head1;
head1->prev = item->prev;
head1->prev->next = head1;
head2->next = item;
item->prev = head2;
}

/**
* list head initializer
*/
Expand Down Expand Up @@ -364,6 +383,15 @@ delete from one list and add_tail as another's head
((tmp) = rlist_next_entry((item), member)); \
(item) = (tmp))

/**
* foreach backward through all list entries safe against removal
*/
#define rlist_foreach_entry_safe_reverse(item, head, member, tmp) \
for ((item) = rlist_last_entry((head), typeof(*item), member); \
&item->member != (head) && \
((tmp) = rlist_prev_entry((item), member)); \
(item) = (tmp))

#if defined(__cplusplus)
} /* extern "C" */
#endif /* defined(__cplusplus) */
Expand Down
39 changes: 37 additions & 2 deletions test/rlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ int
main(void)
{
int i;
struct test *it;
struct test *it, *tmp;
struct rlist *rlist;

header();
plan(87);
plan(104);
ok(rlist_empty(&head), "list is empty");
for (i = 0; i < ITEMS; i++) {
items[i].no = i;
Expand Down Expand Up @@ -128,6 +128,41 @@ main(void)
ok(rlist_prev_entry_safe(&items[0], &head, list) == NULL,
"prev is null");

rlist_create(&head);
i = 0;
rlist_foreach_entry_safe_reverse(it, &head, list, tmp)
++i;
ok(i == 0, "list is empty")
for (i = 0; i < ITEMS; i++) {
items[i].no = i;
rlist_add(&head, &(items[i].list));
}
i = 0;
rlist_foreach_entry_safe_reverse(it, &head, list, tmp) {
rlist_del_entry(it, list);
is(it, items + i, "element (reverse) %d", i);
i++;
}
rlist_add(&head, &items[0].list);
rlist_cut_before(&head2, &head, head.next);
ok(rlist_empty(&head2), "list is empty");
for (i = 1; i < ITEMS; i++) {
items[i].no = i;
rlist_add_tail(&head, &(items[i].list));
}
rlist_cut_before(&head2, &head, head.next);
ok(rlist_empty(&head2), "list is empty");
rlist_cut_before(&head2, &head, &items[ITEMS / 2].list);
i = 0;
rlist_foreach_entry(it, &head2, list) {
is(it, items + i, "element (first half) %d", i);
i++;
}
rlist_foreach_entry(it, &head, list) {
is(it, items + i, "element (second half) %d", i);
i++;
}

int rc = check_plan();
footer();
return rc;
Expand Down
19 changes: 18 additions & 1 deletion test/rlist.result
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*** main ***
1..87
1..104
ok 1 - list is empty
ok 2 - rlist_nil is empty
ok 3 - head2 is empty
Expand Down Expand Up @@ -87,4 +87,21 @@ ok 84 - element (foreach_entry) 2
ok 85 - element (foreach_entry) 1
ok 86 - element (foreach_entry) 0
ok 87 - prev is null
ok 88 - list is empty
ok 89 - element (reverse) 0
ok 90 - element (reverse) 1
ok 91 - element (reverse) 2
ok 92 - element (reverse) 3
ok 93 - element (reverse) 4
ok 94 - element (reverse) 5
ok 95 - element (reverse) 6
ok 96 - list is empty
ok 97 - list is empty
ok 98 - element (first half) 0
ok 99 - element (first half) 1
ok 100 - element (first half) 2
ok 101 - element (second half) 3
ok 102 - element (second half) 4
ok 103 - element (second half) 5
ok 104 - element (second half) 6
*** main: done ***

0 comments on commit 31cd36d

Please sign in to comment.