xref: /btstack/src/btstack_linked_list.h (revision 2c539f6e7ed27061f08fb0b85a82276e4b995fb3)
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  *  linked_list.h
40  *
41  *  Created by Matthias Ringwald on 7/13/09.
42  */
43 
44 #ifndef __LINKED_LIST_H
45 #define __LINKED_LIST_H
46 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 typedef struct linked_item {
52     struct linked_item *next; // <-- next element in list, or NULL
53     void *user_data;          // <-- pointer to struct base
54 } linked_item_t;
55 
56 typedef linked_item_t * btstack_linked_list_t;
57 
58 typedef struct {
59 	int advance_on_next;
60     linked_item_t * prev;	// points to the item before the current one
61     linked_item_t * curr;	// points to the current item (to detect item removal)
62 } linked_list_iterator_t;
63 
64 
65 void            linked_item_set_user(linked_item_t *item, void *user_data);        // <-- set user data
66 void *          linked_item_get_user(linked_item_t *item);                         // <-- get user data
67 int             linked_list_empty(btstack_linked_list_t * list);
68 void            linked_list_add(btstack_linked_list_t * list, linked_item_t *item);        // <-- add item to list as first element
69 void            linked_list_add_tail(btstack_linked_list_t * list, linked_item_t *item);   // <-- add item to list as last element
70 int             linked_list_remove(btstack_linked_list_t * list, linked_item_t *item);     // <-- remove item from list
71 linked_item_t * linked_list_get_last_item(btstack_linked_list_t * list);                   // <-- find the last item in the list
72 
73 /**
74  * @brief Counts number of items in list
75  * @returns number of items in list
76  */
77 int linked_list_count(btstack_linked_list_t * list);
78 
79 //
80 // iterator for linked lists. alloes to remove current element. also robust against removal of current element by linked_list_remove
81 //
82 void            linked_list_iterator_init(linked_list_iterator_t * it, btstack_linked_list_t * list);
83 int             linked_list_iterator_has_next(linked_list_iterator_t * it);
84 linked_item_t * linked_list_iterator_next(linked_list_iterator_t * it);
85 void            linked_list_iterator_remove(linked_list_iterator_t * it);
86 
87 void test_linked_list(void);
88 
89 #if defined __cplusplus
90 }
91 #endif
92 
93 #endif // __LINKED_LIST_H
94