aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortoelke <toelke@140774ce-b5e7-0310-ab8b-a85725594a96>2010-09-08 11:14:29 +0000
committertoelke <toelke@140774ce-b5e7-0310-ab8b-a85725594a96>2010-09-08 11:14:29 +0000
commit040917f7b5e4e624c211507863866c65bc673d07 (patch)
tree0abfbc77dbcb573b8921e134f426bba54f6c5f54 /src
parent181102ae77329b15a9e4fd898599a0ff2ba0a041 (diff)
implemented a way to add an element to the end of a slist
git-svn-id: https://gnunet.org/svn/gnunet@12920 140774ce-b5e7-0310-ab8b-a85725594a96
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_container_lib.h12
-rw-r--r--src/util/container_slist.c34
-rw-r--r--src/util/test_container_slist.c28
3 files changed, 73 insertions, 1 deletions
diff --git a/src/include/gnunet_container_lib.h b/src/include/gnunet_container_lib.h
index 19946d6f79..b19dd52dc0 100644
--- a/src/include/gnunet_container_lib.h
+++ b/src/include/gnunet_container_lib.h
@@ -966,6 +966,18 @@ void GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
/**
+ * Add a new element to the end of the list
+ * @param l list
+ * @param disp memory disposition
+ * @param buf payload buffer
+ * @param len length of the buffer
+ */
+void GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
+ enum GNUNET_CONTAINER_SListDisposition disp,
+ const void *buf, size_t len);
+
+
+/**
* Append a singly linked list to another
* @param dst list to append to
* @param src source
diff --git a/src/util/container_slist.c b/src/util/container_slist.c
index fb9ab55587..8c4886ef54 100644
--- a/src/util/container_slist.c
+++ b/src/util/container_slist.c
@@ -65,6 +65,11 @@ struct GNUNET_CONTAINER_SList
struct GNUNET_CONTAINER_SList_Elem *head;
/**
+ * Tail of the linked list.
+ */
+ struct GNUNET_CONTAINER_SList_Elem *tail;
+
+ /**
* Number of elements in the list.
*/
unsigned int length;
@@ -141,6 +146,30 @@ GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l,
e = create_elem (disp, buf, len);
e->next = l->head;
l->head = e;
+ if (l->tail == NULL) l->tail = e;
+ l->length++;
+}
+
+/**
+ * Add a new element to the end of the list
+ * @param l list
+ * @param disp memory disposition
+ * @param buf payload buffer
+ * @param len length of the buffer
+ */
+void
+GNUNET_CONTAINER_slist_add_end (struct GNUNET_CONTAINER_SList *l,
+ enum GNUNET_CONTAINER_SListDisposition disp,
+ const void *buf, size_t len)
+{
+ struct GNUNET_CONTAINER_SList_Elem *e;
+
+ e = create_elem (disp, buf, len);
+ if (l->tail != NULL)
+ l->tail->next = e;
+ if (l->head == NULL)
+ l->head = e;
+ l->tail = e;
l->length++;
}
@@ -228,6 +257,7 @@ GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
e = n;
}
l->head = NULL;
+ l->tail = NULL;
l->length = 0;
}
@@ -279,6 +309,8 @@ GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i)
i->last->next = next;
else
i->list->head = next;
+ if (next == NULL)
+ i->list->tail = i->last;
if (i->elem->disp == GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC)
GNUNET_free (i->elem->elem);
GNUNET_free (i->elem);
@@ -307,6 +339,8 @@ GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before,
before->last->next = e;
else
before->list->head = e;
+ if (e->next == NULL)
+ before->list->tail = e;
before->list->length++;
}
diff --git a/src/util/test_container_slist.c b/src/util/test_container_slist.c
index af3c3f39a3..fc3e8a2a4c 100644
--- a/src/util/test_container_slist.c
+++ b/src/util/test_container_slist.c
@@ -101,7 +101,7 @@ main (int argc, char *argv[])
i = 99;
CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_NO);
i = 198;
- CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
+ CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_YES);
GNUNET_CONTAINER_slist_clear (l);
CHECK (GNUNET_CONTAINER_slist_count (l) == 0);
@@ -116,6 +116,32 @@ main (int argc, char *argv[])
GNUNET_CONTAINER_slist_destroy (l);
+ /*check slist_add_end*/
+ l = GNUNET_CONTAINER_slist_create ();
+ for (i = 0; i < 100; i++)
+ GNUNET_CONTAINER_slist_add_end (l,
+ GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ &i, sizeof (i));
+
+ CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
+
+ for (it = GNUNET_CONTAINER_slist_begin (l), i = 0;
+ GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
+ GNUNET_CONTAINER_slist_next (it), i++)
+ {
+ p = GNUNET_CONTAINER_slist_get (it, &s);
+
+ if ((p == NULL) ||
+ (i != *(int *) p) ||
+ (s != sizeof (i)))
+ {
+ GNUNET_CONTAINER_slist_iter_destroy (it);
+ CHECK (0);
+ }
+ }
+
+ GNUNET_CONTAINER_slist_destroy (l);
+
/*check if disp = GNUNET_CONTAINER_SLIST_DISPOSITION_DYNAMIC*/
l = GNUNET_CONTAINER_slist_create ();