aboutsummaryrefslogtreecommitdiff
path: root/arraylist.c
blob: bb4d0c2a6e37f6d4a3582c1781e692714da3ee0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* arraylist */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "util.h"

typedef int T;

struct ArrayList {
	T **   array;
	size_t size;
	size_t capacity;
};

static T **
ArrayList_allocateArray(void *ptr, size_t nelem)
{
	T **new_ptr = reallocarray(ptr, nelem, sizeof *new_ptr);

	if ( new_ptr == NULL ) {
		ERROR("out of memory");
	}

	return new_ptr;
}

void
ArrayList_init(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	arrayList->array    = NULL;
	arrayList->size     = 0;
	arrayList->capacity = 0;
}

struct ArrayList *
ArrayList_new(void)
{
	struct ArrayList *arrayList = malloc(sizeof *arrayList);
	if ( arrayList != NULL ) {
		ArrayList_init(arrayList);
	}
	return arrayList;
}

void
ArrayList_free(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	free(arrayList->array);
	ArrayList_init(arrayList);
}

void
ArrayList_delete(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	ArrayList_free(arrayList);
	free(arrayList);
}

static void
ArrayList_growIfNeeded(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	if ( arrayList->size == arrayList->capacity ) {
		if ( arrayList->array == NULL ) { // leere Liste
			assert(arrayList->size == 0);
			assert(arrayList->capacity == 0);

			static const size_t DEFAULT_CAPACITY = 10;

			arrayList->capacity = DEFAULT_CAPACITY;
			arrayList->array    = ArrayList_allocateArray(NULL, DEFAULT_CAPACITY);
		}
		else {
			size_t new_capacity  = (arrayList->capacity * 3) / 2; // *= 1.5
			T **   new_arrayList = ArrayList_allocateArray(arrayList->array, new_capacity);

			arrayList->capacity = new_capacity;
			arrayList->array    = new_arrayList;
		}

		assert(arrayList->array != NULL);
	}
}

void
ArrayList_append(struct ArrayList *arrayList, T *ptr)
{
	assert(arrayList != NULL);

	ArrayList_growIfNeeded(arrayList);

	arrayList->array[arrayList->size++] = ptr;
}

void
ArrayList_insertAt(struct ArrayList *arrayList, size_t idx, T *ptr)
{
	assert(arrayList != NULL);
	assert(idx <= arrayList->size); // allow last position

	ArrayList_growIfNeeded(arrayList);

	memmove(&arrayList->array[idx + 1], &arrayList->array[idx], (arrayList->size - idx) * sizeof(arrayList->array[0]));
	arrayList->array[idx] = ptr;
	++arrayList->size;
}

T *
ArrayList_getAt(struct ArrayList *arrayList, size_t idx)
{
	assert(arrayList != NULL);
	assert(idx < arrayList->size);

	return arrayList->array[idx];
}

size_t
ArrayList_size(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	return arrayList->size;
}

bool
ArrayList_empty(struct ArrayList *arrayList)
{
	assert(arrayList != NULL);

	return arrayList->size == 0;
}

void
ArrayList_removeAt(struct ArrayList *arrayList, size_t idx, void clear_func(T *, void *), void *args)
{
	assert(arrayList != NULL);
	assert(idx < arrayList->size);

	if ( clear_func != NULL ) {
		clear_func(arrayList->array[idx], args);
	}

#if defined(ARRAYLIST_KEEP_ORDER)
	memmove(&arrayList->arrayList[idx], &arrayList->arrayList[idx + 1], (arrayList->size - (idx + 1)) * sizeof(arrayList->arrayList[0]));
#else
	arrayList->array[idx] = arrayList->array[arrayList->size - 1];
#endif

	--arrayList->size;
}

struct qsortHelper_context {
	int (*cmp)(const T *, const T *, void *);
	void *args;
};

typedef const T *const_T_ptr;

static int
ArrayList_qsortHelper(void *ctx, const void *a, const void *b)
{
	const const_T_ptr *aa = a;
	const const_T_ptr *bb = b;

	const struct qsortHelper_context *context = ctx;

	return context->cmp(*aa, *bb, context->args);
}

void
ArrayList_sort(struct ArrayList *arrayList, int cmp(const T *, const T *, void *), void *args)
{
	assert(arrayList != NULL);
	assert(cmp != NULL);

	struct qsortHelper_context context = {
		.cmp  = cmp,
		.args = args
	};

	// Das vollständige qsort_x()-Drama: https://stackoverflow.com/a/46042467
	qsort_r(arrayList->array, arrayList->size, sizeof(arrayList->array[0]), &context, ArrayList_qsortHelper);
}

bool
ArrayList_linearSearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc)
{
	assert(arrayList != NULL);
	assert(cmp != NULL);
	assert(loc != NULL);

	for ( size_t idx = 0; idx != arrayList->size; ++idx ) {
		if ( cmp(key, arrayList->array[idx], args) == 0 ) {
			*loc = idx;
			return true;
		}
	}
	return false;
}

struct bsearchHelper_context {
	void *key;
	int (*cmp)(void *, const T *, void *);
	void *args;
};

static int
ArrayList_bsearchHelper(const void *ctx, const void *el)
{
	const struct bsearchHelper_context *context = ctx;
	const const_T_ptr *                 element = el;

	return context->cmp(context->key, *element, context->args);
}

bool
ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc)
{
	assert(arrayList != NULL);
	assert(cmp != NULL);
	assert(loc != NULL);

	struct bsearchHelper_context context = {
		.key  = key,
		.cmp  = cmp,
		.args = args
	};

	T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof(arrayList->array[0]), ArrayList_bsearchHelper);
	if ( pos != NULL ) {
		*loc = (size_t)(pos - arrayList->array);
		return true;
	}
	else {
		return false;
	}
}

void
ArrayList_shuffle(struct ArrayList *arrayList, size_t rng(size_t, void *), void *args)
{
	assert(arrayList != NULL);
	assert(rng != NULL);

	if ( arrayList->size > 1 ) {
		for ( size_t i = arrayList->size - 1; i > 0; --i ) {
			size_t j = rng(i + 1, args);

			// swap
			T *t                = arrayList->array[j];
			arrayList->array[j] = arrayList->array[i];
			arrayList->array[i] = t;
		}
	}
}

static void
ArrayList_debugPrint(struct ArrayList *arrayList)
{
	for ( size_t idx = 0; idx != ArrayList_size(arrayList); ++idx ) {
		T *ptr = ArrayList_getAt(arrayList, idx);
		printf("%d, ", *ptr);
	}
	putchar('\n');
}

static int
my_compare(const T *a, const T *b, void *args)
{
	(void) args;

	if ( *a > *b )
		return 1;
	else if ( *a < *b )
		return -1;
	else
		return 0;
}

static int
my_search_compare(void *key, const T *element, void *args)
{
	(void) args;

	int *a = key;

	if ( *a < *element )
		return -1;
	else if ( *a > *element )
		return 1;
	else
		return 0;
}

static size_t
my_rng(size_t max, void *args)
{
	(void) args;
	return (size_t) rand() % max;
}

static void
ArrayList_testSort(struct ArrayList *arrayList)
{
	for ( size_t i = 1; i < arrayList->size; ++i ) {
		if ( *arrayList->array[i - 1] > *arrayList->array[i] ) {
			fprintf(stderr, "Fehler: %zu (%d, %d)\n", i, *arrayList->array[i - 1], *arrayList->array[i]);
			exit(EXIT_FAILURE);
		}
	}
}

int
main(void)
{
	srand(time(NULL));

	int a = 11, b = 22, c = 33, d = 44;

	struct ArrayList *arrayList = ArrayList_new();

	ArrayList_append(arrayList, &a);
	ArrayList_append(arrayList, &b);
	ArrayList_append(arrayList, &c);
	ArrayList_append(arrayList, &d);
	ArrayList_debugPrint(arrayList); // 11, 22, 33, 44

	ArrayList_removeAt(arrayList, 0, NULL, NULL);
	ArrayList_debugPrint(arrayList); // 44, 22, 33

	ArrayList_insertAt(arrayList, 3, &a);
	ArrayList_debugPrint(arrayList); // 44, 22, 33, 11

	ArrayList_sort(arrayList, my_compare, NULL);
	ArrayList_testSort(arrayList);

	ArrayList_debugPrint(arrayList); // 11, 22, 33, 44

	size_t loc;
	int    dummy = 44;
	if ( ArrayList_binarySearch(arrayList, &dummy, my_search_compare, NULL, &loc) ) {
		printf("Gefunden: %zu\n", loc);
	}

	ArrayList_shuffle(arrayList, my_rng, NULL);
	ArrayList_debugPrint(arrayList); // ??

	ArrayList_delete(arrayList);

	return EXIT_SUCCESS;
}