diff options
author | Taiyu <taiyu.len@gmail.com> | 2015-08-11 10:44:29 -0700 |
---|---|---|
committer | Taiyu <taiyu.len@gmail.com> | 2015-08-11 10:44:29 -0700 |
commit | 04909886737f715615eb02f9963c66d1c9586ae6 (patch) | |
tree | 355c7ba95574dc6afe2f5256ccaf59fdd406acb3 | |
parent | Merge pull request #16 from taiyu-len/multikey_handling (diff) | |
download | sway-04909886737f715615eb02f9963c66d1c9586ae6.tar.gz sway-04909886737f715615eb02f9963c66d1c9586ae6.tar.zst sway-04909886737f715615eb02f9963c66d1c9586ae6.zip |
list_insert now works as it should
-rw-r--r-- | sway/list.c | 25 |
1 files 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) { | |||
11 | return list; | 11 | return list; |
12 | } | 12 | } |
13 | 13 | ||
14 | static | ||
15 | void list_resize(list_t *list) { | ||
16 | if (list->length == list->capacity) { | ||
17 | list->capacity += 10; | ||
18 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
19 | } | ||
20 | } | ||
21 | |||
14 | void list_free(list_t *list) { | 22 | void list_free(list_t *list) { |
15 | if (list == NULL) { | 23 | if (list == NULL) { |
16 | return; | 24 | return; |
@@ -20,25 +28,20 @@ void list_free(list_t *list) { | |||
20 | } | 28 | } |
21 | 29 | ||
22 | void list_add(list_t *list, void *item) { | 30 | void list_add(list_t *list, void *item) { |
23 | if (list->length == list->capacity) { | 31 | list_resize(list); |
24 | list->capacity += 10; | ||
25 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
26 | } | ||
27 | list->items[list->length++] = item; | 32 | list->items[list->length++] = item; |
28 | } | 33 | } |
29 | 34 | ||
30 | void list_insert(list_t *list, int index, void *item) { | 35 | void list_insert(list_t *list, int index, void *item) { |
31 | // TODO: Implement this properly | 36 | list_resize(list); |
32 | if (list->length == list->capacity) { | 37 | memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); |
33 | list->capacity += 10; | 38 | list->length++; |
34 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | 39 | list->items[index] = item; |
35 | } | ||
36 | list->items[list->length++] = item; | ||
37 | } | 40 | } |
38 | 41 | ||
39 | void list_del(list_t *list, int index) { | 42 | void list_del(list_t *list, int index) { |
40 | list->length--; | 43 | list->length--; |
41 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); | 44 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); |
42 | } | 45 | } |
43 | 46 | ||
44 | void list_cat(list_t *list, list_t *source) { | 47 | void list_cat(list_t *list, list_t *source) { |