forked from petervaro/cutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsll.c
2423 lines (2284 loc) · 172 KB
/
csll.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
/* INFO ************************************************************************
** **
** cutils **
** ====== **
** **
** Modern and Lightweight C Utilities **
** Version: 0.8.96.219 (20140908) **
** **
** File: csll.c **
** **
** For more information about the project, visit <http://www.cutils.org>. **
** Copyright (C) 2014 Peter Varo **
** **
** This program is free software: you can redistribute it and/or modify it **
** under the terms of the GNU General Public License as published by the **
** Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program 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 General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program, most likely a file in the root directory, **
** called 'LICENSE'. If not, see <http://www.gnu.org/licenses>. **
** **
************************************************************************ INFO */
#include <stddef.h> /* ptrdiff_t */
#include <string.h> /* strlen() */
#include "internal/fmtc.h" /* cutils_fmtc_repr */
#include "internal/fcmp.h" /* cutils_fcmp_compare */
#include <stdio.h> /* FILE, snprintf() */
#include <stdlib.h> /* size_t, malloc(), free(), labs() */
#include <string.h> /* memcpy() */
/* If 'optimised' or the 'exceptions are not available' */
#if defined CSLL_OPT
#define cutils_cexc_raise(msg, len)
#else
#include "cexc.h"
#include "internal/defs.h"
#endif
/*----------------------------------------------------------------------------*/
/* Exception messages */
#undef TYPE_REPR
#define TYPE_REPR "SinglyLinkedList"
#undef SUBTYPE_REPR
#define SUBTYPE_REPR TYPE_REPR "_iterator"
/*----------------------------------------------------------------------------*/
/* Shorthands for this source */
#undef _concat_underscore
#undef concat_underscore
#undef LINKED_LIST
#undef METHOD
#undef SUPPORT
#undef SUPPORT_METHOD
#define _concat_underscore(token1, token2) token1##_##token2
#define concat_underscore(token1, token2) _concat_underscore(token1, token2)
#define LINKED_LIST cutils_csll_SinglyLinkedList_void_ptr
#define METHOD(func) concat_underscore(LINKED_LIST, func)
#define SUPPORT(type) concat_underscore(LINKED_LIST, type)
#define SUPPORT_METHOD(type, func) concat_underscore(LINKED_LIST, type##_##func)
/*----------------------------------------------------------------------------*/
/* Support type */
typedef struct sll_node
{
struct sll_node *next; /* Pointer to next node (link) */
char data[]; /* Internally stored data */
} SLLNode;
/*----------------------------------------------------------------------------*/
/* Base type */
typedef struct
{
size_t length; /* Number of items in the list */
SLLNode *head; /* Pointer to first node */
SLLNode *tail; /* Pointer to last node */
} LINKED_LIST;
/*----------------------------------------------------------------------------*/
/* Iterator type (doubly linked node) */
typedef struct
{
LINKED_LIST *list;
SLLNode *prev; /* Pointer to previous node */
SLLNode *curr; /* Pointer to current node */
SLLNode *next; /* Pointer to next node (link) */
} SLLIterator;
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Iterator type (public alias) */
typedef SLLIterator SUPPORT(iterator);
/*----------------------------------------------------------------------------*/
static inline void
__csll_node_ins(LINKED_LIST *linlist,
SLLNode *node_prev, /* node before sub-sequence */
SLLNode *node_head, /* sub-sequence start */
SLLNode *node_tail, /* sub-sequence end */
SLLNode *node_next) /* node after sub-sequence */
{
/* If head will be nth item */
if (node_prev)
node_prev->next = node_head;
/* If head will be the first item */
else
linlist->head = node_head;
/* If tail will be nth item */
if (node_next)
node_tail->next = node_next;
/* If tail will be the last item */
else
{
linlist->tail = node_tail;
node_tail->next = NULL;
}
}
/*----------------------------------------------------------------------------*/
static inline void
__csll_node_pop(LINKED_LIST *linlist,
SLLNode *node_prev, /* node before sub-sequence */
SLLNode *node_next) /* node after sub-sequence */
{
/* If current node was nth item */
if (node_prev)
node_prev->next = node_next;
/* If current node was first item */
else
linlist->head = node_next;
/* If current node was last item */
if (!node_next)
linlist->tail = node_prev;
}
/*----------------------------------------------------------------------------*/
static inline bool
__csll_node_new(LINKED_LIST *linlist,
const char *exception_msg,
size_t exception_msg_size,
SLLNode *node_prev,
SLLNode *node_next,
size_t item_size,
size_t item_count,
char *items)
{
/* Node pointers */
SLLNode *node_head, *node_tail, *node_temp;
/* Allocate space for the first new node */
node_tail = node_head = malloc(sizeof(SLLNode) + item_size);
if (!node_head)
{
cutils_cexc_raise(exception_msg, exception_msg_size);
return false; /* Internal allocation failed */
}
/* Store item in first node */
memcpy(node_head->data, items, item_size);
/* Allocate space for each of the remaining nodes */
for (size_t i=1; i<item_count; i++)
{
/* Allocate space for a new node */
node_temp = malloc(sizeof(SLLNode) + item_size);
if (!node_temp)
{
cutils_cexc_raise(exception_msg, exception_msg_size);
return false; /* Internal allocation failed */
}
node_tail->next = node_temp;
memcpy(node_temp->data, items + i*item_size, item_size);
node_tail = node_temp;
}
/* Insert new nodes */
__csll_node_ins(linlist, node_prev, node_head, node_tail, node_next);
/* Update item counter */
linlist->length += item_count;
return true; /* Items successfully added */
}
/*----------------------------------------------------------------------------*/
bool
METHOD(new)(LINKED_LIST **linlist,
size_t item_size,
size_t count,
void *source)
{
/* Allocate space for list */
LINKED_LIST *_linlist = malloc(sizeof(LINKED_LIST));
if (!_linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_ALLOC_FAIL(TYPE_REPR, "new")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
*linlist = NULL;
return false; /* Internal allocation failed */
}
/* Set list to empty */
_linlist->head = NULL;
_linlist->tail = NULL;
_linlist->length = 0;
/* Assign newly created list to passed pointer */
*linlist = _linlist;
#ifndef CSLL_OPT
if (count && source)
{
#endif
if (!__csll_node_new(_linlist,
/* message */EXCEPTION_MSG,
/* message length */sizeof EXCEPTION_MSG,
/* previous node */NULL,
/* next node */NULL,
/* item size */item_size,
/* item count */count,
/* data if items */(char *)source))
return false; /* Internal allocation failed */
#undef EXCEPTION_MSG
#ifndef CSLL_OPT
}
#endif
return true; /* The list have been successfully allocated */
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool
METHOD(new_default1)(LINKED_LIST **linlist)
{
return METHOD(new)(linlist, 0, 0, NULL);
}
/*----------------------------------------------------------------------------*/
void
METHOD(del)(LINKED_LIST *linlist)
{
#ifndef CSLL_OPT
if (!linlist)
{
#endif
/* Remove all items */
SLLNode *next,
*node = linlist->head;
while (node)
{
next = node->next;
free(node);
node = next;
}
#ifndef CSLL_OPT
}
#endif
/* Delete base itself */
free(linlist);
}
/*----------------------------------------------------------------------------*/
size_t
METHOD(len)(LINKED_LIST *linlist)
{
#ifndef CSLL_OPT
/* Not initialised */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "len")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Cannot operate on nothing */
}
#endif
return linlist->length;
}
/*----------------------------------------------------------------------------*/
void
METHOD(clear)(LINKED_LIST *linlist)
{
#ifndef CSLL_OPT
/* Not initialised */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "clear")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return; /* Cannot operate on nothing */
}
#endif
/* Remove all items */
SLLNode *next,
*node = linlist->head;
while (node)
{
next = node->next;
free(node);
node = next;
}
/* Set list empty */
linlist->length = 0;
linlist->head = NULL;
linlist->tail = NULL;
}
/*----------------------------------------------------------------------------*/
bool
METHOD(swap)(LINKED_LIST *linlist,
size_t index1,
size_t index2,
size_t count)
{
size_t length;
#ifndef CSLL_OPT
/* If pointer to list is pointing to NULL */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "swap")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Cannot operate on nothing */
}
#endif /* CSLL_OPT */
/* No swapping needed */
if ((index1 == index2) || !count)
{
return true; /* Successfully did nothing */
}
/* If list empty */
else if (!(length = linlist->length))
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_EMPTY(TYPE_REPR, "swap")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return true; /* Successfully did nothing */
}
#ifndef CSLL_OPT
/* If index1 or index2 out of range */
else if (index1 >= length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "swap", "2nd", index1)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Not valid index */
}
else if (index2 >= length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "swap", "3rd", index2)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Not valid index */
}
/* If blocks are overlapping */
if (labs(index1 - index2) < count)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_OVERLAP(TYPE_REPR, "swap")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Not valid count */
}
/* If blocks are out of bounds */
if ((index1 + count) > length || (index2 + count) > length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "swap", "4th", count)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Not valid count */
}
#endif /* CSLL_OPT */
SLLNode *node1_prev, /* node before node at index1 */
*node1_head, /* node at index1 */
*node1_tail, /* node at index1 + (count - 1) */
*node1_next, /* node after node at index1 + (count - 1) */
*node2_prev, /* node before node at index2 */
*node2_head, /* node at index2 */
*node2_tail, /* node at index2 + (count - 1) */
*node2_next, /* node after node at index2 + (count - 1) */
*temp1 = linlist->head,
*temp2;
size_t counter = 0;
bool found_head = false,
found_tail = false;
/* CORE FUNCTIONALITY
If one of the indices is 0 */
if (!index1 || !index2)
{
/* Start counter */
counter = count;
/* Ste the first node */
node1_prev = NULL;
node1_head = temp1;
found_head = true;
}
/* Start iterate through the list */
for (size_t i=1;; i++)
{
/* Get next node too if current node is not NULL */
if (temp1)
temp2 = temp1->next;
else
temp2 = NULL;
/* If looking for the end of the sub-sequence */
if (counter && !--counter)
{
/* If already found one */
if (found_tail)
{
node2_tail = temp1;
node2_next = temp2;
break;
}
/* If this is the first match */
node1_tail = temp1;
node1_next = temp2;
found_tail = true;
}
/* If one of the indices is a match */
if (index1 == i || index2 == i)
{
/* Start counter */
counter = count;
/* If already found one */
if (found_head)
{
node2_prev = temp1;
node2_head = temp2;
}
else
/* If this is the first match */
{
node1_prev = temp1;
node1_head = temp2;
found_head = true;
}
}
/* Set next node */
temp1 = temp2;
}
/* If sequences are direct neighbours */
if (node1_tail == node2_prev || node2_tail == node1_prev)
{
__csll_node_pop(linlist, node1_prev, node1_next);
__csll_node_ins(linlist, node2_tail, node1_head, node1_tail, node2_next);
}
/* If at least there is one node between sequences */
else
{
__csll_node_ins(linlist, node2_prev, node1_head, node1_tail, node2_next);
__csll_node_ins(linlist, node1_prev, node2_head, node2_tail, node1_next);
}
return true; /* Swapping was successful */
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool
METHOD(swap_default3)(LINKED_LIST *linlist,
size_t index1,
size_t index2)
{
return METHOD(swap)(linlist, index1, index2, 1);
}
/*----------------------------------------------------------------------------*/
bool
METHOD(reverse)(LINKED_LIST *linlist)
{
/* If pointer to list is pointing to NULL */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "reverse")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Cannot operate on nothing */
}
/* If list is empty */
if (!linlist->length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_EMPTY(TYPE_REPR, "reverse")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return true; /* Successfully did nothing */
}
/* CORE FUNCTIONALITY
Create/get/set essential values */
SLLNode *node_prev = linlist->head,
*node_curr = node_prev->next,
*node_next;
/* Swap items */
node_prev->next = NULL;
while (node_curr)
{
/* Get next node */
node_next = node_curr->next;
/* Reverse order */
node_curr->next = node_prev;
node_prev = node_curr;
/* Set next node */
node_curr = node_next;
}
/* Set head and tail pointers */
linlist->tail = linlist->head;
linlist->head = node_prev;
return true; /* Successfully reversed */
}
/*----------------------------------------------------------------------------*/
bool
METHOD(append)(LINKED_LIST *linlist,
size_t item_size,
size_t count,
void *source)
{
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "append")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Cannot operate on nothing */
}
else if (!source)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_NULL(TYPE_REPR, "append", "3rd", source)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return true; /* Successfully did nothing */
}
else
#ifndef CSLL_OPT
if (count)
{
#endif
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_ALLOC_FAIL(TYPE_REPR, "append")
if (!__csll_node_new(linlist,
/* message */EXCEPTION_MSG,
/* message length */sizeof EXCEPTION_MSG,
/* previous node */linlist->tail,
/* next node */NULL,
/* item size */item_size,
/* item count */count,
/* data if items */(char *)source))
return false; /* Internal allocation failed */
#ifndef CSLL_OPT
}
#endif
return true; /* Successfully added new items or nothing */
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool
METHOD(append_default3)(LINKED_LIST *linlist,
size_t item_size,
void *source)
{
return METHOD(append)(linlist, item_size, 1, source);
}
/*----------------------------------------------------------------------------*/
bool
METHOD(push)(LINKED_LIST *linlist,
size_t index,
size_t item_size,
size_t count,
void *source)
{
#ifndef CSLL_OPT
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "append")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return false; /* Cannot operate on nothing */
}
else if (!source)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_NULL(TYPE_REPR, "append", "3rd", source)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return true; /* Successfully did nothing */
}
else if (count)
{
#endif /* CSLL_OPT */
/* CORE FUNCTIONALITY */
SLLNode *node_prev,
*node_next;
/* If insert as first item(s) */
if (!index)
{
node_prev = NULL;
node_next = linlist->head;
}
/* If insert as nth item(s) */
else
{
node_prev = linlist->head;
for (size_t i=1;; i++)
{
/* Get next node */
if (node_prev)
node_next = node_prev->next;
else
node_next = NULL;
/* If index is a match */
if (index == i)
break;
/* Set next node */
node_prev = node_next;
}
}
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_ALLOC_FAIL(TYPE_REPR, "push")
if (!__csll_node_new(linlist,
/* message */EXCEPTION_MSG,
/* message length */sizeof EXCEPTION_MSG,
/* previous node */node_prev,
/* next node */node_next,
/* item size */item_size,
/* item count */count,
/* data if items */(char *)source))
return false; /* Internal allocation failed */
#ifndef CSLL_OPT
}
#endif
return true; /* Successfully added new items or nothing */
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool
METHOD(push_default4)(LINKED_LIST *linlist,
size_t index,
size_t item_size,
void *source)
{
return METHOD(push)(linlist, index, item_size, 1, source);
}
/*----------------------------------------------------------------------------*/
size_t
METHOD(pull)(LINKED_LIST *linlist,
size_t index,
size_t count)
{
size_t length;
#ifndef CSLL_OPT
/* Not initialised */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "pull")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Cannot operate on nothing */
}
/* Nothing to remove */
else if (!count)
{
return 0; /* Successfully removed nothing */
}
/* Empty list */
else if (!(length = linlist->length))
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_EMPTY(TYPE_REPR, "pull")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
/* Out of range */
else if (index >= length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "pull", "2nd", index)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
#else
/* Nothing to remove or Empty array */
if (!count || !(length = linlist->length)) return 0;
#endif /* CSLL_OPT */
/* CORE FUNCTIONALITY
if sub-sequence size out of bound */
else if ((index + count) >= length)
count = length - index;
/* Create/get/set essential values */
SLLNode *node_prev,
*node_curr,
*node_next,
*temp = linlist->head;
size_t counter = 0;
/* If index is 0 */
if (!index)
{
/* Start counting */
counter = count;
/* Set start and first item */
node_prev = NULL;
node_curr = temp;
}
/* Start iterate through the list */
for (size_t i=1;; i++)
{
/* Get next node too if current node is not NULL */
if (temp)
node_next = temp->next;
else
node_next = NULL;
/* If nth or last node of the sub-sequence */
if (counter)
{
/* Free node and its data from memory */
free(node_curr);
/* If end of the sub-sequence */
if (!--counter)
{
/* Pop out the already deleted nodes */
__csll_node_pop(linlist, node_prev, node_next);
linlist->length -= count;
return count;
}
/* Get next node */
node_curr = temp = node_next;
continue;
}
/* If index is a match */
if (index == i)
{
/* Start counting */
counter = count;
/* Set start and first item */
node_prev = temp;
node_curr = node_next;
}
/* Set next node */
temp = node_next;
}
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
size_t
METHOD(pull_default2)(LINKED_LIST *linlist,
size_t index)
{
return METHOD(pull)(linlist, index, 1);
}
/*----------------------------------------------------------------------------*/
/* TODO: pop() has only three extra statements compared to pull()
1) else if (!destination) ...
2) char *dest = (char *)destination;
3) memcpy(dest + (count - counter)*item_size, node_curr->data, item_size);
try to "merge" these two together -- with a macro?
also sub() has the same lines except no popping and freeing
(and their error message "names" differes of course) */
size_t
METHOD(pop)(LINKED_LIST *linlist,
size_t index,
size_t item_size,
size_t count,
void *destination)
{
size_t length;
#ifndef CSLL_OPT
/* Not initialised */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "pop")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Cannot operate on nothing */
}
/* Nothing to remove */
else if (!count)
{
return 0; /* Successfully removed nothing */
}
/* Invalid destination */
else if (!destination)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_NULL(TYPE_REPR, "pop", "4th", destination)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully popped nothing */
}
/* Empty list */
else if (!(length = linlist->length))
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_EMPTY(TYPE_REPR, "pop")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
/* Out of range */
else if (index >= length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "pop", "2nd", index)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
#else
/* Nothing to remove or Empty array */
if (!count || !(length = linlist->length)) return 0;
#endif /* CSLL_OPT */
/* CORE FUNCTIONALITY
if sub-sequence size out of bound */
else if ((index + count) >= length)
count = length - index;
/* Create/get/set essential values */
SLLNode *node_prev,
*node_curr,
*node_next,
*temp = linlist->head;
size_t counter = 0;
char *_destination = (char *)destination;
/* If index is 0 */
if (!index)
{
/* Start counting */
counter = count;
/* Set start and first item */
node_prev = NULL;
node_curr = temp;
}
/* Start iterate through the list */
for (size_t i=1;; i++)
{
/* Get next node too if current node is not NULL */
if (temp)
node_next = temp->next;
else
node_next = NULL;
/* If nth or last node of the sub-sequence */
if (counter)
{
/* Free node and its data from memory */
memcpy(_destination + (count - counter)*item_size,
node_curr->data, item_size);
free(node_curr);
/* If end of the sub-sequence */
if (!--counter)
{
/* Pop out the already deleted nodes */
__csll_node_pop(linlist, node_prev, node_next);
linlist->length -= count;
return count;
}
/* Set next node */
node_curr = temp = node_next;
continue;
}
/* If index is a match */
if (index == i)
{
/* Start counting */
counter = count;
/* Set start and first item */
node_prev = temp;
node_curr = node_next;
}
/* Set next node */
temp = node_next;
}
}
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
size_t
METHOD(pop_default4)(LINKED_LIST *linlist,
size_t index,
size_t item_size,
void *destination)
{
return METHOD(pop)(linlist, index, item_size, 1, destination);
}
/*----------------------------------------------------------------------------*/
size_t
METHOD(sub)(LINKED_LIST *linlist,
size_t index,
size_t item_size,
size_t count,
void *destination)
{
size_t length;
#ifndef CSLL_OPT
/* Not initialised */
if (!linlist)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_NULL_POINTER(TYPE_REPR, "sub")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Cannot operate on nothing */
}
/* Nothing to remove */
else if (!count)
{
return 0; /* Successfully removed nothing */
}
/* Invalid destination */
else if (!destination)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_NULL(TYPE_REPR, "sub", "4th", destination)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully popped nothing */
}
/* Empty list */
else if (!(length = linlist->length))
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG EXCEPTION_MESSAGE_EMPTY(TYPE_REPR, "sub")
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
/* Out of range */
else if (index >= length)
{
#undef EXCEPTION_MSG
#define EXCEPTION_MSG \
EXCEPTION_MESSAGE_ARGUMENT_OUTOF(TYPE_REPR, "sub", "2nd", index)
cutils_cexc_raise(EXCEPTION_MSG, sizeof EXCEPTION_MSG);
return 0; /* Successfully removed nothing */
}
#else
/* Nothing to remove or Empty array */
if (!count || !(length = linlist->length)) return 0;
#endif /* CSLL_OPT */
/* CORE FUNCTIONALITY
if sub-sequence size out of bound */
else if ((index + count) >= length)
count = length - index;
/* Create/get/set essential values */
SLLNode *node_prev,
*node_curr,
*node_next,
*temp = linlist->head;
size_t counter = 0;
char *_destination = (char *)destination;
/* If index is 0 */
if (!index)
{