Fri, 11 May 2018 18:46:31 +0200
example code for the usage of a UcxList
docs/src/modules.md | file | annotate | diff | comparison | revisions |
1.1 --- a/docs/src/modules.md Fri May 11 18:35:08 2018 +0200 1.2 +++ b/docs/src/modules.md Fri May 11 18:46:31 2018 +0200 1.3 @@ -62,7 +62,7 @@ 1.4 */ 1.5 typedef struct { 1.6 time_t ts; 1.7 - // other important data 1.8 + /* other important data */ 1.9 } MyObject; 1.10 1.11 /* ----------- 1.12 @@ -70,10 +70,10 @@ 1.13 */ 1.14 1.15 UcxAVLTree* tree = ucx_avl_new(ucx_longintcmp); 1.16 -// ... populate tree with objects, use '& MyObject.ts' as key ... 1.17 +/* ... populate tree with objects, use '& MyObject.ts' as key ... */ 1.18 1.19 1.20 -// Now find every item, with 30 <= ts <= 70 1.21 +/* Now find every item, with 30 <= ts <= 70 */ 1.22 time_t ts_start = 30; 1.23 time_t ts_end = 70; 1.24 1.25 @@ -141,18 +141,18 @@ 1.26 1.27 UcxBuffer* linebuf = 1.28 ucx_buffer_new( 1.29 - NULL, // the buffer should manage the memory area for us 1.30 - chunksize, // initial buffer size should be the chunk size 1.31 - UCX_BUFFER_AUTOEXTEND); // the buffer will grow when necessary 1.32 + NULL, /* the buffer should manage the memory area for us */ 1.33 + 2*chunksize, /* initial size should be twice the chunk size */ 1.34 + UCX_BUFFER_AUTOEXTEND); /* the buffer will grow when necessary */ 1.35 1.36 size_t lineno = 1; 1.37 do { 1.38 - // read line chunk 1.39 + /* read line chunk */ 1.40 size_t read = ucx_stream_ncopy( 1.41 input, linebuf, fread, ucx_buffer_write, chunksize); 1.42 if (read == 0) break; 1.43 1.44 - // handle line endings 1.45 + /* handle line endings */ 1.46 do { 1.47 sstr_t bufstr = ucx_buffer_to_sstr(linebuf); 1.48 sstr_t nl = sstrchr(bufstr, '\n'); 1.49 @@ -163,13 +163,13 @@ 1.50 1.51 printf("%zu: %" PRIsstr "\n", lineno++, SFMT(linestr)); 1.52 1.53 - // shift the buffer to the next line 1.54 + /* shift the buffer to the next line */ 1.55 ucx_buffer_shift_left(linebuf, linelen+1); 1.56 } while(1); 1.57 1.58 } while(1); 1.59 1.60 - // print the 'noeol' line, if any 1.61 + /* print the 'noeol' line, if any */ 1.62 sstr_t lastline = ucx_buffer_to_sstr(linebuf); 1.63 if (lastline.length > 0) { 1.64 printf("%zu: %" PRIsstr, lineno, SFMT(lastline)); 1.65 @@ -191,6 +191,53 @@ 1.66 linked list. Among the common operations like insert, remove, search and sort, 1.67 we allow convenient iteration via a special `UCX_FOREACH` macro. 1.68 1.69 +### Remove duplicates from an array of strings 1.70 + 1.71 +Assume you are given an array of `sstr_t` and want to create a list of these 1.72 +strings without duplicates. 1.73 +```C 1.74 +#include <stdio.h> 1.75 +#include <ucx/list.h> 1.76 +#include <ucx/string.h> 1.77 +#include <ucx/utils.h> 1.78 + 1.79 +UcxList* remove_duplicates(sstr_t* array, size_t arrlen) { 1.80 + UcxList* list = NULL; 1.81 + for (size_t i = 0 ; i < arrlen ; ++i) { 1.82 + if (ucx_list_find(list, array+i, ucx_sstrcmp, NULL) == -1) { 1.83 + sstr_t* s = malloc(sizeof(sstr_t)); 1.84 + *s = sstrdup(array[i]); 1.85 + list = ucx_list_append(list, s); 1.86 + } 1.87 + } 1.88 + return list; 1.89 +} 1.90 + 1.91 +/* we will need this function to clean up the list contents later */ 1.92 +void free_sstr(void* ptr) { 1.93 + sstr_t* s = ptr; 1.94 + free(s->ptr); 1.95 + free(s); 1.96 +} 1.97 + 1.98 +/* ... */ 1.99 + 1.100 +sstr_t* array = /* some array of strings */ 1.101 +size_t arrlen = /* the length of the array */ 1.102 + 1.103 +UcxList* list = remove_duplicates(array,arrlen); 1.104 + 1.105 +/* Iterate over the list and print the elements */ 1.106 +UCX_FOREACH(elem, list) { 1.107 + sstr_t s = *((sstr_t*)elem->data); 1.108 + printf("%" PRIsstr "\n", SFMT(s)); 1.109 +} 1.110 + 1.111 +/* Use our free function to free the duplicated strings. */ 1.112 +ucx_list_free_content(list, free_sstr); 1.113 +ucx_list_free(list); 1.114 +``` 1.115 + 1.116 ## Logging 1.117 1.118 *Header file:* [logging.h](api/logging_8h.html) 1.119 @@ -233,30 +280,30 @@ 1.120 ### Example: Loading properties from a file 1.121 1.122 ```C 1.123 -// Open the file as usual 1.124 +/* Open the file as usual */ 1.125 FILE* file = fopen("myprops.properties", "r"); 1.126 if (!file) { 1.127 // error handling 1.128 return 1; 1.129 } 1.130 1.131 -// Load the properties from the file 1.132 +/* Load the properties from the file */ 1.133 UcxMap* myprops = ucx_map_new(16); 1.134 if (ucx_properties_load(myprops, file)) { 1.135 - // error handling 1.136 + /* ... error handling ... */ 1.137 fclose(file); 1.138 ucx_map_free(myprops); 1.139 return 1; 1.140 } 1.141 1.142 -// Print out the key/value pairs 1.143 +/* Print out the key/value pairs */ 1.144 char* propval; 1.145 UcxMapIterator propiter = ucx_map_iterator(myprops); 1.146 UCX_MAP_FOREACH(key, propval, propiter) { 1.147 printf("%s = %s\n", (char*)key.data, propval); 1.148 } 1.149 1.150 -// Don't forget to free the values before freeing the map 1.151 +/* Don't forget to free the values before freeing the map */ 1.152 ucx_map_free_content(myprops, NULL); 1.153 ucx_map_free(myprops); 1.154 fclose(file); 1.155 @@ -395,7 +442,7 @@ 1.156 return 1; 1.157 } 1.158 1.159 - FILE *srcf = fopen(argv[1], "r"); // insert error handling on your own 1.160 + FILE *srcf = fopen(argv[1], "r"); /* insert error handling on your own */ 1.161 FILE *destf = fopen(argv[2], "w"); 1.162 1.163 size_t n = ucx_stream_copy(srcf, destf, fread, fwrite); 1.164 @@ -430,7 +477,7 @@ 1.165 i, prime(i) ? "prime" : "not prime"); 1.166 } 1.167 1.168 -// print the result to stdout 1.169 +/* print the result to stdout */ 1.170 printf("%s", (char*)strbuffer->space); 1.171 1.172 ucx_buffer_free(strbuffer);