|
14 | 14 | #include <limits.h>
|
15 | 15 |
|
16 | 16 | #ifdef STDC_HEADERS
|
17 |
| -# include <stdlib.h> |
18 |
| -# include <string.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <string.h> |
19 | 19 | #endif /* STDC_HEADERS */
|
20 | 20 |
|
21 | 21 | #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
|
22 |
| -# include <strings.h> |
| 22 | +#include <strings.h> |
23 | 23 | #endif /* HAVE_STRINGS_H */
|
24 | 24 |
|
25 | 25 | #ifndef SIZE_T_MAX
|
|
36 | 36 |
|
37 | 37 | #include "arraylist.h"
|
38 | 38 |
|
39 |
| -struct array_list* |
40 |
| -array_list_new(array_list_free_fn *free_fn) |
| 39 | +struct array_list *array_list_new(array_list_free_fn *free_fn) |
41 | 40 | {
|
42 |
| - struct array_list *arr; |
43 |
| - |
44 |
| - arr = (struct array_list*)calloc(1, sizeof(struct array_list)); |
45 |
| - if(!arr) return NULL; |
46 |
| - arr->size = ARRAY_LIST_DEFAULT_SIZE; |
47 |
| - arr->length = 0; |
48 |
| - arr->free_fn = free_fn; |
49 |
| - if(!(arr->array = (void**)calloc(arr->size, sizeof(void*)))) { |
50 |
| - free(arr); |
51 |
| - return NULL; |
52 |
| - } |
53 |
| - return arr; |
| 41 | + struct array_list *arr; |
| 42 | + |
| 43 | + arr = (struct array_list *)calloc(1, sizeof(struct array_list)); |
| 44 | + if (!arr) |
| 45 | + return NULL; |
| 46 | + arr->size = ARRAY_LIST_DEFAULT_SIZE; |
| 47 | + arr->length = 0; |
| 48 | + arr->free_fn = free_fn; |
| 49 | + if (!(arr->array = (void **)calloc(arr->size, sizeof(void *)))) |
| 50 | + { |
| 51 | + free(arr); |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + return arr; |
54 | 55 | }
|
55 | 56 |
|
56 |
| -extern void |
57 |
| -array_list_free(struct array_list *arr) |
| 57 | +extern void array_list_free(struct array_list *arr) |
58 | 58 | {
|
59 |
| - size_t i; |
60 |
| - for(i = 0; i < arr->length; i++) |
61 |
| - if(arr->array[i]) arr->free_fn(arr->array[i]); |
62 |
| - free(arr->array); |
63 |
| - free(arr); |
| 59 | + size_t i; |
| 60 | + for (i = 0; i < arr->length; i++) |
| 61 | + if (arr->array[i]) |
| 62 | + arr->free_fn(arr->array[i]); |
| 63 | + free(arr->array); |
| 64 | + free(arr); |
64 | 65 | }
|
65 | 66 |
|
66 |
| -void* |
67 |
| -array_list_get_idx(struct array_list *arr, size_t i) |
| 67 | +void *array_list_get_idx(struct array_list *arr, size_t i) |
68 | 68 | {
|
69 |
| - if(i >= arr->length) return NULL; |
70 |
| - return arr->array[i]; |
| 69 | + if (i >= arr->length) |
| 70 | + return NULL; |
| 71 | + return arr->array[i]; |
71 | 72 | }
|
72 | 73 |
|
73 | 74 | static int array_list_expand_internal(struct array_list *arr, size_t max)
|
74 | 75 | {
|
75 |
| - void *t; |
76 |
| - size_t new_size; |
77 |
| - |
78 |
| - if(max < arr->size) return 0; |
79 |
| - /* Avoid undefined behaviour on size_t overflow */ |
80 |
| - if( arr->size >= SIZE_T_MAX / 2 ) |
81 |
| - new_size = max; |
82 |
| - else |
83 |
| - { |
84 |
| - new_size = arr->size << 1; |
85 |
| - if (new_size < max) |
86 |
| - new_size = max; |
87 |
| - } |
88 |
| - if (new_size > (~((size_t)0)) / sizeof(void*)) return -1; |
89 |
| - if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1; |
90 |
| - arr->array = (void**)t; |
91 |
| - (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*)); |
92 |
| - arr->size = new_size; |
93 |
| - return 0; |
| 76 | + void *t; |
| 77 | + size_t new_size; |
| 78 | + |
| 79 | + if (max < arr->size) |
| 80 | + return 0; |
| 81 | + /* Avoid undefined behaviour on size_t overflow */ |
| 82 | + if (arr->size >= SIZE_T_MAX / 2) |
| 83 | + new_size = max; |
| 84 | + else |
| 85 | + { |
| 86 | + new_size = arr->size << 1; |
| 87 | + if (new_size < max) |
| 88 | + new_size = max; |
| 89 | + } |
| 90 | + if (new_size > (~((size_t)0)) / sizeof(void *)) |
| 91 | + return -1; |
| 92 | + if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
| 93 | + return -1; |
| 94 | + arr->array = (void **)t; |
| 95 | + (void)memset(arr->array + arr->size, 0, (new_size - arr->size) * sizeof(void *)); |
| 96 | + arr->size = new_size; |
| 97 | + return 0; |
94 | 98 | }
|
95 | 99 |
|
96 |
| -int |
97 |
| -array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
| 100 | +int array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
98 | 101 | {
|
99 |
| - if (idx > SIZE_T_MAX - 1 ) return -1; |
100 |
| - if(array_list_expand_internal(arr, idx+1)) return -1; |
101 |
| - if(idx < arr->length && arr->array[idx]) |
102 |
| - arr->free_fn(arr->array[idx]); |
103 |
| - arr->array[idx] = data; |
104 |
| - if(arr->length <= idx) arr->length = idx + 1; |
105 |
| - return 0; |
| 102 | + if (idx > SIZE_T_MAX - 1) |
| 103 | + return -1; |
| 104 | + if (array_list_expand_internal(arr, idx + 1)) |
| 105 | + return -1; |
| 106 | + if (idx < arr->length && arr->array[idx]) |
| 107 | + arr->free_fn(arr->array[idx]); |
| 108 | + arr->array[idx] = data; |
| 109 | + if (arr->length <= idx) |
| 110 | + arr->length = idx + 1; |
| 111 | + return 0; |
106 | 112 | }
|
107 | 113 |
|
108 |
| -int |
109 |
| -array_list_add(struct array_list *arr, void *data) |
| 114 | +int array_list_add(struct array_list *arr, void *data) |
110 | 115 | {
|
111 |
| - return array_list_put_idx(arr, arr->length, data); |
| 116 | + return array_list_put_idx(arr, arr->length, data); |
112 | 117 | }
|
113 | 118 |
|
114 |
| -void |
115 |
| -array_list_sort(struct array_list *arr, int(*compar)(const void *, const void *)) |
| 119 | +void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *)) |
116 | 120 | {
|
117 |
| - qsort(arr->array, arr->length, sizeof(arr->array[0]), compar); |
| 121 | + qsort(arr->array, arr->length, sizeof(arr->array[0]), compar); |
118 | 122 | }
|
119 | 123 |
|
120 |
| -void* array_list_bsearch(const void **key, struct array_list *arr, |
121 |
| - int (*compar)(const void *, const void *)) |
| 124 | +void *array_list_bsearch(const void **key, struct array_list *arr, |
| 125 | + int (*compar)(const void *, const void *)) |
122 | 126 | {
|
123 |
| - return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), |
124 |
| - compar); |
| 127 | + return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar); |
125 | 128 | }
|
126 | 129 |
|
127 |
| -size_t |
128 |
| -array_list_length(struct array_list *arr) |
| 130 | +size_t array_list_length(struct array_list *arr) |
129 | 131 | {
|
130 |
| - return arr->length; |
| 132 | + return arr->length; |
131 | 133 | }
|
132 | 134 |
|
133 |
| -int |
134 |
| -array_list_del_idx( struct array_list *arr, size_t idx, size_t count ) |
| 135 | +int array_list_del_idx(struct array_list *arr, size_t idx, size_t count) |
135 | 136 | {
|
136 | 137 | size_t i, stop;
|
137 | 138 |
|
138 | 139 | stop = idx + count;
|
139 |
| - if ( idx >= arr->length || stop > arr->length ) return -1; |
140 |
| - for ( i = idx; i < stop; ++i ) { |
141 |
| - if ( arr->array[i] ) arr->free_fn( arr->array[i] ); |
| 140 | + if (idx >= arr->length || stop > arr->length) |
| 141 | + return -1; |
| 142 | + for (i = idx; i < stop; ++i) |
| 143 | + { |
| 144 | + if (arr->array[i]) |
| 145 | + arr->free_fn(arr->array[i]); |
142 | 146 | }
|
143 |
| - memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) ); |
| 147 | + memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *)); |
144 | 148 | arr->length -= count;
|
145 | 149 | return 0;
|
146 | 150 | }
|
0 commit comments