-
Notifications
You must be signed in to change notification settings - Fork 0
/
libdll.h
1576 lines (1351 loc) · 42.1 KB
/
libdll.h
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 libdll.h
*
* \brief Single-header realization of Double Linked List API based on C++11 std::list on
* C
*
* Copyright (C) 2020 Taras Maliukh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef LIBDLL_H
#define LIBDLL_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
//
// ----------------------------
// libdll specifications and macroses
// ----------------------------
//
#ifdef LIBDLL_UNSAFE_USAGE
# undef LIBDLL_UNSAFE_USAGE
/**
* Removing all nullability checks in all functions.
*
* \warning May cause better performance but undefined behavior as well.
*/
# define LIBDLL_UNSAFE_USAGE 1
#endif /* LIBDLL_UNSAFE_USAGE */
/**
* Use this macros as \c destructor argument for #dll_new_obj if you do not
* allocate anything inside the \c data , but the \c data itself was allocated before you
* put in #dll_new_obj .
*/
#define LIBDLL_DESTRUCTOR_DEFAULT ((dll_callback_destructor_fn_t)-0x1UL)
/**
* Use this macros as \c destructor argument for #dll_new_obj if you don't need
* to free any memory in list-objects \c data or even pointer to \c data itself.
*/
#define LIBDLL_DESTRUCTOR_NULL ((dll_callback_destructor_fn_t)NULL)
//
// ----------------------------
// Macros definitions
// ----------------------------
//
#ifdef __glibc_likely
# define __dll_likely(_cond) __glibc_likely(_cond)
#else
# define __dll_likely(_cond) (_cond)
#endif
#ifdef __glibc_unlikely
# define __dll_unlikely(_cond) __glibc_unlikely(_cond)
#else
# define __dll_unlikely(_cond) (_cond)
#endif
#define __dll_inline static inline
//
// ----------------------------
// Data structure definitions
// ----------------------------
//
/**
* A callback typedef for comparing \c data from 2 list-objects, or list-object
* with any other given data by user, or example: #dll_remove , #dll_sort or #dll_map .
*
* \param obj_data list-object data.
* \param other other list-object or user data.
* \param index an index of current obj_data.
*
* \return comparing result value.
*/
typedef ssize_t (*dll_callback_fn_t)(void * restrict obj_data,
void * restrict other,
size_t index);
/**
* An extended version of #dll_callback_fn_t
*
* \param obj_a list-object data.
* \param obj_b other list-object or user data.
* \param any an any data pointer.
* \param index an index of current obj_data.
*
* \return comparing result value.
*/
typedef ssize_t (*dll_callback_ext_fn_t)(void * restrict obj_a,
void * restrict obj_b,
void * restrict any,
size_t index);
/**
* A destructor callback typedef for list-object desctructor function.
*
* \param obj_data list-object data.
*/
typedef void (*dll_callback_destructor_fn_t)(void * restrict obj_data);
/**
* A callback function for #dll_map.
*
* \param obj_data list-object data.
* \param any an any additional data.
* \param index index of current list-object passed to this callback, starts from 1.
*/
typedef void * (*dll_callback_mapper_fn_t)(void * restrict obj_data,
void * restrict any,
size_t index);
/**
* A list-object structure.
*
* \typedef dll_obj_t
*/
typedef struct __s_dll_obj {
/** a pointer to next list-object. */
struct __s_dll_obj * restrict next;
/** a pointer to previous list-object. */
struct __s_dll_obj * restrict prev;
/** an any user defined data. */
void * restrict data;
/**
* \destructor_description
*/
dll_callback_destructor_fn_t destructor;
/** a \c data size. */
size_t size;
} dll_obj_t;
/**
* A doubly linked list structure.
*
* \typedef dll_t
*/
typedef struct {
/** a head of list. */
dll_obj_t * restrict head;
/** a tail of list. */
dll_obj_t * restrict tail;
/** a counter of list-objects in list. */
size_t objs_count;
} dll_t;
/**
* A list-object iterator structure.
*
* \typedef dll_iterator_t
*/
typedef struct {
/** a current iterator list-object */
dll_obj_t * restrict __obj;
/** an original list, for which iterator was created */
dll_t * restrict __dll;
/** current list-object index. */
size_t __index;
} dll_iterator_t;
//
// ----------------------------
// Function prototypes
// ----------------------------
//
/**
* \b Creates a new and empty list with this function.
*
* \return allocated memory for new list, \c NULL otherwise
*/
__dll_inline dll_t * dll_new(void);
/**
* \b Creates a new from given parameters.
*
* \attention Strongly recommended to set-up \p destructor if you have allocated memory
* inside \p data .
*
* \param data any data you want to put inside of list.
* \param size a size of \p data .
* \param destructor \destructor_description
*
* \return a new list-object, \c NULL otherwise
*/
__dll_inline dll_obj_t * dll_new_obj(void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor);
/**
* \b Pushes a provided list-object to front of given \p dll list.
*
* \param dll destination list.
* \param obj list-object.
*
* \return \p obj on success, \c NULL otherwise
*/
__dll_inline dll_obj_t * dll_push_front(dll_t * restrict dll, dll_obj_t * restrict obj);
/**
* \b Pushes a provided obj list-object to end of given \p dll list.
*
* \param dll destination list.
* \param obj list-object.
*
* \return \p obj on success, \c NULL otherwise
*/
__dll_inline dll_obj_t * dll_push_back(dll_t * restrict dll, dll_obj_t * restrict obj);
/**
* \b Creates a new list-object via #dll_new_obj with given parameters and
* pushing it in front of \p dll list.
*
* \param dll destination list.
* \param data any data.
* \param size size of \p data.
* \param destructor \destructor_description
*
* \return a new list-object, \c NULL otherwise
*/
__dll_inline dll_obj_t * dll_emplace_front(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor);
/**
* \b Creates a new list-object via #dll_new_obj with given parameters and
* pushing it at the end of \p dll list.
*
* \param dll destination list.
* \param data any data.
* \param size size of \p data.
* \param destructor \destructor_description
*
* \return a new created list-object.
*/
__dll_inline dll_obj_t * dll_emplace_back(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor);
/**
* \b Unlinks the first(head) list-object from the list.
*
* \param dll list.
*
* \return unlinked list-object
*/
__dll_inline dll_obj_t * dll_pop_front(dll_t * restrict dll);
/**
* \b Unlinks the last(tail) list-object from the list.
*
* \param dll list.
*
* \return unlinked list-object
*/
__dll_inline dll_obj_t * dll_pop_back(dll_t * restrict dll);
/**
* \b Erases all elements from the \p dll list.
*
* \attention After this call, #dll_size returns zero. Any pointers referring to contained
* elements can be invalid if you specified a \c destructor callback-function for each
* individual list-object, alsot lost memory leaks errors can occur as well if you don't
* specified a \c destructor function for each individual list-object but memory inside of
* this list-object was allocated.
*
* \param dll list.
*
* \return \c true on success, \c false otherwise
*/
__dll_inline bool dll_clear(dll_t * restrict dll);
/**
* \b Inserts a provided list-object \p obj on the specified location \p pos in
* the list \p dll .
*
* \param dll list to where new object will be injected.
* \param obj list-object to be inserted.
* \param pos injection position where list-object will be inserted
*
* \note \p pos must starts from 0 to #dll_size .
*
* \return inserted list-object or NULL if something is wrong.
*/
__dll_inline dll_obj_t *
dll_insert(dll_t * restrict dll, dll_obj_t * restrict obj, size_t pos);
/**
* \b Inserts a new list-object created via #dll_new_obj on the specified
* location \p pos in the list \p dll.
*
* \param dll list.
* \param data any data.
* \param size size of \p data.
* \param destructor \destructor_description
*
* \return inserted list-object or NULL if something is wrong.
*/
__dll_inline dll_obj_t * dll_emplace(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor,
size_t pos);
/**
* \b Erases the specified range of list-objects from the list \p dll .
*
* \param dll list .
* \param start start of the range.
* \param end end of the range.
*
* \note If \p end is zero or bigger than \p start then only 1
* list-object at \c start position will be erased.
*
* \note \p start and \p end must starts from 1.
*
* \attention \c NULL returns in cases if: \p dll list has no objects or \p dll is
* \c NULL ; \c start bigger than #dll_size;
*
* \return a last list-object after deleting range or NULL.
*/
__dll_inline dll_obj_t * dll_erase(dll_t * restrict dll, size_t start, size_t end);
/**
* \b Going throught all the list-objects in \p dll list and calls a
* provided \p fn callback-function for each list-object.
*
* \note Function works only if \p dll and \p fn is not a \c NULL .
*
* \param dll list.
* \param fn callback with only 1 argument - `void*` to any data inside list-object.
* \param any any data to be passed to the \p fn second argument.
*
* \return \c true on success, \c false otherwise.
*/
__dll_inline bool
dll_foreach(const dll_t * restrict dll, dll_callback_fn_t fn, void * restrict any);
/**
* \b Creates a new #dll_iterator_t iterator for provided \p dll list.
*
* \param dll list.
*
* \return a new iterator
*/
__dll_inline dll_iterator_t dll_iterator(dll_t * restrict const dll);
/**
* \b Access the data inside provided #dll_iterator_t \p it .
*
* \note returns \c NULL if iterator at the end or at the start of list.
*
* \param it an iterator.
*
* \return list-object
*/
__dll_inline dll_obj_t * dll_iterator_get_obj(const dll_iterator_t * restrict const it);
/**
* \b Access any data from list-object inside provided iterator #dll_iterator_t \p it .
*
* \note returns \c NULL if iterator at the end or at the start of list.
*
* \param it an iterator.
*
* \return any data
*/
__dll_inline void * dll_iterator_get_data(const dll_iterator_t * restrict const it);
/**
* \b Access current index of iterator #dll_iterator \p it .
*
* \note Returns \c ~0UL if iterator is \c NULL .
*
* \param it an iterator.
*
* \return unsigned integer represents current iterator index
*/
__dll_inline size_t dll_iterator_get_index(const dll_iterator_t * restrict const it);
/**
* \b Replaces \c data inside the iterator #dll_iterator_t \p it .
*
* \note \b Replacing data inside the iterator means replacing it at the original list for
* which iterator was created as well.
*
* \param it an iterator.
* \param destructor \destructor_description
*
* \return any previous data
*/
__dll_inline void * dll_iterator_set_data(const dll_iterator_t * restrict const it,
void * restrict data,
dll_callback_destructor_fn_t destructor);
/**
* \b Moves the #dll_iterator_t to next list-object.
*
* \return true while not meets the end of list.
*/
__dll_inline bool dll_next(dll_iterator_t * restrict const it);
/**
* \b Moves the #dll_iterator_t to previous list-object.
*
* \return true while not meets a start of list.
*/
__dll_inline bool dll_prev(dll_iterator_t * restrict const it);
/**
* **Access the first**(head) list-object in \p dll list as an
* #dll_iterator_t iterator.
*
* \note \b Equlas to #dll_iterator function.
*
* \param dll list.
*
* \return iterator at the begining of list.
*/
__dll_inline dll_iterator_t dll_front(dll_t * restrict dll);
/**
* **Access the last**(tail) list-object in \p dll list as an
* #dll_iterator_t iterator.
*
* \param dll list.
*
* \return iterator at the end of list.
*/
__dll_inline dll_iterator_t dll_back(dll_t * restrict dll);
/**
* \b Checks whether the list \p dll is empty or not.
*
* \param dll list.
*
* \return \c true if the list is empty, \c false otherwise.
*/
__dll_inline bool dll_empty(const dll_t * restrict dll);
/**
* \b Get the count of elements in the provided list \p dll.
*
* \param dll list.
*
* \return count of elements in the list.
*/
__dll_inline size_t dll_size(const dll_t * restrict dll);
/**
* \b Get the data inside provided list-object \p obj.
*
* \param obj a list-object.
*
* \return any data.
*/
__dll_inline void * dll_obj_get_data(const dll_obj_t * restrict const obj);
/**
* \b Merges two lists into a \p dst list.
*
* \note After merging \p dst list will be sorted if you specifed a \p fn_sort function.
*
* \attentnion The \p src list will be clear after merging it to the \p dst list.
*
* \param dst destination list.
* \param src source list.
* \param fn_sort after merging sorter callback.
*
* \return \c true if lists succesfly merged and src cleared, as well as sorted(optional),
* otherwise \c false.
*/
__dll_inline bool dll_merge(dll_t * restrict dst,
dll_t * restrict src,
dll_callback_ext_fn_t fn_sort,
void * fn_sort_any);
/**
* \b Moves list-objects from \p src to \p dst.
*
* \param dst destination list.
* \param src second list. Full or part of this list will be clear after applying this
* function.
* \param dst_pos to what position in list \p dst - list \p src will be transfered.
* \param src_start start position of list \p src from which list-objects will be
* transefered to \p dst .
* \param src_end end position of transfering from \p src list.
*
* \exception If \c dst_pos , \c src_start , and \c src_end are zero, OR if \p dst_pos and
* \p src_start at the end of lists \p dst and \p src when \p src_end is 0 - then
* \p src will be merged at the end of \p dst via #dll_merge .
*
* \attention \p dst_pos , \p src_start ,and \p src_end must starts from 1
*
* \warning \c false returns in case if: \p src_end less than or equal to \p src_start ;
* \p src list is empty; \p dst or \p src is \c NULL .
*
* \return \c true on success, \c false otherwise
*/
__dll_inline bool dll_splice(dll_t * restrict const dst,
dll_t * restrict const src,
size_t dst_pos,
size_t src_start,
size_t src_end);
/**
* \b Removes all list-objects satisfying specific criterias via \p fn_cmp
* from \p dll. Applies for all list-objects for which \p fn_cmp return a zero value.
*
* \param dll list.
* \param fn_cmp comparator for list-objects data and given \p any data.
* \param any any additional data to be given to the second argument of \p fn_cmp .
*
* \return count of removed objects from list \p dll .
*/
__dll_inline size_t dll_remove(dll_t * restrict dll,
dll_callback_fn_t fn_cmp,
void * restrict any);
/**
* \b Reverses the order of the list-objects in the list.
*
* \param dll list to be reversed.
*
* \return true on succes, false otherwise
*/
__dll_inline bool dll_reverse(const dll_t * restrict const dll);
/**
* \b Searches for specific \p data via \p fn_search when it returns zero value.
*
* \param dll list.
* \param fn_search function which will deside is given \c data is valid
* \param any an any additional data that will be passed to the second argument of
* \p fn_search
*
* \return first occurrence of searched data in list \p dll , \c NULL otherwise.
*/
__dll_inline void *
dll_find(dll_t * restrict dll, dll_callback_fn_t fn_search, void * restrict any);
/**
* \b Creates a new array populated with the results of calling a provided function
* \p mapper on every list-object for the calling list \p dll .
*
* \param dll mapping list
* \param mapper callback function to produce results for each list-object
* \param any an any additional data to be passed to the \p mapper callback function
*
* \return A new array of pointers with each element being the result of the callback
* function.
*/
__dll_inline void ** dll_map(const dll_t * restrict const dll,
dll_callback_mapper_fn_t mapper,
void * restrict any);
/**
* \b Removes all consecutive duplicate list-objects from the list.
*
* \note Only the first list-object in each group of equal list-objects is left.
* List-objects compares via \p fn_cmp and only if \p fn_cmp returns zero means the
* list-objects are equals.
*
* \param dll list to be checked for duplicates.
* \param fn_cmp comparator for list-objects.
*
* \return count of removed objects from a list.
*/
__dll_inline size_t dll_unique(dll_t * restrict dll,
dll_callback_ext_fn_t fn_cmp,
void * any);
/**
* \b Sorts all the list-objects via \p fn_sort in \p dll list using a recursive merge
* sort.
*
* \note if list empty or has only 1 list-object then it's consider as sorted and returns
* \c true .
*
* \param dll list to be sorted.
* \param fn_sort callback-function to compare list-objects.
* \param any any data to be passed to fn_sort callback.
*
* \returns \c true on success, \c false otherwise.
*/
__dll_inline bool
dll_sort(dll_t * restrict dll, dll_callback_ext_fn_t fn_sort, void * any);
/**
* \b Compares two lists if they are not equals.
*
* \note if \p fn_cmp is not provided then list-objects will be compared by it's \c size
* and \c data .
*
* \param dll_a first list to compare
* \param dll_b second list to compare
* \param fn_cmp list-objects comparator
*
* \return true if lists are equals
*/
__dll_inline bool dll_is_equal(const dll_t * restrict const dll_a,
const dll_t * restrict const dll_b,
dll_callback_ext_fn_t fn_cmp,
void * any);
/**
* **Unlink and free** a list-object \p obj from list \p dll.
*
* \param dll list.
* \param obj list-object to be deleted.
*
* \return \c true on success, \c false otherwise
*/
__dll_inline bool dll_delete(dll_t * restrict dll, dll_obj_t * restrict obj);
/**
* \b Unlink (only removes list-object from list but not deleting it) a #dll_obj_t
* list-object \p obj from list \p dll .
*
* \param dll list.
* \param obj list-object to be unlinked.
*
* \return unlinked \o obj on success, \c NULL otherwise.
*/
__dll_inline dll_obj_t * dll_unlink(dll_t * restrict dll, dll_obj_t * restrict obj);
/**
* \b Free a list-object \p obj and data in it.
*
* \attention BE CERAFUL - It's doesn't works with links to next and previous
* list-objects, so calling this function without #dll_unlink may cause \c SIGSEGV in
* future work with list. Better soultion may be for you to use a #dll_delete instead.
*
* \param obj a list-object.
*
* \return true on success, false otherwise
*/
__dll_inline bool dll_free_obj(dll_obj_t * restrict * restrict obj);
/**
* \b Free the whole list with all list-objects linked to this list and its data.
*
* \param dll list.
*
* \return true on success, false otherwise
*/
__dll_inline bool dll_free(dll_t * restrict * restrict dll);
/*
* ----------------------------
* Function definitions
* ----------------------------
*/
__dll_inline dll_t * dll_new(void) {
dll_t * restrict out = calloc(1, sizeof(*out));
return out;
}
__dll_inline dll_obj_t * dll_new_obj(void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor) {
dll_obj_t * restrict out = NULL;
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == (out = calloc(1, sizeof(*out))))) {
return NULL;
}
#else
out = calloc(1, sizeof(*out));
#endif /* LIBDLL_UNSAFE_USAGE */
out->data = data;
out->size = size;
out->destructor = destructor;
return out;
}
__dll_inline dll_obj_t * dll_push_front(dll_t * restrict dll, dll_obj_t * restrict obj) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == obj)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
obj->next = NULL;
obj->prev = NULL;
++dll->objs_count;
if (NULL == dll->head) {
dll->head = dll->tail = obj;
} else {
dll->head->prev = obj;
obj->next = dll->head;
dll->head = obj;
}
return obj;
}
/**
* \b Pushes a provided \p obj list-object to the end of given \p dll list.
*
* \param dll destination list.
* \param obj list-object.
*
* \return \p obj on success, \c NULL otherwise
*/
__dll_inline dll_obj_t * dll_push_back(dll_t * restrict dll, dll_obj_t * restrict obj) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == obj)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
obj->next = NULL;
obj->prev = NULL;
++dll->objs_count;
if (NULL == dll->head) {
dll->head = dll->tail = obj;
} else {
dll->tail->next = obj;
obj->prev = dll->tail;
dll->tail = obj;
}
return obj;
}
__dll_inline dll_obj_t * dll_emplace_front(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor) {
dll_obj_t * restrict new_obj = dll_new_obj(data, size, destructor);
dll_obj_t * restrict __ret = dll_push_front(dll, new_obj);
return __ret;
}
__dll_inline dll_obj_t * dll_emplace_back(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor) {
dll_obj_t * restrict new_obj = dll_new_obj(data, size, destructor);
dll_obj_t * restrict __ret = dll_push_back(dll, new_obj);
return __ret;
}
__dll_inline dll_obj_t * dll_pop_front(dll_t * restrict dll) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == dll->head)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
dll_obj_t * restrict head = dll->head;
dll->head = head->next;
dll_obj_t * restrict __ret = dll_unlink(dll, head);
return __ret;
}
__dll_inline dll_obj_t * dll_pop_back(dll_t * restrict dll) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == dll->tail)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
dll_obj_t * restrict tail = dll->tail;
dll->tail = tail->prev;
dll_obj_t * restrict __ret = dll_unlink(dll, tail);
return __ret;
}
__dll_inline bool dll_clear(dll_t * restrict dll) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll)) {
return false;
}
#endif /* LIBDLL_UNSAFE_USAGE */
while (dll->objs_count) {
dll_obj_t * restrict obj = dll_pop_front(dll);
#ifndef LIBDLL_UNSAFE_USAGE
if (obj) {
const register bool is_obj_freed =
#endif /* LIBDLL_UNSAFE_USAGE */
dll_free_obj(&obj);
#ifndef LIBDLL_UNSAFE_USAGE
if (!is_obj_freed) {
return false;
}
} else {
return false;
}
#endif /* LIBDLL_UNSAFE_USAGE */
}
return true;
}
__dll_inline dll_obj_t *
dll_insert(dll_t * restrict dll, dll_obj_t * restrict obj, size_t pos) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == obj)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
if (dll->objs_count < pos) {
return NULL;
}
if (NULL == dll->head || 0 == pos) {
dll_obj_t * restrict __ret =
dll_emplace_front(dll, obj->data, obj->size, obj->destructor);
return __ret;
} else {
dll_obj_t * restrict iter = dll->head;
for (size_t i = 0; (pos - 1) > i && iter; ++i) {
iter = iter->next;
}
obj->next = iter->next;
obj->prev = iter;
if (iter->next) {
iter->next->prev = obj;
}
iter->next = obj;
++dll->objs_count;
return obj;
}
}
__dll_inline dll_obj_t * dll_emplace(dll_t * restrict dll,
void * restrict data,
size_t size,
dll_callback_destructor_fn_t destructor,
size_t pos) {
dll_obj_t * restrict new_obj = dll_new_obj(data, size, destructor);
dll_obj_t * restrict __ret = dll_insert(dll, new_obj, pos);
return __ret;
}
__dll_inline dll_obj_t * __dlli_get_obj_at_index(dll_t * restrict dll, size_t pos) {
const size_t dll_size = dll->objs_count;
dll_obj_t * restrict obj = NULL;
if ((dll_size / 2) >= pos) {
obj = dll->head;
for (size_t i = 0; obj && pos > i; ++i) {
obj = obj->next;
}
} else {
obj = dll->tail;
for (size_t i = dll_size ? dll_size - 1 : dll_size; obj && pos < i; --i) {
obj = obj->prev;
}
}
return obj;
}
__dll_inline dll_obj_t * dll_erase(dll_t * restrict dll, size_t start, size_t end) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */
if (0 == dll->objs_count || dll->objs_count < start) {
return NULL;
}
dll_obj_t * restrict erasing = __dlli_get_obj_at_index(dll, start);
dll_obj_t * restrict save = NULL;
size_t i = start ? start - 1 : start;
do {
save = erasing->next;
#ifndef LIBDLL_UNSAFE_USAGE
if (false == dll_delete(dll, erasing)) {
return NULL;
}
#else
dll_delete(dll, erasing);
#endif
erasing = save;
} while (end > i++ && erasing);
return erasing ? erasing : dll->tail;
}
__dll_inline bool
dll_foreach(const dll_t * restrict dll, dll_callback_fn_t fn, void * restrict any) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == dll || NULL == fn)) {
return false;
}
#endif /* LIBDLL_UNSAFE_USAGE */
size_t i = 0;
for (dll_obj_t * restrict iobj = dll->head; iobj; iobj = iobj->next) {
fn(iobj->data, any, i++);
}
return true;
}
__dll_inline dll_iterator_t dll_iterator(dll_t * restrict const dll) {
dll_iterator_t it = {
#ifndef LIBDLL_UNSAFE_USAGE
dll ? dll->head : NULL,
#else
dll->head,
#endif
dll,
0};
return it;
}
__dll_inline bool dll_next(dll_iterator_t * restrict const it) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == it || (it && NULL == it->__dll))) {
return false;
}
#endif /* LIBDLL_UNSAFE_USAGE */
it->__obj = it->__obj ? it->__obj->next : NULL;
++it->__index;
return !!it->__obj;
}
__dll_inline bool dll_prev(dll_iterator_t * restrict const it) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == it || (it && NULL == it->__dll))) {
return false;
}
#endif /* LIBDLL_UNSAFE_USAGE */
it->__obj = it->__obj ? it->__obj->prev : NULL;
--it->__index;
return !!it->__obj;
}
__dll_inline dll_obj_t * dll_iterator_get_obj(const dll_iterator_t * restrict const it) {
#ifndef LIBDLL_UNSAFE_USAGE
if (__dll_unlikely(NULL == it)) {
return NULL;
}
#endif /* LIBDLL_UNSAFE_USAGE */