New list interface.

This commit is contained in:
Scott Anderson 2017-05-09 21:16:14 +12:00
parent ee81b1aecb
commit d404c4ab28
3 changed files with 242 additions and 118 deletions

View file

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.1.0)
project(sway C) project(sway C)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)

View file

@ -1,134 +1,177 @@
#include "list.h" #include "list.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
list_t *create_list(void) { list_t *list_new(size_t memb_size, size_t capacity) {
list_t *list = malloc(sizeof(list_t)); if (capacity == 0)
if (!list) { capacity = 8;
list_t *l = malloc(sizeof(*l) + memb_size * capacity);
if (!l) {
return NULL; return NULL;
} }
list->capacity = 10;
list->length = 0; l->capacity = capacity;
list->items = malloc(sizeof(void*) * list->capacity); l->length = 0;
return list; l->memb_size = memb_size;
return l;
} }
static void list_resize(list_t *list) { static bool resize(list_t **list) {
if (list->length == list->capacity) { list_t *l = *list;
list->capacity += 10;
list->items = realloc(list->items, sizeof(void*) * list->capacity); if (l->length < l->capacity) {
} return true;
} }
void list_free(list_t *list) { size_t new_cap = l->capacity * 2;
if (list == NULL) { l = realloc(l, sizeof(*l) + l->memb_size * new_cap);
if (!l) {
return false;
}
*list = l;
l->capacity = new_cap;
return true;
}
void list_add(list_t **list, const void *data) {
if (!data || !list || !*list || !resize(list)) {
return; return;
} }
free(list->items); list_t *l = *list;
free(list);
memcpy(l->data + l->memb_size * l->length, data, l->memb_size);
++l->length;
} }
void list_foreach(list_t *list, void (*callback)(void *item)) { void list_insert(list_t **list, size_t index, const void *data) {
if (list == NULL || callback == NULL) { if (!data || !list || !*list || index > (*list)->length || !resize(list)) {
return; return;
} }
for (int i = 0; i < list->length; i++) { list_t *l = *list;
callback(list->items[i]);
size_t size = l->memb_size;
unsigned char (*array)[size] = (void *)l->data;
memmove(&array[index + 1], &array[index], size * (l->length - index));
memcpy(&array[index], data, size);
++l->length;
}
void list_delete(list_t *list, size_t index) {
if (!list || index >= list->length) {
return;
}
size_t size = list->memb_size;
unsigned char (*array)[size] = (void *)list->data;
memmove(&array[index], &array[index + 1], size * (list->length - index));
--list->length;
}
void list_swap(list_t *list, size_t i1, size_t i2) {
if (!list || i1 >= list->length || i2 >= list->length) {
return;
}
size_t size = list->memb_size;
unsigned char (*array)[size] = (void *)list->data;
unsigned char tmp[size];
memcpy(tmp, &array[i1], size);
memcpy(&array[i1], &array[i2], size);
memcpy(&array[i2], tmp, size);
}
void *list_get(list_t *list, size_t index) {
if (!list || index >= list->length) {
return NULL;
}
return list->data + list->memb_size * index;
}
void list_qsort(list_t *list, int compare(const void *, const void *)) {
if (!list || !compare) {
return;
}
qsort(list->data, list->length, list->memb_size, compare);
}
void list_isort(list_t *list, int compare(const void *, const void *)) {
if (!list || !compare) {
return;
}
size_t size = list->memb_size;
unsigned char (*array)[size] = (void *)list->data;
for (size_t i = 1; i < list->length; ++i) {
unsigned char tmp[size];
memcpy(tmp, &array[i], size);
ssize_t j = i - 1;
while (j >= 0 && compare(&array[j], tmp) > 0) {
memcpy(&array[j + 1], &array[j], size);
--j;
}
memcpy(array[j + 1], tmp, size);
} }
} }
void list_add(list_t *list, void *item) { ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *),
list_resize(list); const void *key, void *ret) {
list->items[list->length++] = item; if (!list || !compare || !key) {
}
void list_insert(list_t *list, int index, void *item) {
list_resize(list);
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index));
list->length++;
list->items[index] = item;
}
void list_del(list_t *list, int index) {
list->length--;
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index));
}
void list_cat(list_t *list, list_t *source) {
int i;
for (i = 0; i < source->length; ++i) {
list_add(list, source->items[i]);
}
}
void list_qsort(list_t *list, int compare(const void *left, const void *right)) {
qsort(list->items, list->length, sizeof(void *), compare);
}
int list_seq_find(list_t *list, int compare(const void *item, const void *data), const void *data) {
for (int i = 0; i < list->length; i++) {
void *item = list->items[i];
if (compare(item, data) == 0) {
return i;
}
}
return -1; return -1;
} }
void list_swap(list_t *list, int src, int dest) { const unsigned char *ptr = bsearch(key, list->data, list->length, list->memb_size, compare);
void *tmp = list->items[src]; if (!ptr) {
list->items[src] = list->items[dest]; return -1;
list->items[dest] = tmp; } else {
if (ret) {
memcpy(ret, ptr, list->memb_size);
}
return (ptr - list->data) / list->memb_size;
}
} }
static void list_rotate(list_t *list, int from, int to) { ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *),
void *tmp = list->items[to]; const void *key, void *ret) {
if (!list || !compare || !key) {
while (to > from) { return -1;
list->items[to] = list->items[to - 1];
to--;
} }
list->items[from] = tmp; size_t size = list->memb_size;
unsigned char (*array)[size] = (void *)list->data;
for (size_t i = 0; i < list->length; ++i) {
if (compare(&array[i], key) == 0) {
if (ret) {
memcpy(ret, &array[i], size);
}
return i;
}
} }
static void list_inplace_merge(list_t *list, int left, int last, int mid, int compare(const void *a, const void *b)) { return -1;
int right = mid + 1; }
if (compare(&list->items[mid], &list->items[right]) <= 0) { void list_foreach_cb(list_t *list, void callback(void *)) {
if (!list || !callback) {
return; return;
} }
while (left <= mid && right <= last) { size_t size = list->memb_size;
if (compare(&list->items[left], &list->items[right]) <= 0) { unsigned char (*array)[size] = (void *)list->data;
left++;
} else {
list_rotate(list, left, right);
left++;
mid++;
right++;
}
}
}
static void list_inplace_sort(list_t *list, int first, int last, int compare(const void *a, const void *b)) { for (size_t i = 0; i < list->length; ++i) {
if (first >= last) { callback(&array[i]);
return;
} else if ((last - first) == 1) {
if (compare(&list->items[first], &list->items[last]) > 0) {
list_swap(list, first, last);
}
} else {
int mid = (int)((last + first) / 2);
list_inplace_sort(list, first, mid, compare);
list_inplace_sort(list, mid + 1, last, compare);
list_inplace_merge(list, first, last, mid, compare);
}
}
void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) {
if (list->length > 1) {
list_inplace_sort(list, 0, list->length - 1, compare);
} }
} }

View file

@ -1,27 +1,108 @@
#ifndef _SWAY_LIST_H #ifndef _SWAY_LIST_H
#define _SWAY_LIST_H #define _SWAY_LIST_H
#include <stdalign.h>
#include <stddef.h>
#include <sys/types.h>
typedef struct { typedef struct {
int capacity; size_t capacity;
int length; size_t length;
void **items; size_t memb_size;
alignas(max_align_t) unsigned char data[];
} list_t; } list_t;
list_t *create_list(void); /*
void list_free(list_t *list); * Creates a new list with an inital capacity.
void list_foreach(list_t *list, void (*callback)(void* item)); * If capacity is zero, some default value is used instead.
void list_add(list_t *list, void *item); *
void list_insert(list_t *list, int index, void *item); * memb_size must be the size of the type stored in this list.
void list_del(list_t *list, int index); * If an element is added to this list which is not the same type
void list_cat(list_t *list, list_t *source); * as the elements of the list, the behavior is undefined.
// See qsort. Remember to use *_qsort functions as compare functions, *
// because they dereference the left and right arguments first! * This list should be freed with the stdlib free() function.
void list_qsort(list_t *list, int compare(const void *left, const void *right)); */
// Return index for first item in list that returns 0 for given compare list_t *list_new(size_t memb_size, size_t capacity);
// function or -1 if none matches.
int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to); /*
// stable sort since qsort is not guaranteed to be stable * Adds an element to the end of the list.
void list_stable_sort(list_t *list, int compare(const void *a, const void *b)); */
// swap two elements in a list void list_add(list_t **list, const void *data);
void list_swap(list_t *list, int src, int dest);
/*
* Adds an element at an arbitrary position in the list, moving
* the elements past index to make space.
* index must be less than or equal to the length of the list.
*/
void list_insert(list_t **list, size_t index, const void *data);
/*
* Deletes the element at an arbitrary position in the list,
* moving the elements past index into the space.
* index must be less than the length of the list.
*/
void list_delete(list_t *list, size_t index);
/*
* Swaps the elements at i1 and i2.
* i1 and i2 must both be less than the length of the list.
*/
void list_swap(list_t *list, size_t i1, size_t i2);
/*
* Gets the element of the list at the index.
* index must be less than the length of the list.
*/
void *list_get(list_t *list, size_t index);
/*
* Sorts the list using the stdlib qsort() function.
*/
void list_qsort(list_t *list, int compare(const void *, const void *));
/*
* Sorts the list using insertion sort.
* This should be used if you need a stable sort, and your list is
* short and/or nearly sorted.
*/
void list_isort(list_t *list, int compare(const void *, const void *));
/*
* Returns the index of key in the list, using the stdlib bsearch() function,
* or -1 if it was not found. If ret is not null, the found element will be
* copied into it.
* The list must be sorted.
*/
ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *),
const void *key, void *ret);
/*
* Returns the index of the key in the list, using a linear search,
* or -1 if it was not found. If ret is not null, the found element will be
* copied into it.
*/
ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *),
const void *key, void *ret);
/*
* Calls a function on every item in the list.
*/
void list_foreach_cb(list_t *list, void callback(void *));
/*
* This is for loop helper, iterating over every element in list.
* iter must be a pointer to the list element's type.
* You should not add, remove, or sort elements from the list while inside this loop.
*
* Example:
* char **ptr;
* list_foreach(list_of_strings, ptr) {
* printf("%s\n", *ptr);
* }
*/
#define list_foreach(list, iter) \
for (iter = (void *)list->data; \
(unsigned char *)iter < list->data + list->memb_size * list->length; \
++iter)
#endif #endif