aboutsummaryrefslogtreecommitdiffstats
path: root/sway/list.c
diff options
context:
space:
mode:
authorLibravatar Taiyu <taiyu.len@gmail.com>2015-08-11 10:44:29 -0700
committerLibravatar Taiyu <taiyu.len@gmail.com>2015-08-11 10:44:29 -0700
commit04909886737f715615eb02f9963c66d1c9586ae6 (patch)
tree355c7ba95574dc6afe2f5256ccaf59fdd406acb3 /sway/list.c
parentMerge pull request #16 from taiyu-len/multikey_handling (diff)
downloadsway-04909886737f715615eb02f9963c66d1c9586ae6.tar.gz
sway-04909886737f715615eb02f9963c66d1c9586ae6.tar.zst
sway-04909886737f715615eb02f9963c66d1c9586ae6.zip
list_insert now works as it should
Diffstat (limited to 'sway/list.c')
-rw-r--r--sway/list.c25
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
14static
15void 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
14void list_free(list_t *list) { 22void 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
22void list_add(list_t *list, void *item) { 30void 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
30void list_insert(list_t *list, int index, void *item) { 35void 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
39void list_del(list_t *list, int index) { 42void 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
44void list_cat(list_t *list, list_t *source) { 47void list_cat(list_t *list, list_t *source) {