xref: /btstack/src/btstack_linked_list.h (revision 80e33422a96c028b3a9c308fc4b9b874712dafb4)
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 /*
39665d90f2SMatthias Ringwald  *  btstack_linked_list.h
404fd23d47SMatthias Ringwald  */
414fd23d47SMatthias Ringwald 
42*80e33422SMatthias Ringwald #ifndef BTSTACK_LINKED_LIST_H
43*80e33422SMatthias Ringwald #define BTSTACK_LINKED_LIST_H
444fd23d47SMatthias Ringwald 
454fd23d47SMatthias Ringwald #if defined __cplusplus
464fd23d47SMatthias Ringwald extern "C" {
474fd23d47SMatthias Ringwald #endif
484fd23d47SMatthias Ringwald 
49f54850a9SMatthias Ringwald /* API_START */
50f54850a9SMatthias Ringwald 
51665d90f2SMatthias Ringwald typedef struct btstack_linked_item {
52665d90f2SMatthias Ringwald     struct btstack_linked_item *next; // <-- next element in list, or NULL
53665d90f2SMatthias Ringwald } btstack_linked_item_t;
544fd23d47SMatthias Ringwald 
558f2a52f4SMatthias Ringwald typedef btstack_linked_item_t * btstack_linked_list_t;
564fd23d47SMatthias Ringwald 
574fd23d47SMatthias Ringwald typedef struct {
584fd23d47SMatthias Ringwald 	int advance_on_next;
59665d90f2SMatthias Ringwald     btstack_linked_item_t * prev;	// points to the item before the current one
60665d90f2SMatthias Ringwald     btstack_linked_item_t * curr;	// points to the current item (to detect item removal)
61665d90f2SMatthias Ringwald } btstack_linked_list_iterator_t;
624fd23d47SMatthias Ringwald 
634fd23d47SMatthias Ringwald 
64665d90f2SMatthias Ringwald // test if list is empty
658f2a52f4SMatthias Ringwald int                     btstack_linked_list_empty(btstack_linked_list_t * list);
66665d90f2SMatthias Ringwald // add item to list as first element
678f2a52f4SMatthias Ringwald void                    btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item);
68665d90f2SMatthias Ringwald // add item to list as last element
698f2a52f4SMatthias Ringwald void                    btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item);
7067711d5eSMatthias Ringwald // pop (get + remove) first element
7167711d5eSMatthias Ringwald btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list);
72665d90f2SMatthias Ringwald // remove item from list
738f2a52f4SMatthias Ringwald int                     btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item);
7467711d5eSMatthias Ringwald // get first element
7567711d5eSMatthias Ringwald btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list);
76665d90f2SMatthias Ringwald // find the last item in the list
778f2a52f4SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list);
784fd23d47SMatthias Ringwald 
794fd23d47SMatthias Ringwald /**
804fd23d47SMatthias Ringwald  * @brief Counts number of items in list
814fd23d47SMatthias Ringwald  * @returns number of items in list
824fd23d47SMatthias Ringwald  */
838f2a52f4SMatthias Ringwald int btstack_linked_list_count(btstack_linked_list_t * list);
844fd23d47SMatthias Ringwald 
854fd23d47SMatthias Ringwald //
86665d90f2SMatthias Ringwald // iterator for linked lists. allows to remove current element.
87665d90f2SMatthias Ringwald // robust against removal of current element by btstack_linked_list_remove.
884fd23d47SMatthias Ringwald //
898f2a52f4SMatthias Ringwald void            btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * list);
90665d90f2SMatthias Ringwald int             btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it);
91665d90f2SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it);
92665d90f2SMatthias Ringwald void            btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it);
934fd23d47SMatthias Ringwald 
94f54850a9SMatthias Ringwald /* API_END */
95f54850a9SMatthias Ringwald 
964fd23d47SMatthias Ringwald void test_linked_list(void);
974fd23d47SMatthias Ringwald 
98f54850a9SMatthias Ringwald 
994fd23d47SMatthias Ringwald #if defined __cplusplus
1004fd23d47SMatthias Ringwald }
1014fd23d47SMatthias Ringwald #endif
1024fd23d47SMatthias Ringwald 
103*80e33422SMatthias Ringwald #endif // BTSTACK_LINKED_LIST_H
104