11 void _list_init(List
* list
, size_t dataSize
)
14 list
->dataSize
= dataSize
;
19 List
* _list_new(size_t dataSize
)
21 List
* list
= (List
*) safe_malloc(sizeof (List
));
22 _list_init(list
, dataSize
);
26 List
* list_copy(List
* list
)
28 List
* listCopy
= _list_new(list
->dataSize
);
29 ListCell
* listCell
= list
->head
;
30 while (listCell
!= NULL
)
32 _list_insert_back(listCopy
, listCell
->data
);
33 listCell
= listCell
->next
;
38 bool list_empty(List
* list
)
40 return (list
->size
== 0);
43 UInt
list_size(List
* list
)
48 void* _list_get(ListCell
* listCell
)
50 return listCell
->data
;
53 void _list_set(List
* list
, ListCell
* listCell
, void* data
)
55 memcpy(listCell
->data
, data
, list
->dataSize
);
58 void _list_insert_first_element(List
* list
, void* data
)
60 ListCell
* newListCell
= (ListCell
*) safe_malloc(sizeof (ListCell
));
61 newListCell
->data
= safe_malloc(list
->dataSize
);
62 memcpy(newListCell
->data
, data
, list
->dataSize
);
63 newListCell
->prev
= NULL
;
64 newListCell
->next
= NULL
;
65 list
->head
= newListCell
;
66 list
->tail
= newListCell
;
70 void _list_insert_before(List
* list
, ListCell
* listCell
, void* data
)
72 ListCell
* newListCell
= (ListCell
*) safe_malloc(sizeof (ListCell
));
73 newListCell
->data
= safe_malloc(list
->dataSize
);
74 memcpy(newListCell
->data
, data
, list
->dataSize
);
75 newListCell
->prev
= listCell
->prev
;
76 newListCell
->next
= listCell
;
77 if (listCell
->prev
!= NULL
)
78 listCell
->prev
->next
= newListCell
;
80 list
->head
= newListCell
;
81 listCell
->prev
= newListCell
;
85 void _list_insert_after(List
* list
, ListCell
* listCell
, void* data
)
87 ListCell
* newListCell
= (ListCell
*) safe_malloc(sizeof (ListCell
));
88 newListCell
->data
= safe_malloc(list
->dataSize
);
89 memcpy(newListCell
->data
, data
, list
->dataSize
);
90 newListCell
->prev
= listCell
;
91 newListCell
->next
= listCell
->next
;
92 if (listCell
->next
!= NULL
)
93 listCell
->next
->prev
= newListCell
;
95 list
->tail
= newListCell
;
96 listCell
->next
= newListCell
;
100 void _list_insert_front(List
* list
, void* data
)
102 if (list
->head
!= NULL
)
103 _list_insert_before(list
, list
->head
, data
);
105 _list_insert_first_element(list
, data
);
108 void _list_insert_back(List
* list
, void* data
)
110 if (list
->tail
!= NULL
)
111 _list_insert_after(list
, list
->tail
, data
);
113 _list_insert_first_element(list
, data
);
116 void list_remove(List
* list
, ListCell
* listCell
)
118 if (listCell
->prev
!= NULL
)
119 listCell
->prev
->next
= listCell
->next
;
121 list
->head
= listCell
->next
;
122 if (listCell
->next
!= NULL
)
123 listCell
->next
->prev
= listCell
->prev
;
125 list
->tail
= listCell
->prev
;
126 safe_free(listCell
->data
);
131 void list_remove_front(List
* list
)
133 list_remove(list
, list
->head
);
136 void list_remove_back(List
* list
)
138 list_remove(list
, list
->tail
);
141 void list_clear(List
* list
)
143 ListCell
* current
= list
->head
;
144 while (current
!= NULL
)
146 safe_free(current
->data
);
147 ListCell
* nextListCell
= current
->next
;
149 current
= nextListCell
;
151 _list_init(list
, list
->dataSize
);
154 void list_destroy(List
* list
)
164 ListIterator
* list_get_iterator(List
* list
)
166 ListIterator
* listI
= (ListIterator
*) safe_malloc(sizeof (ListIterator
));
168 listI
->current
= NULL
;
169 listI_reset_head(listI
);
173 void listI_reset_head(ListIterator
* listI
)
175 listI
->current
= listI
->list
->head
;
178 void listI_reset_tail(ListIterator
* listI
)
180 listI
->current
= listI
->list
->tail
;
183 bool listI_has_data(ListIterator
* listI
)
185 return (listI
->current
!= NULL
);
188 void listI_remove(ListIterator
* listI
, Direction direction
)
190 ListCell
* toTrash
= listI
->current
;
194 listI
->current
= listI
->current
->next
;
197 listI
->current
= listI
->current
->prev
;
200 list_remove(listI
->list
, toTrash
);
203 void listI_move_next(ListIterator
* listI
)
205 if (listI
->current
!= NULL
)
206 listI
->current
= listI
->current
->next
;
209 void listI_move_prev(ListIterator
* listI
)
211 if (listI
->current
!= NULL
)
212 listI
->current
= listI
->current
->prev
;
215 void listI_destroy(ListIterator
* listI
)