diff options
| author | Thomas Schmucker <ts@its1.de> | 2021-12-29 10:49:19 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2021-12-29 10:49:19 +0100 |
| commit | 5e696c74c9649b0e54133d46a3ae92e5ce0632fb (patch) | |
| tree | ef9d4e6bcf9797b74e13ddbf943f424ac1d49664 /arraylist.c | |
| parent | a9fecebe13be05c15a5adf5a6fcefacb73d0d035 (diff) | |
| parent | 6ced2a50056acbf7caec404a1edfa3b07fa1ac5d (diff) | |
| download | data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.tar.gz data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.tar.bz2 data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.zip | |
Merge branch 'feat/remove-null-checks'
Diffstat (limited to 'arraylist.c')
| -rw-r--r-- | arraylist.c | 48 |
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) | |||
| 31 | void | 31 | void |
| 32 | ArrayList_init(struct ArrayList *arrayList) | 32 | ArrayList_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 * | |||
| 42 | ArrayList_new(void) | 42 | ArrayList_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) | |||
| 51 | void | 51 | void |
| 52 | ArrayList_free(struct ArrayList *arrayList) | 52 | ArrayList_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) | |||
| 60 | void | 60 | void |
| 61 | ArrayList_delete(struct ArrayList *arrayList) | 61 | ArrayList_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) | |||
| 69 | static void | 69 | static void |
| 70 | ArrayList_growIfNeeded(struct ArrayList *arrayList) | 70 | ArrayList_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 | ||
| 96 | void | 96 | void |
| 97 | ArrayList_append(struct ArrayList *arrayList, T *ptr) | 97 | ArrayList_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) | |||
| 106 | void | 106 | void |
| 107 | ArrayList_insertAt(struct ArrayList *arrayList, size_t idx, T *ptr) | 107 | ArrayList_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) | |||
| 119 | T * | 119 | T * |
| 120 | ArrayList_getAt(struct ArrayList *arrayList, size_t idx) | 120 | ArrayList_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) | |||
| 128 | size_t | 128 | size_t |
| 129 | ArrayList_size(struct ArrayList *arrayList) | 129 | ArrayList_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) | |||
| 136 | bool | 136 | bool |
| 137 | ArrayList_empty(struct ArrayList *arrayList) | 137 | ArrayList_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) | |||
| 144 | void | 144 | void |
| 145 | ArrayList_removeAt(struct ArrayList *arrayList, size_t idx, void clear_func(T *, void *), void *args) | 145 | ArrayList_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) | |||
| 181 | void | 181 | void |
| 182 | ArrayList_sort(struct ArrayList *arrayList, int cmp(const T *, const T *, void *), void *args) | 182 | ArrayList_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 * | |||
| 196 | bool | 196 | bool |
| 197 | ArrayList_linearSearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc) | 197 | ArrayList_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) | |||
| 227 | bool | 227 | bool |
| 228 | ArrayList_binarySearch(struct ArrayList *arrayList, void *key, int cmp(void *, const T *, void *), void *args, size_t *loc) | 228 | ArrayList_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 | |||
| 250 | void | 250 | void |
| 251 | ArrayList_shuffle(struct ArrayList *arrayList, size_t rng(size_t, void *), void *args) | 251 | ArrayList_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 ) { |
