| 8 |
8 |
| 9 ```C |
9 ```C |
| 10 #include <cx/linked_list.h> |
10 #include <cx/linked_list.h> |
| 11 |
11 |
| 12 CxList *cxLinkedListCreate(const CxAllocator *allocator, |
12 CxList *cxLinkedListCreate(const CxAllocator *allocator, |
| 13 cx_compare_func comparator, size_t elem_size); |
13 size_t elem_size); |
| 14 |
|
| 15 CxList *cxLinkedListCreateSimple(size_t elem_size); |
|
| 16 |
14 |
| 17 #include <cx/array_list.h> |
15 #include <cx/array_list.h> |
| 18 |
16 |
| 19 CxList *cxArrayListCreate(const CxAllocator *allocator, |
17 CxList *cxArrayListCreate(const CxAllocator *allocator, |
| 20 cx_compare_func comparator, size_t elem_size, |
18 size_t elem_size, size_t initial_capacity); |
| 21 size_t initial_capacity); |
|
| 22 |
|
| 23 CxList *cxArrayListCreateSimple(size_t elem_size, |
|
| 24 size_t initial_capacity); |
|
| 25 ``` |
19 ``` |
| 26 |
20 |
| 27 The function `cxLinkedListCreate()` creates a new linked list with the specified `allocator` which stores elements of size `elem_size`. |
21 The function `cxLinkedListCreate()` creates a new linked list with the specified `allocator` which stores elements of size `elem_size`. |
| 28 The elements are supposed to be compared with the specified `comparator` (cf. [](#access-and-find)). |
22 The elements are supposed to be compared with the specified `comparator` (cf. [](#access-and-find)). |
| 29 The function `cxLinkedListCreateSimple()` will use the [default allocator](allocator.h.md#default-allocator) and does not specify a compare function. |
|
| 30 |
23 |
| 31 Array lists can be created with `cxArrayListCreate()` where the additional argument `initial_capacity` specifies for how many elements the underlying array shall be initially allocated. |
24 Array lists can be created with `cxArrayListCreate()` where the additional argument `initial_capacity` specifies for how many elements the underlying array shall be initially allocated. |
| 32 |
25 |
| 33 If `CX_STORE_POINTERS` is used as `elem_size`, the actual element size will be `sizeof(void*)` and the list will behave slightly differently when accessing elements. |
26 If `CX_STORE_POINTERS` is used as `elem_size`, the actual element size will be `sizeof(void*)` and the list will behave slightly differently when accessing elements. |
| 34 Lists that are storing pointers will always return the stored pointer directly, instead of returning a pointer into the list's memory, thus saving you from unnecessary dereferencing. |
27 Lists that are storing pointers will always return the stored pointer directly, instead of returning a pointer into the list's memory, thus saving you from unnecessary dereferencing. |
| 49 #include <cx/linked_list.h> |
42 #include <cx/linked_list.h> |
| 50 #include <regex.h> |
43 #include <regex.h> |
| 51 |
44 |
| 52 CxList *create_pattern_list(void) { |
45 CxList *create_pattern_list(void) { |
| 53 // create a linked list to store patterns |
46 // create a linked list to store patterns |
| 54 CxList *list = cxLinkedListCreateSimple(sizeof(regex_t)); |
47 CxList *list = cxLinkedListCreate(NULL, sizeof(regex_t)); |
| 55 |
48 |
| 56 // automatically free the pattern when list gets destroyed |
49 // automatically free the pattern when list gets destroyed |
| 57 cxDefineDestructor(list, regfree); |
50 cxSetDestructor(list, regfree); |
| 58 |
51 |
| 59 return list; |
52 return list; |
| 60 } |
53 } |
| 61 |
54 |
| 62 int add_pattern(CxList *list, const char *value) { |
55 int add_pattern(CxList *list, const char *value) { |
| 510 // your custom list data goes here |
503 // your custom list data goes here |
| 511 } my_list; |
504 } my_list; |
| 512 |
505 |
| 513 CxList *my_list_create( |
506 CxList *my_list_create( |
| 514 const CxAllocator *allocator, |
507 const CxAllocator *allocator, |
| 515 cx_compare_func cmpfun, |
|
| 516 size_t elem_size |
508 size_t elem_size |
| 517 ) { |
509 ) { |
| 518 if (allocator == NULL) { |
510 if (allocator == NULL) { |
| 519 allocator = cxDefaultAllocator; |
511 allocator = cxDefaultAllocator; |
| 520 } |
512 } |
| 521 |
513 |
| 522 my_list *list = cxCalloc(allocator, 1, sizeof(my_list)); |
514 my_list *list = cxCalloc(allocator, 1, sizeof(my_list)); |
| 523 if (list == NULL) return NULL; |
515 if (list == NULL) return NULL; |
| 524 cx_list_init((CxList*)list, &my_list_class, |
516 cx_list_init((CxList*)list, &my_list_class, |
| 525 allocator, cmpfun, elem_size); |
517 allocator, elem_size); |
| 526 |
518 |
| 527 // initialize your custom list data here |
519 // initialize your custom list data here |
| 528 |
520 |
| 529 return (CxList *) list; |
521 return (CxList *) list; |
| 530 } |
522 } |