From 04909886737f715615eb02f9963c66d1c9586ae6 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Tue, 11 Aug 2015 10:44:29 -0700 Subject: [PATCH 1/2] list_insert now works as it should --- sway/list.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/sway/list.c b/sway/list.c index 68455f89..f01efc92 100644 --- a/sway/list.c +++ b/sway/list.c @@ -11,6 +11,14 @@ list_t *create_list(void) { return list; } +static +void list_resize(list_t *list) { + if (list->length == list->capacity) { + list->capacity += 10; + list->items = realloc(list->items, sizeof(void*) * list->capacity); + } +} + void list_free(list_t *list) { if (list == NULL) { return; @@ -20,25 +28,20 @@ void list_free(list_t *list) { } void list_add(list_t *list, void *item) { - if (list->length == list->capacity) { - list->capacity += 10; - list->items = realloc(list->items, sizeof(void*) * list->capacity); - } + list_resize(list); list->items[list->length++] = item; } void list_insert(list_t *list, int index, void *item) { - // TODO: Implement this properly - if (list->length == list->capacity) { - list->capacity += 10; - list->items = realloc(list->items, sizeof(void*) * list->capacity); - } - list->items[list->length++] = 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->capacity - index - 1)); + memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); } void list_cat(list_t *list, list_t *source) { From 4c8749822249f9cb5a80c662789732bcf7463893 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Tue, 11 Aug 2015 11:12:50 -0700 Subject: [PATCH 2/2] style --- sway/list.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sway/list.c b/sway/list.c index f01efc92..1be1e17c 100644 --- a/sway/list.c +++ b/sway/list.c @@ -11,8 +11,7 @@ list_t *create_list(void) { return list; } -static -void list_resize(list_t *list) { +static void list_resize(list_t *list) { if (list->length == list->capacity) { list->capacity += 10; list->items = realloc(list->items, sizeof(void*) * list->capacity);