mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-26 06:55:59 +03:00
Improve the c_list functions.
This commit is contained in:
parent
9968e364d4
commit
6ef53dd1a4
2 changed files with 64 additions and 73 deletions
107
src/std/c_list.c
107
src/std/c_list.c
|
@ -36,17 +36,20 @@ c_list_t *c_list_append(c_list_t *list, void *data) {
|
||||||
c_list_t *new, *last;
|
c_list_t *new, *last;
|
||||||
|
|
||||||
new = c_list_alloc();
|
new = c_list_alloc();
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
new->data = data;
|
new->data = data;
|
||||||
|
|
||||||
if (list) {
|
if (list == NULL) {
|
||||||
|
return new;
|
||||||
|
} else {
|
||||||
last = c_list_last(list);
|
last = c_list_last(list);
|
||||||
|
|
||||||
last->next = new;
|
last->next = new;
|
||||||
new->prev = last;
|
new->prev = last;
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
} else {
|
|
||||||
return new;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,9 +60,12 @@ c_list_t *c_list_prepend(c_list_t *list, void *data) {
|
||||||
c_list_t *new, *first;
|
c_list_t *new, *first;
|
||||||
|
|
||||||
new = c_list_alloc();
|
new = c_list_alloc();
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
new->data = data;
|
new->data = data;
|
||||||
|
|
||||||
if (list) {
|
if (list != NULL) {
|
||||||
first = c_list_first(list);
|
first = c_list_first(list);
|
||||||
|
|
||||||
first->prev = new;
|
first->prev = new;
|
||||||
|
@ -89,6 +95,9 @@ c_list_t *c_list_insert(c_list_t *list, void *data, long position) {
|
||||||
}
|
}
|
||||||
|
|
||||||
new = c_list_alloc();
|
new = c_list_alloc();
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
new->data = data;
|
new->data = data;
|
||||||
|
|
||||||
/* List is not empty */
|
/* List is not empty */
|
||||||
|
@ -117,10 +126,13 @@ c_list_t *c_list_insert_sorted(c_list_t *list, void *data, c_list_compare_fn fun
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
new = c_list_alloc();
|
new = c_list_alloc();
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
new->data = data;
|
new->data = data;
|
||||||
|
|
||||||
/* list is empty */
|
/* list is empty */
|
||||||
if (!list) {
|
if (list == NULL) {
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +197,7 @@ c_list_t *c_list_remove(c_list_t *list, void *data) {
|
||||||
|
|
||||||
temp = list;
|
temp = list;
|
||||||
|
|
||||||
while (temp) {
|
while (temp != NULL) {
|
||||||
if (temp->data != data) {
|
if (temp->data != data) {
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
} else {
|
} else {
|
||||||
|
@ -204,7 +216,7 @@ c_list_t *c_list_remove(c_list_t *list, void *data) {
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
free (temp);
|
SAFE_FREE(temp);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,13 +232,13 @@ void c_list_free(c_list_t *list) {
|
||||||
c_list_t *temp;
|
c_list_t *temp;
|
||||||
list = c_list_last(list);
|
list = c_list_last(list);
|
||||||
|
|
||||||
while (list->prev) {
|
while (list->prev != NULL) {
|
||||||
temp = list;
|
temp = list;
|
||||||
list = list->prev;
|
list = list->prev;
|
||||||
|
|
||||||
free (temp);
|
SAFE_FREE(temp);
|
||||||
}
|
}
|
||||||
free(list);
|
SAFE_FREE(list);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,10 +246,10 @@ void c_list_free(c_list_t *list) {
|
||||||
* Gets the next element in a c_list.
|
* Gets the next element in a c_list.
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_next(c_list_t *list) {
|
c_list_t *c_list_next(c_list_t *list) {
|
||||||
if (list) {
|
if (list == NULL) {
|
||||||
return list->next;
|
|
||||||
} else {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
} else {
|
||||||
|
return list->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,10 +257,10 @@ c_list_t *c_list_next(c_list_t *list) {
|
||||||
* Gets the previous element in a c_list.
|
* Gets the previous element in a c_list.
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_previous(c_list_t *list) {
|
c_list_t *c_list_previous(c_list_t *list) {
|
||||||
if (list) {
|
if (list == NULL) {
|
||||||
return list->prev;
|
|
||||||
} else {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
} else {
|
||||||
|
return list->prev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,7 +270,7 @@ c_list_t *c_list_previous(c_list_t *list) {
|
||||||
unsigned long c_list_length(c_list_t *list) {
|
unsigned long c_list_length(c_list_t *list) {
|
||||||
unsigned long length = 0;
|
unsigned long length = 0;
|
||||||
|
|
||||||
if (list) {
|
if (list != NULL) {
|
||||||
while (list->next) {
|
while (list->next) {
|
||||||
length++;
|
length++;
|
||||||
list = list->next;
|
list = list->next;
|
||||||
|
@ -273,7 +285,7 @@ unsigned long c_list_length(c_list_t *list) {
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_first(c_list_t *list) {
|
c_list_t *c_list_first(c_list_t *list) {
|
||||||
|
|
||||||
if (list) {
|
if (list != NULL) {
|
||||||
while (list->prev) {
|
while (list->prev) {
|
||||||
list = list->prev;
|
list = list->prev;
|
||||||
}
|
}
|
||||||
|
@ -287,7 +299,7 @@ c_list_t *c_list_first(c_list_t *list) {
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_last(c_list_t *list) {
|
c_list_t *c_list_last(c_list_t *list) {
|
||||||
|
|
||||||
if (list) {
|
if (list != NULL) {
|
||||||
while (list->next) {
|
while (list->next) {
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
|
@ -301,7 +313,7 @@ c_list_t *c_list_last(c_list_t *list) {
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_position(c_list_t *list, long position) {
|
c_list_t *c_list_position(c_list_t *list, long position) {
|
||||||
|
|
||||||
while ((position-- > 0) && list) {
|
while ((position-- > 0) && list != NULL) {
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,17 +324,17 @@ c_list_t *c_list_position(c_list_t *list, long position) {
|
||||||
* Finds the element in a c_list_t which contains the given data.
|
* Finds the element in a c_list_t which contains the given data.
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_find(c_list_t *list, void *data) {
|
c_list_t *c_list_find(c_list_t *list, void *data) {
|
||||||
if (list) {
|
if (list == NULL) {
|
||||||
while (list) {
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (list != NULL) {
|
||||||
if (list->data == data) {
|
if (list->data == data) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -332,8 +344,8 @@ c_list_t *c_list_find(c_list_t *list, void *data) {
|
||||||
c_list_t *c_list_find_custom(c_list_t *list, void *data, c_list_compare_fn func) {
|
c_list_t *c_list_find_custom(c_list_t *list, void *data, c_list_compare_fn func) {
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
if (list && func) {
|
if (list != NULL && func != NULL) {
|
||||||
while (list) {
|
while (list != NULL) {
|
||||||
cmp = ((c_list_compare_fn) func) (list->data, data);
|
cmp = ((c_list_compare_fn) func) (list->data, data);
|
||||||
if (cmp == 0) {
|
if (cmp == 0) {
|
||||||
return list;
|
return list;
|
||||||
|
@ -345,28 +357,10 @@ c_list_t *c_list_find_custom(c_list_t *list, void *data, c_list_compare_fn func)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Sorts the elements of a c_list.
|
|
||||||
*/
|
|
||||||
c_list_t *c_list_sort(c_list_t *list, c_list_compare_fn func) {
|
|
||||||
c_list_t *second;
|
|
||||||
/* list is empty */
|
|
||||||
if (list == NULL) {
|
|
||||||
return NULL;
|
|
||||||
/* list has only one element */
|
|
||||||
} else if (list->next == NULL) {
|
|
||||||
return list;
|
|
||||||
} else {
|
|
||||||
/* split list */
|
|
||||||
second = c_list_split(list);
|
|
||||||
return c_list_merge(c_list_sort(list, func), c_list_sort(second, func), func);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Internal used function to merge 2 lists using a compare function
|
* Internal used function to merge 2 lists using a compare function
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_merge(c_list_t *list1, c_list_t *list2, c_list_compare_fn func) {
|
static c_list_t *c_list_merge(c_list_t *list1, c_list_t *list2, c_list_compare_fn func) {
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
/* lists are emty */
|
/* lists are emty */
|
||||||
|
@ -395,7 +389,7 @@ c_list_t *c_list_merge(c_list_t *list1, c_list_t *list2, c_list_compare_fn func)
|
||||||
/*
|
/*
|
||||||
* Internally used function to split 2 lists.
|
* Internally used function to split 2 lists.
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_split(c_list_t *list) {
|
static c_list_t *c_list_split(c_list_t *list) {
|
||||||
c_list_t *second;
|
c_list_t *second;
|
||||||
|
|
||||||
/* list is empty */
|
/* list is empty */
|
||||||
|
@ -425,3 +419,22 @@ c_list_t *c_list_split(c_list_t *list) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sorts the elements of a c_list.
|
||||||
|
*/
|
||||||
|
c_list_t *c_list_sort(c_list_t *list, c_list_compare_fn func) {
|
||||||
|
c_list_t *second;
|
||||||
|
/* list is empty */
|
||||||
|
if (list == NULL) {
|
||||||
|
return NULL;
|
||||||
|
/* list has only one element */
|
||||||
|
} else if (list->next == NULL) {
|
||||||
|
return list;
|
||||||
|
} else {
|
||||||
|
/* split list */
|
||||||
|
second = c_list_split(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c_list_merge(c_list_sort(list, func), c_list_sort(second, func), func);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -270,27 +270,5 @@ c_list_t *c_list_find_custom(c_list_t *list, void *data, c_list_compare_fn func)
|
||||||
*/
|
*/
|
||||||
c_list_t *c_list_sort(c_list_t *list, c_list_compare_fn func);
|
c_list_t *c_list_sort(c_list_t *list, c_list_compare_fn func);
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal used function to merge 2 lists using a compare function
|
|
||||||
*
|
|
||||||
* @param list1 A pointer to c_list.
|
|
||||||
*
|
|
||||||
* @param list2 A pointer to c_list.
|
|
||||||
*
|
|
||||||
* @return New start of the list, which may have changed, so make
|
|
||||||
* sure you store the new value.
|
|
||||||
*/
|
|
||||||
c_list_t *c_list_merge(c_list_t *list1, c_list_t *list2, c_list_compare_fn func);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internally used function to split 2 lists.
|
|
||||||
*
|
|
||||||
* @param list A pointer to c_list.
|
|
||||||
*
|
|
||||||
* @return New start of the list, which may have changed, so make
|
|
||||||
* sure you store the new value.
|
|
||||||
*/
|
|
||||||
c_list_t *c_list_split(c_list_t *list);
|
|
||||||
|
|
||||||
#endif /* _C_LIST_H */
|
#endif /* _C_LIST_H */
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue