Renamed 'data' to 'items', to be more consistent with the old API.

This commit is contained in:
Scott Anderson 2017-05-10 13:18:20 +12:00
parent 8429e871f8
commit 95737e38b8
2 changed files with 31 additions and 30 deletions

View file

@ -6,25 +6,26 @@
#include <string.h> #include <string.h>
list_t *list_new(size_t memb_size, size_t capacity) { list_t *list_new(size_t memb_size, size_t capacity) {
if (capacity == 0) if (capacity == 0) {
capacity = 8; capacity = 8;
}
list_t *l = malloc(sizeof(*l)); list_t *list = malloc(sizeof(*list));
if (!l) { if (!list) {
return NULL; return NULL;
} }
l->capacity = capacity; list->capacity = capacity;
l->length = 0; list->length = 0;
l->memb_size = memb_size; list->memb_size = memb_size;
l->data = malloc(memb_size * capacity); list->items = malloc(memb_size * capacity);
if (!l->data) { if (!list->items) {
free(l); free(list);
return NULL; return NULL;
} }
return l; return list;
} }
void list_free(list_t *list) { void list_free(list_t *list) {
@ -32,7 +33,7 @@ void list_free(list_t *list) {
return; return;
} }
free(list->data); free(list->items);
free(list); free(list);
} }
@ -42,12 +43,12 @@ static bool resize(list_t *list) {
} }
size_t new_cap = list->capacity * 2; size_t new_cap = list->capacity * 2;
void *data = realloc(list->data, list->memb_size * new_cap); void *items = realloc(list->items, list->memb_size * new_cap);
if (!data) { if (!items) {
return false; return false;
} }
list->data = data; list->items = items;
list->capacity = new_cap; list->capacity = new_cap;
return true; return true;
} }
@ -58,7 +59,7 @@ void list_add(list_t *list, const void *data) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
memcpy(&array[list->length], data, size); memcpy(&array[list->length], data, size);
++list->length; ++list->length;
@ -72,7 +73,7 @@ void list_insert(list_t *list, size_t index, const void *data) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
memmove(&array[index + 1], &array[index], size * (list->length - index)); memmove(&array[index + 1], &array[index], size * (list->length - index));
memcpy(&array[index], data, size); memcpy(&array[index], data, size);
++list->length; ++list->length;
@ -84,7 +85,7 @@ void list_delete(list_t *list, size_t index) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
memmove(&array[index], &array[index + 1], size * (list->length - index)); memmove(&array[index], &array[index + 1], size * (list->length - index));
--list->length; --list->length;
@ -95,12 +96,12 @@ void list_delete(list_t *list, size_t index) {
*/ */
if (list->length <= list->capacity / 4 && list->capacity >= 8) { if (list->length <= list->capacity / 4 && list->capacity >= 8) {
size_t new_cap = list->capacity / 2; size_t new_cap = list->capacity / 2;
void *data = realloc(list->data, list->memb_size * new_cap); void *items = realloc(list->items, list->memb_size * new_cap);
if (!data) { if (!items) {
return; return;
} }
list->data = data; list->items = items;
list->capacity = new_cap; list->capacity = new_cap;
} }
} }
@ -111,7 +112,7 @@ void list_swap(list_t *list, size_t i1, size_t i2) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
unsigned char tmp[size]; unsigned char tmp[size];
memcpy(tmp, &array[i1], size); memcpy(tmp, &array[i1], size);
@ -125,7 +126,7 @@ void *list_get(list_t *list, size_t index) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
return &array[index]; return &array[index];
} }
@ -135,7 +136,7 @@ void list_qsort(list_t *list, int compare(const void *, const void *)) {
return; return;
} }
qsort(list->data, list->length, list->memb_size, compare); qsort(list->items, list->length, list->memb_size, compare);
} }
void list_isort(list_t *list, int compare(const void *, const void *)) { void list_isort(list_t *list, int compare(const void *, const void *)) {
@ -144,7 +145,7 @@ void list_isort(list_t *list, int compare(const void *, const void *)) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
for (size_t i = 1; i < list->length; ++i) { for (size_t i = 1; i < list->length; ++i) {
unsigned char tmp[size]; unsigned char tmp[size];
@ -167,14 +168,14 @@ ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *)
return -1; return -1;
} }
const unsigned char *ptr = bsearch(key, list->data, list->length, list->memb_size, compare); const unsigned char *ptr = bsearch(key, list->items, list->length, list->memb_size, compare);
if (!ptr) { if (!ptr) {
return -1; return -1;
} else { } else {
if (ret) { if (ret) {
memcpy(ret, ptr, list->memb_size); memcpy(ret, ptr, list->memb_size);
} }
return (ptr - (unsigned char *)list->data) / list->memb_size; return (ptr - (unsigned char *)list->items) / list->memb_size;
} }
} }
@ -186,7 +187,7 @@ ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *)
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
if (compare(&array[i], key) == 0) { if (compare(&array[i], key) == 0) {
@ -206,7 +207,7 @@ void list_foreach(list_t *list, void callback(void *)) {
} }
size_t size = list->memb_size; size_t size = list->memb_size;
unsigned char (*array)[size] = list->data; unsigned char (*array)[size] = list->items;
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
callback(&array[i]); callback(&array[i]);
@ -218,5 +219,5 @@ void *list_end(list_t *list) {
return NULL; return NULL;
} }
return (unsigned char *)list->data + list->memb_size * list->length; return (unsigned char *)list->items + list->memb_size * list->length;
} }

View file

@ -8,7 +8,7 @@ typedef struct {
size_t capacity; size_t capacity;
size_t length; size_t length;
size_t memb_size; size_t memb_size;
void *data; void *items;
} list_t; } list_t;
/* /*