core/containers/queue: Add _q_for_each() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-30 14:45:39 -05:00
parent f26b50bdf8
commit e87e37414d
2 changed files with 30 additions and 1 deletions

View File

@ -16,6 +16,20 @@ struct _q_iter {
};
/* Called to initialize a queue iterator. */
static inline void _q_iter_init(const struct _queue *queue, struct _q_iter *it)
{
it->it_iter = g_list_first(queue->_queue.head);
it->it_pos = 0;
}
/* Called to advance a queue iterator by one step. */
static inline void _q_iter_next(struct _q_iter *it)
{
it->it_iter = g_list_next(it->it_iter);
it->it_pos++;
}
/* 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)
@ -30,6 +44,9 @@ static inline gpointer _q_iter_val(struct _q_iter *it)
return (it->it_iter) ? it->it_iter->data : NULL;
}
#define _q_for_each(queue, it) \
for (_q_iter_init(queue, it); (it)->it_iter; _q_iter_next(it))
#define _Q_INIT() \
{ \
@ -62,5 +79,4 @@ static inline guint _q_add_tail(struct _queue *queue, gpointer data)
return _q_size(queue) - 1;
}
#endif /* OCARINA_CORE_CONTAINERS_QUEUE_H */

View File

@ -57,6 +57,19 @@ static void test_stress(unsigned int N)
test_loop_equal(test_q_iter_val(&it), N - 1, i);
}
} test_loop_passed();
/* _q_for_each() */
i = 0;
_q_for_each(&queue, &it) {
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), N - (i + 1), i);
} else {
test_loop_equal(test_q_iter_val(&it), i - N, i);
}
i++;
} test_loop_passed();
}
static void test_basics() { test_stress(10); }