109 lines
2.5 KiB
C
109 lines
2.5 KiB
C
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#ifndef OCARINA_CORE_CONTAINERS_QUEUE_H
|
|
#define OCARINA_CORE_CONTAINERS_QUEUE_H
|
|
|
|
#include <glib.h>
|
|
|
|
struct _queue {
|
|
GQueue _queue;
|
|
};
|
|
|
|
struct _q_iter {
|
|
guint it_pos;
|
|
GList *it_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 rewind a queue iterator by one step. */
|
|
static inline void _q_iter_prev(struct _q_iter *it)
|
|
{
|
|
it->it_iter = g_list_previous(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)
|
|
{
|
|
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_for_each(queue, it) \
|
|
for (_q_iter_init(queue, it); (it)->it_iter; _q_iter_next(it))
|
|
|
|
|
|
#define _Q_INIT() \
|
|
{ \
|
|
._queue = G_QUEUE_INIT, \
|
|
}
|
|
|
|
/* Called to initialize a queue. */
|
|
static inline void _q_init(struct _queue *queue)
|
|
{
|
|
g_queue_init(&queue->_queue);
|
|
}
|
|
|
|
/* Called to find the size of a queue. */
|
|
static inline guint _q_size(struct _queue *queue)
|
|
{
|
|
return g_queue_get_length(&queue->_queue);
|
|
}
|
|
|
|
/* Called to add an item to the head of a queue. */
|
|
static inline guint _q_add_head(struct _queue *queue, gpointer data)
|
|
{
|
|
g_queue_push_head(&queue->_queue, data);
|
|
return 0;
|
|
}
|
|
|
|
/* Called to add an item to the tail of a queue. */
|
|
static inline guint _q_add_tail(struct _queue *queue, gpointer data)
|
|
{
|
|
g_queue_push_tail(&queue->_queue, data);
|
|
return _q_size(queue) - 1;
|
|
}
|
|
|
|
/* Called to add an item to a sorted queue. */
|
|
guint _q_add_sorted(struct _queue *, gpointer, GCompareDataFunc, gpointer);
|
|
|
|
/* Called to remove all items from a queue */
|
|
static inline void _q_clear(struct _queue *queue)
|
|
{
|
|
g_queue_clear(&queue->_queue);
|
|
}
|
|
|
|
/* Called to remove an item by iterator. */
|
|
gpointer _q_remove_it(struct _queue *, struct _q_iter *);
|
|
|
|
/* Called to sort the queue. */
|
|
static inline void _q_sort(struct _queue *queue, GCompareDataFunc func,
|
|
gpointer user_data)
|
|
{
|
|
g_queue_sort(&queue->_queue, func, user_data);
|
|
}
|
|
|
|
#endif /* OCARINA_CORE_CONTAINERS_QUEUE_H */
|