| 1 |
/* |
| 2 |
* list.c: List operations |
| 3 |
* |
| 4 |
* Written by: Ullrich Hafner |
| 5 |
* |
| 6 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 7 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* $Date: 2000/06/14 20:49:37 $ |
| 12 |
* $Author: hafner $ |
| 13 |
* $Revision: 5.1 $ |
| 14 |
* $State: Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "types.h" |
| 22 |
#include "macros.h" |
| 23 |
#include "error.h" |
| 24 |
|
| 25 |
#include "misc.h" |
| 26 |
#include "list.h" |
| 27 |
|
| 28 |
/***************************************************************************** |
| 29 |
|
| 30 |
public code |
| 31 |
|
| 32 |
*****************************************************************************/ |
| 33 |
|
| 34 |
list_t * |
| 35 |
alloc_list (size_t size_of_element) |
| 36 |
/* |
| 37 |
* List constructor: |
| 38 |
* Allocate a new list. |
| 39 |
* Size of list element values is given by 'size_of_element'. |
| 40 |
* |
| 41 |
* Return value: |
| 42 |
* pointer to an empty list |
| 43 |
*/ |
| 44 |
{ |
| 45 |
list_t *new_list = Calloc (1, sizeof (list_t)); |
| 46 |
|
| 47 |
assert (size_of_element > 0); |
| 48 |
|
| 49 |
new_list->head = NULL; |
| 50 |
new_list->tail = NULL; |
| 51 |
new_list->size_of_element = size_of_element; |
| 52 |
|
| 53 |
return new_list; |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
free_list (list_t *list) |
| 58 |
/* |
| 59 |
* List destructor: |
| 60 |
* Discard list and its elements. |
| 61 |
* |
| 62 |
* No return value. |
| 63 |
* |
| 64 |
* Side effects: |
| 65 |
* struct 'list' is discarded |
| 66 |
*/ |
| 67 |
{ |
| 68 |
assert (list); |
| 69 |
|
| 70 |
while (list_remove (list, HEAD, NULL)) |
| 71 |
; |
| 72 |
Free (list); |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
list_insert (list_t *list, pos_e pos, const void *data) |
| 77 |
/* |
| 78 |
* Insert a new 'list' element at head ('pos' = HEAD) or |
| 79 |
* tail ('pos' = TAIL) of 'list'. |
| 80 |
* 'data' is a pointer to a memory segment of size |
| 81 |
* 'list'->size_of_element containing the value to store. |
| 82 |
* The value is directly copied - no references are stored. |
| 83 |
* |
| 84 |
* No return value. |
| 85 |
* |
| 86 |
* Side effects: |
| 87 |
* lists current tail or head is replaced by the new element |
| 88 |
*/ |
| 89 |
{ |
| 90 |
node_t *element; |
| 91 |
|
| 92 |
assert (list && data); |
| 93 |
|
| 94 |
element = Calloc (1, sizeof (node_t)); |
| 95 |
element->value = Calloc (1, list->size_of_element); |
| 96 |
memcpy (element->value, data, list->size_of_element); |
| 97 |
|
| 98 |
if (pos == TAIL) |
| 99 |
{ |
| 100 |
element->next = NULL; |
| 101 |
element->prev = list->tail; |
| 102 |
if (list->tail) |
| 103 |
list->tail->next = element; |
| 104 |
list->tail = element; |
| 105 |
if (!list->head) |
| 106 |
list->head = element; |
| 107 |
} |
| 108 |
else /* pos == HEAD */ |
| 109 |
{ |
| 110 |
element->prev = NULL; |
| 111 |
element->next = list->head; |
| 112 |
if (list->head) |
| 113 |
list->head->prev = element; |
| 114 |
list->head = element; |
| 115 |
if (!list->tail) |
| 116 |
list->tail = element; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
bool_t |
| 121 |
list_remove (list_t *list, pos_e pos, void *data) |
| 122 |
/* |
| 123 |
* Remove 'list' element from head or tail of 'list'. |
| 124 |
* |
| 125 |
* Return value: |
| 126 |
* TRUE on success, |
| 127 |
* FALSE if list is empty or |
| 128 |
* if list value data is NULL |
| 129 |
* |
| 130 |
* Side effects: |
| 131 |
* lists current head or tail is removed |
| 132 |
* value of the removed list element (if not NULL) is copied to |
| 133 |
* 'data' (if 'data' is not NULL) |
| 134 |
*/ |
| 135 |
{ |
| 136 |
node_t *element; |
| 137 |
void *valueptr; |
| 138 |
|
| 139 |
assert (list); |
| 140 |
|
| 141 |
if (pos == TAIL) |
| 142 |
{ |
| 143 |
element = list->tail; |
| 144 |
if (element) |
| 145 |
{ |
| 146 |
list->tail = element->prev; |
| 147 |
valueptr = element->value; |
| 148 |
Free (element); |
| 149 |
} |
| 150 |
else |
| 151 |
valueptr = NULL; |
| 152 |
if (!list->tail) /* 'element' was last node */ |
| 153 |
list->head = NULL; |
| 154 |
} |
| 155 |
else /* pos == HEAD */ |
| 156 |
{ |
| 157 |
element = list->head; |
| 158 |
if (element) |
| 159 |
{ |
| 160 |
list->head = element->next; |
| 161 |
valueptr = element->value; |
| 162 |
Free (element); |
| 163 |
} |
| 164 |
else |
| 165 |
valueptr = NULL; |
| 166 |
if (!list->head) /* 'element' was last node */ |
| 167 |
list->tail = NULL; |
| 168 |
} |
| 169 |
|
| 170 |
if (valueptr) /* copy value of node */ |
| 171 |
{ |
| 172 |
if (data) |
| 173 |
memcpy (data, valueptr, list->size_of_element); |
| 174 |
Free (valueptr); |
| 175 |
} |
| 176 |
|
| 177 |
return valueptr ? TRUE : FALSE; |
| 178 |
} |
| 179 |
|
| 180 |
bool_t |
| 181 |
list_element_n (const list_t *list, pos_e pos, unsigned n, void *data) |
| 182 |
/* |
| 183 |
* Get value of 'list' element number 'n'. |
| 184 |
* (First element is list head if 'pos' == HEAD |
| 185 |
* or list tail if 'pos' == TAIL. |
| 186 |
* Accordingly, traverse the list in ascending or descending order). |
| 187 |
* |
| 188 |
* Return value: |
| 189 |
* TRUE on success, FALSE if there is no element 'n' |
| 190 |
* |
| 191 |
* Side effects: |
| 192 |
* value of list element 'n' is copied to 'data' |
| 193 |
*/ |
| 194 |
{ |
| 195 |
node_t *element; |
| 196 |
|
| 197 |
assert (list && data); |
| 198 |
|
| 199 |
if (pos == HEAD) |
| 200 |
for (element = list->head; element != NULL && n; |
| 201 |
element = element->next, n--) |
| 202 |
; |
| 203 |
else |
| 204 |
for (element = list->tail; element != NULL && n; |
| 205 |
element = element->prev, n--) |
| 206 |
; |
| 207 |
|
| 208 |
if (element) |
| 209 |
{ |
| 210 |
memcpy (data, element->value, list->size_of_element); |
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
else |
| 214 |
return FALSE; |
| 215 |
} |
| 216 |
|
| 217 |
unsigned |
| 218 |
list_sizeof (const list_t *list) |
| 219 |
/* |
| 220 |
* Count number of 'list' elements. |
| 221 |
* |
| 222 |
* Return value: |
| 223 |
* number of 'list' elements. |
| 224 |
*/ |
| 225 |
{ |
| 226 |
node_t *element; |
| 227 |
unsigned n = 0; |
| 228 |
|
| 229 |
assert (list); |
| 230 |
|
| 231 |
for (element = list->head; element != NULL; element = element->next) |
| 232 |
n++; |
| 233 |
|
| 234 |
return n; |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
list_foreach (const list_t *list, void (*function)(void *, void *), void *data) |
| 239 |
/* |
| 240 |
* Call 'function' for each element of the 'list'. |
| 241 |
* Parameters given to 'function' are a pointer to the value of the |
| 242 |
* current 'list' element and the user pointer 'data'. |
| 243 |
* |
| 244 |
* No return value. |
| 245 |
*/ |
| 246 |
{ |
| 247 |
node_t *element; |
| 248 |
|
| 249 |
assert (list && function && data); |
| 250 |
|
| 251 |
for (element = list->head; element; element = element->next) |
| 252 |
function (element->value, data); |
| 253 |
} |
| 254 |
|