diff options
Diffstat (limited to 'common/list.c')
-rw-r--r-- | common/list.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/common/list.c b/common/list.c index ee268c9a..c3e2fe20 100644 --- a/common/list.c +++ b/common/list.c | |||
@@ -17,7 +17,7 @@ list_t *create_list(void) { | |||
17 | 17 | ||
18 | static void list_resize(list_t *list) { | 18 | static void list_resize(list_t *list) { |
19 | if (list->length == list->capacity) { | 19 | if (list->length == list->capacity) { |
20 | list->capacity += 10; | 20 | list->capacity *= 2; |
21 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | 21 | list->items = realloc(list->items, sizeof(void*) * list->capacity); |
22 | } | 22 | } |
23 | } | 23 | } |
@@ -30,15 +30,6 @@ void list_free(list_t *list) { | |||
30 | free(list); | 30 | free(list); |
31 | } | 31 | } |
32 | 32 | ||
33 | void list_foreach(list_t *list, void (*callback)(void *item)) { | ||
34 | if (list == NULL || callback == NULL) { | ||
35 | return; | ||
36 | } | ||
37 | for (int i = 0; i < list->length; i++) { | ||
38 | callback(list->items[i]); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | void list_add(list_t *list, void *item) { | 33 | void list_add(list_t *list, void *item) { |
43 | list_resize(list); | 34 | list_resize(list); |
44 | list->items[list->length++] = item; | 35 | list->items[list->length++] = item; |
@@ -57,8 +48,7 @@ void list_del(list_t *list, int index) { | |||
57 | } | 48 | } |
58 | 49 | ||
59 | void list_cat(list_t *list, list_t *source) { | 50 | void list_cat(list_t *list, list_t *source) { |
60 | int i; | 51 | for (int i = 0; i < source->length; ++i) { |
61 | for (i = 0; i < source->length; ++i) { | ||
62 | list_add(list, source->items[i]); | 52 | list_add(list, source->items[i]); |
63 | } | 53 | } |
64 | } | 54 | } |
@@ -156,3 +146,15 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { | |||
156 | list_inplace_sort(list, 0, list->length - 1, compare); | 146 | list_inplace_sort(list, 0, list->length - 1, compare); |
157 | } | 147 | } |
158 | } | 148 | } |
149 | |||
150 | void list_free_items_and_destroy(list_t *list) { | ||
151 | if (!list) { | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | for (int i = 0; i < list->length; ++i) { | ||
156 | free(list->items[i]); | ||
157 | } | ||
158 | list_free(list); | ||
159 | } | ||
160 | |||