-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathcc_slist.c
1593 lines (1358 loc) · 43.9 KB
/
cc_slist.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
/*
* Collections-C
* Copyright (C) 2013-2015 Srđan Panić <[email protected]>
*
* This file is part of Collections-C.
*
* Collections-C 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.
*
* Collections-C 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 Collections-C. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cc_slist.h"
struct cc_slist_s {
size_t size;
SNode *head;
SNode *tail;
void *(*mem_alloc) (size_t size);
void *(*mem_calloc) (size_t blocks, size_t size);
void (*mem_free) (void *block);
};
static void* unlinkn (CC_SList *list, SNode *node, SNode *prev);
static bool unlinkn_all (CC_SList *list, void (*cb) (void*));
static void splice_between (CC_SList *list1, CC_SList *list2, SNode *base, SNode *end);
static bool link_all_externally (CC_SList *list, SNode **h, SNode **t);
static enum cc_stat get_node_at (CC_SList *list, size_t index, SNode **node, SNode **prev);
static enum cc_stat get_node (CC_SList *list, void *element, SNode **node, SNode **prev);
/**
* Initializes the fields CC_SListConf struct to default values.
*
* @param[in] conf the CC_SListConf struct that is being initialized.
*/
void cc_slist_conf_init(CC_SListConf *conf)
{
conf->mem_alloc = malloc;
conf->mem_calloc = calloc;
conf->mem_free = free;
}
/**
* Creates a new empty list and returns a status code.
*
* @param[out] out Pointer to a CC_SList that is being created.
*
* @return CC_OK if the creation was successful, or CC_ERR_ALLOC if the
* memory allocation for the new CC_SList structure failed.
*/
enum cc_stat cc_slist_new(CC_SList **out)
{
CC_SListConf conf;
cc_slist_conf_init(&conf);
return cc_slist_new_conf(&conf, out);
}
/**
* Creates a new empty CC_SList based on the specified CC_SListConf struct and
* returns a status code.
*
* The CC_SList is allocated using the allocators specified in the CC_SListConf
* struct. The allocation may fail if the underlying allocator fails.
*
* @param[in] conf CC_SList configuration struct. All fields must be initialized
* to appropriate values.
*
* @param[out] out Pointer to a CC_SList that is being createdo
*
* @return CC_OK if the creation was successful, or CC_ERR_ALLOC if the
* memory allocation for the new CC_SList structure failed.
*/
enum cc_stat cc_slist_new_conf(CC_SListConf const * const conf, CC_SList **out)
{
CC_SList *list = conf->mem_calloc(1, sizeof(CC_SList));
if (!list)
return CC_ERR_ALLOC;
list->mem_alloc = conf->mem_alloc;
list->mem_calloc = conf->mem_calloc;
list->mem_free = conf->mem_free;
*out = list;
return CC_OK;
}
/**
* Destroys the list structure, but leaves the data that is holds intact.
*
* @param[in] list CC_SList that is to be destroyed
*/
void cc_slist_destroy(CC_SList *list)
{
cc_slist_remove_all(list);
list->mem_free(list);
}
/**
* Destroys the list structure along with all the data it holds.
*
* @note
* This function should not be called on a list that has some of it's elements
* allocated on the stack.
*
* @param[in] list CC_SList that is to be destroyed
*/
void cc_slist_destroy_cb(CC_SList *list, void (*cb) (void*))
{
cc_slist_remove_all_cb(list, cb);
list->mem_free(list);
}
/**
* Adds a new element to the list. The element is appended to the list making it
* the last element of the list.
*
* @param[in] list CC_SList to which the element is being added
* @param[in] element element that is being added
*
* @return CC_OK if the element was successfully added, or CC_ERR_ALLOC if the
* memory allocation for the new element has failed.
*/
enum cc_stat cc_slist_add(CC_SList *list, void *element)
{
return cc_slist_add_last(list, element);
}
/**
* Prepends a new element to the list (adds a new "head") making it the first
* element of the list.
*
* @param[in] list CC_SList to which the element is being added
* @param[in] element element that is being added
*
* @return CC_OK if the element was successfully added, or CC_ERR_ALLOC if the
* memory allocation for the new element has failed.
*/
enum cc_stat cc_slist_add_first(CC_SList *list, void *element)
{
SNode *node = list->mem_calloc(1, sizeof(SNode));
if (!node)
return CC_ERR_ALLOC;
node->data = element;
if (list->size == 0) {
list->head = node;
list->tail = node;
} else {
node->next = list->head;
list->head = node;
}
list->size++;
return CC_OK;
}
/**
* Appends a new element to the list (adds a new "tail") making it the last
* element of the list.
*
* @param[in] list CC_SList to which the element is being added
* @param[in] element element that is being added
*
* @return CC_OK if the element was successfully added, or CC_ERR_ALLOC if the
* memory allocation for the new element has failed.
*/
enum cc_stat cc_slist_add_last(CC_SList *list, void *element)
{
SNode *node = list->mem_calloc(1, sizeof(SNode));
if (!node)
return CC_ERR_ALLOC;
node->data = element;
if (list->size == 0) {
list->head = node;
list->tail = node;
} else {
list->tail->next = node;
list->tail = node;
}
list->size++;
return CC_OK;
}
/**
* Adds a new element at the specified location in the CC_SList and shifts all
* subsequent elements by one. The index at which the new element is being
* added must be within the bounds of the list.
*
* @note This operation cannot be performed on an empty list.
*
* @param[in] list CC_SList to which this element is being added
* @param[in] element element that is being added
* @param[in] index the position in the list at which the new element is being
* added
*
* @return CC_OK if the element was successfully added, CC_ERR_OUT_OF_RANGE if
* the specified index was not in range, or CC_ERR_ALLOC if the memory
* allocation for the new element failed.
*/
enum cc_stat cc_slist_add_at(CC_SList *list, void *element, size_t index)
{
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list, index, &node, &prev);
if (status != CC_OK)
return status;
SNode *new = list->mem_calloc(1, sizeof(SNode));
if (!new)
return CC_ERR_ALLOC;
new->data = element;
if (!prev) {
new->next = list->head;
list->head = new;
} else {
SNode *tmp = prev->next;
prev->next = new;
new->next = tmp;
}
list->size++;
return CC_OK;
}
/**
* Adds all elements from the second list to the first. The elements from the
* second list are added after the last element of the first list.
*
* @param[in] list1 CC_SList to which the elements are being added
* @param[in] list2 CC_SList from which the elements are being taken
*
* @return CC_OK if the elements where successfully added, or CC_ERR_ALLOC if
* the memory allocation for the new elements failed.
*/
enum cc_stat cc_slist_add_all(CC_SList *list1, CC_SList *list2)
{
if (list2->size == 0)
return CC_OK;
SNode *head = NULL;
SNode *tail = NULL;
if (!link_all_externally(list2, &head, &tail))
return CC_ERR_ALLOC;
if (list1->size == 0) {
list1->head = head;
list1->tail = tail;
} else {
list1->tail->next = head;
list1->tail = tail;
}
list1->size += list2->size;
return CC_OK;
}
/**
* Adds all element from the second list to the first at the specified position
* by shifting all subsequent elements by the size of the second list. The index
* must be within the range of the first list.
*
* @param[in] list1 CC_SList to which the elements are being added
* @param[in] list2 CC_SList from which the elements are being taken
* @param[in] index position in the first list at which the elements should be
* added
*
* @return CC_OK if the elements were successfully added,
* CC_ERR_INDEX_OUT_OF_BOUNDS if the index was out of range, or
* CC_ERR_ALLOC if the memory allocation for the new elements failed.
*/
enum cc_stat cc_slist_add_all_at(CC_SList *list1, CC_SList *list2, size_t index)
{
if (list2->size == 0)
return CC_OK;
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list1, index, &node, &prev);
if (status != CC_OK)
return status;
SNode *head = NULL;
SNode *tail = NULL;
if (!link_all_externally(list2, &head, &tail))
return CC_ERR_ALLOC;
if (!prev) {
tail->next = node;
list1->head = head;
} else {
prev->next = head;
tail->next = node;
}
list1->size += list2->size;
return CC_OK;
}
/**
* Duplicates the structure of the list without directly attaching it to a
* specific list. If the operation fails, everything is cleaned up and false
* is returned to indicate the failure.
*
* @param[in] list the list whose structure is being duplicated
* @param[in, out] h the pointer to which the new head will be attached
* @param[in, out] t the pointer to which the new tail will be attached
*
* @return true if the operation was successful
*/
static bool link_all_externally(CC_SList *list, SNode **h, SNode **t)
{
SNode *ins = list->head;
size_t i;
for (i = 0; i < list->size; i++) {
SNode *new = list->mem_calloc(1, sizeof(SNode));
if (!new) {
while (*h) {
SNode *tmp = (*h)->next;
list->mem_free(*h);
*h = tmp;
}
return false;
}
new->data = ins->data;
if (!*h) {
*h = new;
*t = new;
} else {
(*t)->next = new;
*t = new;
}
ins = ins->next;
}
return true;
}
/**
* Splices the two CC_SLists together by appending the second list to the
* first. This function moves all the elements from the second list into
* the first list, leaving the second list empty.
*
* @param[in] list1 The consumer list to which the elements are moved.
* @param[in] list2 The producer list from which the elements are moved.
*
* @return CC_OK if the elements were successfully moved
*/
enum cc_stat cc_slist_splice(CC_SList *list1, CC_SList *list2)
{
if (list2->size == 0)
return CC_OK;
if (list1->size == 0) {
list1->head = list2->head;
list1->tail = list2->tail;
} else {
list1->tail->next = list2->head;
list1->tail = list2->tail;
}
list1->size += list2->size;
list2->head = NULL;
list2->tail = NULL;
list2->size = 0;
return CC_OK;
}
/**
* Splices the two CC_SLists together at the specified index of the first list.
* this function moves all the elements from the second list into the first
* list at the position specified by the <code>index</code> parameter. After
* this operation the second list will be left empty.
*
* @param[in] list1 the consumer list to which the elements are moved
* @param[in] list2 the producer list from which the elements are moved
* @param[in] index the index in the first list after which the elements
* from the second list should be inserted
*
* @return CC_OK if the elements were successfully moved, CC_ERR_OUT_OF_RANGE if
* the index was not in range,
*/
enum cc_stat cc_slist_splice_at(CC_SList *list1, CC_SList *list2, size_t index)
{
if (list2->size == 0)
return CC_OK;
if (index >= list1->size)
return CC_ERR_OUT_OF_RANGE;
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list1, index, &node, &prev);
if (status != CC_OK)
return status;
splice_between(list1, list2, prev, node);
return CC_OK;
}
/**
* Inserts the second list between the two nodes of the first list. If the left
* node is NULL the head of the second list will be become the head of the first
* list. Similarly if the right node is null the tail of the first list will
* become the tail of the second list.
*
* @param[in, out] l1 the consumer list
* @param[in, out] l2 the producer list
* @param[in] left the node after which the elements are being added
* @param[in] right the node behind which the elements are being added
*/
static INLINE void splice_between(CC_SList *l1, CC_SList *l2, SNode *base, SNode *end)
{
if (!base) {
l2->tail->next = l1->head;
l1->head = l2->head;
} else if (!end) {
l1->tail->next = l2->head;
l1->tail = l2->tail;
} else {
base->next = l2->head;
l2->tail->next = end;
}
l1->size += l2->size;
l2->head = NULL;
l2->tail = NULL;
l2->size = 0;
}
/**
* Removes the first occurrence of the element from the specified CC_SList
* and optionally sets the out parameter to the value of the removed element.
*
* @param[in] list CC_SList from which the element is being removed
* @param[in] element element that is being removed
* @param[out] out Pointer to where the removed value is stored, or NULL
* if it is to be ignored
*
* @return CC_OK if the element was successfully removed, or
* CC_ERR_VALUE_NOT_FOUND if the element was not found.
*/
enum cc_stat cc_slist_remove(CC_SList *list, void *element, void **out)
{
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node(list, element, &node, &prev);
if (status != CC_OK)
return status;
void *val = unlinkn(list, node, prev);
if (out)
*out = val;
return CC_OK;
}
/**
* Removes the element at the specified index and optionally sets
* the out parameter to the value of the removed element. The index
* must be within the bounds of the list.
*
* @param[in] list CC_SList from which the element is being removed
* @param[in] index Index of the element that is being removed. Must be be
* within the index range of the list.
* @param[out] out Pointer to where the removed value is stored,
* or NULL if it is to be ignored
*
* @return CC_OK if the element was successfully removed, or CC_ERR_OUT_OF_RANGE
* if the index was out of range.
*/
enum cc_stat cc_slist_remove_at(CC_SList *list, size_t index, void **out)
{
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list, index, &node, &prev);
if (status != CC_OK)
return status;
void *e = unlinkn(list, node, prev);
if (out)
*out = e;
return CC_OK;
}
/**
* Removes the first (head) element of the list and optionally sets the out
* parameter to the value of the removed element.
*
* @param[in] list CC_SList from which the first element is being removed
* @param[out] out Pointer to where the removed value is stored, or NULL if it is
* to be ignored
*
* @return CC_OK if the element was successfully removed, or CC_ERR_VALUE_NOT_FOUND
* if the list is empty.
*/
enum cc_stat cc_slist_remove_first(CC_SList *list, void **out)
{
if (list->size == 0)
return CC_ERR_VALUE_NOT_FOUND;
void *e = unlinkn(list, list->head, NULL);
if (out)
*out = e;
return CC_OK;
}
/**
* Removes the last (tail) element of the list and optionally sets the out
* parameter to the value of the removed element.
*
* @param[in] list CC_SList from which the last element is being removed
* @param[out] out Pointer to where the removed value is stored, or NULL if it is
* to be ignored
*
* @return CC_OK if the element was successfully removed, or CC_ERR_VALUE_NOT_FOUND
* if the list is empty.
*/
enum cc_stat cc_slist_remove_last(CC_SList *list, void **out)
{
if (list->size == 0)
return CC_ERR_VALUE_NOT_FOUND;
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list, list->size - 1, &node, &prev);
if (status != CC_OK)
return status;
void *e = unlinkn(list, node, prev);
if (out)
*out = e;
return CC_OK;
}
/**
* Removes all elements from the specified list.
*
* @param[in] list CC_SList from which all elements are being removed
*
* @return CC_OK if the elements were successfully removed, or CC_ERR_VALUE_NOT_FOUND
* if the list was already empty.
*/
enum cc_stat cc_slist_remove_all(CC_SList *list)
{
bool unlinked = unlinkn_all(list, NULL);
if (unlinked) {
list->head = NULL;
list->tail = NULL;
return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
}
/**
* Removes and frees all the elements from the specified list.
*
* @note
* This function should not be called on a list that has some of it's elements
* allocated on the stack.
*
* @param[in] list CC_SList from which all the elements are being removed and freed
*
* @return CC_OK if the element were successfully removed and freed, or
* CC_ERR_VALUE_NOT_FOUND if the list was already empty.
*/
enum cc_stat cc_slist_remove_all_cb(CC_SList *list, void (*cb) (void*))
{
bool unlinked = unlinkn_all(list, cb);
if (unlinked) {
list->head = NULL;
list->tail = NULL;
return CC_OK;
}
return CC_ERR_VALUE_NOT_FOUND;
}
/**
* Replaces an element at the specified location and optionally sets the out parameter
* to the value of the replaced element. The specified index must be within the bounds
* of the list.
*
* @param[in] list CC_SList on which this operation is performed
* @param[in] element the replacement element
* @param[in] index index of the element being replaced
* @param[out] out Pointer to where the replaced element is stored, or NULL if
* it is to be ignored
*
* @return CC_OK if the element was successfully replaced, or CC_ERR_OUT_OF_RANGE
* if the index was out of range.
*/
enum cc_stat cc_slist_replace_at(CC_SList *list, void *element, size_t index, void **out)
{
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list, index, &node, &prev);
if (status != CC_OK)
return status;
void *old = node->data;
node->data = element;
if (out)
*out = old;
return CC_OK;
}
/**
* Gets the first element from the specified list and sets the out parameter to
* its value.
*
* @param[in] list CC_SList whose first element is being returned
* @param[in] out Pointer to where the element is stored
*
* @return CC_OK if the element was found, or CC_ERR_VALUE_NOT_FOUND if not.
*/
enum cc_stat cc_slist_get_first(CC_SList *list, void **out)
{
if (list->size == 0)
return CC_ERR_VALUE_NOT_FOUND;
*out = list->head->data;
return CC_OK;
}
/**
* Gets the last element from the specified list and sets the out parameter to
* its value.
*
* @param[in] list CC_SList whose last element is being returned
* @param[out] out Pointer to where the element is stored
*
* @return CC_OK if the element was found, or CC_ERR_VALUE_NOT_FOUND if not.
*/
enum cc_stat cc_slist_get_last(CC_SList *list, void **out)
{
if (list->size == 0)
return CC_ERR_VALUE_NOT_FOUND;
*out = list->tail->data;
return CC_OK;
}
/**
* Gets the list element from the specified index and sets the out parameter to
* its value.
*
* @param[in] list CC_SList from which the element is being returned.
* @param[in] index The index of a list element being returned. The index must
* be within the bound of the list.
* @param[out] out Pointer to where the element is stored
*
* @return CC_OK if the element was found, or CC_ERR_OUT_OF_RANGE if the index
* was out of range.
*/
enum cc_stat cc_slist_get_at(CC_SList *list, size_t index, void **out)
{
SNode *prev = NULL;
SNode *node = NULL;
enum cc_stat status = get_node_at(list, index, &node, &prev);
if (status != CC_OK)
return status;
*out = node->data;
return CC_OK;
}
/**
* Returns the number of elements in the specified CC_SList.
*
* @param[in] list CC_SList whose size is being returned
*
* @return The number of elements contained in the specified CC_SList.
*/
size_t cc_slist_size(CC_SList *list)
{
return list->size;
}
/**
* Reverses the order of elements in the specified list.
*
* @param[in] list CC_SList that is being reversed
*/
void cc_slist_reverse(CC_SList *list)
{
if (list->size == 0 || list->size == 1)
return;
SNode *prev = NULL;
SNode *next = NULL;
SNode *flip = list->head;
list->tail = list->head;
while (flip) {
next = flip->next;
flip->next = prev;
prev = flip;
flip = next;
}
list->head = prev;
}
/**
* Creates a sublist of the specified list. The created sublist contains all
* the elements from the list that are contained between the two indices
* including the elements at the indices. For example if a list contains 5
* elements [5, 6, 7, 8, 9], a sublist from index 1 to 3 will will be a new
* list of length 3, containing [6, 7, 8]. The created sublist is only a copy of
* the original lists structure, meaning the data it points to is not copied.
*
* @note The sublist is allocated using the original lists allocators and also
* inherits the configuration of the original list.
*
* @param[in] list CC_SList from which the sublist is taken
* @param[in] from The beginning index, ie., the first element to be included.
* Must be a positive integer and may not exceed the list size
* or the end index.
* @param[in] to The ending index, ie., the last element to be included. Must
* be a positive integer no greater that the list size and no
* smaller that the beginning index.
* @param[out] out Pointer to where the new sublist is stored.
*
* @return CC_OK if the sublist was successfully created, CC_ERR_INVALID_RANGE
* if the specified index range is invalid, or CC_ERR_ALLOC if the memory allocation
* for the new sublist failed.
*/
enum cc_stat cc_slist_sublist(CC_SList *list, size_t from, size_t to, CC_SList **out)
{
if (from > to || to >= list->size)
return CC_ERR_INVALID_RANGE;
SNode *base = NULL;
SNode *node = NULL;
CC_SList *sub;
enum cc_stat status = cc_slist_new(&sub);
if (status != CC_OK)
return status;
status = get_node_at(list, from, &node, &base);
if (status != CC_OK) {
cc_slist_destroy(sub);
return status;
}
size_t i;
for (i = from; i <= to; i++) {
status = cc_slist_add(sub, node->data);
if (status != CC_OK) {
cc_slist_destroy(sub);
return status;
}
node = node->next;
}
*out = sub;
return CC_OK;
}
/**
* Creates a shallow copy of the specified list. A shallow copy is a copy of the
* list structure. This operation does not copy the actual data that this list
* holds.
*
* @note The new list is allocated using the original lists allocators and also
* inherits the configuration of the original list.
*
* @param[in] list CC_SList to be copied
* @param[out] out Pointer to where the newly created copy is stored
*
* @return CC_OK if the copy was successfully created, or CC_ERR_ALLOC if the
* memory allocation for the copy failed.
*/
enum cc_stat cc_slist_copy_shallow(CC_SList *list, CC_SList **out)
{
CC_SList *copy;
enum cc_stat status = cc_slist_new(©);
if (status != CC_OK)
return status;
SNode *node = list->head;
while (node) {
status = cc_slist_add(copy, node->data);
if (status != CC_OK) {
cc_slist_destroy(copy);
return status;
}
node = node->next;
}
*out = copy;
return CC_OK;
}
/**
* Creates a deep copy of the specified list. This function copies the structure
* of the list along with all the data it holds. The element copying is done
* through the specified copy function that should return a pointer to the copy
* of the element passed to it.
*
* @note The new list is allocated using the original lists allocators and also
* inherits the configuration of the original list.
*
* @param[in] list CC_SList to be copied
* @param[in] cp the copy function that should return a pointer to the copy of
* the data.
* @param[out] out Pointer to where the newly created copy is stored
*
* @return CC_OK if the copy was successfully created, or CC_ERR_ALLOC if the
* memory allocation for the copy failed.
*/
enum cc_stat cc_slist_copy_deep(CC_SList *list, void *(*cp) (void*), CC_SList **out)
{
CC_SList *copy;
enum cc_stat status = cc_slist_new(©);
if (status != CC_OK)
return status;
SNode *node = list->head;
while (node) {
status = cc_slist_add(copy, cp(node->data));
if (status != CC_OK) {
cc_slist_destroy(copy);
return status;
}
node = node->next;
}
*out = copy;
return CC_OK;
}
/**
* Returns an integer representing the number of occurrences of the specified
* element within the CC_SList.
*
* @param[in] list CC_SList on which the search is performed
* @param[in] element element being searched for
*
* @return number of found matches
*/
size_t cc_slist_contains(CC_SList *list, void *element)
{
SNode *node = list->head;
size_t e_count = 0;
while (node) {
if (node->data == element)
e_count++;
node = node->next;
}
return e_count;
}
/**
* Returns the number of occurrences of the value pointed to by <code>element</code>
* within the specified CC_SList.
*
* @param[in] list CC_SList on which the search is performed
* @param[in] element element being searched for
* @param[in] cmp Comparator function which returns 0 if the values passed to it are equal
*
* @return number of occurrences of the value
*/
size_t cc_slist_contains_value(CC_SList *list, void *element, int (*cmp) (const void*, const void*))
{
SNode *node = list->head;
size_t e_count = 0;
while (node) {
if (cmp(node->data, element) == 0)
e_count++;
node = node->next;
}
return e_count;
}
/**
* Gets the index of the specified element. The returned index is the index
* of the first occurrence of the element starting from the beginning of the list.
*
* @param[in] list the CC_SList on which this operation is performed
* @param[in] element the element whose index is being looked up
* @param[out] index Pointer to where the index is stored
*
* @return CC_OK if the index was found, or CC_OUT_OF_RANGE if not.
*/
enum cc_stat cc_slist_index_of(CC_SList *list, void *element, size_t *index)
{
SNode *node = list->head;
size_t i = 0;
while (node) {
if (node->data == element) {
*index = i;
return CC_OK;
}
i++;
node = node->next;
}
return CC_ERR_OUT_OF_RANGE;
}
/**
* Creates an array representation of the specified list. None of the elements
* are copied into the array and thus any modification of the elements within
* the array will affect the list elements as well. The size of the created
* array is the same as the size of the list from which the array was constructed.
*
* @param[in] list CC_SList on which this operation is being performed
* @param[out] out Pointer to where the newly created array is stored
*
* @return CC_OK if the array was successfully created, or CC_ERR_ALLOC if the
* memory allocation for the new array failed.
*/
enum cc_stat cc_slist_to_array(CC_SList *list, void ***out)
{
void **array = list->mem_alloc(list->size * sizeof(void*));
if (!array)
return CC_ERR_ALLOC;
SNode *node = list->head;
size_t i;
for (i = 0; i < list->size; i++) {
array[i] = node->data;
node = node->next;
}
*out = array;
return CC_OK;
}
/**
* Sorts the specified list. This function makes no guaranties that the
* sort will be performed in place or in a stable way.
*
* @note