diff options
author | 2015-08-11 14:21:01 -0400 | |
---|---|---|
committer | 2015-08-11 14:21:01 -0400 | |
commit | fe9037ace331d39fde00574a161b88a7f0ee0d44 (patch) | |
tree | 9484c0db80f70ad684648f6546bdfc8826f8eab6 | |
parent | Mention IRC channel in readme (diff) | |
parent | style (diff) | |
download | sway-fe9037ace331d39fde00574a161b88a7f0ee0d44.tar.gz sway-fe9037ace331d39fde00574a161b88a7f0ee0d44.tar.zst sway-fe9037ace331d39fde00574a161b88a7f0ee0d44.zip |
Merge pull request #18 from taiyu-len/master
list_insert now works as it should
-rw-r--r-- | sway/list.c | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/sway/list.c b/sway/list.c index 68455f89..1be1e17c 100644 --- a/sway/list.c +++ b/sway/list.c | |||
@@ -11,6 +11,13 @@ list_t *create_list(void) { | |||
11 | return list; | 11 | return list; |
12 | } | 12 | } |
13 | 13 | ||
14 | static void list_resize(list_t *list) { | ||
15 | if (list->length == list->capacity) { | ||
16 | list->capacity += 10; | ||
17 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
18 | } | ||
19 | } | ||
20 | |||
14 | void list_free(list_t *list) { | 21 | void list_free(list_t *list) { |
15 | if (list == NULL) { | 22 | if (list == NULL) { |
16 | return; | 23 | return; |
@@ -20,25 +27,20 @@ void list_free(list_t *list) { | |||
20 | } | 27 | } |
21 | 28 | ||
22 | void list_add(list_t *list, void *item) { | 29 | void list_add(list_t *list, void *item) { |
23 | if (list->length == list->capacity) { | 30 | list_resize(list); |
24 | list->capacity += 10; | ||
25 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
26 | } | ||
27 | list->items[list->length++] = item; | 31 | list->items[list->length++] = item; |
28 | } | 32 | } |
29 | 33 | ||
30 | void list_insert(list_t *list, int index, void *item) { | 34 | void list_insert(list_t *list, int index, void *item) { |
31 | // TODO: Implement this properly | 35 | list_resize(list); |
32 | if (list->length == list->capacity) { | 36 | memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); |
33 | list->capacity += 10; | 37 | list->length++; |
34 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | 38 | list->items[index] = item; |
35 | } | ||
36 | list->items[list->length++] = item; | ||
37 | } | 39 | } |
38 | 40 | ||
39 | void list_del(list_t *list, int index) { | 41 | void list_del(list_t *list, int index) { |
40 | list->length--; | 42 | list->length--; |
41 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); | 43 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); |
42 | } | 44 | } |
43 | 45 | ||
44 | void list_cat(list_t *list, list_t *source) { | 46 | void list_cat(list_t *list, list_t *source) { |