59 |
59 |
60 The following example shows, how a `UcxArray` can be built with |
60 The following example shows, how a `UcxArray` can be built with |
61 a standard dynamic C array (pointer+length) as basis. |
61 a standard dynamic C array (pointer+length) as basis. |
62 |
62 |
63 ```C |
63 ```C |
64 #include <stdio.h> |
64 UcxArray* create_unique(sstr_t* array, size_t arrlen) { |
65 #include <ucx/array.h> |
|
66 #include <ucx/string.h> |
|
67 #include <ucx/utils.h> |
|
68 |
|
69 UcxArray remove_duplicates(sstr_t* array, size_t arrlen) { |
|
70 // worst case is no duplicates, hence the capacity is set to arrlen |
65 // worst case is no duplicates, hence the capacity is set to arrlen |
71 UcxArray result = ucx_array_new(arrlen, sizeof(sstr_t)); |
66 UcxArray* result = ucx_array_new(arrlen, sizeof(sstr_t)); |
72 // only append elements, if they are not already present in the array |
67 // only append elements, if they are not already present in the array |
73 for (size_t i = 0 ; i < arrlen ; ++i) { |
68 for (size_t i = 0 ; i < arrlen ; ++i) { |
74 if (!ucx_array_contains(result, array+i, ucx_cmp_sstr, NULL)) { |
69 if (!ucx_array_contains(result, array+i, ucx_cmp_sstr, NULL)) { |
75 ucx_array_append(&result, array+i); |
70 ucx_array_append_from(result, array+i, 1); |
76 } |
71 } |
77 } |
72 } |
78 // make the array as small as possible |
73 // make the array as small as possible |
79 ucx_array_shrink(&result); |
74 ucx_array_shrink(result); |
80 return result; |
75 return result; |
81 } |
76 } |
82 |
77 |
83 /* ... */ |
78 /* ... */ |
84 |
79 |
85 sstr_t* array = /* some standard array of strings */ |
80 sstr_t* array = /* some standard array of strings */ |
86 size_t arrlen = /* the length of the array */ |
81 size_t arrlen = /* the length of the array */ |
87 |
82 |
88 UcxArray result = remove_duplicates(array,arrlen); |
83 UcxArray* result = create_unique(array,arrlen); |
89 |
84 |
90 /* Iterate over the array and print the elements */ |
85 /* Iterate over the array and print the elements */ |
91 for (size_t i = 0 ; i < result.size ; i++) { |
86 sstr_t* unique = result->data; |
92 sstr_t s = ucx_array_at_typed(sstr_t, result, i); |
87 for (size_t i = 0 ; i < result->size ; i++) { |
93 printf("%" PRIsstr "\n", SFMT(s)); |
88 printf("%" PRIsstr "\n", SFMT(unique[i])); |
94 } |
89 } |
95 |
90 |
96 /* Free the array. */ |
91 /* Free the array. */ |
97 ucx_array_free(&result); |
92 ucx_array_free(result); |
|
93 ``` |
|
94 ### Preventing out of bounds writes |
|
95 |
|
96 The functions `ucx_array_reserve()`, `ucx_array_resize()`, `ucx_array_grow()`, |
|
97 and `ucx_array_shrink()` allow easy management of the array capacity. |
|
98 Imagine you want to add `n` elements to an array. If your `n` elements are |
|
99 already somewhere else consecutively in memory, you can use |
|
100 `ucx_array_append_from()` and benefit from the autogrow facility in this family |
|
101 of functions. Otherwise, you can ask the array to have enough capacity for |
|
102 holding additional `n` elements. |
|
103 |
|
104 ```C |
|
105 size_t n = // ... elements to add |
|
106 if (ucx_array_grow(array, n)) { |
|
107 fprintf(stderr, "Cannot add %zu elements to the array.\n", n); |
|
108 return 1; |
|
109 } |
|
110 for (size_t i = 0 ; i < n ; i++) { |
|
111 ((int*)array->data)[array->size++] = 80; |
|
112 } |
98 ``` |
113 ``` |
99 |
114 |
100 ## AVL Tree |
115 ## AVL Tree |
101 |
116 |
102 *Header file:* [avl.h](api/avl_8h.html) |
117 *Header file:* [avl.h](api/avl_8h.html) |