3D-ICE 3.0.0
list_template.c
1/******************************************************************************
2 * This file is part of 3D-ICE, version 3.1.0 . *
3 * *
4 * 3D-ICE is free software: you can redistribute it and/or modify it under *
5 * the terms of the GNU General Public License as published by the Free *
6 * Software Foundation, either version 3 of the License, or any later *
7 * version. *
8 * *
9 * 3D-ICE is distributed in the hope that it will be useful, but WITHOUT *
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
12 * more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along *
15 * with 3D-ICE. If not, see <http://www.gnu.org/licenses/>. *
16 * *
17 * Copyright (C) 2021 *
18 * Embedded Systems Laboratory - Ecole Polytechnique Federale de Lausanne *
19 * All Rights Reserved. *
20 * *
21 * Authors: Arvind Sridhar Alessandro Vincenzi *
22 * Giseong Bak Martino Ruggiero *
23 * Thomas Brunschwiler Eder Zulian *
24 * Federico Terraneo Darong Huang *
25 * Luis Costero Marina Zapater *
26 * David Atienza *
27 * *
28 * For any comment, suggestion or request about 3D-ICE, please register and *
29 * write to the mailing list (see http://listes.epfl.ch/doc.cgi?liste=3d-ice) *
30 * Any usage of 3D-ICE for research, commercial or other purposes must be *
31 * properly acknowledged in the resulting products or publications. *
32 * *
33 * EPFL-STI-IEL-ESL Mail : 3d-ice@listes.epfl.ch *
34 * Batiment ELG, ELG 130 (SUBSCRIPTION IS NECESSARY) *
35 * Station 11 *
36 * 1015 Lausanne, Switzerland Url : http://esl.epfl.ch/3d-ice *
37 ******************************************************************************/
38
39#ifndef node_data_init
40#error missing macro node_data_init
41#endif
42
43#ifndef node_data_destroy
44#error missing macro node_data_destroy
45#endif
46
47#ifndef node_data_copy
48#error missing macro node_data_copy
49#endif
50
51#ifndef node_data_equal
52#error missing macro node_data_equal
53#endif
54
55#ifndef node_data_print
56#error missing macro node_data_print
57#endif
58
59#include <stdlib.h> // For the memory functions malloc/free
60
61/******************************************************************************/
62
63FIMP (FPROTO1 (
64
65void, TTT_list_init, TTTList_t *list),
66
67 list->Size = (Quantity_t) 0u ;
68 list->First = NULL ;
69 list->Last = NULL ;
70)
71
72/******************************************************************************/
73
74FIMP (FPROTO2 (
75
76void, TTT_list_copy, TTTList_t *dst, TTTList_t *src),
77
78 TTT_list_destroy (dst) ;
79
80 TTTListNode_t *node ;
81
82 for (node = TTT_list_begin (src) ;
83 node != NULL ;
84 node = TTT_list_next (node))
85
86 TTT_list_insert_end (dst, TTT_list_data(node)) ;
87)
88
89/******************************************************************************/
90
91FIMP (FPROTO1 (
92
93void, TTT_list_destroy, TTTList_t *list),
94
95 TTTListNode_t* next = NULL ;
96
97 while (list->First != NULL)
98 {
99 next = TTT_list_next (list->First) ;
100
101 node_data_destroy (TTT_list_data (list->First)) ;
102
103 free (list->First) ;
104
105 list->First = next ;
106 }
107
108 TTT_list_init (list) ;
109)
110
111/******************************************************************************/
112
113FIMP( FPROTO1 (
114
115TTTListNode_t *, TTT_list_begin, TTTList_t *list),
116
117 return list->First ;
118)
119
120/******************************************************************************/
121
122FIMP( FPROTO1 (
123
124TTTListNode_t *, TTT_list_end, TTTList_t *list),
125
126 return list->Last ;
127)
128
129/******************************************************************************/
130
131FIMP( FPROTO1 (
132
133TTTListNode_t *, TTT_list_next, TTTListNode_t *node),
134
135 return node->Next ;
136)
137
138/******************************************************************************/
139
140FIMP( FPROTO1 (
141
142TTTListNode_t *, TTT_list_prev, TTTListNode_t *node),
143
144 return node->Prev ;
145)
146
147/******************************************************************************/
148
149FIMP( FPROTO1 (
150
151TTT_t *, TTT_list_data, TTTListNode_t *node),
152
153 return &node->Data ;
154)
155
156/******************************************************************************/
157
158FIMP( FPROTO2 (
159
160void, TTT_list_insert_begin, TTTList_t *list, TTT_t *data),
161
162 TTTListNode_t *newnode = (TTTListNode_t *) malloc (sizeof(TTTListNode_t)) ;
163
164 if (newnode == NULL)
165
166 return ;
167
168 node_data_init (TTT_list_data(newnode)) ;
169
170 node_data_copy (TTT_list_data(newnode), data) ;
171
172 newnode->Next = NULL ;
173 newnode->Prev = NULL ;
174
175 if (list->First == NULL && list->Last == NULL)
176 {
177 list->First = newnode ;
178 list->Last = newnode ;
179
180 list->Size++ ;
181
182 return ;
183 }
184
185 if (list->First == NULL || list->Last == NULL)
186 {
187 fprintf (stderr, "ERROR: bad list status\n") ;
188
189 return ;
190 }
191
192 newnode->Next = list->First ;
193
194 list->First->Prev = newnode ;
195
196 list->First = newnode ;
197
198 list->Size++ ;
199)
200
201/******************************************************************************/
202
203FIMP( FPROTO2 (
204
205void, TTT_list_insert_end, TTTList_t *list, TTT_t *data),
206
207 TTTListNode_t *newnode = (TTTListNode_t *) malloc (sizeof(TTTListNode_t)) ;
208
209 if (newnode == NULL)
210
211 return ;
212
213 node_data_init (TTT_list_data(newnode)) ;
214
215 node_data_copy (TTT_list_data(newnode), data) ;
216
217 newnode->Next = NULL ;
218 newnode->Prev = NULL ;
219
220 if (list->First == NULL && list->Last == NULL)
221 {
222 list->First = newnode ;
223 list->Last = newnode ;
224
225 list->Size++ ;
226
227 return ;
228 }
229
230 if (list->First == NULL && list->Last == NULL)
231 {
232 fprintf (stderr, "ERROR: bad list status\n") ;
233
234 return ;
235 }
236
237 newnode->Prev = list->Last ;
238
239 list->Last->Next = newnode ;
240
241 list->Last = newnode ;
242
243 list->Size++ ;
244)
245
246/******************************************************************************/
247
248FIMP( FPROTO2 (
249
250TTT_t *, TTT_list_find, TTTList_t *list, TTT_t *data),
251
252 TTTListNode_t *node ;
253
254 for (node = TTT_list_begin(list) ;
255 node != NULL ;
256 node = TTT_list_next (node))
257
258 if (node_data_equal (TTT_list_data (node), data) == true)
259
260 break ;
261
262 if (node == NULL)
263
264 return NULL ;
265
266 return TTT_list_data (node) ;
267)
268
269/******************************************************************************/
270
271FIMP( FPROTO3 (
272
273void, TTT_list_print, TTTList_t *list, FILE *stream, String_t prefix),
274
275 TTTListNode_t *node ;
276
277 for (node = TTT_list_begin(list) ;
278 node != NULL ;
279 node = TTT_list_next (node))
280 {
281 node_data_print (TTT_list_data(node), stream, prefix) ;
282
283 fprintf (stream, "%s\n", prefix) ;
284 }
285)
286
287/******************************************************************************/
char * String_t
Definition: string_t.h:55
uint32_t Quantity_t
Definition: types.h:59