- About project
- Installation
- Usage
- Indexpair struct
- Predicate
- Initialize array
- Get value
- Set value
- Destroy
- Add element
- Add array
- Add another dynamic array
- Clear
- Contains element
- Element exists
- Find with predicate
- Find index within range using predicate
- Find index with start index using predicate
- Find index using predicate
- Find last element index within specified range using predicate
- Find last element index with specified start index using predicate
- Do something to every list element
- Find element index within specified range
- Find element index with specified starting index
- Find element index
- Insert element
- Find last element index within specified range
- Find last element index with specified starting index
- Find last element index
- Remove element
- Remove elements using predicate
- Remove element at index
- Reverse list
This is a small dynamic array implementation with a lot functionality. Written in pure C. You can use any object as list element, even custom structs!
- Download header and .c source file (if you want to compile it directly into project) or download .so file from releases.
- Include header into your project.
- Just use!
Library has indexpair struct, defined with typedef. It contains index, if function should return index, and exit code. Using that, you can check if function exited successfully. To access values, do this:
int restored = 0;
indexpair ret = list_get(&listobj, &restored, 2);
int return_code = ret.code; // You got retcode
int index = ret.index; // Index. Returned only from special functions
There is typedef'd predicate type with signature
int (*predicate)(void*,void*)
This is a type for special functions, which require predicate. Using it, you can find values even when you are using custom structs as elements!
Signature:
list list_init(int initial_count, int elementsize);
Array initialization:
list listobj = list_init(1, sizeof(int));
In this example, you allocated array with size 1 and element size equal to sizeof int.
Signature:
indexpair list_get(list* listobj, void* element, uint32_t index);
Get value from array:
int restored = 0;
list_get(&listobj, &restored, 2);
Signature:
indexpair list_set(list* listobj, void* element, uint32_t index);
Set array element value:
int value = 5;
list_set(&listobj, &value, 2);
Signature:
indexpair list_destroy(list* listobj);
Destroy array:
list_destroy(&listobj);
Signature:
indexpair list_add(list* listobj, void* element);
Append element to array and allocate more memory if needed:
int element = 5;
list_add(&listobj, &element);
Signature:
indexpair list_addrange1(list* listobj, void* arr, int count);
Append whole array and allocate more memory if needed:
int arr[3] = {5, 6, 1};
list_addrange1(&listobj, &arr, sizeof(arr) / sizeof(int));
Signature:
indexpair list_addrange2(list* listobj, list* src);
Append another dynamic array:
list_addrange2(&listobj, &another);
Signature:
indexpair list_clear(list* listobj);
Zero element count but do nothing with allocated memory:
list_clear(&listobj);
Signature:
indexpair list_contains(list* listobj, void* element);
Check if array contains element:
int element = 5;
list_contains(&listobj, &element);
Signature:
indexpair list_exists(list* listobj, predicate comparer, void* element);
Check if element exists using predicate passed from user:
custom_type element = {0};
list_exists(&listobj, comparer, &element);
Signature:
indexpair list_find(list* list, void* elementtofind, predicate comparer, void* item);
Find element with predicate:
custom_type elementtofind = {0};
custom_type restored;
list_find(&listobj, &elementtofind, comparer, &restored);
Signature:
indexpair list_findindex1(list* list, uint32_t startindex, uint32_t endindex, predicate comparer, void* elementtofind);
Find element index within specified range using predicate:
custom_type elementtofind = {0};
list_findindex1(&listobj, 0, 10, comparer, &elementtofind);
Signature:
indexpair list_findindex2(list* list, uint32_t startindex, predicate comparer, void* elementtofind);
Find element index with specified start index and custom comparer function:
custom_type elementtofind = {0};
list_findindex2(&listobj, 5, comparer, &elementtofind);
Signature:
indexpair list_find_index3(list* list, predicate comparer, void* elementtofind);
Find element index using predicate:
custom_type elementtofind = {0};
list_findindex3(&listobj, comparer, &element_to_find);
Signature:
indexpair list_findlast(list* list, void* elementtofind, predicate comparer, void* item);
Find last occurrence of element using predicate:
custom_type elementtofind = {0};
custom_type restored;
list_findlast(&listobj, &elementtofind, comparer, &restored);
Signature:
indexpair list_findlastindex1(list* list, uint32_t startindex, uint32_t endindex, predicate comparer, void* elementtofind);
Find last occurrence of element within specified range using predicate:
custom_type elementtofind = {0};
listfindlastindex1(&listobj, 0, 10, comparer, &elementtofind);
Signature:
indexpair list_findlastindex2(list* list, uint32_t startindex, predicate comparer, void* elementtofind);
Find last occurrence of element with specified start index and predicate:
custom_type elementtofind = {0};
list_findlastindex2(&listobj, 5, comparer, &elementtofind);
Signature:
indexpair list_findlastindex3(list* list, predicate comparer, void* elementtofind);
Find last occurrence of element using predicate:
custom_type elementtofind = {0};
list_findlastindex3(&listobj, comparer, &elementtofind);
Signature:
indexpair list_foreach(list* list, void (*action)(void*));
Perform action on each element in the array:
list_foreach(&listobj, custom_action);
Signature:
indexpair list_indexof1(list* list, void* elementtofind, uint32_t startindex, uint32_t endindex);
Get index of element within specified range:
custom_type elementtofind = {0};
list_indexof1(&listobj, &elementtofind, 0, 10);
Signature:
indexpair list_indexof2(list* list, void* elementtofind, uint32_t startindex);
Get index of element with specified start index:
custom_type elementtofind = {0};
list_indexof2(&listobj, &elementtofind, 5);
Signature:
indexpair list_indexof3(list* list, void* elementtofind);
Get index of element starting from beginning:
custom_type elementtofind = {0};
list_indexof3(&listobj, &elementtofind);
Signature:
indexpair listinsert(struct list* list, void* element, uint32t index);
Insert element at specific index:
custom_type newelement = {0};
list_insert(&listobj, &newelement, 5);
Signature:
indexpair list_lastindexof1(list* list, void* elementtofind, uint32_t startindex, uint32_t endindex);
Get last index of element within specified range:
custom_type elementtofind = {0};
list_lastindexof1(&listobj, &elementtofind, 0, 10);
Signature:
indexpair list_lastindexof2(list* list, void* elementtofind, uint32_t startindex);
Get last index of element with specified start index:
custom_type elementtofind = {0};
list_lastindexof2(&listobj, &elementtofind, 5);
Signature:
indexpair list_lastindexof3(list* list, void* elementtofind);
Get last index of element:
custom_type elementtofind = {0};
list_lastindexof3(&listobj, &elementtofind);
Signature:
indexpair list_remove(list* list, void* elementtofind);
Remove element from the list:
custom_type element_to_remove = {0};
list_remove(&listobj, &element_to_remove);
Signature:
indexpair list_removeall(list* list, void* elementtofind, predicate comparer);
Remove all occurrences of element using predicate:
custom_type element_to_remove = {0};
list_removeall(&listobj, &element_to_remove, comparer);
Signature:
indexpair list_removeat(list* list, uint32_t index);
Remove element at specific index:
list_removeat(&listobj, 5);
Signature:
indexpair list_reverse(list* list);
Reverse the order of elements in the list:
list_reverse(&listobj);