Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list_insert now works as it should #18

Merged
merged 2 commits into from
Aug 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions sway/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ list_t *create_list(void) {
return list;
}

static void list_resize(list_t *list) {
if (list->length == list->capacity) {
list->capacity += 10;
list->items = realloc(list->items, sizeof(void*) * list->capacity);
}
}

void list_free(list_t *list) {
if (list == NULL) {
return;
Expand All @@ -20,25 +27,20 @@ void list_free(list_t *list) {
}

void list_add(list_t *list, void *item) {
if (list->length == list->capacity) {
list->capacity += 10;
list->items = realloc(list->items, sizeof(void*) * list->capacity);
}
list_resize(list);
list->items[list->length++] = item;
}

void list_insert(list_t *list, int index, void *item) {
// TODO: Implement this properly
if (list->length == list->capacity) {
list->capacity += 10;
list->items = realloc(list->items, sizeof(void*) * list->capacity);
}
list->items[list->length++] = item;
list_resize(list);
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index));
list->length++;
list->items[index] = item;
}

void list_del(list_t *list, int index) {
list->length--;
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->capacity - index - 1));
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using length here is a waste of cycles. If we memmove with the capacity, we move fewer items (leaving garbage from items[list->length ... list->capacity]).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capacity is the one that moves more though.
length is always less then capacity, so it moves less.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my bad, I read the diff backwards.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, i wanted to try to implement the multikey handling with list_t but compiler complains about int->void* void*->int to much.
ill send another pr with it edited a bit later. maybe

}

void list_cat(list_t *list, list_t *source) {
Expand Down