xref: /btstack/src/btstack_linked_list.h (revision 8b2bf72f1c19d4e86e548e9b22509122e6a5121b)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  btstack_linked_list.h
40  */
41 
42 #ifndef BTSTACK_LINKED_LIST_H
43 #define BTSTACK_LINKED_LIST_H
44 
45 #include "btstack_bool.h"
46 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 /* API_START */
52 
53 typedef struct btstack_linked_item {
54     struct btstack_linked_item *next; // <-- next element in list, or NULL
55 } btstack_linked_item_t;
56 
57 typedef btstack_linked_item_t * btstack_linked_list_t;
58 
59 typedef struct {
60 	int advance_on_next;
61     btstack_linked_item_t * prev;	// points to the item before the current one
62     btstack_linked_item_t * curr;	// points to the current item (to detect item removal)
63 } btstack_linked_list_iterator_t;
64 
65 
66 /**
67  * @brief Test if list is empty.
68  * @param list
69  * @returns true if list is empty
70  */
71 bool btstack_linked_list_empty(btstack_linked_list_t * list);
72 
73 /**
74  * @brief Add item to list as first element.
75  * @param list
76  * @param item
77  */
78 void btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item);
79 
80 /**
81  * @brief Add item to list as last element.
82  * @param list
83  * @param item
84  */
85 void btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item);
86 
87 /**
88  * @brief Pop (get + remove) first element.
89  * @param list
90  * @returns first element or NULL if list is empty
91  */
92 btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list);
93 
94 /**
95  * @brief Remove item from list
96  * @param list
97  * @param item
98  * @returns 0 if item was found in list, -1 if item was not in list
99  */
100 int btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item);
101 
102 /**
103  * @brief Get first element.
104  * @param list
105  * @returns first element or NULL if list is empty
106  */
107 btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list);
108 
109 /**
110  * @brief Get last element.
111  * @param list
112  * @returns first element or NULL if list is empty
113  */
114 btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list);
115 
116 /**
117  * @brief Counts number of items in list
118  * @returns number of items in list
119  */
120 int btstack_linked_list_count(btstack_linked_list_t * list);
121 
122 
123 
124 /**
125  * @brief Initialize Linked List Iterator
126  * @note robust against removal of current element by btstack_linked_list_remove
127  * @param it iterator context
128  * @param list
129  */
130 void btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * list);
131 
132 /**
133  * @brief Has next element
134  * @param it iterator context
135  * @returns true if next element is available
136  */
137 bool btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it);
138 
139 /**
140  * @brief Get next list eleemnt
141  * @param it iterator context
142  * @returns list element
143  */
144 btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it);
145 
146 /**
147  * @brief Remove current list element from list
148  * @param it iterator context
149  */
150 void btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it);
151 
152 /* API_END */
153 
154 void test_linked_list(void);
155 
156 
157 #if defined __cplusplus
158 }
159 #endif
160 
161 #endif // BTSTACK_LINKED_LIST_H
162