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