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