2014-04-10 17:04:06 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
void
|
|
|
|
list_free_list(struct list *list)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
2014-10-22 19:46:51 +00:00
|
|
|
free(list->items);
|
2014-04-10 17:04:06 +00:00
|
|
|
list->allocated = list->count = 0;
|
2014-10-22 19:46:51 +00:00
|
|
|
list->items = NULL;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
void
|
|
|
|
list_free_items(struct list *list, list_free_fun destructor)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
for (uint32_t i = 0; i < list->count; ++i)
|
|
|
|
destructor(list->items[i]);
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
list_free_list(list);
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
void*
|
|
|
|
list_get_items(const struct list *list, uint32_t *out_nmemb)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
if (out_nmemb)
|
|
|
|
*out_nmemb = list->count;
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
return list->items;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 17:55:45 +00:00
|
|
|
/** !!! Frees the old list, not items !!! */
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_set_items_no_copy(struct list *list, void *items, uint32_t nmemb)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
list_free_list(list);
|
2014-04-10 17:04:06 +00:00
|
|
|
|
|
|
|
if (!items || nmemb == 0) {
|
|
|
|
items = NULL;
|
|
|
|
nmemb = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
list->items = items;
|
2014-04-10 17:04:06 +00:00
|
|
|
list->allocated = list->count = nmemb;
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 17:55:45 +00:00
|
|
|
/** !!! Frees the old items and list !!! */
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_set_items(struct list *list, const void *items, uint32_t nmemb, list_free_fun destructor)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
|
|
|
if (!items || nmemb == 0) {
|
2014-10-22 19:46:51 +00:00
|
|
|
list_free_items(list, destructor);
|
|
|
|
return true;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
void *new_items;
|
|
|
|
if (!(new_items = calloc(sizeof(void*), nmemb)))
|
|
|
|
return false;
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
memcpy(new_items, items, sizeof(void*) * nmemb);
|
|
|
|
return list_set_items_no_copy(list, new_items, nmemb);
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_grow(struct list *list, uint32_t step)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
|
|
|
void *tmp;
|
2014-10-25 18:53:48 +00:00
|
|
|
uint32_t nsize = sizeof(void*) * (list->allocated + step);
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
if (!(tmp = realloc(list->items, nsize)))
|
|
|
|
return false;
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
list->items = tmp;
|
2014-04-10 17:04:06 +00:00
|
|
|
list->allocated += step;
|
2014-10-25 18:53:48 +00:00
|
|
|
memset(&list->items[list->count], 0, sizeof(void*) * (list->allocated - list->count));
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_add_item_at(struct list *list, void *item, uint32_t index)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
assert(list && item);
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
if ((!list->items || list->allocated <= list->count) && !list_grow(list, 32))
|
|
|
|
return false;
|
2014-04-10 17:04:06 +00:00
|
|
|
|
|
|
|
if (index + 1 != list->count) {
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t i = index;
|
2014-10-25 18:53:48 +00:00
|
|
|
memmove(&list->items[i + 1], &list->items[i], sizeof(void*) * (list->count - i));
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
list->items[index] = item;
|
2014-04-10 17:04:06 +00:00
|
|
|
list->count++;
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_add_item(struct list *list, void *item)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
2014-10-22 19:46:51 +00:00
|
|
|
return list_add_item_at(list, item, list->count);
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_remove_item_at(struct list *list, uint32_t index)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
|
|
|
assert(list);
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t i = index;
|
|
|
|
if (!list->items || list->count <= i)
|
|
|
|
return false;
|
2014-04-10 17:04:06 +00:00
|
|
|
|
2014-10-25 18:53:48 +00:00
|
|
|
memmove(&list->items[i], &list->items[i + 1], sizeof(void*) * (list->count - i));
|
|
|
|
list->count--;
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
bool
|
|
|
|
list_remove_item(struct list *list, const void *item)
|
2014-04-10 17:04:06 +00:00
|
|
|
{
|
2014-10-24 22:38:30 +00:00
|
|
|
assert(list && item);
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < list->count && list->items[i] != item; ++i);
|
|
|
|
return list_remove_item_at(list, i);
|
2014-04-10 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
void
|
|
|
|
list_sort(struct list *list, int (*compar)(const void *a, const void *b))
|
|
|
|
{
|
|
|
|
assert(list && compar);
|
|
|
|
qsort(list->items, list->count, sizeof(void*), compar);
|
|
|
|
}
|
|
|
|
|
2014-04-10 17:04:06 +00:00
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|