1 /*
2     bctoolbox
3     Copyright (C) 2016  Belledonne Communications SARL
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef BCTBX_LIST_H_
20 #define BCTBX_LIST_H_
21 
22 #include "bctoolbox/port.h"
23 
24 #ifdef __cplusplus
25 extern "C"{
26 #endif
27 
28 typedef struct _bctbx_list {
29 	struct _bctbx_list *next;
30 	struct _bctbx_list *prev;
31 	void *data;
32 } bctbx_list_t;
33 
34 typedef int (*bctbx_compare_func)(const void *, const void*);
35 typedef void (*bctbx_list_iterate_func)(void *);
36 typedef void (*bctbx_list_iterate2_func)(void *, void *);
37 typedef void (*bctbx_list_free_func)(void *);
38 typedef void* (*bctbx_list_copy_func)(void *);
39 
40 BCTBX_PUBLIC bctbx_list_t * bctbx_list_new(void *data);
41 BCTBX_PUBLIC bctbx_list_t * bctbx_list_append(bctbx_list_t * elem, void * data);
42 BCTBX_PUBLIC bctbx_list_t * bctbx_list_append_link(bctbx_list_t * elem, bctbx_list_t *new_elem);
43 BCTBX_PUBLIC bctbx_list_t * bctbx_list_prepend(bctbx_list_t * elem, void * data);
44 BCTBX_PUBLIC bctbx_list_t * bctbx_list_prepend_link(bctbx_list_t* elem, bctbx_list_t *new_elem);
45 BCTBX_PUBLIC bctbx_list_t * bctbx_list_last_elem(const bctbx_list_t *l);
46 BCTBX_PUBLIC bctbx_list_t * bctbx_list_first_elem(const bctbx_list_t *l);
47 BCTBX_PUBLIC bctbx_list_t * bctbx_list_free(bctbx_list_t * elem);
48 BCTBX_PUBLIC bctbx_list_t * bctbx_list_concat(bctbx_list_t * first, bctbx_list_t * second);
49 BCTBX_PUBLIC bctbx_list_t * bctbx_list_remove(bctbx_list_t * first, void *data);
50 BCTBX_PUBLIC bctbx_list_t * bctbx_list_remove_custom(bctbx_list_t *first, bctbx_compare_func compare_func, const void *user_data);
51 BCTBX_PUBLIC bctbx_list_t * bctbx_list_pop_front(bctbx_list_t *list, void **front_data);
52 BCTBX_PUBLIC size_t bctbx_list_size(const bctbx_list_t * first);
53 BCTBX_PUBLIC void bctbx_list_for_each(const bctbx_list_t * list, bctbx_list_iterate_func func);
54 BCTBX_PUBLIC void bctbx_list_for_each2(const bctbx_list_t * list, bctbx_list_iterate2_func func, void *user_data);
55 /**
56  * Removes the element pointed by elem from the list. The element itself is not freed, allowing
57  * to be chained in another list for example.
58  * Use bctbx_list_erase_link() if you simply want to delete an element of a list.
59 **/
60 BCTBX_PUBLIC bctbx_list_t * bctbx_list_unlink(bctbx_list_t * list, bctbx_list_t * elem);
61 /**
62  * Delete the element pointed by 'elem' from the list.
63 **/
64 BCTBX_PUBLIC bctbx_list_t * bctbx_list_erase_link(bctbx_list_t * list, bctbx_list_t * elem);
65 BCTBX_PUBLIC bctbx_list_t * bctbx_list_find(bctbx_list_t *list, const void *data);
66 BCTBX_PUBLIC bctbx_list_t * bctbx_list_free(bctbx_list_t *list);
67 /*frees list elements and associated data, using the supplied function pointer*/
68 BCTBX_PUBLIC bctbx_list_t * bctbx_list_free_with_data(bctbx_list_t *list, bctbx_list_free_func freefunc);
69 
70 BCTBX_PUBLIC bctbx_list_t * bctbx_list_find_custom(const bctbx_list_t * list, bctbx_compare_func cmp, const void *user_data);
71 BCTBX_PUBLIC void * bctbx_list_nth_data(const bctbx_list_t * list, int index);
72 BCTBX_PUBLIC int bctbx_list_position(const bctbx_list_t * list, bctbx_list_t * elem);
73 BCTBX_PUBLIC int bctbx_list_index(const bctbx_list_t * list, void *data);
74 BCTBX_PUBLIC bctbx_list_t * bctbx_list_insert_sorted(bctbx_list_t * list, void *data, bctbx_compare_func cmp);
75 BCTBX_PUBLIC bctbx_list_t * bctbx_list_insert(bctbx_list_t * list, bctbx_list_t * before, void *data);
76 BCTBX_PUBLIC bctbx_list_t * bctbx_list_copy(const bctbx_list_t * list);
77 /*copy list elements and associated data, using the supplied function pointer*/
78 BCTBX_PUBLIC bctbx_list_t* bctbx_list_copy_with_data(const bctbx_list_t* list, bctbx_list_copy_func copyfunc);
79 /*Same as bctbx_list_copy_with_data but in reverse order*/
80 BCTBX_PUBLIC bctbx_list_t* bctbx_list_copy_reverse_with_data(const bctbx_list_t* list, bctbx_list_copy_func copyfunc);
81 
82 BCTBX_PUBLIC bctbx_list_t* bctbx_list_next(const bctbx_list_t *elem);
83 BCTBX_PUBLIC void* bctbx_list_get_data(const bctbx_list_t *elem);
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif /* BCTLBX_LIST_H_ */
90