Add g_queue_insert_{before,after}_link
When building trees of objects, I seem to have a common pattern of something like the following, so that most operations can be done in O(1) time if you have a node and it's parent (or possibly a sibling).
typedef struct {
GQueue children;
GList link;
} Node;
In this example, the child's link
is used as the GList
item in the parents children
.
However, when implementing this in various places, I seem to still copy around some code like the following.
static inline void
_g_queue_insert_before_link (GQueue *queue,
GList *sibling,
GList *item)
{
item->prev = sibling ? sibling->prev : NULL;
item->next = sibling;
sibling->prev = item;
if (item->prev == NULL)
queue->head = item;
else
item->prev->next = item;
queue->length++;
}
static inline void
_g_queue_insert_after_link (GQueue *queue,
GList *sibling,
GList *item)
{
item->prev = sibling;
item->next = sibling ? sibling->next : NULL;
sibling->next = item;
if (item->next == NULL)
queue->tail = item;
else
item->next->prev = item;
queue->length++;
}
It probably makes sense to just add that to glib if there are no objections. Will make a MR if you like.
Edited by Christian Hergert