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 4280e33422SMatthias Ringwald #ifndef BTSTACK_LINKED_LIST_H 4380e33422SMatthias Ringwald #define BTSTACK_LINKED_LIST_H 444fd23d47SMatthias Ringwald 458b2bf72fSMatthias Ringwald #include "btstack_bool.h" 468b2bf72fSMatthias Ringwald 474fd23d47SMatthias Ringwald #if defined __cplusplus 484fd23d47SMatthias Ringwald extern "C" { 494fd23d47SMatthias Ringwald #endif 504fd23d47SMatthias Ringwald 51f54850a9SMatthias Ringwald /* API_START */ 52f54850a9SMatthias Ringwald 53665d90f2SMatthias Ringwald typedef struct btstack_linked_item { 54665d90f2SMatthias Ringwald struct btstack_linked_item *next; // <-- next element in list, or NULL 55665d90f2SMatthias Ringwald } btstack_linked_item_t; 564fd23d47SMatthias Ringwald 578f2a52f4SMatthias Ringwald typedef btstack_linked_item_t * btstack_linked_list_t; 584fd23d47SMatthias Ringwald 594fd23d47SMatthias Ringwald typedef struct { 604fd23d47SMatthias Ringwald int advance_on_next; 61665d90f2SMatthias Ringwald btstack_linked_item_t * prev; // points to the item before the current one 62665d90f2SMatthias Ringwald btstack_linked_item_t * curr; // points to the current item (to detect item removal) 63665d90f2SMatthias Ringwald } btstack_linked_list_iterator_t; 644fd23d47SMatthias Ringwald 654fd23d47SMatthias Ringwald 66c27688cfSMatthias Ringwald /** 67c27688cfSMatthias Ringwald * @brief Test if list is empty. 68c27688cfSMatthias Ringwald * @param list 69c27688cfSMatthias Ringwald * @returns true if list is empty 70c27688cfSMatthias Ringwald */ 718b2bf72fSMatthias Ringwald bool btstack_linked_list_empty(btstack_linked_list_t * list); 72c27688cfSMatthias Ringwald 73c27688cfSMatthias Ringwald /** 74c27688cfSMatthias Ringwald * @brief Add item to list as first element. 75c27688cfSMatthias Ringwald * @param list 76c27688cfSMatthias Ringwald * @param item 7778d0c67aSMatthias Ringwald * @returns true if item was added, false if item already in list 78c27688cfSMatthias Ringwald */ 7978d0c67aSMatthias Ringwald bool btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item); 80c27688cfSMatthias Ringwald 81c27688cfSMatthias Ringwald /** 82c27688cfSMatthias Ringwald * @brief Add item to list as last element. 83c27688cfSMatthias Ringwald * @param list 84c27688cfSMatthias Ringwald * @param item 8578d0c67aSMatthias Ringwald * @returns true if item was added, false if item already in list 86c27688cfSMatthias Ringwald */ 8778d0c67aSMatthias Ringwald bool btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item); 88c27688cfSMatthias Ringwald 89c27688cfSMatthias Ringwald /** 90c27688cfSMatthias Ringwald * @brief Pop (get + remove) first element. 91c27688cfSMatthias Ringwald * @param list 92c27688cfSMatthias Ringwald * @returns first element or NULL if list is empty 93c27688cfSMatthias Ringwald */ 9467711d5eSMatthias Ringwald btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list); 95c27688cfSMatthias Ringwald 96c27688cfSMatthias Ringwald /** 97c27688cfSMatthias Ringwald * @brief Remove item from list 98c27688cfSMatthias Ringwald * @param list 99c27688cfSMatthias Ringwald * @param item 100*d58a1b5fSMatthias Ringwald * @returns true if item was removed, false if it is no't in list 101c27688cfSMatthias Ringwald */ 102*d58a1b5fSMatthias Ringwald bool btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item); 103c27688cfSMatthias Ringwald 104c27688cfSMatthias Ringwald /** 105c27688cfSMatthias Ringwald * @brief Get first element. 106c27688cfSMatthias Ringwald * @param list 107c27688cfSMatthias Ringwald * @returns first element or NULL if list is empty 108c27688cfSMatthias Ringwald */ 10967711d5eSMatthias Ringwald btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list); 110c27688cfSMatthias Ringwald 111c27688cfSMatthias Ringwald /** 112c27688cfSMatthias Ringwald * @brief Get last element. 113c27688cfSMatthias Ringwald * @param list 114c27688cfSMatthias Ringwald * @returns first element or NULL if list is empty 115c27688cfSMatthias Ringwald */ 1168f2a52f4SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list); 1174fd23d47SMatthias Ringwald 1184fd23d47SMatthias Ringwald /** 1194fd23d47SMatthias Ringwald * @brief Counts number of items in list 1204fd23d47SMatthias Ringwald * @returns number of items in list 1214fd23d47SMatthias Ringwald */ 1228f2a52f4SMatthias Ringwald int btstack_linked_list_count(btstack_linked_list_t * list); 1234fd23d47SMatthias Ringwald 124c27688cfSMatthias Ringwald 125c27688cfSMatthias Ringwald 126c27688cfSMatthias Ringwald /** 127c27688cfSMatthias Ringwald * @brief Initialize Linked List Iterator 128c27688cfSMatthias Ringwald * @note robust against removal of current element by btstack_linked_list_remove 129c27688cfSMatthias Ringwald * @param it iterator context 130c27688cfSMatthias Ringwald * @param list 131c27688cfSMatthias Ringwald */ 1328f2a52f4SMatthias Ringwald void btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * list); 133c27688cfSMatthias Ringwald 134c27688cfSMatthias Ringwald /** 135c27688cfSMatthias Ringwald * @brief Has next element 136c27688cfSMatthias Ringwald * @param it iterator context 137c27688cfSMatthias Ringwald * @returns true if next element is available 138c27688cfSMatthias Ringwald */ 1398b2bf72fSMatthias Ringwald bool btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it); 140c27688cfSMatthias Ringwald 141c27688cfSMatthias Ringwald /** 142c27688cfSMatthias Ringwald * @brief Get next list eleemnt 143c27688cfSMatthias Ringwald * @param it iterator context 144c27688cfSMatthias Ringwald * @returns list element 145c27688cfSMatthias Ringwald */ 146665d90f2SMatthias Ringwald btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it); 147c27688cfSMatthias Ringwald 148c27688cfSMatthias Ringwald /** 149c27688cfSMatthias Ringwald * @brief Remove current list element from list 150c27688cfSMatthias Ringwald * @param it iterator context 151c27688cfSMatthias Ringwald */ 152665d90f2SMatthias Ringwald void btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it); 1534fd23d47SMatthias Ringwald 154f54850a9SMatthias Ringwald /* API_END */ 155f54850a9SMatthias Ringwald 1564fd23d47SMatthias Ringwald void test_linked_list(void); 1574fd23d47SMatthias Ringwald 158f54850a9SMatthias Ringwald 1594fd23d47SMatthias Ringwald #if defined __cplusplus 1604fd23d47SMatthias Ringwald } 1614fd23d47SMatthias Ringwald #endif 1624fd23d47SMatthias Ringwald 16380e33422SMatthias Ringwald #endif // BTSTACK_LINKED_LIST_H 164