Added a "tail" pointer to the vector to make insertions more efficient.

This commit is contained in:
Robert James Kaes 2002-05-13 23:32:16 +00:00
parent 16e96c79e8
commit d46cba8a0b
2 changed files with 19 additions and 11 deletions

View File

@ -1,5 +1,8 @@
2002-05-13 Robert James Kaes <rjkaes@flarenet.com> 2002-05-13 Robert James Kaes <rjkaes@flarenet.com>
* src/vector.c: Added the "tail" pointer to make insertions more
efficient.
* src/hashmap.c (hashmap_insert): Fixed a potential SEGFAULT if * src/hashmap.c (hashmap_insert): Fixed a potential SEGFAULT if
the memory for the new hashmap entry could not be allocated. the memory for the new hashmap entry could not be allocated.
(hashmap_remove): Fixed a problem where an entry could have it's (hashmap_remove): Fixed a problem where an entry could have it's

View File

@ -1,4 +1,4 @@
/* $Id: vector.c,v 1.3 2002-04-18 17:57:19 rjkaes Exp $ /* $Id: vector.c,v 1.4 2002-05-13 23:32:16 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
@ -42,7 +42,8 @@ struct vectorentry_s {
struct vector_s { struct vector_s {
size_t num_entries; size_t num_entries;
struct vectorentry_s *vector; struct vectorentry_s *head;
struct vectorentry_s *tail;
}; };
/* /*
@ -62,7 +63,7 @@ vector_create(void)
return NULL; return NULL;
vector->num_entries = 0; vector->num_entries = 0;
vector->vector = NULL; vector->head = vector->tail = NULL;
return vector; return vector;
} }
@ -81,13 +82,13 @@ vector_delete(vector_t vector)
if (!vector) if (!vector)
return -EINVAL; return -EINVAL;
ptr = vector->vector; ptr = vector->head;
while (ptr) { while (ptr) {
next = ptr->next; next = ptr->next;
safefree(ptr->data); safefree(ptr->data);
safefree(ptr); safefree(ptr);
ptr = next; ptr = next;
} }
safefree(vector); safefree(vector);
@ -107,7 +108,7 @@ vector_delete(vector_t vector)
int int
vector_insert(vector_t vector, void *data, ssize_t len) vector_insert(vector_t vector, void *data, ssize_t len)
{ {
struct vectorentry_s *entry, **ptr; struct vectorentry_s *entry;
if (!vector || !data || len <= 0) if (!vector || !data || len <= 0)
return -EINVAL; return -EINVAL;
@ -126,11 +127,15 @@ vector_insert(vector_t vector, void *data, ssize_t len)
entry->len = len; entry->len = len;
entry->next = NULL; entry->next = NULL;
ptr = &vector->vector; if (vector->tail) {
while (*ptr) /* If "tail" is not NULL, it points to the last entry */
ptr = &((*ptr)->next); vector->tail->next = entry;
vector->tail = entry;
} else {
/* No "tail", meaning no entries. Create both. */
vector->head = vector->tail = entry;
}
*ptr = entry;
vector->num_entries++; vector->num_entries++;
return 0; return 0;
@ -156,7 +161,7 @@ vector_getentry(vector_t vector, size_t pos, void **data)
return -ERANGE; return -ERANGE;
loc = 0; loc = 0;
ptr = vector->vector; ptr = vector->head;
while (loc != pos) { while (loc != pos) {
ptr = ptr->next; ptr = ptr->next;