xref: /btstack/src/btstack_linked_list.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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_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_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_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_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 btstack_data_source_t.next is first element in data_source
102  */
103 int  btstack_linked_list_remove(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_linked_list_t * list){
119     btstack_linked_item_t *it;
120     int counter = 0;
121     for (it = (btstack_linked_item_t *) list; it->next ; it = it->next) {
122         counter++;
123     }
124     return counter;
125 }
126 
127 //
128 // Linked List Iterator implementation
129 //
130 
131 void btstack_linked_list_iterator_init(btstack_linked_list_iterator_t * it, btstack_linked_list_t * head){
132     it->advance_on_next = 0;
133     it->prev = (btstack_linked_item_t*) head;
134     it->curr = * head;
135 }
136 
137 int btstack_linked_list_iterator_has_next(btstack_linked_list_iterator_t * it){
138     // 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);
139     if (!it->advance_on_next){
140         return it->curr != NULL;
141     }
142     if (it->prev->next != it->curr){
143         // current item has been removed
144         return it->prev->next != NULL;
145     }
146     // current items has not been removed
147     return it->curr->next != NULL;
148 }
149 
150 btstack_linked_item_t * btstack_linked_list_iterator_next(btstack_linked_list_iterator_t * it){
151     if (it->advance_on_next){
152         if (it->prev->next == it->curr){
153             it->prev = it->curr;
154             it->curr = it->curr->next;
155         } else {
156             // curr was removed from the list, set it again but don't advance prev
157             it->curr = it->prev->next;
158         }
159     } else {
160         it->advance_on_next = 1;
161     }
162     return it->curr;
163 }
164 
165 void btstack_linked_list_iterator_remove(btstack_linked_list_iterator_t * it){
166     it->curr = it->curr->next;
167     it->prev->next = it->curr;
168     it->advance_on_next = 0;
169 }
170