|
1 | 1 | /*
|
2 |
| - * $Id: arraylist.c,v 1.2 2004/07/21 01:24:33 mclark Exp $ |
| 2 | + * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
3 | 3 | *
|
4 |
| - * Copyright Metaparadigm Pte. Ltd. 2004. |
| 4 | + * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
5 | 5 | * Michael Clark <michael@metaparadigm.com>
|
6 | 6 | *
|
7 |
| - * This library is free software; you can redistribute it and/or |
8 |
| - * modify it under the terms of the GNU Lesser General Public (LGPL) |
9 |
| - * License as published by the Free Software Foundation; either |
10 |
| - * version 2.1 of the License, or (at your option) any later version. |
11 |
| - * |
12 |
| - * This library is distributed in the hope that it will be useful, |
13 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
| - * Lesser General Public License for more details: http://www.gnu.org/ |
| 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. |
16 | 9 | *
|
17 | 10 | */
|
18 | 11 |
|
19 |
| -#include <stdlib.h> |
20 |
| -#include <string.h> |
21 |
| -#include <strings.h> |
| 12 | +#include "../config.h" |
22 | 13 |
|
23 |
| -#include "bits.h" |
24 |
| -#include "arraylist.h" |
| 14 | +#ifdef STDC_HEADERS |
| 15 | +# include <stdlib.h> |
| 16 | +# include <string.h> |
| 17 | +#endif /* STDC_HEADERS */ |
25 | 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" |
26 | 24 |
|
27 | 25 | struct array_list*
|
28 | 26 | array_list_new(array_list_free_fn *free_fn)
|
29 | 27 | {
|
30 |
| - struct array_list *this; |
| 28 | + struct array_list *arr; |
31 | 29 |
|
32 |
| - if(!(this = calloc(1, sizeof(struct array_list)))) return NULL; |
33 |
| - this->size = ARRAY_LIST_DEFAULT_SIZE; |
34 |
| - this->length = 0; |
35 |
| - this->free_fn = free_fn; |
36 |
| - if(!(this->array = calloc(sizeof(void*), this->size))) { |
37 |
| - free(this); |
| 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); |
38 | 37 | return NULL;
|
39 | 38 | }
|
40 |
| - return this; |
| 39 | + return arr; |
41 | 40 | }
|
42 | 41 |
|
43 | 42 | extern void
|
44 |
| -array_list_free(struct array_list *this) |
| 43 | +array_list_free(struct array_list *arr) |
45 | 44 | {
|
46 | 45 | int i;
|
47 |
| - for(i = 0; i < this->length; i++) |
48 |
| - if(this->array[i]) this->free_fn(this->array[i]); |
49 |
| - free(this->array); |
50 |
| - free(this); |
| 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); |
51 | 50 | }
|
52 | 51 |
|
53 | 52 | void*
|
54 |
| -array_list_get_idx(struct array_list *this, int i) |
| 53 | +array_list_get_idx(struct array_list *arr, int i) |
55 | 54 | {
|
56 |
| - if(i >= this->length) return NULL; |
57 |
| - return this->array[i]; |
| 55 | + if(i >= arr->length) return NULL; |
| 56 | + return arr->array[i]; |
58 | 57 | }
|
59 | 58 |
|
60 |
| -static int array_list_expand_internal(struct array_list *this, int max) |
| 59 | +static int array_list_expand_internal(struct array_list *arr, int max) |
61 | 60 | {
|
62 | 61 | void *t;
|
63 | 62 | int new_size;
|
64 | 63 |
|
65 |
| - if(max < this->size) return 0; |
66 |
| - new_size = max(this->size << 1, max); |
67 |
| - if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1; |
68 |
| - this->array = t; |
69 |
| - bzero(this->array + this->size, (new_size-this->size)*sizeof(void*)); |
70 |
| - this->size = new_size; |
| 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; |
71 | 72 | return 0;
|
72 | 73 | }
|
73 | 74 |
|
74 | 75 | int
|
75 |
| -array_list_put_idx(struct array_list *this, int idx, void *data) |
| 76 | +array_list_put_idx(struct array_list *arr, int idx, void *data) |
76 | 77 | {
|
77 |
| - if(array_list_expand_internal(this, idx)) return -1; |
78 |
| - if(this->array[idx]) this->free_fn(this->array[idx]); |
79 |
| - this->array[idx] = data; |
80 |
| - if(this->length <= idx) this->length = idx + 1; |
| 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; |
81 | 82 | return 0;
|
82 | 83 | }
|
83 | 84 |
|
84 | 85 | int
|
85 |
| -array_list_add(struct array_list *this, 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 |
| 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]), sort_fn); |
| 95 | +} |
| 96 | + |
| 97 | +void* array_list_bsearch(const void **key, struct array_list *arr, |
| 98 | + int (*sort_fn)(const void *, const void *)) |
86 | 99 | {
|
87 |
| - return array_list_put_idx(this, this->length, data); |
| 100 | + return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), |
| 101 | + sort_fn); |
88 | 102 | }
|
89 | 103 |
|
90 | 104 | int
|
91 |
| -array_list_length(struct array_list *this) |
| 105 | +array_list_length(struct array_list *arr) |
92 | 106 | {
|
93 |
| - return this->length; |
| 107 | + return arr->length; |
94 | 108 | }
|
0 commit comments