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 #define BTSTACK_FILE__ "btstack_linked_list.c" 39 40 /* 41 * linked_list.c 42 * 43 * Created by Matthias Ringwald on 7/13/09. 44 */ 45 46 #include "btstack_linked_list.h" 47 #include "btstack_debug.h" 48 49 #include <stddef.h> 50 51 /** 52 * tests if list is empty 53 */ 54 bool btstack_linked_list_empty(btstack_linked_list_t * list){ 55 return *list == (void *) 0; 56 } 57 58 /** 59 * btstack_linked_list_get_last_item 60 */ 61 btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list){ // <-- find the last item in the list 62 btstack_linked_item_t *lastItem = NULL; 63 btstack_linked_item_t *it; 64 for (it = *list; it != NULL; it = it->next){ 65 if (it != NULL) { 66 lastItem = it; 67 } 68 } 69 return lastItem; 70 } 71 72 73 /** 74 * btstack_linked_list_add 75 */ 76 bool btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item){ // <-- add item to list 77 // check if already in list 78 btstack_linked_item_t *it; 79 for (it = *list; it != NULL; it = it->next){ 80 if (it == item) { 81 return false; 82 } 83 } 84 // add first 85 item->next = *list; 86 *list = item; 87 return true; 88 } 89 90 bool btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item){ // <-- add item to list as last element 91 // check if already in list 92 btstack_linked_item_t *it; 93 for (it = (btstack_linked_item_t *) list; it->next != NULL ; it = it->next){ 94 if (it->next == item) { 95 return false; 96 } 97 } 98 item->next = (btstack_linked_item_t*) 0; 99 it->next = item; 100 return true; 101 } 102 103 bool btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item){ // <-- remove item from list 104 if (!item) return false; 105 btstack_linked_item_t *it; 106 for (it = (btstack_linked_item_t *) list; it != NULL; it = it->next){ 107 if (it->next == item){ 108 it->next = item->next; 109 return true; 110 } 111 } 112 return false; 113 } 114 115 /** 116 * @return number of items in list 117 */ 118 int btstack_linked_list_count(btstack_linked_list_t * list){ 119 btstack_linked_item_t *it; 120 int counter = 0; 121 for (it = (btstack_linked_item_t *) list; it->next != NULL; it = it->next) { 122 counter++; 123 } 124 return counter; 125 } 126 127 // get first element 128 btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list){ 129 return * list; 130 } 131 132 // pop (get + remove) first element 133 btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list){ 134 btstack_linked_item_t * item = *list; 135 if (!item) return NULL; 136 *list = item->next; 137 return item; 138 } 139 140 141 // 142 // Linked List Iterator implementation 143 // 144 145 void btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * list){ 146 it->advance_on_next = 0; 147 it->prev = (btstack_linked_item_t*) list; 148 it->curr = * list; 149 } 150 151 bool btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it){ 152 // log_info("btstack_linked_list_iterator_has_next: advance on next %u, it->prev %p, it->curr %p", it->advance_on_next, it->prev, it->curr); 153 if (!it->advance_on_next){ 154 return it->curr != NULL; 155 } 156 if (it->prev->next != it->curr){ 157 // current item has been removed 158 return it->prev->next != NULL; 159 } 160 // current items has not been removed 161 return it->curr->next != NULL; 162 } 163 164 btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it){ 165 if (it->advance_on_next){ 166 if (it->prev->next == it->curr){ 167 it->prev = it->curr; 168 it->curr = it->curr->next; 169 } else { 170 // curr was removed from the list, set it again but don't advance prev 171 it->curr = it->prev->next; 172 } 173 } else { 174 it->advance_on_next = 1; 175 } 176 return it->curr; 177 } 178 179 void btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it){ 180 btstack_assert(it->prev->next == it->curr); 181 it->curr = it->curr->next; 182 it->prev->next = it->curr; 183 it->advance_on_next = 0; 184 } 185