|
Packit Service |
def718 |
/*
|
|
Packit Service |
def718 |
* $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
|
|
Packit Service |
def718 |
*
|
|
Packit Service |
def718 |
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
|
Packit Service |
def718 |
* Michael Clark <michael@metaparadigm.com>
|
|
Packit Service |
def718 |
*
|
|
Packit Service |
def718 |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
def718 |
* it under the terms of the MIT license. See COPYING for details.
|
|
Packit Service |
def718 |
*
|
|
Packit Service |
def718 |
*/
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include "config.h"
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include <limits.h>
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#ifdef STDC_HEADERS
|
|
Packit Service |
def718 |
# include <stdlib.h>
|
|
Packit Service |
def718 |
# include <string.h>
|
|
Packit Service |
def718 |
#endif /* STDC_HEADERS */
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
|
|
Packit Service |
def718 |
# include <strings.h>
|
|
Packit Service |
def718 |
#endif /* HAVE_STRINGS_H */
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#ifndef SIZE_T_MAX
|
|
Packit Service |
def718 |
#if SIZEOF_SIZE_T == SIZEOF_INT
|
|
Packit Service |
def718 |
#define SIZE_T_MAX UINT_MAX
|
|
Packit Service |
def718 |
#elif SIZEOF_SIZE_T == SIZEOF_LONG
|
|
Packit Service |
def718 |
#define SIZE_T_MAX ULONG_MAX
|
|
Packit Service |
def718 |
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
|
|
Packit Service |
def718 |
#define SIZE_T_MAX ULLONG_MAX
|
|
Packit Service |
def718 |
#else
|
|
Packit Service |
def718 |
#error Unable to determine size of size_t
|
|
Packit Service |
def718 |
#endif
|
|
Packit Service |
def718 |
#endif
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
#include "arraylist.h"
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
struct array_list*
|
|
Packit Service |
def718 |
array_list_new(array_list_free_fn *free_fn)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
struct array_list *arr;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
arr = (struct array_list*)calloc(1, sizeof(struct array_list));
|
|
Packit Service |
def718 |
if(!arr) return NULL;
|
|
Packit Service |
def718 |
arr->size = ARRAY_LIST_DEFAULT_SIZE;
|
|
Packit Service |
def718 |
arr->length = 0;
|
|
Packit Service |
def718 |
arr->free_fn = free_fn;
|
|
Packit Service |
def718 |
if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
|
|
Packit Service |
def718 |
free(arr);
|
|
Packit Service |
def718 |
return NULL;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
return arr;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
extern void
|
|
Packit Service |
def718 |
array_list_free(struct array_list *arr)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
size_t i;
|
|
Packit Service |
def718 |
for(i = 0; i < arr->length; i++)
|
|
Packit Service |
def718 |
if(arr->array[i]) arr->free_fn(arr->array[i]);
|
|
Packit Service |
def718 |
free(arr->array);
|
|
Packit Service |
def718 |
free(arr);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
void*
|
|
Packit Service |
def718 |
array_list_get_idx(struct array_list *arr, size_t i)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
if(i >= arr->length) return NULL;
|
|
Packit Service |
def718 |
return arr->array[i];
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
static int array_list_expand_internal(struct array_list *arr, size_t max)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
void *t;
|
|
Packit Service |
def718 |
size_t new_size;
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
if(max < arr->size) return 0;
|
|
Packit Service |
def718 |
/* Avoid undefined behaviour on size_t overflow */
|
|
Packit Service |
def718 |
if( arr->size >= SIZE_T_MAX / 2 )
|
|
Packit Service |
def718 |
new_size = max;
|
|
Packit Service |
def718 |
else
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
new_size = arr->size << 1;
|
|
Packit Service |
def718 |
if (new_size < max)
|
|
Packit Service |
def718 |
new_size = max;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
|
|
Packit Service |
def718 |
if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
|
|
Packit Service |
def718 |
arr->array = (void**)t;
|
|
Packit Service |
def718 |
(void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
|
|
Packit Service |
def718 |
arr->size = new_size;
|
|
Packit Service |
def718 |
return 0;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int
|
|
Packit Service |
def718 |
array_list_put_idx(struct array_list *arr, size_t idx, void *data)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
if (idx > SIZE_T_MAX - 1 ) return -1;
|
|
Packit Service |
def718 |
if(array_list_expand_internal(arr, idx+1)) return -1;
|
|
Packit Service |
def718 |
if(idx < arr->length && arr->array[idx])
|
|
Packit Service |
def718 |
arr->free_fn(arr->array[idx]);
|
|
Packit Service |
def718 |
arr->array[idx] = data;
|
|
Packit Service |
def718 |
if(arr->length <= idx) arr->length = idx + 1;
|
|
Packit Service |
def718 |
return 0;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int
|
|
Packit Service |
def718 |
array_list_add(struct array_list *arr, void *data)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
return array_list_put_idx(arr, arr->length, data);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
void
|
|
Packit Service |
def718 |
array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
qsort(arr->array, arr->length, sizeof(arr->array[0]), sort_fn);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
void* array_list_bsearch(const void **key, struct array_list *arr,
|
|
Packit Service |
def718 |
int (*sort_fn)(const void *, const void *))
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]),
|
|
Packit Service |
def718 |
sort_fn);
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
size_t
|
|
Packit Service |
def718 |
array_list_length(struct array_list *arr)
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
return arr->length;
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
|
|
Packit Service |
def718 |
int
|
|
Packit Service |
def718 |
array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
|
|
Packit Service |
def718 |
{
|
|
Packit Service |
def718 |
size_t i, stop;
|
|
Packit Service |
def718 |
|
|
Packit Bot |
f5502f |
/* Avoid overflow in calculation with large indices. */
|
|
Packit Bot |
f5502f |
if (idx > SIZE_T_MAX - count)
|
|
Packit Bot |
f5502f |
return -1;
|
|
Packit Service |
def718 |
stop = idx + count;
|
|
Packit Service |
def718 |
if ( idx >= arr->length || stop > arr->length ) return -1;
|
|
Packit Service |
def718 |
for ( i = idx; i < stop; ++i ) {
|
|
Packit Service |
def718 |
if ( arr->array[i] ) arr->free_fn( arr->array[i] );
|
|
Packit Service |
def718 |
}
|
|
Packit Service |
def718 |
memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) );
|
|
Packit Service |
def718 |
arr->length -= count;
|
|
Packit Service |
def718 |
return 0;
|
|
Packit Service |
def718 |
}
|