summaryrefslogtreecommitdiffstats
path: root/src/llist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/llist.c')
-rw-r--r--src/llist.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/llist.c b/src/llist.c
index eeded6b..a85cfa9 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -43,6 +43,7 @@ void
llist_init (llist_t *l)
{
l->head = NULL;
+ l->tail = NULL;
}
/*
@@ -60,6 +61,7 @@ llist_free (llist_t *l)
}
l->head = NULL;
+ l->tail = NULL;
}
/*
@@ -113,6 +115,19 @@ llist_next (llist_item_t *i)
}
/*
+ * Return the successor of a list item if it is matched by some filter
+ * callback. Return NULL otherwise.
+ */
+llist_item_t *
+llist_next_filter (llist_item_t *i, long data, llist_fn_match_t fn_match)
+{
+ if (i && i->next && fn_match (i->next->data, data))
+ return i->next;
+ else
+ return NULL;
+}
+
+/*
* Get the actual data of an item.
*/
void *
@@ -128,7 +143,6 @@ void
llist_add (llist_t *l, void *data)
{
llist_item_t *o = mem_malloc (sizeof (llist_item_t));
- llist_item_t *i;
if (o)
{
@@ -136,12 +150,11 @@ llist_add (llist_t *l, void *data)
o->next = NULL;
if (!l->head)
- l->head = o;
+ l->head = l->tail = o;
else
{
- for (i = l->head; i->next; i = i->next)
- ;
- i->next = o;
+ l->tail->next = o;
+ l->tail = o;
}
}
}
@@ -161,7 +174,12 @@ llist_add_sorted (llist_t *l, void *data, llist_fn_cmp_t fn_cmp)
o->next = NULL;
if (!l->head)
- l->head = o;
+ l->head = l->tail = o;
+ else if (fn_cmp(o->data, l->tail->data) >= 0)
+ {
+ l->tail->next = o;
+ l->tail = o;
+ }
else if (fn_cmp(o->data, l->head->data) < 0)
{
o->next = l->head;
@@ -198,6 +216,9 @@ llist_remove (llist_t *l, llist_item_t *i)
{
if (j)
j->next = i->next;
+ if (i == l->tail)
+ l->tail = j;
+
mem_free (i);
}
}