/* ******************** Singly linked list *************** */
-/* Handle to a singly linked list */
+/**
+ * Handle to a singly linked list
+ */
struct GNUNET_CONTAINER_SList;
-/* Handle to a singly linked list iterator */
+/**
+ * Handle to a singly linked list iterator
+ */
struct GNUNET_CONTAINER_SList_Iterator;
*/
void GNUNET_CONTAINER_slist_add (struct GNUNET_CONTAINER_SList *l, int disp, const void *buf, size_t len);
+
/**
* Create a new singly linked list
* @return the new list
*/
-struct GNUNET_CONTAINER_SList *GNUNET_CONTAINER_slist_create ();
+struct GNUNET_CONTAINER_SList *GNUNET_CONTAINER_slist_create (void);
+
/**
* Destroy a singly linked list
*/
void GNUNET_CONTAINER_slist_destroy (struct GNUNET_CONTAINER_SList *l);
+
/**
* Return the beginning of a list
+ *
* @param l list
- * @return iterator pointing to the beginning
+ * @return iterator pointing to the beginning, free using "GNUNET_free"
*/
-const struct GNUNET_CONTAINER_SList_Iterator *GNUNET_CONTAINER_slist_begin(const struct GNUNET_CONTAINER_SList *l);
+struct GNUNET_CONTAINER_SList_Iterator *
+GNUNET_CONTAINER_slist_begin(struct GNUNET_CONTAINER_SList *l);
+
/**
* Clear a list
+ *
* @param l list
*/
void GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l);
+
/**
* Check if a list contains a certain element
* @param l list
*/
int GNUNET_CONTAINER_slist_contains (const struct GNUNET_CONTAINER_SList *l, const void *buf, size_t len);
+
/**
* Count the elements of a list
* @param l list
*/
int GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l);
+
/**
* Remove an element from the list
* @param i iterator that points to the element to be removed
*/
void GNUNET_CONTAINER_slist_erase (struct GNUNET_CONTAINER_SList_Iterator *i);
+
/**
* Insert an element into a list at a specific position
* @param before where to insert the new element
*/
void GNUNET_CONTAINER_slist_insert (struct GNUNET_CONTAINER_SList_Iterator *before, int disp, const void *buf, size_t len);
+
/**
* Advance an iterator to the next element
* @param i iterator
*/
int GNUNET_CONTAINER_slist_next (struct GNUNET_CONTAINER_SList_Iterator *i);
+
/**
* Check if an iterator points beyond the end of a list
* @param i iterator
*/
int GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i);
+
/**
* Retrieve the element at a specific position in a list
+ *
* @param i iterator
- * @param len payload length
+ * @param len set to the payload length
* @return payload
*/
-void *GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i, size_t *len);
+const void *
+GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
+ size_t *len);
#if 0 /* keep Emacsens' auto-indent happy */
struct GNUNET_CONTAINER_SList_Elem
{
- void *elem;
+ struct GNUNET_CONTAINER_SList_Elem *next;
+ const void *elem;
size_t len;
int disp;
- struct GNUNET_CONTAINER_SList_Elem *next;
};
struct GNUNET_CONTAINER_SList
{
- struct GNUNET_CONTAINER_SList_Elem head;
+ struct GNUNET_CONTAINER_SList_Elem *head;
+ unsigned int length;
};
struct GNUNET_CONTAINER_SList_Iterator
{
+ struct GNUNET_CONTAINER_SList *list;
struct GNUNET_CONTAINER_SList_Elem *last;
struct GNUNET_CONTAINER_SList_Elem *elem;
};
{
struct GNUNET_CONTAINER_SList_Elem *e;
- e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
- e->disp = disp;
if (disp == GNUNET_MEM_DISP_TRANSIENT)
{
- e->elem = GNUNET_malloc (len);
- memcpy (e->elem, buf, len);
+ e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem) + len);
+ memcpy (&e[1], buf, len);
+ e->elem = (const void*) &e[1];
}
else
- e->elem = (void *) buf;
+ {
+ e = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Elem));
+ e->elem = buf;
+ }
+ e->disp = disp;
e->len = len;
-
return e;
}
+
/**
* Add a new element to the list
* @param l list
struct GNUNET_CONTAINER_SList_Elem *e;
e = create_elem (disp, buf, len);
- e->next = l->head.next;
- l->head.next = e;
+ e->next = l->head;
+ l->head = e;
+ l->length++;
}
+
/**
* Create a new singly linked list
* @return the new list
struct GNUNET_CONTAINER_SList *
GNUNET_CONTAINER_slist_create ()
{
- struct GNUNET_CONTAINER_SList *ret;
-
- ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
- if (NULL == ret)
- return NULL;
-
- memset (&ret->head, 0, sizeof (struct GNUNET_CONTAINER_SList));
-
- return ret;
+ return GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList));
}
/**
GNUNET_free (l);
}
+
/**
* Return the beginning of a list
* @param l list
* @return iterator pointing to the beginning
*/
-const struct GNUNET_CONTAINER_SList_Iterator *
-GNUNET_CONTAINER_slist_begin (const struct GNUNET_CONTAINER_SList *l)
+struct GNUNET_CONTAINER_SList_Iterator *
+GNUNET_CONTAINER_slist_begin (struct GNUNET_CONTAINER_SList *l)
{
struct GNUNET_CONTAINER_SList_Iterator *ret;
ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_SList_Iterator));
- ret->elem = l->head.next;
- ret->last = (struct GNUNET_CONTAINER_SList_Elem *) &l->head;
+ ret->elem = l->head;
+ ret->list = l;
return ret;
}
+
/**
* Clear a list
* @param l list
void
GNUNET_CONTAINER_slist_clear (struct GNUNET_CONTAINER_SList *l)
{
- struct GNUNET_CONTAINER_SList_Elem *e, *n;
+ struct GNUNET_CONTAINER_SList_Elem *e;
+ struct GNUNET_CONTAINER_SList_Elem *n;
- e = l->head.next;
+ e = l->head;
while (e != NULL)
{
- if (e->disp != GNUNET_MEM_DISP_STATIC)
- GNUNET_free (e->elem);
n = e->next;
GNUNET_free (e);
e = n;
}
- l->head.next = NULL;
+ l->head = NULL;
+ l->length = 0;
}
+
/**
* Check if a list contains a certain element
+ *
* @param l list
* @param buf payload buffer to find
* @param lenght of the payload
{
struct GNUNET_CONTAINER_SList_Elem *e;
- for (e = l->head.next; e != NULL; e = e->next)
- if (e->len == len && memcmp (buf, e->elem, len) == 0)
+ for (e = l->head; e != NULL; e = e->next)
+ if ( (e->len == len) &&
+ (memcmp (buf, e->elem, len) == 0) )
return GNUNET_YES;
-
return GNUNET_NO;
}
+
/**
* Count the elements of a list
* @param l list
int
GNUNET_CONTAINER_slist_count (const struct GNUNET_CONTAINER_SList *l)
{
- int n;
- struct GNUNET_CONTAINER_SList_Elem *e;
-
- for (n = 0, e = l->head.next; e != NULL; e = e->next)
- n++;
-
- return n;
+ return l->length;
}
+
/**
* Remove an element from the list
+ *
* @param i iterator that points to the element to be removed
*/
void
struct GNUNET_CONTAINER_SList_Elem *next;
next = i->elem->next;
- i->last->next = next;
- if (i->elem->disp != GNUNET_MEM_DISP_STATIC)
- GNUNET_free (i->elem->elem);
+ if (i->last != NULL)
+ i->last->next = next;
+ else
+ i->list->head = next;
GNUNET_free (i->elem);
+ i->list->length--;
i->elem = next;
}
+
/**
* Insert an element into a list at a specific position
* @param before where to insert the new element
e = create_elem (disp, buf, len);
e->next = before->elem;
- before->last->next = e;
+ if (before->last != NULL)
+ before->last->next = e;
+ else
+ before->list->head = e;
+ before->list->length++;
}
+
/**
* Advance an iterator to the next element
* @param i iterator
i->last = i->elem;
i->elem = i->elem->next;
- return i->elem != NULL;
+ return (i->elem != NULL) ? GNUNET_YES : GNUNET_NO;
}
+
/**
* Check if an iterator points beyond the end of a list
+ *
* @param i iterator
* @return GNUNET_YES if the end has been reached, GNUNET_NO if the iterator
* points to a valid element
int
GNUNET_CONTAINER_slist_end (struct GNUNET_CONTAINER_SList_Iterator *i)
{
- return i->elem == NULL;
+ return (i->elem == NULL) ? GNUNET_YES : GNUNET_NO;
}
+
/**
* Retrieve the element at a specific position in a list
* @param i iterator
* @param len payload length
* @return payload
*/
-void *
+const void *
GNUNET_CONTAINER_slist_get (const struct GNUNET_CONTAINER_SList_Iterator *i,
size_t * len)
{
*len = i->elem->len;
return i->elem->elem;
}
+
+/* end of container_slist.c */
struct GNUNET_CONTAINER_SList *l;
struct GNUNET_CONTAINER_SList_Iterator *it;
unsigned int i, j, s;
-
+ const void *p;
GNUNET_log_setup ("test-container-slist", "WARNING", NULL);
GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
GNUNET_CONTAINER_slist_next (it), i--)
{
- void *p;
-
p = GNUNET_CONTAINER_slist_get (it, &s);
CHECK (p != NULL);
j = *(int *) p;
GNUNET_CONTAINER_slist_insert (it, GNUNET_MEM_DISP_TRANSIENT, &j,
sizeof (j));
}
+ GNUNET_free (it);
CHECK (GNUNET_CONTAINER_slist_count (l) == 200);
i = 198;
CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)));
for (it = GNUNET_CONTAINER_slist_begin (l);
GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;)
{
- void *p;
-
p = GNUNET_CONTAINER_slist_get (it, &s);
CHECK (p != NULL);
CHECK (s == sizeof (i));
GNUNET_CONTAINER_slist_erase (it);
}
+ GNUNET_free (it);
CHECK (GNUNET_CONTAINER_slist_count (l) == 100);
i = 99;
CHECK (GNUNET_CONTAINER_slist_contains (l, &i, sizeof (i)) == GNUNET_NO);