1 /*
2 * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
3 *
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
9 *
10 */
11
12 #include "config.h"
13
14 #ifdef STDC_HEADERS
15 # include <stdlib.h>
16 # include <string.h>
17 #endif /* STDC_HEADERS */
18
19 #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
20 # include <strings.h>
21 #endif /* HAVE_STRINGS_H */
22
23 #include "arraylist.h"
24
25 struct array_list*
array_list_new(array_list_free_fn * free_fn)26 array_list_new(array_list_free_fn *free_fn)
27 {
28 struct array_list *arr;
29
30 arr = (struct array_list*)calloc(1, sizeof(struct array_list));
31 if(!arr) return NULL;
32 arr->size = ARRAY_LIST_DEFAULT_SIZE;
33 arr->length = 0;
34 arr->free_fn = free_fn;
35 if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
36 free(arr);
37 return NULL;
38 }
39 return arr;
40 }
41
42 extern void
array_list_free(struct array_list * arr)43 array_list_free(struct array_list *arr)
44 {
45 int i;
46 for(i = 0; i < arr->length; i++)
47 if(arr->array[i]) arr->free_fn(arr->array[i]);
48 free(arr->array);
49 free(arr);
50 }
51
52 void*
array_list_get_idx(struct array_list * arr,int i)53 array_list_get_idx(struct array_list *arr, int i)
54 {
55 if(i >= arr->length) return NULL;
56 return arr->array[i];
57 }
58
array_list_expand_internal(struct array_list * arr,int max)59 static int array_list_expand_internal(struct array_list *arr, int max)
60 {
61 void *t;
62 int new_size;
63
64 if(max < arr->size) return 0;
65 new_size = arr->size << 1;
66 if (new_size < max)
67 new_size = max;
68 if(!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
69 arr->array = (void**)t;
70 (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
71 arr->size = new_size;
72 return 0;
73 }
74
75 int
array_list_put_idx(struct array_list * arr,int idx,void * data)76 array_list_put_idx(struct array_list *arr, int idx, void *data)
77 {
78 if(array_list_expand_internal(arr, idx+1)) return -1;
79 if(arr->array[idx]) arr->free_fn(arr->array[idx]);
80 arr->array[idx] = data;
81 if(arr->length <= idx) arr->length = idx + 1;
82 return 0;
83 }
84
85 int
array_list_add(struct array_list * arr,void * data)86 array_list_add(struct array_list *arr, void *data)
87 {
88 return array_list_put_idx(arr, arr->length, data);
89 }
90
91 void
array_list_sort(struct array_list * arr,int (* sort_fn)(const void *,const void *))92 array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
93 {
94 qsort(arr->array, arr->length, sizeof(arr->array[0]),
95 (int (*)(const void *, const void *))sort_fn);
96 }
97
98 int
array_list_length(struct array_list * arr)99 array_list_length(struct array_list *arr)
100 {
101 return arr->length;
102 }
103