Contiki-NG
list.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
33 */
34
35/**
36 * \file
37 * Linked list library implementation.
38 *
39 * \author Adam Dunkels <adam@sics.se>
40 *
41 */
42
43/**
44 * \addtogroup list
45 * @{
46 */
47#include "contiki.h"
48#include "lib/list.h"
49
50#include <string.h>
51/*---------------------------------------------------------------------------*/
52struct list {
53 struct list *next;
54};
55/*---------------------------------------------------------------------------*/
56void
58{
59 *list = NULL;
60}
61/*---------------------------------------------------------------------------*/
62void *
64{
65 return *list;
66}
67/*---------------------------------------------------------------------------*/
68void
70{
71 *dest = *src;
72}
73/*---------------------------------------------------------------------------*/
74void *
76{
77 struct list *l;
78
79 if(*list == NULL) {
80 return NULL;
81 }
82
83 for(l = *list; l->next != NULL; l = l->next);
84
85 return l;
86}
87/*---------------------------------------------------------------------------*/
88void
89list_add(list_t list, void *item)
90{
91 struct list *l;
92
93 /* Make sure not to add the same element twice */
94 list_remove(list, item);
95
96 ((struct list *)item)->next = NULL;
97
98 l = list_tail(list);
99
100 if(l == NULL) {
101 *list = item;
102 } else {
103 l->next = item;
104 }
105}
106/*---------------------------------------------------------------------------*/
107void
108list_push(list_t list, void *item)
109{
110 /* Make sure not to add the same element twice */
111 list_remove(list, item);
112
113 ((struct list *)item)->next = *list;
114 *list = item;
115}
116/*---------------------------------------------------------------------------*/
117void *
119{
120 struct list *l, *r;
121
122 if(*list == NULL) {
123 return NULL;
124 }
125 if(((struct list *)*list)->next == NULL) {
126 l = *list;
127 *list = NULL;
128 return l;
129 }
130
131 for(l = *list; l->next->next != NULL; l = l->next);
132
133 r = l->next;
134 l->next = NULL;
135
136 return r;
137}
138/*---------------------------------------------------------------------------*/
139void *
141{
142 struct list *l;
143 l = *list;
144 if(*list != NULL) {
145 *list = ((struct list *)*list)->next;
146 }
147
148 return l;
149}
150/*---------------------------------------------------------------------------*/
151void
152list_remove(list_t list, const void *item)
153{
154 struct list *l, *r;
155
156 if(*list == NULL) {
157 return;
158 }
159
160 r = NULL;
161 for(l = *list; l != NULL; l = l->next) {
162 if(l == item) {
163 if(r == NULL) {
164 /* First on list */
165 *list = l->next;
166 } else {
167 /* Not first on list */
168 r->next = l->next;
169 }
170 l->next = NULL;
171 return;
172 }
173 r = l;
174 }
175}
176/*---------------------------------------------------------------------------*/
177int
179{
180 struct list *l;
181 int n = 0;
182
183 for(l = *list; l != NULL; l = l->next) {
184 ++n;
185 }
186
187 return n;
188}
189/*---------------------------------------------------------------------------*/
190void
191list_insert(list_t list, void *previtem, void *newitem)
192{
193 if(previtem == NULL) {
194 list_push(list, newitem);
195 } else {
196 list_remove(list, newitem);
197 ((struct list *)newitem)->next = ((struct list *)previtem)->next;
198 ((struct list *)previtem)->next = newitem;
199 }
200}
201/*---------------------------------------------------------------------------*/
202void *
203list_item_next(const void *item)
204{
205 return item == NULL ? NULL : ((struct list *)item)->next;
206}
207/*---------------------------------------------------------------------------*/
208bool
209list_contains(const_list_t list, const void *item)
210{
211 struct list *l;
212 for(l = *list; l != NULL; l = l->next) {
213 if(item == l) {
214 return true;
215 }
216 }
217 return false;
218}
219/*---------------------------------------------------------------------------*/
220/** @} */
void list_init(list_t list)
Initialize a list.
Definition: list.c:57
void * list_chop(list_t list)
Remove the last object on the list.
Definition: list.c:118
int list_length(const_list_t list)
Get the length of a list.
Definition: list.c:178
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:89
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:152
void * list_pop(list_t list)
Remove the first object on a list.
Definition: list.c:140
void * list_item_next(const void *item)
Get the next item following this item.
Definition: list.c:203
void ** list_t
The linked list type.
Definition: list.h:135
void list_push(list_t list, void *item)
Add an item to the start of the list.
Definition: list.c:108
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition: list.c:63
bool list_contains(const_list_t list, const void *item)
Check if the list contains an item.
Definition: list.c:209
void *const * const_list_t
The non-modifiable linked list type.
Definition: list.h:140
void list_copy(list_t dest, const_list_t src)
Duplicate a list.
Definition: list.c:69
void * list_tail(const_list_t list)
Get the tail of a list.
Definition: list.c:75
void list_insert(list_t list, void *previtem, void *newitem)
Insert an item after a specified item on the list.
Definition: list.c:191
Linked list manipulation routines.