Another gitignore update. Documentation sources are not included in repository now.
[SecondTask.git] / Sources / Foundation / foundation_slist.c
bloba891316a1ef55568e6f3b9d59ceeba32ec59dd52
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)
8 );
9 pSlist->head = 0;
10 pSlist->destruct_function = input_destroy_function;
11 pSlist->compare_key_function = input_compare_function;
12 pSlist->size = 0;
13 return pSlist;
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);
21 free(input_slist);
24 foundation_error_t push_back_foundation_slist(
25 struct foundation_slist_t *input_slist,
26 void *element,
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;
38 new_node->next = 0;
40 if (input_slist->head == 0) {
41 input_slist->head = new_node;
42 input_slist->size++;
43 return FOUNDATION_ERROR_SUCCESS;
45 current = input_slist->head;
46 while (current->next != 0) {
47 current = current->next;
49 current->next = new_node;
50 input_slist->size++;
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) {
58 void *elem;
59 get_raw_foundation_object(
60 input_slist_node->element,
61 &elem
63 if (input_slist->destruct_function) {
64 (input_slist->destruct_function)(elem);
65 delete_foundation_object(
66 input_slist_node->element
68 } else {
69 free(elem);
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) {
78 int i = 0;
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;}
85 if (position == 0) {
86 input_slist->head = current->next;
87 __remove_foundation_list(input_slist, current);
88 input_slist->size--;
89 return;
91 for (i = 1; i < position - 1; i++) {
92 current = current->next;
95 temp = current->next;
96 current->next = current->next->next;
97 __remove_foundation_list(input_slist, temp);
99 input_slist->size--;
102 foundation_error_t insert_foundation_slist(
103 struct foundation_slist_t *input_slist,
104 int position,
105 void *element,
106 size_t element_size) {
107 int i = 0;
108 struct foundation_slist_node_t *current = input_slist->head;
109 struct foundation_slist_node_t *new_node = 0;
111 if (position == 1) {
112 new_node = malloc(
113 sizeof(struct foundation_slist_node_t)
115 new_node->element = new_foundation_object(
116 element,
117 element_size
119 if (!new_node->element) {
120 free(new_node);
121 return FOUNDATION_SLIST_INSERT_FAILED;
123 new_node->next = input_slist->head;
124 input_slist->head = new_node;
125 input_slist->size++;
126 return FOUNDATION_ERROR_SUCCESS;
129 if (position >= input_slist->size + 1) {
130 return push_back_foundation_slist(
131 input_slist,
132 element,
133 element_size
137 for (i = 1; i < position - 1; i++) {
138 current = current->next;
140 new_node = malloc(
141 sizeof(struct foundation_slist_node_t)
143 new_node->element = new_foundation_object(
144 element,
145 element_size
147 if (!new_node->element) {
148 free(new_node);
149 return FOUNDATION_SLIST_INSERT_FAILED;
152 new_node->next = current->next;
153 current->next = new_node;
154 input_slist->size++;
156 return FOUNDATION_ERROR_SUCCESS;
159 void for_each_foundation_slist(
160 struct foundation_slist_t *input_slist,
161 void (*input_foreach_function)(void *)) {
162 void *element;
163 struct foundation_slist_node_t *current = input_slist->head;
164 while (current != 0) {
165 get_raw_foundation_object(
166 current->element,
167 &element
169 (input_foreach_function)(element);
170 free(element);
171 current = current->next;
175 foundation_bool_t find_foundation_slist(
176 struct foundation_slist_t *input_slist,
177 void *find_value,
178 void **output_value) {
179 struct foundation_slist_node_t *current = input_slist->head;
180 while (current != 0) {
181 get_raw_foundation_object(
182 current->element,
183 output_value
185 if ((input_slist->compare_key_function)
186 (find_value, *output_value) != 0) {
187 break;
189 free(*output_value);
190 current = current->next;
192 if (current) {
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;
206 } else {
207 input_iterator->current_element = (
208 (struct foundation_slist_node_t *)
209 input_iterator->current_element)->next;
211 if (!input_iterator->current_element) {
212 return 0;
215 return ((struct foundation_slist_node_t *)
216 input_iterator->current_element)->element;
219 static void *get_value_foundation_slist(
220 void *input_object) {
221 void *element;
222 get_raw_foundation_object(input_object, &element);
223 return element;
226 static void replace_value_foundation_slist(
227 struct foundation_iterator_t *input_iterator,
228 void *element,
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) {
238 void *old_element;
240 get_raw_foundation_object(pObj,
241 &old_element
243 pSlist->destruct_function(old_element);
245 replace_raw_foundation_object(
246 pObj,
247 element,
248 element_size
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;
263 return itr;
266 void delete_iterator_foundation_slist(
267 struct foundation_iterator_t *input_pointer) {
268 free(input_pointer);