1 #include "foundation_slist.h"
3 struct foundation_slist_t
*new_foundation_slist(
4 foundation_compare input_compare_function
,
5 foundation_destroy input_destroy_function
) {
6 struct foundation_slist_t
*pSlist
= malloc(
7 sizeof(struct foundation_slist_t
)
10 pSlist
->destruct_function
= input_destroy_function
;
11 pSlist
->compare_key_function
= input_compare_function
;
16 void delete_foundation_slist(
17 struct foundation_slist_t
*input_slist
) {
18 while (input_slist
->size
!= 0) {
19 remove_foundation_slist(input_slist
, 0);
24 foundation_error_t
push_back_foundation_slist(
25 struct foundation_slist_t
*input_slist
,
27 size_t element_size
) {
29 struct foundation_slist_node_t
*current
= 0;
30 struct foundation_slist_node_t
*new_node
= malloc(
31 sizeof(struct foundation_slist_node_t
)
34 new_node
->element
= new_foundation_object(element
, element_size
);
35 if (!new_node
->element
) {
36 return FOUNDATION_SLIST_INSERT_FAILED
;
40 if (input_slist
->head
== 0) {
41 input_slist
->head
= new_node
;
43 return FOUNDATION_ERROR_SUCCESS
;
45 current
= input_slist
->head
;
46 while (current
->next
!= 0) {
47 current
= current
->next
;
49 current
->next
= new_node
;
52 return FOUNDATION_ERROR_SUCCESS
;
55 static void __remove_foundation_list(
56 struct foundation_slist_t
*input_slist
,
57 struct foundation_slist_node_t
*input_slist_node
) {
59 get_raw_foundation_object(
60 input_slist_node
->element
,
63 if (input_slist
->destruct_function
) {
64 (input_slist
->destruct_function
)(elem
);
65 delete_foundation_object(
66 input_slist_node
->element
70 delete_foundation_object(
71 input_slist_node
->element
74 free(input_slist_node
);
77 void remove_foundation_slist(struct foundation_slist_t
*input_slist
, int position
) {
80 struct foundation_slist_node_t
*current
= input_slist
->head
;
81 struct foundation_slist_node_t
*temp
= 0;
83 if (position
> input_slist
->size
) {return;}
86 input_slist
->head
= current
->next
;
87 __remove_foundation_list(input_slist
, current
);
91 for (i
= 1; i
< position
- 1; i
++) {
92 current
= current
->next
;
96 current
->next
= current
->next
->next
;
97 __remove_foundation_list(input_slist
, temp
);
102 foundation_error_t
insert_foundation_slist(
103 struct foundation_slist_t
*input_slist
,
106 size_t element_size
) {
108 struct foundation_slist_node_t
*current
= input_slist
->head
;
109 struct foundation_slist_node_t
*new_node
= 0;
113 sizeof(struct foundation_slist_node_t
)
115 new_node
->element
= new_foundation_object(
119 if (!new_node
->element
) {
121 return FOUNDATION_SLIST_INSERT_FAILED
;
123 new_node
->next
= input_slist
->head
;
124 input_slist
->head
= new_node
;
126 return FOUNDATION_ERROR_SUCCESS
;
129 if (position
>= input_slist
->size
+ 1) {
130 return push_back_foundation_slist(
137 for (i
= 1; i
< position
- 1; i
++) {
138 current
= current
->next
;
141 sizeof(struct foundation_slist_node_t
)
143 new_node
->element
= new_foundation_object(
147 if (!new_node
->element
) {
149 return FOUNDATION_SLIST_INSERT_FAILED
;
152 new_node
->next
= current
->next
;
153 current
->next
= new_node
;
156 return FOUNDATION_ERROR_SUCCESS
;
159 void for_each_foundation_slist(
160 struct foundation_slist_t
*input_slist
,
161 void (*input_foreach_function
)(void *)) {
163 struct foundation_slist_node_t
*current
= input_slist
->head
;
164 while (current
!= 0) {
165 get_raw_foundation_object(
169 (input_foreach_function
)(element
);
171 current
= current
->next
;
175 foundation_bool_t
find_foundation_slist(
176 struct foundation_slist_t
*input_slist
,
178 void **output_value
) {
179 struct foundation_slist_node_t
*current
= input_slist
->head
;
180 while (current
!= 0) {
181 get_raw_foundation_object(
185 if ((input_slist
->compare_key_function
)
186 (find_value
, *output_value
) != 0) {
190 current
= current
->next
;
193 return foundation_true
;
195 return foundation_false
;
199 static struct foundation_object_t
*get_next_foundation_slist(
200 struct foundation_iterator_t
*input_iterator
) {
202 struct foundation_slist_t
*pSlist
=
203 (struct foundation_slist_t
*)input_iterator
->container
;
204 if (!input_iterator
->current_element
) {
205 input_iterator
->current_element
= pSlist
->head
;
207 input_iterator
->current_element
= (
208 (struct foundation_slist_node_t
*)
209 input_iterator
->current_element
)->next
;
211 if (!input_iterator
->current_element
) {
215 return ((struct foundation_slist_node_t
*)
216 input_iterator
->current_element
)->element
;
219 static void *get_value_foundation_slist(
220 void *input_object
) {
222 get_raw_foundation_object(input_object
, &element
);
226 static void replace_value_foundation_slist(
227 struct foundation_iterator_t
*input_iterator
,
229 size_t element_size
) {
230 struct foundation_slist_t
*pSlist
=
231 (struct foundation_slist_t
*)
232 input_iterator
->container
;
233 struct foundation_object_t
*pObj
=
234 ((struct foundation_slist_node_t
*)
235 input_iterator
->current_element
)->element
;
237 if (pSlist
->destruct_function
) {
240 get_raw_foundation_object(pObj
,
243 pSlist
->destruct_function(old_element
);
245 replace_raw_foundation_object(
252 struct foundation_iterator_t
*new_iterator_foundation_slist(
253 struct foundation_slist_t
*pSlist
) {
254 struct foundation_iterator_t
*itr
= malloc(
255 sizeof(struct foundation_iterator_t
)
257 itr
->get_next
= get_next_foundation_slist
;
258 itr
->get_value
= get_value_foundation_slist
;
259 itr
->replace_value
= replace_value_foundation_slist
;
260 itr
->container
= pSlist
;
261 itr
->current_element
= NULL
;
262 itr
->current_index
= 0;
266 void delete_iterator_foundation_slist(
267 struct foundation_iterator_t
*input_pointer
) {