Contiki-NG
dbl-list.h
1 /*
2  * Copyright (c) 2017, George Oikonomou - http://www.spd.gr
3  * Copyright (c) 2017, James Pope
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*---------------------------------------------------------------------------*/
33 /** \addtogroup data
34  * @{
35  *
36  * \defgroup doubly-linked-list Doubly-linked list
37  *
38  * This library provides functions for the creation and manipulation of
39  * doubly-linked lists.
40  *
41  * A doubly-linked list is declared using the DBL_LIST macro.
42  * Elements must be allocated by the calling code and must be of a C struct
43  * datatype. In this struct, the first field must be a pointer called \e next.
44  * The second field must be a pointer called \e previous.
45  * These fields will be used by the library to maintain the list. Application
46  * code must not modify these fields directly.
47  *
48  * Functions that modify the list (add / remove) will, in the general case,
49  * update the list's head and item order. If you call one of these functions
50  * as part of a list traversal, it is advised to stop / restart traversing
51  * after the respective function returns.
52  * @{
53  */
54 /*---------------------------------------------------------------------------*/
55 #ifndef DBL_LIST_H_
56 #define DBL_LIST_H_
57 /*---------------------------------------------------------------------------*/
58 #include "contiki.h"
59 
60 #include <stdint.h>
61 #include <stdbool.h>
62 #include <string.h>
63 /*---------------------------------------------------------------------------*/
64 /**
65  * \brief Define a doubly-linked list.
66  *
67  * This macro defines a doubly-linked list.
68  *
69  * The datatype for elements must be a C struct.
70  * The struct's first member must be a pointer called \e next.
71  * The second field must be a pointer called \e previous.
72  * These fields will be used by the library to maintain the list. Application
73  * code must not modify these fields directly.
74  *
75  * \param name The name of the doubly-linked list.
76  */
77 #define DBL_LIST(name) \
78  static void *name##_dbl_list = NULL; \
79  static dbl_list_t name = (dbl_list_t)&name##_dbl_list
80 /*---------------------------------------------------------------------------*/
81 /**
82  * \brief The doubly-linked list datatype
83  */
84 typedef void **dbl_list_t;
85 /*---------------------------------------------------------------------------*/
86 /**
87  * \brief Initialise a doubly-linked list.
88  * \param dll The doubly-linked list.
89  */
90 void dbl_list_init(dbl_list_t dll);
91 
92 /**
93  * \brief Return the tail of a doubly-linked list.
94  * \param dll The doubly-linked list.
95  * \return A pointer to the list's head, or NULL if the list is empty
96  */
97 void *dbl_list_head(dbl_list_t dll);
98 
99 /**
100  * \brief Return the tail of a doubly-linked list.
101  * \param dll The doubly-linked list.
102  * \return A pointer to the list's tail, or NULL if the list is empty
103  */
104 void *dbl_list_tail(dbl_list_t dll);
105 
106 /**
107  * \brief Add an element to the head of a doubly-linked list.
108  * \param dll The doubly-linked list.
109  * \param element A pointer to the element to be added.
110  *
111  * Calling this function will update the list's head and item order. If you
112  * call this function as part of a list traversal, it is advised to stop
113  * traversing after this function returns.
114  */
115 void dbl_list_add_head(dbl_list_t dll, void *element);
116 
117 /**
118  * \brief Add an element to the tail of a doubly-linked list.
119  * \param dll The doubly-linked list.
120  * \param element A pointer to the element to be added.
121  *
122  * Calling this function will update the list's head and item order. If you
123  * call this function as part of a list traversal, it is advised to stop
124  * traversing after this function returns.
125  */
126 void dbl_list_add_tail(dbl_list_t dll, void *element);
127 
128 /**
129  * \brief Add an element to a doubly linked list after an existing element.
130  * \param dll The doubly-linked list.
131  * \param existing A pointer to the existing element.
132  * \param element A pointer to the element to be added.
133  *
134  * This function will add \e element after \e existing
135  *
136  * The function will not verify that \e existing is already part of the list.
137  *
138  * Calling this function will update the list's head and item order. If you
139  * call this function as part of a list traversal, it is advised to stop
140  * traversing after this function returns.
141  */
142 void dbl_list_add_after(dbl_list_t dll, void *existing, void *element);
143 
144 /**
145  * \brief Add an element to a doubly linked list before an existing element.
146  * \param dll The doubly-linked list.
147  * \param existing A pointer to the existing element.
148  * \param element A pointer to the element to be added.
149  *
150  * This function will add \e element before \e existing
151  *
152  * The function will not verify that \e existing is already part of the list.
153  *
154  * Calling this function will update the list's head and item order. If you
155  * call this function as part of a list traversal, it is advised to stop
156  * traversing after this function returns.
157  */
158 void dbl_list_add_before(dbl_list_t dll, void *existing, void *element);
159 
160 /**
161  * \brief Remove an element from a doubly-linked list.
162  * \param dll The doubly-linked list.
163  * \param element A pointer to the element to be removed.
164  *
165  * Calling this function will update the list's head and item order. If you
166  * call this function as part of a list traversal, it is advised to stop
167  * traversing after this function returns.
168  */
169 void dbl_list_remove(dbl_list_t dll, void *element);
170 
171 /**
172  * \brief Get the length of a doubly-linked list.
173  * \param dll The doubly-linked list.
174  * \return The number of elements in the list
175  */
176 unsigned long dbl_list_length(dbl_list_t dll);
177 
178 /**
179  * \brief Determine whether a doubly-linked list is empty.
180  * \param dll The doubly-linked list.
181  * \retval true The list is empty
182  * \retval false The list is not empty
183  */
184 bool dbl_list_is_empty(dbl_list_t dll);
185 /*---------------------------------------------------------------------------*/
186 #endif /* DBL_LIST_H_ */
187 /*---------------------------------------------------------------------------*/
188 /**
189  * @}
190  * @}
191  */
unsigned long dbl_list_length(dbl_list_t dll)
Get the length of a doubly-linked list.
Definition: dbl-list.c:204
void dbl_list_init(dbl_list_t dll)
Initialise a doubly-linked list.
Definition: dbl-list.c:54
void dbl_list_add_tail(dbl_list_t dll, void *element)
Add an element to the tail of a doubly-linked list.
Definition: dbl-list.c:136
void dbl_list_add_before(dbl_list_t dll, void *existing, void *element)
Add an element to a doubly linked list before an existing element.
Definition: dbl-list.c:180
void dbl_list_remove(dbl_list_t dll, void *element)
Remove an element from a doubly-linked list.
Definition: dbl-list.c:80
void dbl_list_add_head(dbl_list_t dll, void *element)
Add an element to the head of a doubly-linked list.
Definition: dbl-list.c:111
void * dbl_list_tail(dbl_list_t dll)
Return the tail of a doubly-linked list.
Definition: dbl-list.c:66
void ** dbl_list_t
The doubly-linked list datatype.
Definition: dbl-list.h:84
void dbl_list_add_after(dbl_list_t dll, void *existing, void *element)
Add an element to a doubly linked list after an existing element.
Definition: dbl-list.c:161
bool dbl_list_is_empty(dbl_list_t dll)
Determine whether a doubly-linked list is empty.
Definition: dbl-list.c:221
void * dbl_list_head(dbl_list_t dll)
Return the tail of a doubly-linked list.
Definition: dbl-list.c:60