aboutsummaryrefslogtreecommitdiff
path: root/arraylist.c
diff options
context:
space:
mode:
Diffstat (limited to 'arraylist.c')
-rw-r--r--arraylist.c48
1 files changed, 24 insertions, 24 deletions
diff --git a/arraylist.c b/arraylist.c
index bb4d0c2..a8cebb0 100644
--- a/arraylist.c
+++ b/arraylist.c
@@ -31,7 +31,7 @@ ArrayList_allocateArray(void *ptr, size_t nelem)
31void 31void
32ArrayList_init(struct ArrayList *arrayList) 32ArrayList_init(struct ArrayList *arrayList)
33{ 33{
34 assert(arrayList != NULL); 34 assert(arrayList);
35 35
36 arrayList->array = NULL; 36 arrayList->array = NULL;
37 arrayList->size = 0; 37 arrayList->size = 0;
@@ -42,7 +42,7 @@ struct ArrayList *
42ArrayList_new(void) 42ArrayList_new(void)
43{ 43{
44 struct ArrayList *arrayList = malloc(sizeof *arrayList); 44 struct ArrayList *arrayList = malloc(sizeof *arrayList);
45 if ( arrayList != NULL ) { 45 if ( arrayList ) {
46 ArrayList_init(arrayList); 46 ArrayList_init(arrayList);
47 } 47 }
48 return arrayList; 48 return arrayList;
@@ -51,7 +51,7 @@ ArrayList_new(void)
51void 51void
52ArrayList_free(struct ArrayList *arrayList) 52ArrayList_free(struct ArrayList *arrayList)
53{ 53{
54 assert(arrayList != NULL); 54 assert(arrayList);
55 55
56 free(arrayList->array); 56 free(arrayList->array);
57 ArrayList_init(arrayList); 57 ArrayList_init(arrayList);
@@ -60,7 +60,7 @@ ArrayList_free(struct ArrayList *arrayList)
60void 60void
61ArrayList_delete(struct ArrayList *arrayList) 61ArrayList_delete(struct ArrayList *arrayList)
62{ 62{
63 assert(arrayList != NULL); 63 assert(arrayList);
64 64
65 ArrayList_free(arrayList); 65 ArrayList_free(arrayList);
66 free(arrayList); 66 free(arrayList);
@@ -69,7 +69,7 @@ ArrayList_delete(struct ArrayList *arrayList)
69static void 69static void
70ArrayList_growIfNeeded(struct ArrayList *arrayList) 70ArrayList_growIfNeeded(struct ArrayList *arrayList)
71{ 71{
72 assert(arrayList != NULL); 72 assert(arrayList);
73 73
74 if ( arrayList->size == arrayList->capacity ) { 74 if ( arrayList->size == arrayList->capacity ) {
75 if ( arrayList->array == NULL ) { // leere Liste 75 if ( arrayList->array == NULL ) { // leere Liste
@@ -89,14 +89,14 @@ ArrayList_growIfNeeded(struct ArrayList *arrayList)
89 arrayList->array = new_arrayList; 89 arrayList->array = new_arrayList;
90 } 90 }
91 91
92 assert(arrayList->array != NULL); 92 assert(arrayList->array);
93 } 93 }
94} 94}
95 95
96void 96void
97ArrayList_append(struct ArrayList *arrayList, T *ptr) 97ArrayList_append(struct ArrayList *arrayList, T *ptr)
98{ 98{
99 assert(arrayList != NULL); 99 assert(arrayList);
100 100
101 ArrayList_growIfNeeded(arrayList); 101 ArrayList_growIfNeeded(arrayList);
102 102
@@ -106,7 +106,7 @@ ArrayList_append(struct ArrayList *arrayList, T *ptr)
106void 106void
107ArrayList_insertAt(struct ArrayList *arrayList, size_t idx, T *ptr) 107ArrayList_insertAt(struct ArrayList *arrayList, size_t idx, T *ptr)
108{ 108{
109 assert(arrayList != NULL); 109 assert(arrayList);
110 assert(idx <= arrayList->size); // allow last position 110 assert(idx <= arrayList->size); // allow last position
111 111
112 ArrayList_growIfNeeded(arrayList); 112 ArrayList_growIfNeeded(arrayList);
@@ -119,7 +119,7 @@ ArrayList_insertAt(struct ArrayList *arrayList, size_t idx, T *ptr)
119T * 119T *
120ArrayList_getAt(struct ArrayList *arrayList, size_t idx) 120ArrayList_getAt(struct ArrayList *arrayList, size_t idx)
121{ 121{
122 assert(arrayList != NULL); 122 assert(arrayList);
123 assert(idx < arrayList->size); 123 assert(idx < arrayList->size);
124 124
125 return arrayList->array[idx]; 125 return arrayList->array[idx];
@@ -128,7 +128,7 @@ ArrayList_getAt(struct ArrayList *arrayList, size_t idx)
128size_t 128size_t
129ArrayList_size(struct ArrayList *arrayList) 129ArrayList_size(struct ArrayList *arrayList)
130{ 130{
131 assert(arrayList != NULL); 131 assert(arrayList);
132 132
133 return arrayList->size; 133 return arrayList->size;
134} 134}
@@ -136,7 +136,7 @@ ArrayList_size(struct ArrayList *arrayList)
136bool 136bool
137ArrayList_empty(struct ArrayList *arrayList) 137ArrayList_empty(struct ArrayList *arrayList)
138{ 138{
139 assert(arrayList != NULL); 139 assert(arrayList);
140 140
141 return arrayList->size == 0; 141 return arrayList->size == 0;
142} 142}
@@ -144,10 +144,10 @@ ArrayList_empty(struct ArrayList *arrayList)
144void 144void
145ArrayList_removeAt(struct ArrayList *arrayList, size_t idx, void clear_func(T *, void *), void *args) 145ArrayList_removeAt(struct ArrayList *arrayList, size_t idx, void clear_func(T *, void *), void *args)
146{ 146{
147 assert(arrayList != NULL); 147 assert(arrayList);
148 assert(idx < arrayList->size); 148 assert(idx < arrayList->size);
149 149
150 if ( clear_func != NULL ) { 150 if ( clear_func ) {
151 clear_func(arrayList->array[idx], args); 151 clear_func(arrayList->array[idx], args);
152 } 152 }
153 153
@@ -181,8 +181,8 @@ ArrayList_qsortHelper(void *ctx, const void *a, const void *b)
181void 181void
182ArrayList_sort(struct ArrayList *arrayList, int cmp(const T *, const T *, void *), void *args) 182ArrayList_sort(struct ArrayList *arrayList, int cmp(const T *, const T *, void *), void *args)
183{ 183{
184 assert(arrayList != NULL); 184 assert(arrayList);
185 assert(cmp != NULL); 185 assert(cmp);
186 186
187 struct qsortHelper_context context = { 187 struct qsortHelper_context context = {
188 .cmp = cmp, 188 .cmp = cmp,
@@ -196,9 +196,9 @@ ArrayList_sort(struct ArrayList *arrayList, int cmp(const T *, const T *, void *
196bool 196bool
197ArrayList_linearSearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc) 197ArrayList_linearSearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc)
198{ 198{
199 assert(arrayList != NULL); 199 assert(arrayList);
200 assert(cmp != NULL); 200 assert(cmp);
201 assert(loc != NULL); 201 assert(loc);
202 202
203 for ( size_t idx = 0; idx != arrayList->size; ++idx ) { 203 for ( size_t idx = 0; idx != arrayList->size; ++idx ) {
204 if ( cmp(key, arrayList->array[idx], args) == 0 ) { 204 if ( cmp(key, arrayList->array[idx], args) == 0 ) {
@@ -227,9 +227,9 @@ ArrayList_bsearchHelper(const void *ctx, const void *el)
227bool 227bool
228ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc) 228ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc)
229{ 229{
230 assert(arrayList != NULL); 230 assert(arrayList);
231 assert(cmp != NULL); 231 assert(cmp);
232 assert(loc != NULL); 232 assert(loc);
233 233
234 struct bsearchHelper_context context = { 234 struct bsearchHelper_context context = {
235 .key = key, 235 .key = key,
@@ -238,7 +238,7 @@ ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, c
238 }; 238 };
239 239
240 T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof(arrayList->array[0]), ArrayList_bsearchHelper); 240 T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof(arrayList->array[0]), ArrayList_bsearchHelper);
241 if ( pos != NULL ) { 241 if ( pos ) {
242 *loc = (size_t)(pos - arrayList->array); 242 *loc = (size_t)(pos - arrayList->array);
243 return true; 243 return true;
244 } 244 }
@@ -250,8 +250,8 @@ ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, c
250void 250void
251ArrayList_shuffle(struct ArrayList *arrayList, size_t rng(size_t, void *), void *args) 251ArrayList_shuffle(struct ArrayList *arrayList, size_t rng(size_t, void *), void *args)
252{ 252{
253 assert(arrayList != NULL); 253 assert(arrayList);
254 assert(rng != NULL); 254 assert(rng);
255 255
256 if ( arrayList->size > 1 ) { 256 if ( arrayList->size > 1 ) {
257 for ( size_t i = arrayList->size - 1; i > 0; --i ) { 257 for ( size_t i = arrayList->size - 1; i > 0; --i ) {