diff options
Diffstat (limited to 'sway/list.c')
-rw-r--r-- | sway/list.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/sway/list.c b/sway/list.c new file mode 100644 index 00000000..120cfbcd --- /dev/null +++ b/sway/list.c | |||
@@ -0,0 +1,37 @@ | |||
1 | #include "list.h" | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | |||
6 | list_t *create_list() { | ||
7 | list_t *list = malloc(sizeof(list_t)); | ||
8 | list->capacity = 10; | ||
9 | list->length = 0; | ||
10 | list->items = malloc(sizeof(void*) * list->capacity); | ||
11 | return list; | ||
12 | } | ||
13 | |||
14 | void list_free(list_t *list) { | ||
15 | free(list->items); | ||
16 | free(list); | ||
17 | } | ||
18 | |||
19 | void list_add(list_t *list, void *item) { | ||
20 | if (list->length == list->capacity) { | ||
21 | list->capacity += 10; | ||
22 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | ||
23 | } | ||
24 | list->items[list->length++] = item; | ||
25 | } | ||
26 | |||
27 | void list_del(list_t *list, int index) { | ||
28 | list->length--; | ||
29 | memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1)); | ||
30 | } | ||
31 | |||
32 | void list_cat(list_t *list, list_t *source) { | ||
33 | int i; | ||
34 | for (i = 0; i < source->length; ++i) { | ||
35 | list_add(list, source->items[i]); | ||
36 | } | ||
37 | } | ||