xref: /btstack/src/btstack_linked_list.h (revision 665d90f25e49e1d328f9893e7fae4c175eed21a6)
14fd23d47SMatthias Ringwald /*
24fd23d47SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
34fd23d47SMatthias Ringwald  *
44fd23d47SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
54fd23d47SMatthias Ringwald  * modification, are permitted provided that the following conditions
64fd23d47SMatthias Ringwald  * are met:
74fd23d47SMatthias Ringwald  *
84fd23d47SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
94fd23d47SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
104fd23d47SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
114fd23d47SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
124fd23d47SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
134fd23d47SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
144fd23d47SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
154fd23d47SMatthias Ringwald  *    from this software without specific prior written permission.
164fd23d47SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
174fd23d47SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
184fd23d47SMatthias Ringwald  *    monetary gain.
194fd23d47SMatthias Ringwald  *
204fd23d47SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
214fd23d47SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
224fd23d47SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
234fd23d47SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
244fd23d47SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
254fd23d47SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
264fd23d47SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
274fd23d47SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
284fd23d47SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
294fd23d47SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
304fd23d47SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314fd23d47SMatthias Ringwald  * SUCH DAMAGE.
324fd23d47SMatthias Ringwald  *
334fd23d47SMatthias Ringwald  * Please inquire about commercial licensing options at
344fd23d47SMatthias Ringwald  * [email protected]
354fd23d47SMatthias Ringwald  *
364fd23d47SMatthias Ringwald  */
374fd23d47SMatthias Ringwald 
384fd23d47SMatthias Ringwald /*
39*665d90f2SMatthias Ringwald  *  btstack_linked_list.h
404fd23d47SMatthias Ringwald  */
414fd23d47SMatthias Ringwald 
42*665d90f2SMatthias Ringwald #ifndef __BTSTACK_LINKED_LIST_H
43*665d90f2SMatthias Ringwald #define __BTSTACK_LINKED_LIST_H
444fd23d47SMatthias Ringwald 
454fd23d47SMatthias Ringwald #if defined __cplusplus
464fd23d47SMatthias Ringwald extern "C" {
474fd23d47SMatthias Ringwald #endif
484fd23d47SMatthias Ringwald 
49*665d90f2SMatthias Ringwald typedef struct btstack_linked_item {
50*665d90f2SMatthias Ringwald     struct btstack_linked_item *next; // <-- next element in list, or NULL
514fd23d47SMatthias Ringwald     void *user_data;          // <-- pointer to struct base
52*665d90f2SMatthias Ringwald } btstack_linked_item_t;
534fd23d47SMatthias Ringwald 
54*665d90f2SMatthias Ringwald typedef btstack_linked_item_t * btstack_btstack_linked_list_t;
554fd23d47SMatthias Ringwald 
564fd23d47SMatthias Ringwald typedef struct {
574fd23d47SMatthias Ringwald 	int advance_on_next;
58*665d90f2SMatthias Ringwald     btstack_linked_item_t * prev;	// points to the item before the current one
59*665d90f2SMatthias Ringwald     btstack_linked_item_t * curr;	// points to the current item (to detect item removal)
60*665d90f2SMatthias Ringwald } btstack_linked_list_iterator_t;
614fd23d47SMatthias Ringwald 
624fd23d47SMatthias Ringwald 
63*665d90f2SMatthias Ringwald // set user data
64*665d90f2SMatthias Ringwald void                    btstack_linked_item_set_user(btstack_linked_item_t *item, void *user_data);
65*665d90f2SMatthias Ringwald // get user data
66*665d90f2SMatthias Ringwald void *                  btstack_linked_item_get_user(btstack_linked_item_t *item);
67*665d90f2SMatthias Ringwald // test if list is empty
68*665d90f2SMatthias Ringwald int                     btstack_linked_list_empty(btstack_btstack_linked_list_t * list);
69*665d90f2SMatthias Ringwald // add item to list as first element
70*665d90f2SMatthias Ringwald void                    btstack_linked_list_add(btstack_btstack_linked_list_t * list, btstack_linked_item_t *item);
71*665d90f2SMatthias Ringwald // add item to list as last element
72*665d90f2SMatthias Ringwald void                    btstack_linked_list_add_tail(btstack_btstack_linked_list_t * list, btstack_linked_item_t *item);
73*665d90f2SMatthias Ringwald // remove item from list
74*665d90f2SMatthias Ringwald int                     btstack_linked_list_remove(btstack_btstack_linked_list_t * list, btstack_linked_item_t *item);
75*665d90f2SMatthias Ringwald // find the last item in the list
76*665d90f2SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_btstack_linked_list_t * list);
774fd23d47SMatthias Ringwald 
784fd23d47SMatthias Ringwald /**
794fd23d47SMatthias Ringwald  * @brief Counts number of items in list
804fd23d47SMatthias Ringwald  * @returns number of items in list
814fd23d47SMatthias Ringwald  */
82*665d90f2SMatthias Ringwald int btstack_linked_list_count(btstack_btstack_linked_list_t * list);
834fd23d47SMatthias Ringwald 
844fd23d47SMatthias Ringwald //
85*665d90f2SMatthias Ringwald // iterator for linked lists. allows to remove current element.
86*665d90f2SMatthias Ringwald // robust against removal of current element by btstack_linked_list_remove.
874fd23d47SMatthias Ringwald //
88*665d90f2SMatthias Ringwald void            btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_btstack_linked_list_t * list);
89*665d90f2SMatthias Ringwald int             btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it);
90*665d90f2SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it);
91*665d90f2SMatthias Ringwald void            btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it);
924fd23d47SMatthias Ringwald 
934fd23d47SMatthias Ringwald void test_linked_list(void);
944fd23d47SMatthias Ringwald 
954fd23d47SMatthias Ringwald #if defined __cplusplus
964fd23d47SMatthias Ringwald }
974fd23d47SMatthias Ringwald #endif
984fd23d47SMatthias Ringwald 
99*665d90f2SMatthias Ringwald #endif // __BTSTACK_LINKED_LIST_H
100