-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscl_list.c
1027 lines (849 loc) · 28.1 KB
/
scl_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file scl_list.c
* @author Mihai Negru (determinant289@gmail.com)
* @version 1.0.0
* @date 2022-06-21
*
* @copyright Copyright (C) 2022-2023 Mihai Negru <determinant289@gmail.com>
* This file is part of C-language-Data-Structures.
*
* C-language-Data-Structures is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* C-language-Data-Structures is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with C-language-Data-Structures. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "./include/scl_list.h"
/**
* @brief Create a Linked List object. Allocation may fail if user
* does not provide a compare and a print function, also in case if
* heap memory is full function will return a `NULL` pointer
*
* @param cmp pointer to a function to compare two sets of data
* @param frd pointer to a function to free the content of data
* basic types like int, float, double, etc... do not need a free function
* so you can pass a NULL pointer
* @param data_size length in bytes of the data data type
* @return list_t* return a new dynamically allocated list or `NULL` if
* allocation went wrong
*/
list_t* create_list(compare_func cmp, free_func frd, size_t data_size) {
/*
* It is required for every linked list to have a compare function
* The free function is optional
*/
if (NULL == cmp) {
errno = EINVAL;
perror("Compare function undefined for linked list");
return NULL;
}
if (0 == data_size) {
errno = EINVAL;
perror("Data size at creation is zero");
return NULL;
}
/* Allocate a new list on heap */
list_t *new_list = malloc(sizeof(*new_list));
/* Check if new list was allocated */
if (NULL != new_list) {
/* Set pointer functions in linked list class */
new_list->cmp = cmp;
new_list->frd = frd;
/* Initialize head, tail and size of new list */
new_list->head = new_list->tail = NULL;
new_list->data_size = data_size;
new_list->size = 0;
} else {
errno = ENOMEM;
perror("Not enough memory for list allocation");
}
/* Return new allocated list or `NULL` */
return new_list;
}
/**
* @brief Create a Linked Node object. Creation of a
* node will fail if the pointer to data is `NULL` or
* heap memory is also full, in this case function will return a `NULL`
* pointer
*
* @param list an allocated linked list object
* @param data pointer to address of a generic data
* @return list_node_t* return a new allocated node object
*/
static list_node_t* create_list_node(const list_t * const __restrict__ list, const void * __restrict__ data) {
/* It is required for data to be a valid pointer */
if (NULL == data) {
return NULL;
}
/* Allocate a new Node on heap */
list_node_t *new_node = malloc(sizeof(*new_node));
/* Check if new node was allocated */
if (NULL != new_node) {
new_node->next = NULL;
/* Allocate heap memory for data */
new_node->data = malloc(list->data_size);
/* Check if data pointer was allocated*/
if (NULL != new_node->data) {
/*
* Copy all bytes from data pointer
* to memory allocated on heap
*/
memcpy(new_node->data, data, list->data_size);
} else {
free(new_node);
new_node = NULL;
errno = ENOMEM;
perror("Not enough memory for data node list allocation");
}
} else {
errno = ENOMEM;
perror("Not enough memory for node list allocation");
}
/* Return a new created node or `NULL` */
return new_node;
}
/**
* @brief Function to free every byte of memory allocated for a specific
* linked list object. The function will iterate through all nodes and will
* free the data content according to frd function provided by user at
* creation of linked list, however if no free function was provided it means
* that data pointer does not contain any dinamically allocated elements.
*
* @param list an allocated linked list object. If list is not allocated
* no operation will be needed
* @return scl_error_t enum object for handling errors
*/
scl_error_t free_list(list_t * const __restrict__ list) {
/* Check if list needs to be deallocated */
if (NULL != list) {
/* Iterate through every node */
while (NULL != list->head) {
list_node_t *iterator = list->head;
list->head = list->head->next;
/* Erase content of an element */
if ((NULL != list->frd) && (NULL != iterator->data)) {
list->frd(iterator->data);
}
/* Free data pointer */
if (NULL != iterator->data) {
free(iterator->data);
}
/* Set data pointer to `NULL` */
iterator->data = NULL;
/* Free node pointer */
if (NULL != iterator) {
free(iterator);
}
/* Set node pointer to `NULL` */
iterator = NULL;
}
/* Free list */
free(list);
return SCL_OK;
}
return SCL_NULL_LIST;
}
/**
* @brief Function to check if a linked list object
* is empty or not. The function tests if head of list
* is `NULL` in that case function will return true, otherwise
* it will return false. A `NULL` list is also considered as an
* empty list
*
* @param list a linked list object
* @return uint8_t true(1) if list is empty and false(0) if list is not
* empty
*/
uint8_t is_list_empty(const list_t * const __restrict__ list) {
if ((NULL == list) || (NULL == list->head)) {
return 1;
}
return 0;
}
/**
* @brief Get the list size object. If list is not
* allocated then function will return SIZE_MAX value.
*
* @param list a linked list object
* @return size_t SIZE_MAX if list is not allocated or
* list size
*/
size_t get_list_size(const list_t * const __restrict__ list) {
if (NULL == list) {
return SIZE_MAX;
}
return list->size;
}
/**
* @brief Get the list head object
*
* @param list a linked list object
* @return const void* `NULL` if list is not allocated
* or actual head data of the list
*/
const void* get_list_head(const list_t * const __restrict__ list) {
if ((NULL == list) || (NULL == list->head)) {
return NULL;
}
return list->head->data;
}
/**
* @brief Get the list tail object
*
* @param list a linked list object
* @return const void* `NULL` if list is not allocated
* or actual tail data of the list
*/
const void* get_list_tail(const list_t * const __restrict__ list) {
if ((NULL == list) || (NULL == list->tail)) {
return NULL;
}
return list->tail->data;
}
/**
* @brief Function to find node that contains
* specific data provided by user. It uses cmp function
* provided by user at the creation of the linked list.
*
* @param list a linked list object
* @param data pointer to a typed data
* @return list_node_t* `NULL` if data is not found or a pointer
* to a double linked list node data containing given data
*/
static list_node_t* list_find_node(const list_t * const __restrict__ list, const void * const __restrict__ data) {
/*
* Check if list and data are valid and
* check if list is not empty
*/
if ((NULL == list) || (NULL == list->head) || (NULL == data)) {
return NULL;
}
list_node_t *iterator = list->head;
/* Find node */
while ((NULL != iterator) && (list->cmp(iterator->data, data) != 0)) {
iterator = iterator->next;
}
return iterator;
}
/**
* @brief Function two swap data between two list nodes. If nodes
* are `NUL`L or the same then no operation will be executed. Function
* will swap data pointers not node pointers. Function may fail if
* list is not allocated
*
* @param list an allocated double linked list object
* @param first_node pointer to value of the first data
* @param second_node pointer to value of the second data
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_swap_data(const list_t * const __restrict__ list, const void * const __restrict__ first_data, const void * const __restrict__ second_data) {
/* Check if list and input nodes are allocated */
if ((NULL == first_data) || (NULL == second_data)) {
return SCL_CANNOT_SWAP_DATA;
}
list_node_t * const list_first = list_find_node(list, first_data);
list_node_t * const list_second = list_find_node(list, second_data);
if ((NULL == list_first) && (NULL == list_second)) {
return SCL_DATA_NOT_FOUND;
}
/* If nodes are the same then no swap is nedeed */
if (list_first == list_second) {
return SCL_SWAP_SAME_DATA;
}
uint8_t *list_first_data = list_first->data;
uint8_t *list_second_data = list_second->data;
size_t data_size = list->data_size;
/*
* Copy adsress value of first data pointer
* and interchange data pointers
*/
while (data_size-- > 0) {
uint8_t temp = *list_first_data;
*list_first_data++ = *list_second_data;
*list_second_data++ = temp;
}
return SCL_OK;
}
/**
* @brief Function to change data of a specific linked list node.
*
* @param list an allocated double linked list object
* @param base_node pointer to value of the base data
* @param new_data a pointer to value of the new data to replace
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_change_data(const list_t * const __restrict__ list, const void * const __restrict__ base_data, const void * __restrict__ new_data) {
/* Check if input is valid */
if (NULL == new_data) {
return SCL_CANNOT_CHANGE_DATA;
}
list_node_t * const list_base = list_find_node(list, base_data);
if (NULL == list_base) {
return SCL_DATA_NOT_FOUND;
}
/* Copy all bytes from new data to current data */
memcpy(list_base->data, new_data, list->data_size);
return SCL_OK;
}
/**
* @brief Function to insert an element to the end of the list.
*
* @param list a linked list object
* @param data a pointer for data to insert in list
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_insert(list_t * const __restrict__ list, const void * __restrict__ data) {
/* Check if list and data are valid */
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == data) {
return SCL_INVALID_DATA;
}
/* Create a new linked list node */
list_node_t *new_node = create_list_node(list, data);
/* Check if node was allocated */
if (NULL == new_node) {
return SCL_NOT_ENOUGHT_MEM_FOR_NODE;
}
/* Insert item in list */
if (NULL == list->head) {
/*
* List is empty so update head
* and tail of the list
*/
list->head = new_node;
list->tail = new_node;
} else {
/* Insert element at the end of the list */
list->tail->next = new_node;
list->tail = new_node;
}
/* Increase size of the list */
++(list->size);
/* Insertion went successfully */
return SCL_OK;
}
/**
* @brief Function to insert an element in order in the list.
* Function will find the position of the new elements according
* to cmp function provided at the creation of the list
*
* @param list a linked list object
* @param data a pointer for data to insert in list
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_insert_order(list_t * const __restrict__ list, const void * __restrict__ data) {
/* Check if list and data are valid */
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == data) {
return SCL_INVALID_DATA;
}
/* Create a new linked list node */
list_node_t *new_node = create_list_node(list, data);
/* Check if node was allocated */
if (NULL == new_node) {
return SCL_NOT_ENOUGHT_MEM_FOR_NODE;
}
/* Insert element into the list */
if (NULL == list->head) {
/*
* List is empty so update
* head and tail of the list
*/
list->head = new_node;
list->tail = new_node;
} else {
list_node_t *iterator = list->head;
list_node_t *prev_iterator = NULL;
/* Find the position for the new element */
while ((NULL != iterator) && (list->cmp(new_node->data, iterator->data) > 0)) {
prev_iterator = iterator;
iterator = iterator->next;
}
if (NULL == prev_iterator) {
/*
* New node must be inserted at the begining
* of the linked list
*/
new_node->next = list->head;
list->head = new_node;
} else {
/* Insert element at the new position */
new_node->next = iterator;
prev_iterator->next = new_node;
if (NULL == iterator) {
list->tail = new_node;
}
}
}
/* Increase size of the list */
++(list->size);
/* Insertion went successfully */
return SCL_OK;
}
/**
* @brief Function to insert an element in front of the list.
*
* @param list a linked list object
* @param data a pointer for data to insert in list
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_insert_front(list_t * const __restrict__ list, const void * __restrict__ data) {
/* Check if list and data are valid */
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == data) {
return SCL_INVALID_DATA;
}
/* Create a new linked list node */
list_node_t *new_node = create_list_node(list, data);
/* Check if node was allocated */
if (NULL == new_node) {
return SCL_NOT_ENOUGHT_MEM_FOR_NODE;
}
/*
* Insert new element at the beginning
* of the linked list
*/
if (NULL == list->head) {
/*
* List is empty so update
* head and tail of the list
*/
list->head = new_node;
list->tail = new_node;
} else {
/*
* Link new node to the list
* and update new head
*/
new_node->next = list->head;
list->head = new_node;
}
/* Increase list size */
++(list->size);
/* Insertion went successfully */
return SCL_OK;
}
/**
* @brief Function to insert an element at a specified index in the list.
* If index is bigger than current list size than element will be inserted
* at the end of the list.
*
* @param list a linked list object
* @param data a pointer for data to insert in list
* @param data_index the index to insert an element into linked list
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_insert_index(list_t * const __restrict__ list, const void * __restrict__ data, size_t data_index) {
/* Check if list and data are valid */
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == data) {
return SCL_INVALID_DATA;
}
/* Insert element at the end of the list */
if (data_index >= list->size)
return list_insert(list, data);
/* Insert element at the beginning of the list */
if (data_index == 0)
return list_insert_front(list, data);
/* Create a new linked list node */
list_node_t *new_node = create_list_node(list, data);
/* Check if new node was allocated */
if (NULL == new_node) {
return SCL_NOT_ENOUGHT_MEM_FOR_NODE;
}
list_node_t *iterator = list->head;
/*
* Find node that point on the
* specified index node
*/
while (--data_index) {
iterator = iterator->next;
}
/* Insert node and update links */
new_node->next = iterator->next;
iterator->next = new_node;
if (NULL == new_node->next) {
list->head = new_node;
}
/* Increase list size */
++(list->size);
/* Insertion went successfully */
return SCL_OK;
}
/**
* @brief Function to find a node from a list from
* a given index. Function will fail if index is bigger than
* current size of list or list is not allocated
*
* @param list a linked list object
* @param data_index index to pick node from
* @return const void* linked list node data from list at specified
* index
*/
const void* list_find_index(const list_t * const __restrict__ list, size_t data_index) {
/* Check if list and index are valid */
if ((NULL == list) || (data_index >= list->size)) {
return NULL;
}
/* Return last element */
if (data_index == (list->size - 1)) {
if (NULL != list->tail) {
return list->tail->data;
}
return NULL;
}
const list_node_t *iterator = list->head;
/* Iterate in list until hit indexed node */
while (data_index--) {
iterator = iterator->next;
}
/* Return a pointe to found node */
if (NULL != iterator) {
return iterator->data;
}
/* Data was not found */
return NULL;
}
/**
* @brief Function to find node that contains
* specific data provided by user. It uses cmp function
* provided by user at the creation of the linked list.
*
* @param list a linked list object
* @param data pointer to a typed data
* @return const void* `NULL` if data is not found or a pointer
* to a linked list node data containing given data
*/
const void* list_find_data(const list_t * const __restrict__ list, const void * const __restrict__ data) {
/*
* Check if list and data are valid and
* check if list is not empty
*/
if ((NULL == list) || (NULL == list->head) || (NULL == data)) {
return NULL;
}
const list_node_t *iterator = list->head;
/* Find node */
while ((NULL != iterator) && (list->cmp(iterator->data, data) != 0)) {
iterator = iterator->next;
}
/* Return a pointer to node or `NULL` */
if (NULL != iterator) {
return iterator->data;
}
return NULL;
}
/**
* @brief Function to delete a node based on a value. Program will
* recieve a list and a pointer to data that user wants to be deleted.
* However data pointer has to be valid and to exist in the current list
* (If you are not sure that data exists you should not call list_find_data because
* delete function will find it by itself and in case it does not exist it will return an error)
*
* @param list a linked list object
* @param data a pointer to a typed data to be removed
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_delete_data(list_t * const __restrict__ list, const void * const __restrict__ data) {
/*
* Check if list is allocated and it is not empty
* Check if data pointer is valid
*/
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == list->head) {
return SCL_DELETE_FROM_EMPTY_OBJECT;
}
if (NULL == data) {
return SCL_INVALID_DATA;
}
list_node_t *iterator = list->head;
list_node_t *prev_iterator = NULL;
/* Find list data associated with data pointer */
while ((NULL != iterator) && (list->cmp(iterator->data, data) != 0)) {
prev_iterator = iterator;
iterator = iterator->next;
}
/* List does not contain such element */
if (NULL == iterator) {
return SCL_DATA_NOT_FOUND_FOR_DELETE;
}
if (NULL == prev_iterator) {
/* Element is the head of the list */
list->head = list->head->next;
} else {
/* Update link with next node */
prev_iterator->next = iterator->next;
/* Update tail if necessary */
if (NULL == iterator->next) {
list->tail = prev_iterator;
}
}
/* Free content of data */
if ((NULL != list->frd) && (NULL != iterator->data)) {
list->frd(iterator->data);
}
/* Free data pointer and set to `NULL` */
if (NULL != iterator->data) {
free(iterator->data);
}
iterator->data = NULL;
/* Free node pointer and set to `NULL` */
if (NULL != iterator) {
free(iterator);
}
iterator = NULL;
/* Decrease list size */
--(list->size);
/* Deletion went successfully */
return SCL_OK;
}
/**
* @brief Function to delete a node based on an index. Program will
* recieve a list and a index from which element will be erased. If
* data_index is bigger than actual size of the list then function will
* fail its execution and will return an error. It is necessary for list to be
* allocated and not be be empty.
*
* @param list a linked list object
* @param data_index node index in the list to be removed starts from 0
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_delete_index(list_t * const __restrict__ list, size_t data_index) {
/*
* Check if list is allocated and it is not empty
* Check if data pointer is valid
*/
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == list->head) {
return SCL_DELETE_FROM_EMPTY_OBJECT;
}
if (data_index >= list->size) {
return SCL_INDEX_OVERFLOWS_SIZE;
}
list_node_t *iterator = list->head;
list_node_t *prev_iterator = NULL;
/* Find node from index */
while (data_index--) {
prev_iterator = iterator;
iterator = iterator->next;
}
if (NULL == prev_iterator) {
/* Removing node is current head */
list->head = list->head->next;
} else {
/* Update links within the nodes */
prev_iterator->next = iterator->next;
/* Update list tail if necessery */
if (NULL == iterator->next) {
list->tail = prev_iterator;
}
}
/* Free content of data */
if ((NULL != list->frd) && (NULL != iterator->data)) {
list->frd(iterator->data);
}
/* Free data pointer and set to `NULL` */
if (NULL != iterator->data) {
free(iterator->data);
}
iterator->data = NULL;
/* Free node pointer and set to `NULL` */
if (NULL != iterator) {
free(iterator);
}
iterator = NULL;
/* Deacrise list size */
--(list->size);
/* Deletion went successfully */
return SCL_OK;
}
/**
* @brief Function to erase a set of nodes from range [left_index; right_index]
* If left_index is greater than right_index that they will be swapped. If right_index
* is bigger than actual size of the list right_index will be updated to the end of
* the list. If both left and right index are bigger than actual list size than
* the last element from linked object will be removed.
*
* @param list a linked list object
* @param left_index left index to start deletion
* @param right_index right index to finish deletion
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_erase(list_t * const __restrict__ list, size_t left_index, size_t right_index) {
/* Check if list is allocated and it is not empty */
if (NULL == list) {
return SCL_NULL_DLIST;
}
if (NULL == list->head) {
return SCL_DELETE_FROM_EMPTY_OBJECT;
}
/*
* Check if boundaries are set right
* Swap if necessary
*/
if (left_index > right_index) {
size_t temp = left_index;
left_index = right_index;
right_index = temp;
}
/* Recalibrate left index if needed */
if (left_index >= list->size) {
left_index = list->size - 1;
}
/* Recalibrate right index if needed */
if (right_index >= list->size) {
right_index = list->size - 1;
}
list_node_t *iterator = list->head;
list_node_t *prev_iterator = NULL;
/* Compute number of nodes from range */
size_t delete_num = right_index - left_index + 1;
/*
* Update iterator and prevIterator
* pointer to beginning of list deletion
*/
while (left_index--) {
prev_iterator = iterator;
iterator = iterator->next;
}
/* Decrease list size */
list->size -= delete_num;
/* Delete every number from given range */
while (delete_num--) {
if (NULL == prev_iterator) {
/* Check if removed node is head */
list->head = list->head->next;
} else {
/* Update nodes links */
prev_iterator->next = iterator->next;
/* Update list tail if neccessary */
if (NULL == iterator->next) {
list->tail = prev_iterator;
}
}
/* Free content of data */
if ((NULL != list->frd) && (NULL != iterator->data)) {
list->frd(iterator->data);
}
/* Free data pointer and set to `NULL` */
if (NULL != iterator->data) {
free(iterator->data);
}
iterator->data = NULL;
/* Free node pointer and set to `NULL` */
if (NULL != iterator) {
free(iterator);
}
iterator = NULL;
/* Remove next node */
if (NULL == prev_iterator) {
iterator = list->head;
} else {
iterator = prev_iterator->next;
}
}
/* Deletion went successfully */
return SCL_OK;
}
/**
* @brief Function to filter a given linked list object. User
* has to provide a function that return true(1) or false(0). If
* filter function return 1 for an item then it will be added in a
* new linked list, otherwise item will not be inserted. If no
* element was inserted in the new linked list than the list will
* be automatically erased from memory
*
* @param list a linked list object
* @param filter a pointer to a filter function
* @return list_t* a filtered linked list object with smaller
* or equal size of the original linked list object
*/
list_t* list_filter(const list_t * const __restrict__ list, filter_func filter) {
/*
* Check if input is valid
* Filter function has to be different from NULL pointer
*/
if ((NULL == list) || (NULL == list->head) || (NULL == filter)) {
return NULL;
}
/* Create a new linked list object */
list_t *filter_list = create_list(list->cmp, list->frd, list->data_size);
/* Check if list was created */
if (NULL != filter_list) {
const list_node_t *iterator = list->head;
/* Iterate through all list nodes */
while (NULL != iterator) {
/* Check if item is filtered or not */
if ((NULL != iterator->data) && (1 == filter(iterator->data))) {
list_insert(filter_list, iterator->data);
}
iterator = iterator->next;
}
/*
* If no element was added to list than free
* space of the new linked list and return NULL
*/
if (NULL == filter_list->head) {
free_list(filter_list);
filter_list = NULL;
}
}
/* Return filtered list or `NULL` */
return filter_list;
}
/**
* @brief Function to traverse all list and
* do action on all data nodes.
*
* @param list a linked list object
* @param action a pointer to an action function(can be also a mapping func)
* @return scl_error_t enum object for handling errors
*/
scl_error_t list_traverse(const list_t * const __restrict__ list, action_func action) {
/*
* Check if list is allocated and is not empty
* Check if user provided a valid map function
*/
if (NULL == list) {
return SCL_NULL_DLIST;
}