From 9c130285d8f17a5e09bbe0b3c3b886ec3145bae9 Mon Sep 17 00:00:00 2001 From: Michael Meffie Date: Thu, 18 Aug 2011 17:04:14 -0400 Subject: [PATCH] opr: queue swap Add inline function to pivot two queues. Implementation by Simon Wilkinson. Change-Id: I704a1ff3e0d6314e2bfe47c870226cb6ffd44b1b Reviewed-on: http://gerrit.openafs.org/5742 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- src/opr/queue.h | 22 ++++++++++++++++++++++ tests/opr/queues-t.c | 24 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/opr/queue.h b/src/opr/queue.h index 44ac5d5..dd45f68 100644 --- a/src/opr/queue.h +++ b/src/opr/queue.h @@ -138,6 +138,28 @@ opr_queue_Count(struct opr_queue *q) { return n; } +static_inline void +opr_queue_Swap(struct opr_queue *a, struct opr_queue *b) +{ + struct opr_queue tq = *b; + + if (a->prev == a) { + b->prev = b->next = b; + } else { + *b = *a; + b->prev->next = b; + b->next->prev = b; + } + + if (tq.prev == b) { + a->prev = a->next = a; + } else { + *a = tq; + a->prev->next = a; + a->next->prev = a; + } +} + /* Remove the members before pivot from Q1, and append them to Q2 */ static_inline void diff --git a/tests/opr/queues-t.c b/tests/opr/queues-t.c index ee806fb..b370b94 100644 --- a/tests/opr/queues-t.c +++ b/tests/opr/queues-t.c @@ -55,12 +55,14 @@ stringIntoQueue(char *string, struct opr_queue *q) { int main(void) { - struct opr_queue q1, q2, *cursor; + struct opr_queue q1, q2, q3, q4, *cursor; - plan(12); + plan(17); opr_queue_Init(&q1); opr_queue_Init(&q2); + opr_queue_Init(&q3); + opr_queue_Init(&q4); opr_queue_Append(&q1, &(newEntry('A')->entry)); opr_queue_Append(&q1, &(newEntry('B')->entry)); @@ -116,5 +118,23 @@ main(void) is_string("NEDMABC12", queueAsString(&q1), "SplitAfterPrepend Q1"); is_string("XYZ89", queueAsString(&q2), "SplitAfterPrepend Q2"); + /* Swap tests */ + opr_queue_Swap(&q3, &q4); + ok(opr_queue_IsEmpty(&q3) && opr_queue_IsEmpty(&q4), "Swap empty queues"); + + opr_queue_Append(&q3, &(newEntry('A')->entry)); + opr_queue_Append(&q3, &(newEntry('B')->entry)); + opr_queue_Append(&q3, &(newEntry('C')->entry)); + opr_queue_Swap(&q3, &q4); + ok(opr_queue_IsEmpty(&q3), "Swap with one empty queue Q3"); + is_string("ABC", queueAsString(&q4), "Swap with one empty queue Q4"); + + opr_queue_Append(&q3, &(newEntry('1')->entry)); + opr_queue_Append(&q3, &(newEntry('2')->entry)); + opr_queue_Append(&q3, &(newEntry('3')->entry)); + opr_queue_Swap(&q3, &q4); + is_string("ABC", queueAsString(&q3), "Swap Q3"); + is_string("123", queueAsString(&q4), "Swap Q4"); + return 0; } -- 1.9.4