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