# Added a vector_prepend() function and recoded the old vector_insert()

to be a general "insert" for both vector_append() and vector_prepend()
This commit is contained in:
Robert James Kaes 2003-05-29 21:07:22 +00:00
parent 8ab278998f
commit c3eaebd1c5
2 changed files with 41 additions and 15 deletions

View File

@ -1,4 +1,4 @@
/* $Id: vector.c,v 1.7 2003-05-29 20:47:52 rjkaes Exp $ /* $Id: vector.c,v 1.8 2003-05-29 21:07:22 rjkaes Exp $
* *
* A vector implementation. The vector can be of an arbitrary length, and * A vector implementation. The vector can be of an arbitrary length, and
* the data for each entry is an lump of data (the size is stored in the * the data for each entry is an lump of data (the size is stored in the
@ -101,16 +101,21 @@ vector_delete(vector_t vector)
* collection of bytes of _len_ octets. The data is copied into the * collection of bytes of _len_ octets. The data is copied into the
* vector, so the original data must be freed to avoid a memory leak. * vector, so the original data must be freed to avoid a memory leak.
* The "data" must be non-NULL and the "len" must be greater than zero. * The "data" must be non-NULL and the "len" must be greater than zero.
* "pos" is either 0 to prepend the data, or 1 to append the data.
* *
* Returns: 0 on success * Returns: 0 on success
* negative number if there are errors * negative number if there are errors
*/ */
int #define INSERT_PREPEND 0
vector_append(vector_t vector, void *data, ssize_t len) #define INSERT_APPEND 1
static int
vector_insert(vector_t vector, void *data, ssize_t len, int pos)
{ {
struct vectorentry_s *entry; struct vectorentry_s *entry;
if (!vector || !data || len <= 0) if (!vector || !data || len <= 0 ||
(pos != INSERT_PREPEND && pos != INSERT_APPEND))
return -EINVAL; return -EINVAL;
entry = safemalloc(sizeof(struct vectorentry_s)); entry = safemalloc(sizeof(struct vectorentry_s));
@ -127,20 +132,41 @@ vector_append(vector_t vector, void *data, ssize_t len)
entry->len = len; entry->len = len;
entry->next = NULL; entry->next = NULL;
if (vector->tail) { /* If there is no head or tail, create them */
/* If "tail" is not NULL, it points to the last entry */ if (!vector->head && !vector->tail)
vector->head = vector->tail = entry;
else if (pos == 0) {
/* prepend the entry */
entry->next = vector->head;
vector->head = entry;
} else {
/* append the entry */
vector->tail->next = entry; vector->tail->next = entry;
vector->tail = entry; vector->tail = entry;
} else {
/* No "tail", meaning no entries. Create both. */
vector->head = vector->tail = entry;
} }
vector->num_entries++; vector->num_entries++;
return 0; return 0;
} }
/*
* The following two function are used to make the API clearer. As you
* can see they simply call the vector_insert() function with appropriate
* arguments.
*/
int
vector_append(vector_t vector, void *data, ssize_t len)
{
return vector_insert(vector, data, len, INSERT_APPEND);
}
int
vector_prepend(vector_t vector, void *data, ssize_t len)
{
return vector_insert(vector, data, len, INSERT_PREPEND);
}
/* /*
* A pointer to the data at position "pos" (zero based) is returned in the * A pointer to the data at position "pos" (zero based) is returned in the
* "data" pointer. If the vector is out of bound, data is set to NULL. * "data" pointer. If the vector is out of bound, data is set to NULL.

View File

@ -1,4 +1,4 @@
/* $Id: vector.h,v 1.2 2003-05-29 20:47:51 rjkaes Exp $ /* $Id: vector.h,v 1.3 2003-05-29 21:07:22 rjkaes Exp $
* *
* A vector implementation. The vector can be of an arbritrary length, and * A vector implementation. The vector can be of an arbritrary length, and
* the data for each entry is an lump of data (the size is stored in the * the data for each entry is an lump of data (the size is stored in the
@ -44,15 +44,15 @@ extern vector_t vector_create(void);
extern int vector_delete(vector_t vector); extern int vector_delete(vector_t vector);
/* /*
* Append an entry to the end of the vector. When you insert a piece of * When you insert a piece of data into the vector, the data will be
* data into the vector, the data will be duplicated, so you must free your * duplicated, so you must free your copy if it was created on the heap.
* copy if it was created on the heap. The data must be non-NULL and the * The data must be non-NULL and the length must be greater than zero.
* length must be greater than zero.
* *
* Returns: negative on error * Returns: negative on error
* 0 upon successful insert. * 0 upon successful insert.
*/ */
extern int vector_append(vector_t vector, void *data, ssize_t len); extern int vector_append(vector_t vector, void *data, ssize_t len);
extern int vector_prepend(vector_t vector, void *data, ssize_t len);
/* /*
* A pointer to the data at position "pos" (zero based) is returned in the * A pointer to the data at position "pos" (zero based) is returned in the