From 9e8caf1e06ba7510160305da11a30d91dcfc23e5 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Fri, 6 Sep 2024 21:10:21 +0200 Subject: reformat source code --- src/allocator.c | 5 +++-- src/arraylist.c | 8 ++++---- src/binary-search-tree.c | 6 +++--- src/deque.c | 2 +- src/double-linked-list.c | 8 ++++---- src/hash-table.c | 6 +++--- src/heap.c | 4 ++-- src/quicksort.c | 15 +++++++-------- src/red-black-tree.c | 32 +++++++++++++++----------------- src/shellsort.c | 4 ++-- src/single-linked-list.c | 4 ++-- src/stack-array.c | 4 ++-- src/stack-linked-list.c | 12 ++++++------ src/treeutil.h | 4 ++-- 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 * allocator_strdup(struct allocator *allocator, const char *str) { size_t n; - char * dest; + char *dest; n = strlen(str) + 1; dest = allocator_malloc(allocator, n); @@ -74,6 +74,7 @@ allocator_malloc_zero(struct allocator *allocator, size_t n) } int -main() +main(void) { + return 0; } 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 @@ typedef int T; struct ArrayList { - T ** array; + T **array; size_t size; size_t capacity; }; @@ -83,7 +83,7 @@ ArrayList_growIfNeeded(struct ArrayList *arrayList) } else { size_t new_capacity = arrayList->capacity * 3 / 2; // *= 1.5 - T ** new_arrayList = ArrayList_allocateArray(arrayList->array, new_capacity); + T **new_arrayList = ArrayList_allocateArray(arrayList->array, new_capacity); arrayList->capacity = new_capacity; arrayList->array = new_arrayList; @@ -225,7 +225,7 @@ static int ArrayList_bsearchHelper(const void *ctx, const void *el) { const struct bsearchHelper_context *context = ctx; - const const_T_ptr * element = el; + const const_T_ptr *element = el; return context->cmp(context->key, *element, context->args); } @@ -245,7 +245,7 @@ ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, c T **pos = bsearch(&context, arrayList->array, arrayList->size, sizeof arrayList->array[0], ArrayList_bsearchHelper); if ( pos ) { - *loc = (size_t)(pos - arrayList->array); + *loc = (size_t) (pos - arrayList->array); return true; } 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 /* --8<-- tree_stack_type */ struct stack_item { struct stack_item *next; - struct tree_node * data; + struct tree_node *data; }; struct stack { @@ -404,7 +404,7 @@ stack_free(struct stack *stack) /* --8<-- tree_queue_type */ struct queue_item { struct queue_item *next; - struct tree_node * data; + struct tree_node *data; }; struct queue { @@ -792,7 +792,7 @@ main(void) show_tree(tree, 0, 0); struct tree_iterator it; - struct tree_node * node = tree_preorder_iterator_first(&it, tree); + struct tree_node *node = tree_preorder_iterator_first(&it, tree); while ( node ) { fprintf(stdout, "%d\n", node->key); 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) assert(d); const size_t capacity = d->map_capacity + d->map_capacity / 2; - T ** map = allocate(capacity, sizeof *map); + T **map = allocate(capacity, sizeof *map); // copy elements 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) } void -ls() +ls(void) { struct dlist list; @@ -459,15 +459,15 @@ ls() for ( int i = 0; i != 30000000; ++i ) dlist_insert_prev(&list, list.tail, rand() % 9999); - //print_list("Unsortiert:", &list); + // print_list("Unsortiert:", &list); printf("start\n"); clock_t start = clock(); dlist_sort(&list); clock_t ende = clock(); printf("Dauer: %.3fsec\n", ((double) ende - start) / CLOCKS_PER_SEC); - //print_list("Sortiert:", &list); - //print_list_rev("Sortiert:", &list); + // print_list("Sortiert:", &list); + // print_list_rev("Sortiert:", &list); dlist_free(&list); } 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; struct hash_item { struct hash_item *next; - char * key; + char *key; T data; }; @@ -48,7 +48,7 @@ static struct hash_item * hash_add(struct hash_item *next, const char *key, T data) { struct hash_item *new_item; - char * new_key; + char *new_key; new_key = strdup(key); // strdup: not standard but commonly used... new_item = malloc(sizeof *new_item); @@ -199,7 +199,7 @@ print(const char *key, T data, void *cl) } int -main() +main(void) { struct hash_tab ht[1]; 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) // Kapitel 5.10, Seite 372 ff. T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 }; - //heapify(heap, 10); + // heapify(heap, 10); assert(is_heap(heap, 10)); print_heap(heap, 10); - //heap[10] = 13; fixup(heap, 10); + // heap[10] = 13; fixup(heap, 10); swap(heap, 0, 9); fixdown(heap, 0, 9); 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 @@ #include #include -#include "util.h" - static int bigrand(void) { @@ -124,7 +122,7 @@ quickSort(int arr[], int low, int high) { while ( low < high ) { /* pi is partitioning index, arr[p] is now - at right place */ + at right place */ int pi = partition(arr, low, high); if ( pi - low < high - pi ) { @@ -299,6 +297,7 @@ heapsort_bu(T *data, int n) // zu sortierendes Feld und seine Länge data[child] = val; // versickerten Wert eintragen } + printf("%d\n", count); } /*----------------------------------------------------------------------*/ @@ -356,7 +355,7 @@ bottom_up_heapsort(T r[], int n) { int k; T x; - T * v; + T *v; v = r - 1; /* The address shift */ @@ -606,7 +605,7 @@ test_sorting(T a[], int n) void do_one_test(int n, void (*do_sort)(T a[], int n), void (*gen_testset)(T a[], int n)) { - T * a; + T *a; clock_t start, ende; a = malloc(sizeof(T) * (size_t) n); @@ -649,10 +648,10 @@ main(void) do_all_tests("heapsort_bu", n, heapsort_bu); do_all_tests("bottom_up_heapsort", n, bottom_up_heapsort); do_all_tests("c_quicksort", n, c_quicksort); - //do_all_tests("g4g_quicksort", n, g4g_quicksort); - //do_all_tests("sed_quicksort", n, sed_quicksort); + // do_all_tests("g4g_quicksort", n, g4g_quicksort); + // do_all_tests("sed_quicksort", n, sed_quicksort); do_all_tests("my_introsort", n, my_introsort); - //do_all_tests("my_quicksort", n, my_quicksort); + // do_all_tests("my_quicksort", n, my_quicksort); do_all_tests("pp_quicksort", n, pp_quicksort); do_all_tests("pp_quicksort_it", n, pp_quicksort_it); 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 @@ #include #include -#include "util.h" - /* The authors of this work have released all rights to it and placed it in the public domain under the Creative Commons CC0 1.0 waiver (http://creativecommons.org/publicdomain/zero/1.0/). @@ -27,22 +25,22 @@ enum rbtree_node_color { RED, BLACK }; typedef struct rbtree_node_t { - void * key; - void * value; - struct rbtree_node_t * left; - struct rbtree_node_t * right; - struct rbtree_node_t * parent; + void *key; + void *value; + struct rbtree_node_t *left; + struct rbtree_node_t *right; + struct rbtree_node_t *parent; enum rbtree_node_color color; -} * rbtree_node; +} *rbtree_node; typedef struct rbtree_t { rbtree_node root; -} * rbtree; +} *rbtree; typedef int (*compare_func)(void *left, void *right); -rbtree rbtree_create(); -void * rbtree_lookup(rbtree t, void *key, compare_func compare); +rbtree rbtree_create(void); +void *rbtree_lookup(rbtree t, void *key, compare_func compare); void rbtree_insert(rbtree t, void *key, void *value, compare_func compare); void rbtree_delete(rbtree t, void *key, compare_func compare); @@ -187,7 +185,7 @@ verify_property_5_helper(node n, int black_count, int *path_black_count) } rbtree -rbtree_create() +rbtree_create(void) { rbtree t = malloc(sizeof *t); t->root = NULL; @@ -538,8 +536,8 @@ static void print_tree_helper(rbtree_node n, int indent); int compare_int(void *leftp, void *rightp) { - int left = (int) leftp; - int right = (int) rightp; + long left = (long) leftp; + long right = (long) rightp; if ( left < right ) return -1; else if ( left > right ) @@ -575,16 +573,16 @@ print_tree_helper(rbtree_node n, int indent) for ( i = 0; i < indent; i++ ) fputs(" ", stdout); if ( n->color == BLACK ) - printf("%d\n", (int) n->key); + printf("%ld\n", (long) n->key); else - printf("<%d>\n", (int) n->key); + printf("<%ld>\n", (long) n->key); if ( n->left != NULL ) { print_tree_helper(n->left, indent + INDENT_STEP); } } int -main() +main(void) { int i; 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; void shellsort(T a[], size_t n) { - //static const size_t gaps[] = { 701, 301, 132, 57, 23, 10, 4, 1 }; + // static const size_t gaps[] = { 701, 301, 132, 57, 23, 10, 4, 1 }; // Wikipedia: - //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 }; + // 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 }; // https://www.cs.princeton.edu/~rs/shell/paperF.pdf 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) /* -->8-- */ int -main() +main(void) { clock_t start; @@ -255,7 +255,7 @@ main() x = list_sort(x); printf("Fertig: %.3lf sec\n", (double) (clock() - start) / CLOCKS_PER_SEC); - //list_apply(x, print_data); + // list_apply(x, print_data); printf("Len: %zu\n", list_length(x)); 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 @@ typedef int T; struct stack { - T * array; + T *array; size_t sz, p; }; /* -->8-- */ @@ -150,7 +150,7 @@ test_stack(void) } int -main() +main(void) { test_stack(); 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) /* -->8-- */ int -main() +main(void) { struct stack stack[1]; @@ -96,11 +96,11 @@ main() /* while ( !stack_empty(stack) ) { - int i; - if ( stack_pop(stack, &i) ) - printf("%d\n", i); - else - ERROR("this shouldn't happen!"); + int i; + if ( stack_pop(stack, &i) ) + printf("%d\n", i); + else + ERROR("this shouldn't happen!"); } */ 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 @@ // aux display and verification routines, helpful but not essential struct trunk { struct trunk *prev; - const char * str; + const char *str; }; static void @@ -21,7 +21,7 @@ show_tree(struct tree_node *root, struct trunk *prev, int is_left) return; struct trunk this_disp = { prev, " " }; - const char * prev_str = this_disp.str; + const char *prev_str = this_disp.str; show_tree(root->right, &this_disp, 1); if ( !prev ) -- cgit v1.3