2007-03-13 08:26:18 +00:00
|
|
|
/*
|
2007-03-13 08:26:23 +00:00
|
|
|
* $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
|
2007-03-13 08:26:18 +00:00
|
|
|
*
|
2007-03-13 08:26:23 +00:00
|
|
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
2007-03-13 08:26:18 +00:00
|
|
|
* Michael Clark <michael@metaparadigm.com>
|
|
|
|
|
*
|
2007-03-13 08:26:23 +00:00
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the MIT license. See COPYING for details.
|
2007-03-13 08:26:18 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-03-13 08:26:20 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2016-01-11 12:15:54 +01:00
|
|
|
#include <limits.h>
|
|
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#ifdef STDC_HEADERS
|
2020-03-28 10:25:00 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
|
|
2012-05-21 23:22:36 +01:00
|
|
|
#if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
|
2020-03-28 10:25:00 +08:00
|
|
|
#include <strings.h>
|
2007-03-13 08:26:20 +00:00
|
|
|
#endif /* HAVE_STRINGS_H */
|
2007-03-13 08:26:18 +00:00
|
|
|
|
2017-08-30 23:35:56 -04:00
|
|
|
#ifndef SIZE_T_MAX
|
2016-05-23 02:10:58 +00:00
|
|
|
#if SIZEOF_SIZE_T == SIZEOF_INT
|
|
|
|
|
#define SIZE_T_MAX UINT_MAX
|
|
|
|
|
#elif SIZEOF_SIZE_T == SIZEOF_LONG
|
|
|
|
|
#define SIZE_T_MAX ULONG_MAX
|
2017-01-07 22:55:31 -05:00
|
|
|
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
|
|
|
|
|
#define SIZE_T_MAX ULLONG_MAX
|
2016-05-23 02:10:58 +00:00
|
|
|
#else
|
|
|
|
|
#error Unable to determine size of size_t
|
|
|
|
|
#endif
|
2017-08-30 23:35:56 -04:00
|
|
|
#endif
|
2016-05-23 02:10:58 +00:00
|
|
|
|
2007-03-13 08:26:18 +00:00
|
|
|
#include "arraylist.h"
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
struct array_list *array_list_new(array_list_free_fn *free_fn)
|
2020-06-20 18:03:04 +00:00
|
|
|
{
|
|
|
|
|
return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
struct array_list *arr;
|
|
|
|
|
|
2020-08-22 12:06:15 +02:00
|
|
|
if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *))
|
|
|
|
|
return NULL;
|
2020-05-24 03:53:32 +00:00
|
|
|
arr = (struct array_list *)malloc(sizeof(struct array_list));
|
2020-03-28 10:25:00 +08:00
|
|
|
if (!arr)
|
|
|
|
|
return NULL;
|
2020-06-20 18:03:04 +00:00
|
|
|
arr->size = initial_size;
|
2020-03-28 10:25:00 +08:00
|
|
|
arr->length = 0;
|
|
|
|
|
arr->free_fn = free_fn;
|
2020-05-24 03:53:32 +00:00
|
|
|
if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
|
2020-03-28 10:25:00 +08:00
|
|
|
{
|
|
|
|
|
free(arr);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
extern void array_list_free(struct array_list *arr)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
size_t i;
|
|
|
|
|
for (i = 0; i < arr->length; i++)
|
|
|
|
|
if (arr->array[i])
|
|
|
|
|
arr->free_fn(arr->array[i]);
|
|
|
|
|
free(arr->array);
|
|
|
|
|
free(arr);
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
void *array_list_get_idx(struct array_list *arr, size_t i)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
if (i >= arr->length)
|
|
|
|
|
return NULL;
|
|
|
|
|
return arr->array[i];
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:56:59 -04:00
|
|
|
static int array_list_expand_internal(struct array_list *arr, size_t max)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
void *t;
|
|
|
|
|
size_t new_size;
|
|
|
|
|
|
|
|
|
|
if (max < arr->size)
|
|
|
|
|
return 0;
|
|
|
|
|
/* Avoid undefined behaviour on size_t overflow */
|
|
|
|
|
if (arr->size >= SIZE_T_MAX / 2)
|
|
|
|
|
new_size = max;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
new_size = arr->size << 1;
|
|
|
|
|
if (new_size < max)
|
|
|
|
|
new_size = max;
|
|
|
|
|
}
|
|
|
|
|
if (new_size > (~((size_t)0)) / sizeof(void *))
|
|
|
|
|
return -1;
|
|
|
|
|
if (!(t = realloc(arr->array, new_size * sizeof(void *))))
|
|
|
|
|
return -1;
|
|
|
|
|
arr->array = (void **)t;
|
|
|
|
|
arr->size = new_size;
|
|
|
|
|
return 0;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 18:03:04 +00:00
|
|
|
int array_list_shrink(struct array_list *arr, size_t empty_slots)
|
|
|
|
|
{
|
|
|
|
|
void *t;
|
|
|
|
|
size_t new_size;
|
|
|
|
|
|
2020-08-22 12:06:15 +02:00
|
|
|
if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length)
|
|
|
|
|
return -1;
|
2020-06-20 18:03:04 +00:00
|
|
|
new_size = arr->length + empty_slots;
|
|
|
|
|
if (new_size == arr->size)
|
|
|
|
|
return 0;
|
|
|
|
|
if (new_size > arr->size)
|
|
|
|
|
return array_list_expand_internal(arr, new_size);
|
|
|
|
|
if (new_size == 0)
|
|
|
|
|
new_size = 1;
|
|
|
|
|
|
|
|
|
|
if (!(t = realloc(arr->array, new_size * sizeof(void *))))
|
|
|
|
|
return -1;
|
|
|
|
|
arr->array = (void **)t;
|
|
|
|
|
arr->size = new_size;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 16:12:22 +03:00
|
|
|
int array_list_insert_idx(struct array_list *arr, size_t idx, void *data)
|
|
|
|
|
{
|
|
|
|
|
size_t move_amount;
|
|
|
|
|
|
|
|
|
|
if (idx >= arr->length)
|
|
|
|
|
return array_list_put_idx(arr, idx, data);
|
|
|
|
|
|
|
|
|
|
/* we're at full size, what size_t can support */
|
|
|
|
|
if (arr->length == SIZE_T_MAX)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (array_list_expand_internal(arr, arr->length + 1))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
move_amount = (arr->length - idx) * sizeof(void *);
|
|
|
|
|
memmove(arr->array + idx + 1, arr->array + idx, move_amount);
|
|
|
|
|
arr->array[idx] = data;
|
|
|
|
|
arr->length++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 03:53:32 +00:00
|
|
|
//static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
|
2020-03-28 10:25:00 +08:00
|
|
|
int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
if (idx > SIZE_T_MAX - 1)
|
|
|
|
|
return -1;
|
|
|
|
|
if (array_list_expand_internal(arr, idx + 1))
|
|
|
|
|
return -1;
|
|
|
|
|
if (idx < arr->length && arr->array[idx])
|
|
|
|
|
arr->free_fn(arr->array[idx]);
|
|
|
|
|
arr->array[idx] = data;
|
2020-05-24 03:53:32 +00:00
|
|
|
if (idx > arr->length)
|
|
|
|
|
{
|
|
|
|
|
/* Zero out the arraylist slots in between the old length
|
|
|
|
|
and the newly added entry so we know those entries are
|
|
|
|
|
empty.
|
|
|
|
|
e.g. when setting array[7] in an array that used to be
|
|
|
|
|
only 5 elements longs, array[5] and array[6] need to be
|
|
|
|
|
set to 0.
|
|
|
|
|
*/
|
|
|
|
|
memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
|
|
|
|
|
}
|
2020-03-28 10:25:00 +08:00
|
|
|
if (arr->length <= idx)
|
|
|
|
|
arr->length = idx + 1;
|
|
|
|
|
return 0;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
int array_list_add(struct array_list *arr, void *data)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-05-24 03:53:32 +00:00
|
|
|
/* Repeat some of array_list_put_idx() so we can skip several
|
|
|
|
|
checks that we know are unnecessary when appending at the end
|
|
|
|
|
*/
|
|
|
|
|
size_t idx = arr->length;
|
|
|
|
|
if (idx > SIZE_T_MAX - 1)
|
|
|
|
|
return -1;
|
|
|
|
|
if (array_list_expand_internal(arr, idx + 1))
|
|
|
|
|
return -1;
|
|
|
|
|
arr->array[idx] = data;
|
|
|
|
|
arr->length++;
|
|
|
|
|
return 0;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
|
2011-10-07 21:07:18 +02:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
|
2014-08-21 15:42:50 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
void *array_list_bsearch(const void **key, struct array_list *arr,
|
|
|
|
|
int (*compar)(const void *, const void *))
|
2014-08-21 15:42:50 +02:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
|
2011-10-07 21:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
size_t array_list_length(struct array_list *arr)
|
2007-03-13 08:26:18 +00:00
|
|
|
{
|
2020-03-28 10:25:00 +08:00
|
|
|
return arr->length;
|
2007-03-13 08:26:18 +00:00
|
|
|
}
|
2015-04-02 14:05:27 -07:00
|
|
|
|
2020-03-28 10:25:00 +08:00
|
|
|
int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
|
2015-04-02 14:05:27 -07:00
|
|
|
{
|
2016-05-23 02:10:58 +00:00
|
|
|
size_t i, stop;
|
2015-04-02 14:05:27 -07:00
|
|
|
|
2020-05-04 19:41:16 +02:00
|
|
|
/* Avoid overflow in calculation with large indices. */
|
|
|
|
|
if (idx > SIZE_T_MAX - count)
|
|
|
|
|
return -1;
|
2015-04-02 14:05:27 -07:00
|
|
|
stop = idx + count;
|
2020-03-28 10:25:00 +08:00
|
|
|
if (idx >= arr->length || stop > arr->length)
|
|
|
|
|
return -1;
|
|
|
|
|
for (i = idx; i < stop; ++i)
|
|
|
|
|
{
|
2020-06-20 18:03:04 +00:00
|
|
|
// Because put_idx can skip entries, we need to check if
|
|
|
|
|
// there's actually anything in each slot we're erasing.
|
2020-03-28 10:25:00 +08:00
|
|
|
if (arr->array[i])
|
|
|
|
|
arr->free_fn(arr->array[i]);
|
2015-04-02 14:05:27 -07:00
|
|
|
}
|
2020-03-28 10:25:00 +08:00
|
|
|
memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
|
2015-04-02 14:05:27 -07:00
|
|
|
arr->length -= count;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|