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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-12-01 13:55:20 -05:00
parent 1fefbb6b4a
commit f26b50bdf8
2 changed files with 39 additions and 0 deletions

View File

@ -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, \

View File

@ -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); }