xref: /btstack/src/btstack_linked_list.h (revision 4fd23d473838d04835bc511e0b2828d1d21a6b1e)
1*4fd23d47SMatthias Ringwald /*
2*4fd23d47SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*4fd23d47SMatthias Ringwald  *
4*4fd23d47SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*4fd23d47SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*4fd23d47SMatthias Ringwald  * are met:
7*4fd23d47SMatthias Ringwald  *
8*4fd23d47SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*4fd23d47SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*4fd23d47SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*4fd23d47SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*4fd23d47SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*4fd23d47SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*4fd23d47SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*4fd23d47SMatthias Ringwald  *    from this software without specific prior written permission.
16*4fd23d47SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*4fd23d47SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*4fd23d47SMatthias Ringwald  *    monetary gain.
19*4fd23d47SMatthias Ringwald  *
20*4fd23d47SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*4fd23d47SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*4fd23d47SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*4fd23d47SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*4fd23d47SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*4fd23d47SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*4fd23d47SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*4fd23d47SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*4fd23d47SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*4fd23d47SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*4fd23d47SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*4fd23d47SMatthias Ringwald  * SUCH DAMAGE.
32*4fd23d47SMatthias Ringwald  *
33*4fd23d47SMatthias Ringwald  * Please inquire about commercial licensing options at
34*4fd23d47SMatthias Ringwald  * [email protected]
35*4fd23d47SMatthias Ringwald  *
36*4fd23d47SMatthias Ringwald  */
37*4fd23d47SMatthias Ringwald 
38*4fd23d47SMatthias Ringwald /*
39*4fd23d47SMatthias Ringwald  *  linked_list.h
40*4fd23d47SMatthias Ringwald  *
41*4fd23d47SMatthias Ringwald  *  Created by Matthias Ringwald on 7/13/09.
42*4fd23d47SMatthias Ringwald  */
43*4fd23d47SMatthias Ringwald 
44*4fd23d47SMatthias Ringwald #ifndef __LINKED_LIST_H
45*4fd23d47SMatthias Ringwald #define __LINKED_LIST_H
46*4fd23d47SMatthias Ringwald 
47*4fd23d47SMatthias Ringwald #if defined __cplusplus
48*4fd23d47SMatthias Ringwald extern "C" {
49*4fd23d47SMatthias Ringwald #endif
50*4fd23d47SMatthias Ringwald 
51*4fd23d47SMatthias Ringwald typedef struct linked_item {
52*4fd23d47SMatthias Ringwald     struct linked_item *next; // <-- next element in list, or NULL
53*4fd23d47SMatthias Ringwald     void *user_data;          // <-- pointer to struct base
54*4fd23d47SMatthias Ringwald } linked_item_t;
55*4fd23d47SMatthias Ringwald 
56*4fd23d47SMatthias Ringwald typedef linked_item_t * bk_linked_list_t;
57*4fd23d47SMatthias Ringwald 
58*4fd23d47SMatthias Ringwald typedef struct {
59*4fd23d47SMatthias Ringwald 	int advance_on_next;
60*4fd23d47SMatthias Ringwald     linked_item_t * prev;	// points to the item before the current one
61*4fd23d47SMatthias Ringwald     linked_item_t * curr;	// points to the current item (to detect item removal)
62*4fd23d47SMatthias Ringwald } linked_list_iterator_t;
63*4fd23d47SMatthias Ringwald 
64*4fd23d47SMatthias Ringwald 
65*4fd23d47SMatthias Ringwald void            linked_item_set_user(linked_item_t *item, void *user_data);        // <-- set user data
66*4fd23d47SMatthias Ringwald void *          linked_item_get_user(linked_item_t *item);                         // <-- get user data
67*4fd23d47SMatthias Ringwald int             linked_list_empty(bk_linked_list_t * list);
68*4fd23d47SMatthias Ringwald void            linked_list_add(bk_linked_list_t * list, linked_item_t *item);        // <-- add item to list as first element
69*4fd23d47SMatthias Ringwald void            linked_list_add_tail(bk_linked_list_t * list, linked_item_t *item);   // <-- add item to list as last element
70*4fd23d47SMatthias Ringwald int             linked_list_remove(bk_linked_list_t * list, linked_item_t *item);     // <-- remove item from list
71*4fd23d47SMatthias Ringwald linked_item_t * linked_list_get_last_item(bk_linked_list_t * list);                   // <-- find the last item in the list
72*4fd23d47SMatthias Ringwald 
73*4fd23d47SMatthias Ringwald /**
74*4fd23d47SMatthias Ringwald  * @brief Counts number of items in list
75*4fd23d47SMatthias Ringwald  * @returns number of items in list
76*4fd23d47SMatthias Ringwald  */
77*4fd23d47SMatthias Ringwald int linked_list_count(bk_linked_list_t * list);
78*4fd23d47SMatthias Ringwald 
79*4fd23d47SMatthias Ringwald //
80*4fd23d47SMatthias Ringwald // iterator for linked lists. alloes to remove current element. also robust against removal of current element by linked_list_remove
81*4fd23d47SMatthias Ringwald //
82*4fd23d47SMatthias Ringwald void            linked_list_iterator_init(linked_list_iterator_t * it, bk_linked_list_t * list);
83*4fd23d47SMatthias Ringwald int             linked_list_iterator_has_next(linked_list_iterator_t * it);
84*4fd23d47SMatthias Ringwald linked_item_t * linked_list_iterator_next(linked_list_iterator_t * it);
85*4fd23d47SMatthias Ringwald void            linked_list_iterator_remove(linked_list_iterator_t * it);
86*4fd23d47SMatthias Ringwald 
87*4fd23d47SMatthias Ringwald void test_linked_list(void);
88*4fd23d47SMatthias Ringwald 
89*4fd23d47SMatthias Ringwald #if defined __cplusplus
90*4fd23d47SMatthias Ringwald }
91*4fd23d47SMatthias Ringwald #endif
92*4fd23d47SMatthias Ringwald 
93*4fd23d47SMatthias Ringwald #endif // __LINKED_LIST_H
94