|
1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * Doubly linked list module. |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 /* |
|
22 * dlist.c |
|
23 * |
|
24 * |
|
25 * |
|
26 * Project: |
|
27 * Mobile Videophone Demonstrator |
|
28 * H.263 Video Decoder |
|
29 * |
|
30 * Contains: |
|
31 * This module contains generic list handling functions for doubly linked |
|
32 * lists, i.e. one can use the same set of functions regardless of the type |
|
33 * of list items. |
|
34 * The list item type has a constraint that it must begin with the skeleton |
|
35 * of a list item (see the definition of DLST_ITEM_SKELETON in dlist.h). |
|
36 * |
|
37 * The prefix ddlst is used in common names defined in this module. |
|
38 * |
|
39 * |
|
40 * |
|
41 */ |
|
42 |
|
43 |
|
44 #include "dlist.h" |
|
45 |
|
46 |
|
47 #ifndef NULL |
|
48 #define NULL 0 |
|
49 #endif |
|
50 |
|
51 |
|
52 /* |
|
53 * dlstClose |
|
54 * |
|
55 * |
|
56 * Parameters: |
|
57 * list a pointer to a list |
|
58 * |
|
59 * Function: |
|
60 * This function deinitializes the given list. Notice that no dynamic |
|
61 * allocation for the list is done. This function must be called when |
|
62 * the list is no longer used. |
|
63 * |
|
64 * Returns: |
|
65 * >= 0 if the function was successful |
|
66 * < 0 indicating an error |
|
67 * |
|
68 */ |
|
69 |
|
70 int dlstClose(dlst_t *list) |
|
71 { |
|
72 list->head = list->tail = list->curr = NULL; |
|
73 list->numItems = 0; |
|
74 |
|
75 return 0; |
|
76 } |
|
77 |
|
78 |
|
79 /* |
|
80 * dlstRemove |
|
81 * |
|
82 * |
|
83 * Parameters: |
|
84 * list a pointer to a list |
|
85 * item used to return a pointer to the removed |
|
86 * list item |
|
87 * |
|
88 * Function: |
|
89 * This function removes an item from the list. Notice that no copying of |
|
90 * contents of the given item is done, i.e. only the pointer to the item |
|
91 * is used. |
|
92 * |
|
93 * If there are no items left in the list, NULL will be returned in |
|
94 * the item parameter. |
|
95 * |
|
96 * Returns: |
|
97 * >= 0 if the function was successful |
|
98 * < 0 indicating an error |
|
99 * |
|
100 */ |
|
101 |
|
102 int dlstRemove(dlst_t *list, void **item) |
|
103 { |
|
104 if (list->curr) { |
|
105 dlstListItem_t |
|
106 *curr = list->curr, |
|
107 *prev = (dlstListItem_t *) (list->curr->prev), |
|
108 *next = (dlstListItem_t *) (list->curr->next); |
|
109 |
|
110 *item = (void *) curr; |
|
111 |
|
112 if (prev) |
|
113 prev->next = curr->next; |
|
114 else |
|
115 list->head = (dlstListItem_t *) curr->next; |
|
116 |
|
117 if (next) |
|
118 next->prev = curr->prev; |
|
119 |
|
120 curr->next = NULL; |
|
121 curr->prev = NULL; |
|
122 |
|
123 if (curr == list->tail) { |
|
124 list->tail = prev; |
|
125 list->curr = prev; |
|
126 } |
|
127 else |
|
128 list->curr = next; |
|
129 |
|
130 list->numItems--; |
|
131 } |
|
132 |
|
133 else |
|
134 *item = NULL; |
|
135 |
|
136 return 0; |
|
137 } |
|
138 |
|
139 |
|
140 /* |
|
141 * dlstOpen |
|
142 * |
|
143 * |
|
144 * Parameters: |
|
145 * list a pointer to a list |
|
146 * |
|
147 * Function: |
|
148 * This function initializes the given list. Notice that no dynamic |
|
149 * allocation for the list is done. This function must be called before |
|
150 * the list is used. |
|
151 * |
|
152 * Returns: |
|
153 * >= 0 if the function was successful |
|
154 * < 0 indicating an error |
|
155 * |
|
156 */ |
|
157 |
|
158 int dlstOpen(dlst_t *list) |
|
159 { |
|
160 list->head = list->tail = list->curr = NULL; |
|
161 list->numItems = 0; |
|
162 |
|
163 return 0; |
|
164 } |
|
165 |
|
166 |
|
167 /* |
|
168 * dlstHead |
|
169 * |
|
170 * |
|
171 * Parameters: |
|
172 * list a pointer to a dlst list |
|
173 * item used to return a pointer to the head item |
|
174 * of the list |
|
175 * |
|
176 * Function: |
|
177 * This function sets the current access point to the head of the list and |
|
178 * returns a pointer to the head item. Notice that no copying of |
|
179 * contents of the given item is done, i.e. only the pointer to the item |
|
180 * is used. |
|
181 * |
|
182 * If there are no items left in the list, NULL will be returned in |
|
183 * the item parameter. |
|
184 * |
|
185 * Returns: |
|
186 * >= 0 if the function was successful |
|
187 * < 0 indicating an error |
|
188 * |
|
189 */ |
|
190 |
|
191 int dlstHead(dlst_t *list, void **item) |
|
192 { |
|
193 if (list->head) |
|
194 *item = (void *) list->head; |
|
195 |
|
196 else |
|
197 *item = NULL; |
|
198 |
|
199 list->curr = list->head; |
|
200 |
|
201 return 0; |
|
202 } |
|
203 |
|
204 |
|
205 /* |
|
206 * dlstTail |
|
207 * |
|
208 * |
|
209 * Parameters: |
|
210 * list a pointer to a dlst list |
|
211 * |
|
212 * Function: |
|
213 * This function sets the current access point to the tail of the list |
|
214 * enabling the item addition to the end of the list. |
|
215 * |
|
216 * Returns: |
|
217 * >= 0 if the function was successful |
|
218 * < 0 indicating an error |
|
219 * |
|
220 */ |
|
221 |
|
222 int dlstTail(dlst_t *list, void **item) |
|
223 { |
|
224 |
|
225 if (list->tail) |
|
226 *item = (void *) list->tail; |
|
227 |
|
228 else |
|
229 *item = NULL; |
|
230 |
|
231 list->curr = list->tail; |
|
232 |
|
233 return 0; |
|
234 } |
|
235 |
|
236 |
|
237 /* |
|
238 * dlstNext |
|
239 * |
|
240 * |
|
241 * Parameters: |
|
242 * list a pointer to a dlst list |
|
243 * item used to return a pointer to the next item |
|
244 * of the list |
|
245 * |
|
246 * Function: |
|
247 * This function sets the current access point to the next item of the list |
|
248 * and returns a pointer to the item. Notice that no copying of |
|
249 * contents of the given item is done, i.e. only the pointer to the item |
|
250 * is used. |
|
251 * |
|
252 * If there are no items left in the list, NULL will be returned in |
|
253 * the item parameter and the current access point shall be left as it is. |
|
254 * |
|
255 * Returns: |
|
256 * >= 0 if the function was successful |
|
257 * < 0 indicating an error |
|
258 * |
|
259 */ |
|
260 |
|
261 int dlstNext(dlst_t *list, void **item) |
|
262 { |
|
263 if (list->curr && list->curr->next) { |
|
264 list->curr = (dlstListItem_t *) (list->curr->next); |
|
265 *item = (void *) list->curr; |
|
266 } |
|
267 |
|
268 else |
|
269 *item = NULL; |
|
270 |
|
271 return 0; |
|
272 } |
|
273 |
|
274 |
|
275 int dlstPrev(dlst_t *list, void **item) |
|
276 { |
|
277 if (list->curr && list->curr->prev) { |
|
278 list->curr = (dlstListItem_t *) (list->curr->prev); |
|
279 *item = (void *) list->curr; |
|
280 } |
|
281 |
|
282 else |
|
283 *item = NULL; |
|
284 |
|
285 return 0; |
|
286 } |
|
287 |
|
288 |
|
289 int dlstCurr(dlst_t *list, void **item) |
|
290 { |
|
291 *item = (void *) list->curr; |
|
292 |
|
293 return 0; |
|
294 } |
|
295 |
|
296 |
|
297 int dlstNextExists(dlst_t *list) |
|
298 { |
|
299 return (list->curr && list->curr->next) ? 1 : 0; |
|
300 } |
|
301 |
|
302 /* |
|
303 * dlstAdd |
|
304 * |
|
305 * |
|
306 * Parameters: |
|
307 * list a pointer to a dlst list |
|
308 * item an item to add to the list |
|
309 * |
|
310 * Function: |
|
311 * This function adds an item into the list. Notice that no copying of |
|
312 * contents of the given item is done, i.e. only the pointer to the item |
|
313 * is used. |
|
314 * |
|
315 * Returns: |
|
316 * >= 0 if the function was successful |
|
317 * < 0 indicating an error |
|
318 * |
|
319 */ |
|
320 |
|
321 int dlstAddBeforeCurr(dlst_t *list, void *item) |
|
322 { |
|
323 dlstListItem_t |
|
324 *curr, *prev, *listItem = (dlstListItem_t *) item; |
|
325 |
|
326 if (list->curr) { |
|
327 curr = (dlstListItem_t *) list->curr, |
|
328 prev = (dlstListItem_t *) list->curr->prev; |
|
329 |
|
330 if (prev) |
|
331 prev->next = item; |
|
332 else |
|
333 list->head = (dlstListItem_t *) item; |
|
334 listItem->prev = prev; |
|
335 |
|
336 curr->prev = item; |
|
337 listItem->next = curr; |
|
338 |
|
339 list->curr = (dlstListItem_t *) item; |
|
340 } |
|
341 |
|
342 else { |
|
343 list->head = list->tail = list->curr = (dlstListItem_t *) item; |
|
344 listItem->next = NULL; |
|
345 listItem->prev = NULL; |
|
346 } |
|
347 |
|
348 list->numItems++; |
|
349 |
|
350 return 0; |
|
351 } |
|
352 |
|
353 int dlstAddAfterCurr(dlst_t *list, void *item) |
|
354 { |
|
355 dlstListItem_t |
|
356 *curr, *next, *listItem = (dlstListItem_t *) item; |
|
357 |
|
358 if (list->curr) { |
|
359 curr = list->curr, |
|
360 next = (dlstListItem_t *) (list->curr->next); |
|
361 |
|
362 curr->next = item; |
|
363 listItem->prev = curr; |
|
364 |
|
365 if (next) |
|
366 next->prev = item; |
|
367 else |
|
368 list->tail = (dlstListItem_t *) item; |
|
369 listItem->next = next; |
|
370 |
|
371 list->curr = (dlstListItem_t *) item; |
|
372 } |
|
373 |
|
374 else { |
|
375 list->head = list->tail = list->curr = (dlstListItem_t *) item; |
|
376 listItem->next = NULL; |
|
377 listItem->prev = NULL; |
|
378 } |
|
379 |
|
380 list->numItems++; |
|
381 |
|
382 return 0; |
|
383 } |
|
384 // End of File |