From f26b50bdf866d704dcb721ed9ed2b9dc79b5dad7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 1 Dec 2015 13:55:20 -0500 Subject: [PATCH] core/containers/queue: Add _q_iter_set() and _q_iter_val() This patch implements basic queue iterator functions for accessing specific positions on the queue. Signed-off-by: Anna Schumaker --- include/core/containers/queue.h | 21 +++++++++++++++++++++ tests/core/containers/queue.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/core/containers/queue.h b/include/core/containers/queue.h index dbaf0d48..d0dcb87b 100644 --- a/include/core/containers/queue.h +++ b/include/core/containers/queue.h @@ -10,6 +10,27 @@ struct _queue { GQueue _queue; }; +struct _q_iter { + guint it_pos; + GList *it_iter; +}; + + +/* Called to set a queue iterator to a specific position. */ +static inline void _q_iter_set(struct _queue *queue, struct _q_iter *it, + unsigned int pos) +{ + it->it_iter = g_queue_peek_nth_link(&queue->_queue, pos); + it->it_pos = pos; +} + +/* Called to access the value of a queue iterator. */ +static inline gpointer _q_iter_val(struct _q_iter *it) +{ + return (it->it_iter) ? it->it_iter->data : NULL; +} + + #define _Q_INIT() \ { \ ._queue = G_QUEUE_INIT, \ diff --git a/tests/core/containers/queue.c b/tests/core/containers/queue.c index d9e4111b..4cc38e4e 100644 --- a/tests/core/containers/queue.c +++ b/tests/core/containers/queue.c @@ -14,9 +14,15 @@ static inline unsigned int test_q_last(struct _queue *queue) return GPOINTER_TO_INT(g_queue_peek_tail(&queue->_queue)); } +static inline unsigned int test_q_iter_val(struct _q_iter *it) +{ + return GPOINTER_TO_INT(_q_iter_val(it)); +} + static void test_stress(unsigned int N) { struct _queue queue = _Q_INIT(); + struct _q_iter it; unsigned int i; /* _q_init() */ @@ -39,6 +45,18 @@ static void test_stress(unsigned int N) test_loop_equal(test_q_last(&queue), i, i); } test_loop_passed(); test_equal(_q_size(&queue), 2 * N); + + /* _q_iter_set() */ + for (i = 0; i < (2 * N); i += N) { + _q_iter_set(&queue, &it, i); + test_loop_not_equal((void *)it.it_iter, NULL, i); + test_loop_equal(it.it_pos, i, i); + if (i == N) { + test_loop_equal(test_q_iter_val(&it), 0, i); + } else { + test_loop_equal(test_q_iter_val(&it), N - 1, i); + } + } test_loop_passed(); } static void test_basics() { test_stress(10); }