Skip to content

Latest commit

 

History

History
410 lines (374 loc) · 11.6 KB

README.md

File metadata and controls

410 lines (374 loc) · 11.6 KB

ListLib

New level dynamic array library.

contributors last commit forks contributors contributors


Contents

About ListLib

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!

Installation

  1. Download header and .c source file (if you want to compile it directly into project) or download .so file from releases.
  2. Include header into your project.
  3. Just use!

Usage

Indexpair struct

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

Predicate

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!

Initialization

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.

Get value

Signature:

indexpair list_get(list* listobj, void* element, uint32_t index);

Get value from array:

int restored = 0;
list_get(&listobj, &restored, 2);

Set value

Signature:

indexpair list_set(list* listobj, void* element, uint32_t index);

Set array element value:

int value = 5;
list_set(&listobj, &value, 2);

Destroy array

Signature:

indexpair list_destroy(list* listobj);

Destroy array:

list_destroy(&listobj);

Add element

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);

Add array

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));

Add another dynamic array

Signature:

indexpair list_addrange2(list* listobj, list* src);

Append another dynamic array:

list_addrange2(&listobj, &another);

Clear array

Signature:

indexpair list_clear(list* listobj);

Zero element count but do nothing with allocated memory:

list_clear(&listobj);

Check if array contains element

Signature:

indexpair list_contains(list* listobj, void* element);

Check if array contains element:

int element = 5;
list_contains(&listobj, &element);

Check element existance

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);

Find element with predicate

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);

Find element index within specified range using predicate

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);

Find element index with specified start index using predicate

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);

Find element index using predicate

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);

Find last occurrence of element using predicate

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);

Find last index of element within specified range using predicate

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);

Find last index of element with start index and using predicate

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);

Find last index of element with predicate

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);

Perform action on each element in the array

Signature:

indexpair list_foreach(list* list, void (*action)(void*));

Perform action on each element in the array:

list_foreach(&listobj, custom_action);

Get index of element within specified range

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);

Get index of element with start index

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);

Get index of element

Signature:

indexpair list_indexof3(list* list, void* elementtofind);

Get index of element starting from beginning:

custom_type elementtofind = {0};
list_indexof3(&listobj, &elementtofind);

Insert element at specific index

Signature:

indexpair listinsert(struct list* list, void* element, uint32t index);

Insert element at specific index:

custom_type newelement = {0};
list_insert(&listobj, &newelement, 5);

Get last index of element within specified range

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);

Get last index of element with start index

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);

Get last index of element

Signature:

indexpair list_lastindexof3(list* list, void* elementtofind);

Get last index of element:

custom_type elementtofind = {0};
list_lastindexof3(&listobj, &elementtofind);

Remove element from the list

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);

Remove all occurrences of element using predicate

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);

Remove element at specific index

Signature:

indexpair list_removeat(list* list, uint32_t index);

Remove element at specific index:

list_removeat(&listobj, 5);

Reverse the order of elements in the list

Signature:

indexpair list_reverse(list* list);

Reverse the order of elements in the list:

list_reverse(&listobj);