aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/allocator.c5
-rw-r--r--src/arraylist.c8
-rw-r--r--src/binary-search-tree.c6
-rw-r--r--src/deque.c2
-rw-r--r--src/double-linked-list.c8
-rw-r--r--src/hash-table.c6
-rw-r--r--src/heap.c4
-rw-r--r--src/quicksort.c15
-rw-r--r--src/red-black-tree.c32
-rw-r--r--src/shellsort.c4
-rw-r--r--src/single-linked-list.c4
-rw-r--r--src/stack-array.c4
-rw-r--r--src/stack-linked-list.c12
-rw-r--r--src/treeutil.h4
14 files changed, 56 insertions, 58 deletions
diff --git a/src/allocator.c b/src/allocator.c
index 98fa69f..d6aeea9 100644
--- a/src/allocator.c
+++ b/src/allocator.c
@@ -49,7 +49,7 @@ char *
49allocator_strdup(struct allocator *allocator, const char *str) 49allocator_strdup(struct allocator *allocator, const char *str)
50{ 50{
51 size_t n; 51 size_t n;
52 char * dest; 52 char *dest;
53 53
54 n = strlen(str) + 1; 54 n = strlen(str) + 1;
55 dest = allocator_malloc(allocator, n); 55 dest = allocator_malloc(allocator, n);
@@ -74,6 +74,7 @@ allocator_malloc_zero(struct allocator *allocator, size_t n)
74} 74}
75 75
76int 76int
77main() 77main(void)
78{ 78{
79 return 0;
79} 80}
diff --git a/src/arraylist.c b/src/arraylist.c
index 0db35ad..7a62fd4 100644
--- a/src/arraylist.c
+++ b/src/arraylist.c
@@ -11,7 +11,7 @@
11typedef int T; 11typedef int T;
12 12
13struct ArrayList { 13struct ArrayList {
14 T ** array; 14 T **array;
15 size_t size; 15 size_t size;
16 size_t capacity; 16 size_t capacity;
17}; 17};
@@ -83,7 +83,7 @@ ArrayList_growIfNeeded(struct ArrayList *arrayList)
83 } 83 }
84 else { 84 else {
85 size_t new_capacity = arrayList->capacity * 3 / 2; // *= 1.5 85 size_t new_capacity = arrayList->capacity * 3 / 2; // *= 1.5
86 T ** new_arrayList = ArrayList_allocateArray(arrayList->array, new_capacity); 86 T **new_arrayList = ArrayList_allocateArray(arrayList->array, new_capacity);
87 87
88 arrayList->capacity = new_capacity; 88 arrayList->capacity = new_capacity;
89 arrayList->array = new_arrayList; 89 arrayList->array = new_arrayList;
@@ -225,7 +225,7 @@ static int
225ArrayList_bsearchHelper(const void *ctx, const void *el) 225ArrayList_bsearchHelper(const void *ctx, const void *el)
226{ 226{
227 const struct bsearchHelper_context *context = ctx; 227 const struct bsearchHelper_context *context = ctx;
228 const const_T_ptr * element = el; 228 const const_T_ptr *element = el;
229 229
230 return context->cmp(context->key, *element, context->args); 230 return context->cmp(context->key, *element, context->args);
231} 231}
@@ -245,7 +245,7 @@ ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, c
245 245
246 T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof arrayList->array[0], ArrayList_bsearchHelper); 246 T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof arrayList->array[0], ArrayList_bsearchHelper);
247 if ( pos ) { 247 if ( pos ) {
248 *loc = (size_t)(pos - arrayList->array); 248 *loc = (size_t) (pos - arrayList->array);
249 return true; 249 return true;
250 } 250 }
251 else { 251 else {
diff --git a/src/binary-search-tree.c b/src/binary-search-tree.c
index 93690e2..780ba70 100644
--- a/src/binary-search-tree.c
+++ b/src/binary-search-tree.c
@@ -318,7 +318,7 @@ tree_apply_postorder(struct tree_node *tree, void (*visit)(T key, void *cl), voi
318/* --8<-- tree_stack_type */ 318/* --8<-- tree_stack_type */
319struct stack_item { 319struct stack_item {
320 struct stack_item *next; 320 struct stack_item *next;
321 struct tree_node * data; 321 struct tree_node *data;
322}; 322};
323 323
324struct stack { 324struct stack {
@@ -404,7 +404,7 @@ stack_free(struct stack *stack)
404/* --8<-- tree_queue_type */ 404/* --8<-- tree_queue_type */
405struct queue_item { 405struct queue_item {
406 struct queue_item *next; 406 struct queue_item *next;
407 struct tree_node * data; 407 struct tree_node *data;
408}; 408};
409 409
410struct queue { 410struct queue {
@@ -792,7 +792,7 @@ main(void)
792 show_tree(tree, 0, 0); 792 show_tree(tree, 0, 0);
793 793
794 struct tree_iterator it; 794 struct tree_iterator it;
795 struct tree_node * node = tree_preorder_iterator_first(&it, tree); 795 struct tree_node *node = tree_preorder_iterator_first(&it, tree);
796 while ( node ) { 796 while ( node ) {
797 fprintf(stdout, "%d\n", node->key); 797 fprintf(stdout, "%d\n", node->key);
798 798
diff --git a/src/deque.c b/src/deque.c
index 4efce63..947c404 100644
--- a/src/deque.c
+++ b/src/deque.c
@@ -102,7 +102,7 @@ grow_map(struct deque *d)
102 assert(d); 102 assert(d);
103 103
104 const size_t capacity = d->map_capacity + d->map_capacity / 2; 104 const size_t capacity = d->map_capacity + d->map_capacity / 2;
105 T ** map = allocate(capacity, sizeof *map); 105 T **map = allocate(capacity, sizeof *map);
106 106
107 // copy elements 107 // copy elements
108 size_t i, j; 108 size_t i, j;
diff --git a/src/double-linked-list.c b/src/double-linked-list.c
index 55e42d7..1e573c0 100644
--- a/src/double-linked-list.c
+++ b/src/double-linked-list.c
@@ -450,7 +450,7 @@ merge_test(void)
450} 450}
451 451
452void 452void
453ls() 453ls(void)
454{ 454{
455 struct dlist list; 455 struct dlist list;
456 456
@@ -459,15 +459,15 @@ ls()
459 for ( int i = 0; i != 30000000; ++i ) 459 for ( int i = 0; i != 30000000; ++i )
460 dlist_insert_prev(&list, list.tail, rand() % 9999); 460 dlist_insert_prev(&list, list.tail, rand() % 9999);
461 461
462 //print_list("Unsortiert:", &list); 462 // print_list("Unsortiert:", &list);
463 printf("start\n"); 463 printf("start\n");
464 clock_t start = clock(); 464 clock_t start = clock();
465 dlist_sort(&list); 465 dlist_sort(&list);
466 clock_t ende = clock(); 466 clock_t ende = clock();
467 467
468 printf("Dauer: %.3fsec\n", ((double) ende - start) / CLOCKS_PER_SEC); 468 printf("Dauer: %.3fsec\n", ((double) ende - start) / CLOCKS_PER_SEC);
469 //print_list("Sortiert:", &list); 469 // print_list("Sortiert:", &list);
470 //print_list_rev("Sortiert:", &list); 470 // print_list_rev("Sortiert:", &list);
471 471
472 dlist_free(&list); 472 dlist_free(&list);
473} 473}
diff --git a/src/hash-table.c b/src/hash-table.c
index 256fac9..8c93bc6 100644
--- a/src/hash-table.c
+++ b/src/hash-table.c
@@ -11,7 +11,7 @@ typedef int T;
11 11
12struct hash_item { 12struct hash_item {
13 struct hash_item *next; 13 struct hash_item *next;
14 char * key; 14 char *key;
15 T data; 15 T data;
16}; 16};
17 17
@@ -48,7 +48,7 @@ static struct hash_item *
48hash_add(struct hash_item *next, const char *key, T data) 48hash_add(struct hash_item *next, const char *key, T data)
49{ 49{
50 struct hash_item *new_item; 50 struct hash_item *new_item;
51 char * new_key; 51 char *new_key;
52 52
53 new_key = strdup(key); // strdup: not standard but commonly used... 53 new_key = strdup(key); // strdup: not standard but commonly used...
54 new_item = malloc(sizeof *new_item); 54 new_item = malloc(sizeof *new_item);
@@ -199,7 +199,7 @@ print(const char *key, T data, void *cl)
199} 199}
200 200
201int 201int
202main() 202main(void)
203{ 203{
204 struct hash_tab ht[1]; 204 struct hash_tab ht[1];
205 char word[100]; 205 char word[100];
diff --git a/src/heap.c b/src/heap.c
index 091e761..45902a4 100644
--- a/src/heap.c
+++ b/src/heap.c
@@ -277,11 +277,11 @@ main(void)
277 // Kapitel 5.10, Seite 372 ff. 277 // Kapitel 5.10, Seite 372 ff.
278 T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 }; 278 T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 };
279 279
280 //heapify(heap, 10); 280 // heapify(heap, 10);
281 assert(is_heap(heap, 10)); 281 assert(is_heap(heap, 10));
282 print_heap(heap, 10); 282 print_heap(heap, 10);
283 283
284 //heap[10] = 13; fixup(heap, 10); 284 // heap[10] = 13; fixup(heap, 10);
285 swap(heap, 0, 9); 285 swap(heap, 0, 9);
286 fixdown(heap, 0, 9); 286 fixdown(heap, 0, 9);
287 287
diff --git a/src/quicksort.c b/src/quicksort.c
index 4657dcd..0b20ab9 100644
--- a/src/quicksort.c
+++ b/src/quicksort.c
@@ -3,8 +3,6 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include <time.h> 4#include <time.h>
5 5
6#include "util.h"
7
8static int 6static int
9bigrand(void) 7bigrand(void)
10{ 8{
@@ -124,7 +122,7 @@ quickSort(int arr[], int low, int high)
124{ 122{
125 while ( low < high ) { 123 while ( low < high ) {
126 /* pi is partitioning index, arr[p] is now 124 /* pi is partitioning index, arr[p] is now
127 at right place */ 125 at right place */
128 int pi = partition(arr, low, high); 126 int pi = partition(arr, low, high);
129 127
130 if ( pi - low < high - pi ) { 128 if ( pi - low < high - pi ) {
@@ -299,6 +297,7 @@ heapsort_bu(T *data, int n) // zu sortierendes Feld und seine Länge
299 297
300 data[child] = val; // versickerten Wert eintragen 298 data[child] = val; // versickerten Wert eintragen
301 } 299 }
300 printf("%d\n", count);
302} 301}
303 302
304/*----------------------------------------------------------------------*/ 303/*----------------------------------------------------------------------*/
@@ -356,7 +355,7 @@ bottom_up_heapsort(T r[], int n)
356{ 355{
357 int k; 356 int k;
358 T x; 357 T x;
359 T * v; 358 T *v;
360 359
361 v = r - 1; /* The address shift */ 360 v = r - 1; /* The address shift */
362 361
@@ -606,7 +605,7 @@ test_sorting(T a[], int n)
606void 605void
607do_one_test(int n, void (*do_sort)(T a[], int n), void (*gen_testset)(T a[], int n)) 606do_one_test(int n, void (*do_sort)(T a[], int n), void (*gen_testset)(T a[], int n))
608{ 607{
609 T * a; 608 T *a;
610 clock_t start, ende; 609 clock_t start, ende;
611 610
612 a = malloc(sizeof(T) * (size_t) n); 611 a = malloc(sizeof(T) * (size_t) n);
@@ -649,10 +648,10 @@ main(void)
649 do_all_tests("heapsort_bu", n, heapsort_bu); 648 do_all_tests("heapsort_bu", n, heapsort_bu);
650 do_all_tests("bottom_up_heapsort", n, bottom_up_heapsort); 649 do_all_tests("bottom_up_heapsort", n, bottom_up_heapsort);
651 do_all_tests("c_quicksort", n, c_quicksort); 650 do_all_tests("c_quicksort", n, c_quicksort);
652 //do_all_tests("g4g_quicksort", n, g4g_quicksort); 651 // do_all_tests("g4g_quicksort", n, g4g_quicksort);
653 //do_all_tests("sed_quicksort", n, sed_quicksort); 652 // do_all_tests("sed_quicksort", n, sed_quicksort);
654 do_all_tests("my_introsort", n, my_introsort); 653 do_all_tests("my_introsort", n, my_introsort);
655 //do_all_tests("my_quicksort", n, my_quicksort); 654 // do_all_tests("my_quicksort", n, my_quicksort);
656 do_all_tests("pp_quicksort", n, pp_quicksort); 655 do_all_tests("pp_quicksort", n, pp_quicksort);
657 do_all_tests("pp_quicksort_it", n, pp_quicksort_it); 656 do_all_tests("pp_quicksort_it", n, pp_quicksort_it);
658 657
diff --git a/src/red-black-tree.c b/src/red-black-tree.c
index 7c2d2c1..5ae0f31 100644
--- a/src/red-black-tree.c
+++ b/src/red-black-tree.c
@@ -6,8 +6,6 @@
6#include <stdlib.h> 6#include <stdlib.h>
7#include <time.h> 7#include <time.h>
8 8
9#include "util.h"
10
11/* The authors of this work have released all rights to it and placed it 9/* The authors of this work have released all rights to it and placed it
12in the public domain under the Creative Commons CC0 1.0 waiver 10in the public domain under the Creative Commons CC0 1.0 waiver
13(http://creativecommons.org/publicdomain/zero/1.0/). 11(http://creativecommons.org/publicdomain/zero/1.0/).
@@ -27,22 +25,22 @@ enum rbtree_node_color { RED,
27 BLACK }; 25 BLACK };
28 26
29typedef struct rbtree_node_t { 27typedef struct rbtree_node_t {
30 void * key; 28 void *key;
31 void * value; 29 void *value;
32 struct rbtree_node_t * left; 30 struct rbtree_node_t *left;
33 struct rbtree_node_t * right; 31 struct rbtree_node_t *right;
34 struct rbtree_node_t * parent; 32 struct rbtree_node_t *parent;
35 enum rbtree_node_color color; 33 enum rbtree_node_color color;
36} * rbtree_node; 34} *rbtree_node;
37 35
38typedef struct rbtree_t { 36typedef struct rbtree_t {
39 rbtree_node root; 37 rbtree_node root;
40} * rbtree; 38} *rbtree;
41 39
42typedef int (*compare_func)(void *left, void *right); 40typedef int (*compare_func)(void *left, void *right);
43 41
44rbtree rbtree_create(); 42rbtree rbtree_create(void);
45void * rbtree_lookup(rbtree t, void *key, compare_func compare); 43void *rbtree_lookup(rbtree t, void *key, compare_func compare);
46void rbtree_insert(rbtree t, void *key, void *value, compare_func compare); 44void rbtree_insert(rbtree t, void *key, void *value, compare_func compare);
47void rbtree_delete(rbtree t, void *key, compare_func compare); 45void rbtree_delete(rbtree t, void *key, compare_func compare);
48 46
@@ -187,7 +185,7 @@ verify_property_5_helper(node n, int black_count, int *path_black_count)
187} 185}
188 186
189rbtree 187rbtree
190rbtree_create() 188rbtree_create(void)
191{ 189{
192 rbtree t = malloc(sizeof *t); 190 rbtree t = malloc(sizeof *t);
193 t->root = NULL; 191 t->root = NULL;
@@ -538,8 +536,8 @@ static void print_tree_helper(rbtree_node n, int indent);
538int 536int
539compare_int(void *leftp, void *rightp) 537compare_int(void *leftp, void *rightp)
540{ 538{
541 int left = (int) leftp; 539 long left = (long) leftp;
542 int right = (int) rightp; 540 long right = (long) rightp;
543 if ( left < right ) 541 if ( left < right )
544 return -1; 542 return -1;
545 else if ( left > right ) 543 else if ( left > right )
@@ -575,16 +573,16 @@ print_tree_helper(rbtree_node n, int indent)
575 for ( i = 0; i < indent; i++ ) 573 for ( i = 0; i < indent; i++ )
576 fputs(" ", stdout); 574 fputs(" ", stdout);
577 if ( n->color == BLACK ) 575 if ( n->color == BLACK )
578 printf("%d\n", (int) n->key); 576 printf("%ld\n", (long) n->key);
579 else 577 else
580 printf("<%d>\n", (int) n->key); 578 printf("<%ld>\n", (long) n->key);
581 if ( n->left != NULL ) { 579 if ( n->left != NULL ) {
582 print_tree_helper(n->left, indent + INDENT_STEP); 580 print_tree_helper(n->left, indent + INDENT_STEP);
583 } 581 }
584} 582}
585 583
586int 584int
587main() 585main(void)
588{ 586{
589 int i; 587 int i;
590 rbtree t = rbtree_create(); 588 rbtree t = rbtree_create();
diff --git a/src/shellsort.c b/src/shellsort.c
index 29e6bf8..d35e5b0 100644
--- a/src/shellsort.c
+++ b/src/shellsort.c
@@ -9,10 +9,10 @@ typedef int T;
9void 9void
10shellsort(T a[], size_t n) 10shellsort(T a[], size_t n)
11{ 11{
12 //static const size_t gaps[] = { 701, 301, 132, 57, 23, 10, 4, 1 }; 12 // static const size_t gaps[] = { 701, 301, 132, 57, 23, 10, 4, 1 };
13 13
14 // Wikipedia: 14 // Wikipedia:
15 //static const size_t gaps[] = { 2147483647, 1131376761, 410151271, 157840433, 58548857, 21521774, 8810089, 3501671, 1355339, 543749, 213331, 84801, 27901, 11969, 4711, 1968, 815, 271, 111, 41, 13, 4, 1 }; 15 // static const size_t gaps[] = { 2147483647, 1131376761, 410151271, 157840433, 58548857, 21521774, 8810089, 3501671, 1355339, 543749, 213331, 84801, 27901, 11969, 4711, 1968, 815, 271, 111, 41, 13, 4, 1 };
16 16
17 // https://www.cs.princeton.edu/~rs/shell/paperF.pdf 17 // https://www.cs.princeton.edu/~rs/shell/paperF.pdf
18 static const size_t gaps[] = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1 }; 18 static const size_t gaps[] = { 1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1 };
diff --git a/src/single-linked-list.c b/src/single-linked-list.c
index d18dae4..ab37dc2 100644
--- a/src/single-linked-list.c
+++ b/src/single-linked-list.c
@@ -218,7 +218,7 @@ print_data(T data, void *cl)
218/* -->8-- */ 218/* -->8-- */
219 219
220int 220int
221main() 221main(void)
222{ 222{
223 clock_t start; 223 clock_t start;
224 224
@@ -255,7 +255,7 @@ main()
255 x = list_sort(x); 255 x = list_sort(x);
256 printf("Fertig: %.3lf sec\n", (double) (clock() - start) / CLOCKS_PER_SEC); 256 printf("Fertig: %.3lf sec\n", (double) (clock() - start) / CLOCKS_PER_SEC);
257 257
258 //list_apply(x, print_data); 258 // list_apply(x, print_data);
259 printf("Len: %zu\n", list_length(x)); 259 printf("Len: %zu\n", list_length(x));
260 260
261 list_free(x); 261 list_free(x);
diff --git a/src/stack-array.c b/src/stack-array.c
index a36de7b..ca68a03 100644
--- a/src/stack-array.c
+++ b/src/stack-array.c
@@ -9,7 +9,7 @@
9typedef int T; 9typedef int T;
10 10
11struct stack { 11struct stack {
12 T * array; 12 T *array;
13 size_t sz, p; 13 size_t sz, p;
14}; 14};
15/* -->8-- */ 15/* -->8-- */
@@ -150,7 +150,7 @@ test_stack(void)
150} 150}
151 151
152int 152int
153main() 153main(void)
154{ 154{
155 test_stack(); 155 test_stack();
156 return EXIT_SUCCESS; 156 return EXIT_SUCCESS;
diff --git a/src/stack-linked-list.c b/src/stack-linked-list.c
index 48b9eba..7c01a1d 100644
--- a/src/stack-linked-list.c
+++ b/src/stack-linked-list.c
@@ -85,7 +85,7 @@ stack_free(struct stack *stack)
85/* -->8-- */ 85/* -->8-- */
86 86
87int 87int
88main() 88main(void)
89{ 89{
90 struct stack stack[1]; 90 struct stack stack[1];
91 91
@@ -96,11 +96,11 @@ main()
96 96
97 /* 97 /*
98 while ( !stack_empty(stack) ) { 98 while ( !stack_empty(stack) ) {
99 int i; 99 int i;
100 if ( stack_pop(stack, &i) ) 100 if ( stack_pop(stack, &i) )
101 printf("%d\n", i); 101 printf("%d\n", i);
102 else 102 else
103 ERROR("this shouldn't happen!"); 103 ERROR("this shouldn't happen!");
104 } 104 }
105 */ 105 */
106 106
diff --git a/src/treeutil.h b/src/treeutil.h
index 76627c0..a68ff35 100644
--- a/src/treeutil.h
+++ b/src/treeutil.h
@@ -1,7 +1,7 @@
1// aux display and verification routines, helpful but not essential 1// aux display and verification routines, helpful but not essential
2struct trunk { 2struct trunk {
3 struct trunk *prev; 3 struct trunk *prev;
4 const char * str; 4 const char *str;
5}; 5};
6 6
7static void 7static void
@@ -21,7 +21,7 @@ show_tree(struct tree_node *root, struct trunk *prev, int is_left)
21 return; 21 return;
22 22
23 struct trunk this_disp = { prev, " " }; 23 struct trunk this_disp = { prev, " " };
24 const char * prev_str = this_disp.str; 24 const char *prev_str = this_disp.str;
25 show_tree(root->right, &this_disp, 1); 25 show_tree(root->right, &this_disp, 1);
26 26
27 if ( !prev ) 27 if ( !prev )