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 BLUEKITCHEN 24 * GMBH 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 * @title Linked List 40 * 41 */ 42 43 #ifndef BTSTACK_LINKED_LIST_H 44 #define BTSTACK_LINKED_LIST_H 45 46 #include "btstack_bool.h" 47 48 #if defined __cplusplus 49 extern "C" { 50 #endif 51 52 /* API_START */ 53 54 typedef struct btstack_linked_item { 55 struct btstack_linked_item *next; // <-- next element in list, or NULL 56 } btstack_linked_item_t; 57 58 typedef btstack_linked_item_t * btstack_linked_list_t; 59 60 typedef struct { 61 int advance_on_next; 62 btstack_linked_item_t * prev; // points to the item before the current one 63 btstack_linked_item_t * curr; // points to the current item (to detect item removal) 64 } btstack_linked_list_iterator_t; 65 66 67 /** 68 * @brief Test if list is empty. 69 * @param list 70 * @return true if list is empty 71 */ 72 bool btstack_linked_list_empty(btstack_linked_list_t * list); 73 74 /** 75 * @brief Add item to list as first element. 76 * @param list 77 * @param item 78 * @return true if item was added, false if item already in list 79 */ 80 bool btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item); 81 82 /** 83 * @brief Add item to list as last element. 84 * @param list 85 * @param item 86 * @return true if item was added, false if item already in list 87 */ 88 bool btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item); 89 90 /** 91 * @brief Pop (get + remove) first element. 92 * @param list 93 * @return first element or NULL if list is empty 94 */ 95 btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list); 96 97 /** 98 * @brief Remove item from list 99 * @param list 100 * @param item 101 * @return true if item was removed, false if it is no't in list 102 */ 103 bool btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item); 104 105 /** 106 * @brief Get first element. 107 * @param list 108 * @return first element or NULL if list is empty 109 */ 110 btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list); 111 112 /** 113 * @brief Get last element. 114 * @param list 115 * @return first element or NULL if list is empty 116 */ 117 btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list); 118 119 /** 120 * @brief Counts number of items in list 121 * @return number of items in list 122 */ 123 int btstack_linked_list_count(btstack_linked_list_t * list); 124 125 126 127 /** 128 * @brief Initialize Linked List Iterator 129 * @note robust against removal of current element by btstack_linked_list_remove 130 * @param it iterator context 131 * @param list 132 */ 133 void btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * list); 134 135 /** 136 * @brief Has next element 137 * @param it iterator context 138 * @return true if next element is available 139 */ 140 bool btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it); 141 142 /** 143 * @brief Get next list eleemnt 144 * @param it iterator context 145 * @return list element 146 */ 147 btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it); 148 149 /** 150 * @brief Remove current list element from list 151 * @param it iterator context 152 */ 153 void btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it); 154 155 /* API_END */ 156 157 void test_linked_list(void); 158 159 160 #if defined __cplusplus 161 } 162 #endif 163 164 #endif // BTSTACK_LINKED_LIST_H 165