Sun, 30 Nov 2025 16:01:48 +0100
Added tag v3.2 for changeset 38d3f40a50ec
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "cx/linked_list.h" |
|
763
741a2040fa33
make cx_cmp_ptr default comparator for pointer lists - relates to #340
Mike Becker <universe@uap-core.de>
parents:
735
diff
changeset
|
30 | #include "cx/compare.h" |
|
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
31 | #include <string.h> |
|
453
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
451
diff
changeset
|
32 | #include <assert.h> |
|
1534
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
33 | |
|
1533
6d65b1e97136
include stdalign.h when not using C23 or newer
Mike Becker <universe@uap-core.de>
parents:
1532
diff
changeset
|
34 | #if __STDC_VERSION__ < 202311L |
|
1534
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
35 | // we cannot simply include stdalign.h |
|
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
36 | // because Solaris is not entirely C11 complaint |
|
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
37 | #ifndef __alignof_is_defined |
|
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
38 | #define alignof _Alignof |
|
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
39 | #define __alignof_is_defined 1 |
|
38d3f40a50ec
instead of including stdalign.h, check if the macro is defined (Solaris does not have the header)
Mike Becker <universe@uap-core.de>
parents:
1533
diff
changeset
|
40 | #endif |
|
1533
6d65b1e97136
include stdalign.h when not using C23 or newer
Mike Becker <universe@uap-core.de>
parents:
1532
diff
changeset
|
41 | #endif |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | |
|
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
592
diff
changeset
|
43 | // LOW LEVEL LINKED LIST FUNCTIONS |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | |
|
592
bb69ef3ad1f3
enclose macro arguments in parenthesis
Mike Becker <universe@uap-core.de>
parents:
552
diff
changeset
|
45 | #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
46 | #define ll_prev(node) CX_LL_PTR(node, loc_prev) |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
47 | #define ll_next(node) CX_LL_PTR(node, loc_next) |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
48 | #define ll_advance(node) CX_LL_PTR(node, loc_advance) |
|
639
309e8b08c60e
temporarily remove pointer lists - see #234
Mike Becker <universe@uap-core.de>
parents:
638
diff
changeset
|
49 | #define ll_data(node) (((char*)(node))+loc_data) |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
51 | void *cx_linked_list_at( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
52 | const void *start, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
53 | size_t start_index, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
54 | ptrdiff_t loc_advance, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
55 | size_t index |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
56 | ) { |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
57 | assert(start != NULL); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
58 | assert(loc_advance >= 0); |
|
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
59 | size_t i = start_index; |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
60 | const void *cur = start; |
|
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
61 | while (i != index && cur != NULL) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
62 | cur = ll_advance(cur); |
|
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
63 | i < index ? i++ : i--; |
|
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
64 | } |
|
508
8aea65ae1eaf
#168 - add attributes and const
Mike Becker <universe@uap-core.de>
parents:
503
diff
changeset
|
65 | return (void *) cur; |
|
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
66 | } |
|
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
67 | |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
68 | void *cx_linked_list_find( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
69 | const void *start, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
70 | ptrdiff_t loc_advance, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
71 | ptrdiff_t loc_data, |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
72 | cx_compare_func cmp_func, |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
73 | const void *elem, |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
74 | size_t *found_index |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
75 | ) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
76 | assert(start != NULL); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
77 | assert(loc_advance >= 0); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
78 | assert(loc_data >= 0); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
79 | assert(cmp_func); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
80 | |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
81 | void *node = (void*) start; |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
82 | size_t index = 0; |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
83 | do { |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
84 | void *current = ll_data(node); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
85 | if (cmp_func(current, elem) == 0) { |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
86 | if (found_index != NULL) { |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
87 | *found_index = index; |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
88 | } |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
89 | return node; |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
90 | } |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
91 | node = ll_advance(node); |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
92 | index++; |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
93 | } while (node != NULL); |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
94 | return NULL; |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
95 | } |
|
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
96 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
97 | void *cx_linked_list_first( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
98 | const void *node, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
99 | ptrdiff_t loc_prev |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
100 | ) { |
|
475
31bf97fdbf71
add cx_linked_list_first() + cx_linked_list_prepend()
Mike Becker <universe@uap-core.de>
parents:
474
diff
changeset
|
101 | return cx_linked_list_last(node, loc_prev); |
|
31bf97fdbf71
add cx_linked_list_first() + cx_linked_list_prepend()
Mike Becker <universe@uap-core.de>
parents:
474
diff
changeset
|
102 | } |
|
31bf97fdbf71
add cx_linked_list_first() + cx_linked_list_prepend()
Mike Becker <universe@uap-core.de>
parents:
474
diff
changeset
|
103 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
104 | void *cx_linked_list_last( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
105 | const void *node, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
106 | ptrdiff_t loc_next |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
107 | ) { |
|
478
599770bb6314
add more nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
477
diff
changeset
|
108 | assert(node != NULL); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
109 | assert(loc_next >= 0); |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
111 | const void *cur = node; |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
112 | const void *last; |
|
456
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
113 | do { |
|
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
114 | last = cur; |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
115 | } while ((cur = ll_next(cur)) != NULL); |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | |
|
508
8aea65ae1eaf
#168 - add attributes and const
Mike Becker <universe@uap-core.de>
parents:
503
diff
changeset
|
117 | return (void *) last; |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | } |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
120 | void *cx_linked_list_prev( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
121 | const void *begin, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
122 | ptrdiff_t loc_next, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
123 | const void *node |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
124 | ) { |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
125 | assert(begin != NULL); |
|
478
599770bb6314
add more nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
477
diff
changeset
|
126 | assert(node != NULL); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
127 | assert(loc_next >= 0); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
128 | if (begin == node) return NULL; |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
129 | const void *cur = begin; |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
130 | const void *next; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
131 | while (1) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
132 | next = ll_next(cur); |
|
508
8aea65ae1eaf
#168 - add attributes and const
Mike Becker <universe@uap-core.de>
parents:
503
diff
changeset
|
133 | if (next == node) return (void *) cur; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
134 | cur = next; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
135 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
136 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
137 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
138 | void cx_linked_list_link( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
139 | void *left, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
140 | void *right, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
141 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
142 | ptrdiff_t loc_next |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
143 | ) { |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
144 | assert(loc_next >= 0); |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
145 | ll_next(left) = right; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
146 | if (loc_prev >= 0) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
147 | ll_prev(right) = left; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
148 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
149 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
150 | |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
151 | void cx_linked_list_unlink( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
152 | void *left, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
153 | void *right, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
154 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
155 | ptrdiff_t loc_next |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
156 | ) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
157 | assert (loc_next >= 0); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
158 | assert(ll_next(left) == right); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
159 | ll_next(left) = NULL; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
160 | if (loc_prev >= 0) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
161 | assert(ll_prev(right) == left); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
162 | ll_prev(right) = NULL; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
163 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
164 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
165 | |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
166 | void cx_linked_list_add( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
167 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
168 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
169 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
170 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
171 | void *new_node |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
172 | ) { |
|
456
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
173 | void *last; |
|
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
174 | if (end == NULL) { |
|
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
175 | assert(begin != NULL); |
|
478
599770bb6314
add more nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
477
diff
changeset
|
176 | last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next); |
|
456
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
177 | } else { |
|
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
178 | last = *end; |
|
227c2eabbef8
change cx_linked_list_last() and add a test for it
Mike Becker <universe@uap-core.de>
parents:
453
diff
changeset
|
179 | } |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
180 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
181 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
182 | |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
183 | void cx_linked_list_prepend( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
184 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
185 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
186 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
187 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
188 | void *new_node |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
189 | ) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
190 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
191 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
192 | |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
193 | void cx_linked_list_insert( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
194 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
195 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
196 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
197 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
198 | void *node, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
199 | void *new_node |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
200 | ) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
201 | cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
202 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
203 | |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
204 | void cx_linked_list_insert_chain( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
205 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
206 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
207 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
208 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
209 | void *node, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
210 | void *insert_begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
211 | void *insert_end |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
212 | ) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
213 | // find the end of the chain, if not specified |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
214 | if (insert_end == NULL) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
215 | insert_end = cx_linked_list_last(insert_begin, loc_next); |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | } |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
217 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
218 | // determine the successor |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
219 | void *successor; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
220 | if (node == NULL) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
221 | assert(begin != NULL || (end != NULL && loc_prev >= 0)); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
222 | if (begin != NULL) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
223 | successor = *begin; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
224 | *begin = insert_begin; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
225 | } else { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
226 | successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
227 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
228 | } else { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
229 | successor = ll_next(node); |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
230 | cx_linked_list_link(node, insert_begin, loc_prev, loc_next); |
|
408
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
231 | } |
|
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
426
diff
changeset
|
232 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
233 | if (successor == NULL) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
234 | // the list ends with the new chain |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
235 | if (end != NULL) { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
236 | *end = insert_end; |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
237 | } |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
238 | } else { |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
239 | cx_linked_list_link(insert_end, successor, loc_prev, loc_next); |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
240 | } |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
241 | } |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
242 | |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
243 | void cx_linked_list_insert_sorted( |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
244 | void **begin, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
245 | void **end, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
246 | ptrdiff_t loc_prev, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
247 | ptrdiff_t loc_next, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
248 | void *new_node, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
249 | cx_compare_func cmp_func |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
250 | ) { |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
251 | assert(ll_next(new_node) == NULL); |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
252 | cx_linked_list_insert_sorted_chain( |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
253 | begin, end, loc_prev, loc_next, new_node, cmp_func); |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
254 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
255 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
256 | static void *cx_linked_list_insert_sorted_chain_impl( |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
257 | void **begin, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
258 | void **end, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
259 | ptrdiff_t loc_prev, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
260 | ptrdiff_t loc_next, |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
261 | void *insert_begin, |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
262 | cx_compare_func cmp_func, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
263 | bool allow_duplicates |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
264 | ) { |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
265 | assert(begin != NULL); |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
266 | assert(loc_next >= 0); |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
267 | assert(insert_begin != NULL); |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
268 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
269 | // strategy: build completely new chains from scratch |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
270 | void *source_original = *begin; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
271 | void *source_argument = insert_begin; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
272 | void *new_begin = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
273 | void *new_end = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
274 | void *dup_begin = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
275 | void *dup_end = NULL; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
276 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
277 | // determine the new start |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
278 | { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
279 | int d = source_original == NULL ? 1 : cmp_func(source_original, source_argument); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
280 | if (d <= 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
281 | // the new chain starts with the original chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
282 | new_begin = new_end = source_original; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
283 | source_original = ll_next(source_original); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
284 | if (d == 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
285 | if (allow_duplicates) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
286 | // duplicate allowed, append it to the chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
287 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
288 | new_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
289 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
290 | // duplicate is not allowed, start a duplicate chain with the argument |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
291 | dup_begin = dup_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
292 | } |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
293 | source_argument = ll_next(source_argument); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
294 | } |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
295 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
296 | // input is smaller, or there is no original chain; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
297 | // start the new chain with the source argument |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
298 | new_begin = new_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
299 | source_argument = ll_next(source_argument); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
300 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
301 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
302 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
303 | // now successively compare the elements and add them to the correct chains |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
304 | while (source_original != NULL && source_argument != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
305 | int d = cmp_func(source_original, source_argument); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
306 | if (d <= 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
307 | // the original is not larger, add it to the chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
308 | cx_linked_list_link(new_end, source_original, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
309 | new_end = source_original; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
310 | source_original = ll_next(source_original); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
311 | if (d == 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
312 | if (allow_duplicates) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
313 | // duplicate allowed, append it to the chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
314 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
315 | new_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
316 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
317 | // duplicate is not allowed, append it to the duplicate chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
318 | if (dup_end == NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
319 | dup_begin = dup_end = source_argument; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
320 | } else { |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
321 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
322 | dup_end = source_argument; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
323 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
324 | } |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
325 | source_argument = ll_next(source_argument); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
326 | } |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
327 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
328 | // the original is larger, append the source argument to the chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
329 | // check if we must discard the source argument as duplicate |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
330 | if (!allow_duplicates && cmp_func(new_end, source_argument) == 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
331 | if (dup_end == NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
332 | dup_begin = dup_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
333 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
334 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
335 | dup_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
336 | } |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
337 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
338 | // no duplicate or duplicates allowed |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
339 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
340 | new_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
341 | } |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
342 | source_argument = ll_next(source_argument); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
343 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
344 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
345 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
346 | if (source_original != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
347 | // something is left from the original chain, append it |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
348 | cx_linked_list_link(new_end, source_original, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
349 | new_end = cx_linked_list_last(source_original, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
350 | } else if (source_argument != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
351 | // something is left from the input chain; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
352 | // when we allow duplicates, append it |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
353 | if (allow_duplicates) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
354 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
355 | new_end = cx_linked_list_last(source_argument, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
356 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
357 | // otherwise we must check one-by-one |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
358 | while (source_argument != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
359 | if (cmp_func(new_end, source_argument) == 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
360 | if (dup_end == NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
361 | dup_begin = dup_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
362 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
363 | cx_linked_list_link(dup_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
364 | dup_end = source_argument; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
365 | } |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
366 | } else { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
367 | cx_linked_list_link(new_end, source_argument, loc_prev, loc_next); |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
368 | new_end = source_argument; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
369 | } |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
370 | source_argument = ll_next(source_argument); |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
371 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
372 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
373 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
374 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
375 | // null the next pointers at the end of the chain |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
376 | ll_next(new_end) = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
377 | if (dup_end != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
378 | ll_next(dup_end) = NULL; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
379 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
380 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
381 | // null the optional prev pointers |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
382 | if (loc_prev >= 0) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
383 | ll_prev(new_begin) = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
384 | if (dup_begin != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
385 | ll_prev(dup_begin) = NULL; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
386 | } |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
387 | } |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
388 | |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
389 | // output |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
390 | *begin = new_begin; |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
391 | if (end != NULL) { |
|
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
392 | *end = new_end; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
393 | } |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
394 | return dup_begin; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
395 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
396 | |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
397 | void cx_linked_list_insert_sorted_chain( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
398 | void **begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
399 | void **end, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
400 | ptrdiff_t loc_prev, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
401 | ptrdiff_t loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
402 | void *insert_begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
403 | cx_compare_func cmp_func |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
404 | ) { |
|
1423
9a72258446cd
fixes various bugs related to skipping duplicates in insert_unique - relates to #557
Mike Becker <universe@uap-core.de>
parents:
1419
diff
changeset
|
405 | cx_linked_list_insert_sorted_chain_impl( |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
406 | begin, end, loc_prev, loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
407 | insert_begin, cmp_func, true); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
408 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
409 | |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
410 | int cx_linked_list_insert_unique( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
411 | void **begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
412 | void **end, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
413 | ptrdiff_t loc_prev, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
414 | ptrdiff_t loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
415 | void *new_node, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
416 | cx_compare_func cmp_func |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
417 | ) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
418 | assert(ll_next(new_node) == NULL); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
419 | return NULL != cx_linked_list_insert_unique_chain( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
420 | begin, end, loc_prev, loc_next, new_node, cmp_func); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
421 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
422 | |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
423 | void *cx_linked_list_insert_unique_chain( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
424 | void **begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
425 | void **end, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
426 | ptrdiff_t loc_prev, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
427 | ptrdiff_t loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
428 | void *insert_begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
429 | cx_compare_func cmp_func |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
430 | ) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
431 | return cx_linked_list_insert_sorted_chain_impl( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
432 | begin, end, loc_prev, loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
433 | insert_begin, cmp_func, false); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
434 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
435 | |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
436 | size_t cx_linked_list_remove_chain( |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
437 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
438 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
439 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
440 | ptrdiff_t loc_next, |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
441 | void *node, |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
442 | size_t num |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
443 | ) { |
|
477
73a93c7a56ae
add more explicit documentation to cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
476
diff
changeset
|
444 | assert(node != NULL); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
445 | assert(loc_next >= 0); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
446 | assert(loc_prev >= 0 || begin != NULL); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
447 | |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
448 | // easy exit |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
449 | if (num == 0) return 0; |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
450 | |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
451 | // find adjacent nodes |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
452 | void *prev; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
453 | if (loc_prev >= 0) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
454 | prev = ll_prev(node); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
455 | } else { |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
456 | prev = cx_linked_list_prev(*begin, loc_next, node); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
457 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
458 | |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
459 | void *next = ll_next(node); |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
460 | size_t removed = 1; |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
461 | for (; removed < num && next != NULL ; removed++) { |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
462 | next = ll_next(next); |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
463 | } |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
464 | |
|
476
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
465 | // update next pointer of prev node, or set begin |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
466 | if (prev == NULL) { |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
467 | if (begin != NULL) { |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
468 | *begin = next; |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
469 | } |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
470 | } else { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
471 | ll_next(prev) = next; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
472 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
473 | |
|
476
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
474 | // update prev pointer of next node, or set end |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
475 | if (next == NULL) { |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
476 | if (end != NULL) { |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
477 | *end = prev; |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
478 | } |
|
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
479 | } else if (loc_prev >= 0) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
480 | ll_prev(next) = prev; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
481 | } |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
482 | |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
483 | return removed; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
484 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
485 | |
|
1426
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
486 | void cx_linked_list_remove( |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
487 | void **begin, |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
488 | void **end, |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
489 | ptrdiff_t loc_prev, |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
490 | ptrdiff_t loc_next, |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
491 | void *node |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
492 | ) { |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
493 | cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1); |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
494 | } |
|
3a89b31f0724
clean up header files and adds support for comparing arbitrary strings with string.h functions
Mike Becker <universe@uap-core.de>
parents:
1423
diff
changeset
|
495 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
496 | size_t cx_linked_list_size( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
497 | const void *node, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
498 | ptrdiff_t loc_next |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
499 | ) { |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
500 | assert(loc_next >= 0); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
501 | size_t size = 0; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
502 | while (node != NULL) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
503 | node = ll_next(node); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
504 | size++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
505 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
506 | return size; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
507 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
508 | |
|
661
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
509 | #ifndef CX_LINKED_LIST_SORT_SBO_SIZE |
|
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
510 | #define CX_LINKED_LIST_SORT_SBO_SIZE 1024 |
|
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
511 | #endif |
|
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
512 | |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
513 | static void cx_linked_list_sort_merge( |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
514 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
515 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
516 | ptrdiff_t loc_data, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
517 | size_t length, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
518 | void *ls, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
519 | void *le, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
520 | void *re, |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
521 | cx_compare_func cmp_func, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
522 | void **begin, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
523 | void **end |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
524 | ) { |
|
661
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
525 | void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE]; |
|
0a71ac9547fd
add CX_LINKED_LIST_SORT_SBO_SIZE macro
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
526 | void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ? |
|
1319
aa1f580f8f59
add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents:
1318
diff
changeset
|
527 | cxMallocDefault(sizeof(void *) * length) : sbo; |
|
1504
467a77a85f43
add missing coverage exclusions for linked_list.c
Mike Becker <universe@uap-core.de>
parents:
1482
diff
changeset
|
528 | if (sorted == NULL) abort(); // LCOV_EXCL_LINE |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
529 | void *rc, *lc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
530 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
531 | lc = ls; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
532 | rc = le; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
533 | size_t n = 0; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
534 | while (lc && lc != le && rc != re) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
535 | if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
536 | sorted[n] = lc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
537 | lc = ll_next(lc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
538 | } else { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
539 | sorted[n] = rc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
540 | rc = ll_next(rc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
541 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
542 | n++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
543 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
544 | while (lc && lc != le) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
545 | sorted[n] = lc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
546 | lc = ll_next(lc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
547 | n++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
548 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
549 | while (rc && rc != re) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
550 | sorted[n] = rc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
551 | rc = ll_next(rc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
552 | n++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
553 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
554 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
555 | // Update pointer |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
556 | if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
|
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
950
diff
changeset
|
557 | for (size_t i = 0 ; i < length - 1; i++) { |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
558 | cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
559 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
560 | ll_next(sorted[length - 1]) = NULL; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
561 | |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
562 | *begin = sorted[0]; |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
563 | *end = sorted[length - 1]; |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
564 | if (sorted != sbo) { |
|
1319
aa1f580f8f59
add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents:
1318
diff
changeset
|
565 | cxFreeDefault(sorted); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
566 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
567 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
568 | |
|
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
592
diff
changeset
|
569 | void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
570 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
571 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
572 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
573 | ptrdiff_t loc_next, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
574 | ptrdiff_t loc_data, |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
575 | cx_compare_func cmp_func |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
576 | ) { |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
577 | assert(begin != NULL); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
578 | assert(loc_next >= 0); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
579 | assert(loc_data >= 0); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
580 | assert(cmp_func); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
581 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
582 | void *lc, *ls, *le, *re; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
583 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
584 | // set start node |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
585 | ls = *begin; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
586 | |
|
702
3390b58ad15a
fix cx_linked_list_sort() not working for empty lists
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
587 | // early exit when this list is empty |
|
3390b58ad15a
fix cx_linked_list_sort() not working for empty lists
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
588 | if (ls == NULL) return; |
|
3390b58ad15a
fix cx_linked_list_sort() not working for empty lists
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
589 | |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
590 | // check how many elements are already sorted |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
591 | lc = ls; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
592 | size_t ln = 1; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
593 | while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
594 | lc = ll_next(lc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
595 | ln++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
596 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
597 | le = ll_next(lc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
598 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
599 | // if first unsorted node is NULL, the list is already completely sorted |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
600 | if (le != NULL) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
601 | void *rc; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
602 | size_t rn = 1; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
603 | rc = le; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
604 | // skip already sorted elements |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
605 | while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
606 | rc = ll_next(rc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
607 | rn++; |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
608 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
609 | re = ll_next(rc); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
610 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
611 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
612 | void *sorted_begin, *sorted_end; |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
613 | cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
614 | ln + rn, ls, le, re, cmp_func, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
615 | &sorted_begin, &sorted_end); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
616 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
617 | // Something left? Sort it! |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
618 | size_t remainder_length = cx_linked_list_size(re, loc_next); |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
619 | if (remainder_length > 0) { |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
620 | void *remainder = re; |
|
639
309e8b08c60e
temporarily remove pointer lists - see #234
Mike Becker <universe@uap-core.de>
parents:
638
diff
changeset
|
621 | cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
622 | |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
623 | // merge sorted list with (also sorted) remainder |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
624 | cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
625 | ln + rn + remainder_length, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
626 | sorted_begin, remainder, NULL, cmp_func, |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
627 | &sorted_begin, &sorted_end); |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
628 | } |
|
703
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
629 | *begin = sorted_begin; |
|
425d4279856f
improve cx_linked_list_sort() - fixes #257
Mike Becker <universe@uap-core.de>
parents:
702
diff
changeset
|
630 | if (end) *end = sorted_end; |
|
469
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
631 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
632 | } |
|
75ae1dccd101
add cx_linked_list_sort()
Mike Becker <universe@uap-core.de>
parents:
467
diff
changeset
|
633 | |
|
486
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
634 | int cx_linked_list_compare( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
635 | const void *begin_left, |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
636 | const void *begin_right, |
|
486
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
637 | ptrdiff_t loc_advance, |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
638 | ptrdiff_t loc_data, |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
639 | cx_compare_func cmp_func |
|
486
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
640 | ) { |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
641 | const void *left = begin_left, *right = begin_right; |
|
486
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
642 | |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
643 | while (left != NULL && right != NULL) { |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
644 | const void *left_data = ll_data(left); |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
645 | const void *right_data = ll_data(right); |
|
552
4373c2a90066
#178 fix that lists of different kind cannot be compared
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
646 | int result = cmp_func(left_data, right_data); |
|
486
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
647 | if (result != 0) return result; |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
648 | left = ll_advance(left); |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
649 | right = ll_advance(right); |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
650 | } |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
651 | |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
652 | if (left != NULL) { return 1; } |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
653 | else if (right != NULL) { return -1; } |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
654 | else { return 0; } |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
655 | } |
|
d7ca126eab7f
add cx_linked_list_compare() and simplifies some tests
Mike Becker <universe@uap-core.de>
parents:
481
diff
changeset
|
656 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
657 | void cx_linked_list_reverse( |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
658 | void **begin, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
659 | void **end, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
660 | ptrdiff_t loc_prev, |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
661 | ptrdiff_t loc_next |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
662 | ) { |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
663 | assert(begin != NULL); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
664 | assert(loc_next >= 0); |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
665 | |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
666 | // swap all links |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
667 | void *prev = NULL; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
668 | void *cur = *begin; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
669 | while (cur != NULL) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
670 | void *next = ll_next(cur); |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
671 | |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
672 | ll_next(cur) = prev; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
673 | if (loc_prev >= 0) { |
|
480
e3be53a3354f
add cx_linked_list_find()
Mike Becker <universe@uap-core.de>
parents:
478
diff
changeset
|
674 | ll_prev(cur) = next; |
|
473
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
675 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
676 | |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
677 | prev = cur; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
678 | cur = next; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
679 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
680 | |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
681 | // update begin and end |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
682 | if (end != NULL) { |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
683 | *end = *begin; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
684 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
685 | *begin = prev; |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
686 | } |
|
1bd4b8c28722
add cx_linked_list_{prev, remove, reverse}
Mike Becker <universe@uap-core.de>
parents:
470
diff
changeset
|
687 | |
|
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
592
diff
changeset
|
688 | // HIGH LEVEL LINKED LIST IMPLEMENTATION |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
689 | |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
690 | static void *cx_ll_node_at( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
691 | const cx_linked_list *list, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
692 | size_t index |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
693 | ) { |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
694 | if (index >= list->base.collection.size) { |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
695 | return NULL; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
696 | } else if (index > list->base.collection.size / 2) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
697 | return cx_linked_list_at(list->end, list->base.collection.size - 1, list->loc_prev, index); |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
698 | } else { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
699 | return cx_linked_list_at(list->begin, 0, list->loc_next, index); |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
700 | } |
|
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
701 | } |
|
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
702 | |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
703 | static void *cx_ll_malloc_node(const cx_linked_list *list) { |
|
1532
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
704 | size_t n; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
705 | if (list->extra_data_len == 0) { |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
706 | n = list->loc_data + list->base.collection.elem_size; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
707 | } else { |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
708 | n = list->loc_extra + list->extra_data_len; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
709 | } |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
710 | return cxZalloc(list->base.collection.allocator, n); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
711 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
712 | |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
713 | static int cx_ll_insert_at( |
|
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
499
diff
changeset
|
714 | struct cx_list_s *list, |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
715 | void *node, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
716 | const void *elem |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
717 | ) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
718 | cx_linked_list *ll = (cx_linked_list *) list; |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
719 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
720 | // create the new new_node |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
721 | void *new_node = cx_ll_malloc_node(ll); |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
722 | |
|
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
723 | // sortir if failed |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
724 | if (new_node == NULL) return 1; |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
725 | |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
726 | // copy the data |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
727 | if (elem != NULL) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
728 | memcpy((char*)new_node + ll->loc_data, elem, list->collection.elem_size); |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
729 | } |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
730 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
731 | // insert |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
732 | cx_linked_list_insert_chain( |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
733 | &ll->begin, &ll->end, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
734 | ll->loc_prev, ll->loc_next, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
735 | node, new_node, new_node |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
736 | ); |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
737 | |
|
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
738 | // increase the size and return |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
739 | list->collection.size++; |
|
448
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
740 | return 0; |
|
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
741 | } |
|
37c5d2fdb36b
implement cx_ll_insert()
Mike Becker <universe@uap-core.de>
parents:
447
diff
changeset
|
742 | |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
743 | static size_t cx_ll_insert_array( |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
744 | struct cx_list_s *list, |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
745 | size_t index, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
746 | const void *array, |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
747 | size_t n |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
748 | ) { |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
749 | // out-of bounds and corner case check |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
750 | if (index > list->collection.size || n == 0) return 0; |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
751 | |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
752 | // find position efficiently |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
753 | void *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
754 | |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
755 | // perform first insert |
|
1065
6eb7b54975ee
improve coverage metrics
Mike Becker <universe@uap-core.de>
parents:
962
diff
changeset
|
756 | if (0 != cx_ll_insert_at(list, node, array)) return 1; |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
757 | |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
758 | // is there more? |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
759 | if (n == 1) return 1; |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
760 | |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
761 | // we now know exactly where we are |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
762 | cx_linked_list *ll = (cx_linked_list *) list; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
763 | node = node == NULL ? ((cx_linked_list *) list)->begin : CX_LL_PTR(node, ll->loc_next); |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
764 | |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
765 | // we can add the remaining nodes and immediately advance to the inserted node |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
766 | const char *source = array; |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
767 | for (size_t i = 1; i < n; i++) { |
|
1433
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
768 | if (source != NULL) { |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
769 | source += list->collection.elem_size; |
|
81c301a59b7c
add cxListEmplaceArray() and cxListEmplaceArrayAt() as preparation for the clone implementation
Mike Becker <universe@uap-core.de>
parents:
1429
diff
changeset
|
770 | } |
|
1065
6eb7b54975ee
improve coverage metrics
Mike Becker <universe@uap-core.de>
parents:
962
diff
changeset
|
771 | if (0 != cx_ll_insert_at(list, node, source)) return i; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
772 | node = CX_LL_PTR(node, ll->loc_next); |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
773 | } |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
774 | return n; |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
775 | } |
|
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
776 | |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
777 | static void *cx_ll_insert_element( |
|
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
778 | struct cx_list_s *list, |
|
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
779 | size_t index, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
780 | const void *element |
|
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
781 | ) { |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
782 | // out-of-bounds check |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
783 | if (index > list->collection.size) return NULL; |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
784 | |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
785 | // find position efficiently |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
786 | void *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
787 | |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
788 | // perform first insert |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
789 | if (cx_ll_insert_at(list, node, element)) return NULL; |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
790 | |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
791 | // return a pointer to the data of the inserted node |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
792 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
793 | if (node == NULL) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
794 | return (char*)(ll->begin) + ll->loc_data; |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
795 | } else { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
796 | char *next = CX_LL_PTR(node, ll->loc_next); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
797 | return next + ll->loc_data; |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
798 | } |
|
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
799 | } |
|
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
800 | |
|
880
54133f14043f
fix cx_ll_insert_sorted_cmp_func not being thread local
Mike Becker <universe@uap-core.de>
parents:
879
diff
changeset
|
801 | static _Thread_local cx_compare_func cx_ll_insert_sorted_cmp_func; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
802 | static _Thread_local off_t cx_ll_insert_sorted_loc_data; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
803 | |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
804 | static int cx_ll_insert_sorted_cmp_helper(const void *l, const void *r) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
805 | const char *left = (const char*)l + cx_ll_insert_sorted_loc_data; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
806 | const char *right = (const char*)r + cx_ll_insert_sorted_loc_data; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
807 | return cx_ll_insert_sorted_cmp_func(left, right); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
808 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
809 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
810 | static size_t cx_ll_insert_sorted_impl( |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
811 | struct cx_list_s *list, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
812 | const void *array, |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
813 | size_t n, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
814 | bool allow_duplicates |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
815 | ) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
816 | cx_linked_list *ll = (cx_linked_list *) list; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
817 | |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
818 | // special case |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
819 | if (n == 0) return 0; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
820 | |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
821 | // create a new chain of nodes |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
822 | void *chain = cx_ll_malloc_node(ll); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
823 | if (chain == NULL) return 0; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
824 | |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
825 | memcpy((char*)chain + ll->loc_data, array, list->collection.elem_size); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
826 | |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
827 | // add all elements from the array to that chain |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
828 | void *prev = chain; |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
829 | const char *src = array; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
830 | size_t inserted = 1; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
831 | for (; inserted < n; inserted++) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
832 | void *next = cx_ll_malloc_node(ll); |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
833 | if (next == NULL) break; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
834 | src += list->collection.elem_size; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
835 | memcpy((char*)next + ll->loc_data, src, list->collection.elem_size); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
836 | CX_LL_PTR(prev, ll->loc_next) = next; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
837 | CX_LL_PTR(next, ll->loc_prev) = prev; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
838 | prev = next; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
839 | } |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
840 | CX_LL_PTR(prev, ll->loc_next) = NULL; |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
841 | |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
842 | // invoke the low level function |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
843 | cx_ll_insert_sorted_cmp_func = list->collection.cmpfunc; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
844 | cx_ll_insert_sorted_loc_data = ll->loc_data; |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
845 | if (allow_duplicates) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
846 | cx_linked_list_insert_sorted_chain( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
847 | &ll->begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
848 | &ll->end, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
849 | ll->loc_prev, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
850 | ll->loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
851 | chain, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
852 | cx_ll_insert_sorted_cmp_helper |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
853 | ); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
854 | list->collection.size += inserted; |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
855 | } else { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
856 | void *duplicates = cx_linked_list_insert_unique_chain( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
857 | &ll->begin, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
858 | &ll->end, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
859 | ll->loc_prev, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
860 | ll->loc_next, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
861 | chain, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
862 | cx_ll_insert_sorted_cmp_helper |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
863 | ); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
864 | list->collection.size += inserted; |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
865 | // free the nodes that did not make it into the list |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
866 | while (duplicates != NULL) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
867 | void *next = CX_LL_PTR(duplicates, ll->loc_next); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
868 | cxFree(list->collection.allocator, duplicates); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
869 | duplicates = next; |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
870 | list->collection.size--; |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
871 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
872 | } |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
873 | |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
874 | return inserted; |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
875 | } |
|
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
876 | |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
877 | static size_t cx_ll_insert_sorted( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
878 | struct cx_list_s *list, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
879 | const void *array, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
880 | size_t n |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
881 | ) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
882 | return cx_ll_insert_sorted_impl(list, array, n, true); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
883 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
884 | |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
885 | static size_t cx_ll_insert_unique( |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
886 | struct cx_list_s *list, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
887 | const void *array, |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
888 | size_t n |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
889 | ) { |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
890 | return cx_ll_insert_sorted_impl(list, array, n, false); |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
891 | } |
|
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
892 | |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
893 | static size_t cx_ll_remove( |
|
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
499
diff
changeset
|
894 | struct cx_list_s *list, |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
895 | size_t index, |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
896 | size_t num, |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
897 | void *targetbuf |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
898 | ) { |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
899 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
900 | void *node = cx_ll_node_at(ll, index); |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
901 | |
|
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
902 | // out-of-bounds check |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
903 | if (node == NULL) return 0; |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
904 | |
|
476
60ff4561dc04
change contract of cx_linked_list_remove()
Mike Becker <universe@uap-core.de>
parents:
475
diff
changeset
|
905 | // remove |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
906 | size_t removed = cx_linked_list_remove_chain( |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
907 | (void **) &ll->begin, |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
908 | (void **) &ll->end, |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
909 | ll->loc_prev, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
910 | ll->loc_next, |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
911 | node, |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
912 | num |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
913 | ); |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
914 | |
|
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
915 | // adjust size |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
916 | list->collection.size -= removed; |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
917 | |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
918 | // copy or destroy the removed chain |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
919 | if (targetbuf == NULL) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
920 | char *n = node; |
|
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
950
diff
changeset
|
921 | for (size_t i = 0; i < removed; i++) { |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
922 | // element destruction |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
923 | cx_invoke_destructor(list, n + ll->loc_data); |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
924 | |
|
925
fd6e191f3268
fix invalid reads when removing linked list nodes
Mike Becker <universe@uap-core.de>
parents:
919
diff
changeset
|
925 | // free the node and advance |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
926 | void *next = CX_LL_PTR(n, ll->loc_next); |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
927 | cxFree(list->collection.allocator, n); |
|
925
fd6e191f3268
fix invalid reads when removing linked list nodes
Mike Becker <universe@uap-core.de>
parents:
919
diff
changeset
|
928 | n = next; |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
929 | } |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
930 | } else { |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
931 | char *dest = targetbuf; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
932 | char *n = node; |
|
962
cd418898af5c
remove cx_for_n() macro - fixes #467
Mike Becker <universe@uap-core.de>
parents:
950
diff
changeset
|
933 | for (size_t i = 0; i < removed; i++) { |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
934 | // copy payload |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
935 | memcpy(dest, n + ll->loc_data, list->collection.elem_size); |
|
447
87b2cdd668ed
implement cx_ll_remove()
Mike Becker <universe@uap-core.de>
parents:
446
diff
changeset
|
936 | |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
937 | // advance target buffer |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
938 | dest += list->collection.elem_size; |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
939 | |
|
925
fd6e191f3268
fix invalid reads when removing linked list nodes
Mike Becker <universe@uap-core.de>
parents:
919
diff
changeset
|
940 | // free the node and advance |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
941 | void *next = CX_LL_PTR(n, ll->loc_next); |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
942 | cxFree(list->collection.allocator, n); |
|
925
fd6e191f3268
fix invalid reads when removing linked list nodes
Mike Becker <universe@uap-core.de>
parents:
919
diff
changeset
|
943 | n = next; |
|
919
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
944 | } |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
945 | } |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
946 | |
|
75da57d4634e
add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
947 | return removed; |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
948 | } |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
949 | |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
950 | static void cx_ll_clear(struct cx_list_s *list) { |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
951 | if (list->collection.size == 0) return; |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
952 | |
|
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
953 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
954 | char *node = ll->begin; |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
955 | while (node != NULL) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
956 | cx_invoke_destructor(list, node + ll->loc_data); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
957 | void *next = CX_LL_PTR(node, ll->loc_next); |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
958 | cxFree(list->collection.allocator, node); |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
959 | node = next; |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
960 | } |
|
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
961 | ll->begin = ll->end = NULL; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
962 | list->collection.size = 0; |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
963 | } |
|
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
964 | |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
965 | static int cx_ll_swap( |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
966 | struct cx_list_s *list, |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
967 | size_t i, |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
968 | size_t j |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
969 | ) { |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
970 | if (i >= list->collection.size || j >= list->collection.size) return 1; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
971 | if (i == j) return 0; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
972 | |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
973 | // perform an optimized search that finds both elements in one run |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
974 | cx_linked_list *ll = (cx_linked_list *) list; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
975 | size_t mid = list->collection.size / 2; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
976 | size_t left, right; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
977 | if (i < j) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
978 | left = i; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
979 | right = j; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
980 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
981 | left = j; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
982 | right = i; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
983 | } |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
984 | void *nleft = NULL, *nright = NULL; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
985 | if (left < mid && right < mid) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
986 | // case 1: both items left from mid |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
987 | nleft = cx_ll_node_at(ll, left); |
|
807
c8d692131b1e
remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents:
764
diff
changeset
|
988 | assert(nleft != NULL); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
989 | nright = nleft; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
990 | for (size_t c = left; c < right; c++) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
991 | nright = CX_LL_PTR(nright, ll->loc_next); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
992 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
993 | } else if (left >= mid && right >= mid) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
994 | // case 2: both items right from mid |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
995 | nright = cx_ll_node_at(ll, right); |
|
807
c8d692131b1e
remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents:
764
diff
changeset
|
996 | assert(nright != NULL); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
997 | nleft = nright; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
998 | for (size_t c = right; c > left; c--) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
999 | nleft = CX_LL_PTR(nleft, ll->loc_prev); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1000 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1001 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1002 | // case 3: one item left, one item right |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1003 | |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1004 | // chose the closest to begin / end |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1005 | size_t closest; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1006 | size_t other; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1007 | size_t diff2boundary = list->collection.size - right - 1; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1008 | if (left <= diff2boundary) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1009 | closest = left; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1010 | other = right; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1011 | nleft = cx_ll_node_at(ll, left); |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1012 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1013 | closest = right; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1014 | other = left; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1015 | diff2boundary = left; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1016 | nright = cx_ll_node_at(ll, right); |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1017 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1018 | |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1019 | // is other element closer to us or closer to boundary? |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1020 | if (right - left <= diff2boundary) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1021 | // search other element starting from already found element |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1022 | if (closest == left) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1023 | nright = nleft; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1024 | for (size_t c = left; c < right; c++) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1025 | nright = CX_LL_PTR(nright, ll->loc_next); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1026 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1027 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1028 | nleft = nright; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1029 | for (size_t c = right; c > left; c--) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1030 | nleft = CX_LL_PTR(nleft, ll->loc_prev); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1031 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1032 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1033 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1034 | // search other element starting at the boundary |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1035 | if (closest == left) { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1036 | nright = cx_ll_node_at(ll, other); |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1037 | } else { |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1038 | nleft = cx_ll_node_at(ll, other); |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1039 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1040 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1041 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1042 | |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1043 | void *prev = CX_LL_PTR(nleft, ll->loc_prev); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1044 | void *next = CX_LL_PTR(nright, ll->loc_next); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1045 | void *midstart = CX_LL_PTR(nleft, ll->loc_next); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1046 | void *midend = CX_LL_PTR(nright, ll->loc_prev); |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1047 | |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1048 | if (prev == NULL) { |
|
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1049 | ll->begin = nright; |
|
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1050 | } else { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1051 | CX_LL_PTR(prev, ll->loc_next) = nright; |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1052 | } |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1053 | CX_LL_PTR(nright, ll->loc_prev) = prev; |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1054 | if (midstart == nright) { |
|
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1055 | // special case: both nodes are adjacent |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1056 | CX_LL_PTR(nright, ll->loc_next) = nleft; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1057 | CX_LL_PTR(nleft, ll->loc_prev) = nright; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1058 | } else { |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1059 | // likely case: a chain is between the two nodes |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1060 | CX_LL_PTR(nright, ll->loc_next) = midstart; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1061 | CX_LL_PTR(midstart, ll->loc_prev) = nright; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1062 | CX_LL_PTR(midend, ll->loc_next) = nleft; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1063 | CX_LL_PTR(nleft, ll->loc_prev) = midend; |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1064 | } |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1065 | CX_LL_PTR(nleft, ll->loc_next) = next; |
|
1113
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1066 | if (next == NULL) { |
|
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1067 | ll->end = nleft; |
|
dce04550fbef
remove CX_LINKED_LIST_SWAP_SBO_SIZE - fixes #551
Mike Becker <universe@uap-core.de>
parents:
1111
diff
changeset
|
1068 | } else { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1069 | CX_LL_PTR(next, ll->loc_prev) = nleft; |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1070 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1071 | |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1072 | return 0; |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1073 | } |
|
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1074 | |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
1075 | static void *cx_ll_at( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1076 | const struct cx_list_s *list, |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
1077 | size_t index |
|
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
1078 | ) { |
|
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
1079 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1080 | char *node = cx_ll_node_at(ll, index); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1081 | return node == NULL ? NULL : node + ll->loc_data; |
|
466
28bc3e10ac28
add special linked list implementation for storing pointers
Mike Becker <universe@uap-core.de>
parents:
457
diff
changeset
|
1082 | } |
|
28bc3e10ac28
add special linked list implementation for storing pointers
Mike Becker <universe@uap-core.de>
parents:
457
diff
changeset
|
1083 | |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1084 | static size_t cx_ll_find_remove( |
|
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
1085 | struct cx_list_s *list, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1086 | const void *elem, |
|
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
1087 | bool remove |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
1088 | ) { |
|
1225
086e63c8dd06
fix cxListFind() crashing on empty linked lists
Mike Becker <universe@uap-core.de>
parents:
1162
diff
changeset
|
1089 | if (list->collection.size == 0) return 0; |
|
086e63c8dd06
fix cxListFind() crashing on empty linked lists
Mike Becker <universe@uap-core.de>
parents:
1162
diff
changeset
|
1090 | |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1091 | size_t index; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1092 | cx_linked_list *ll = (cx_linked_list *) list; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1093 | char *node = cx_linked_list_find( |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1094 | ll->begin, |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1095 | ll->loc_next, ll->loc_data, |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1096 | list->collection.cmpfunc, elem, |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1097 | &index |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1098 | ); |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1099 | if (node == NULL) { |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1100 | return list->collection.size; |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1101 | } |
|
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
1102 | if (remove) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1103 | cx_invoke_destructor(list, node + ll->loc_data); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1104 | cx_linked_list_remove(&ll->begin, &ll->end, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1105 | ll->loc_prev, ll->loc_next, node); |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1106 | list->collection.size--; |
|
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1107 | cxFree(list->collection.allocator, node); |
|
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
1108 | } |
|
1162
e3bb67b72d33
remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents:
1113
diff
changeset
|
1109 | return index; |
|
466
28bc3e10ac28
add special linked list implementation for storing pointers
Mike Becker <universe@uap-core.de>
parents:
457
diff
changeset
|
1110 | } |
|
28bc3e10ac28
add special linked list implementation for storing pointers
Mike Becker <universe@uap-core.de>
parents:
457
diff
changeset
|
1111 | |
|
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
499
diff
changeset
|
1112 | static void cx_ll_sort(struct cx_list_s *list) { |
|
470
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
469
diff
changeset
|
1113 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1114 | cx_linked_list_sort(&ll->begin, &ll->end, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1115 | ll->loc_prev, ll->loc_next, ll->loc_data, |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1116 | list->collection.cmpfunc); |
|
470
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
469
diff
changeset
|
1117 | } |
|
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
469
diff
changeset
|
1118 | |
|
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
499
diff
changeset
|
1119 | static void cx_ll_reverse(struct cx_list_s *list) { |
| 490 | 1120 | cx_linked_list *ll = (cx_linked_list *) list; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1121 | cx_linked_list_reverse(&ll->begin, &ll->end, ll->loc_prev, ll->loc_next); |
| 490 | 1122 | } |
| 1123 | ||
|
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1124 | static int cx_ll_compare( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1125 | const struct cx_list_s *list, |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1126 | const struct cx_list_s *other |
|
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1127 | ) { |
|
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1128 | cx_linked_list *left = (cx_linked_list *) list; |
|
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1129 | cx_linked_list *right = (cx_linked_list *) other; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1130 | assert(left->loc_next == right->loc_next); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1131 | assert(left->loc_data == right->loc_data); |
|
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1132 | return cx_linked_list_compare(left->begin, right->begin, |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1133 | left->loc_next, left->loc_data, |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1134 | list->collection.cmpfunc); |
|
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1135 | } |
|
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1136 | |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1137 | static bool cx_ll_iter_valid(const void *it) { |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1138 | const struct cx_iterator_s *iter = it; |
|
495
2856c74e18ba
add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents:
494
diff
changeset
|
1139 | return iter->elem_handle != NULL; |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1140 | } |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1141 | |
|
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
1142 | static void cx_ll_iter_next(void *it) { |
|
853
d4baf4dd55c3
simplify iterator structures
Mike Becker <universe@uap-core.de>
parents:
850
diff
changeset
|
1143 | struct cx_iterator_s *iter = it; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1144 | cx_linked_list *ll = iter->src_handle; |
|
854
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1145 | if (iter->base.remove) { |
|
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1146 | iter->base.remove = false; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1147 | struct cx_list_s *list = iter->src_handle; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1148 | char *node = iter->elem_handle; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1149 | iter->elem_handle = CX_LL_PTR(node, ll->loc_next); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1150 | cx_invoke_destructor(list, node + ll->loc_data); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1151 | cx_linked_list_remove(&ll->begin, &ll->end, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1152 | ll->loc_prev, ll->loc_next, node); |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1153 | list->collection.size--; |
|
1387
9bdd053820b7
the elem_count member of an iterator was not updated after removing an element flagged by cxIteratorFlagRemoval() - fixes #728
Mike Becker <universe@uap-core.de>
parents:
1367
diff
changeset
|
1154 | iter->elem_count--; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1155 | cxFree(list->collection.allocator, node); |
|
495
2856c74e18ba
add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents:
494
diff
changeset
|
1156 | } else { |
|
2856c74e18ba
add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents:
494
diff
changeset
|
1157 | iter->index++; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1158 | void *node = iter->elem_handle; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1159 | iter->elem_handle = CX_LL_PTR(node, ll->loc_next); |
|
495
2856c74e18ba
add the feature to remove items during iteration
Mike Becker <universe@uap-core.de>
parents:
494
diff
changeset
|
1160 | } |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1161 | } |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1162 | |
|
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1163 | static void cx_ll_iter_prev(void *it) { |
|
853
d4baf4dd55c3
simplify iterator structures
Mike Becker <universe@uap-core.de>
parents:
850
diff
changeset
|
1164 | struct cx_iterator_s *iter = it; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1165 | cx_linked_list *ll = iter->src_handle; |
|
854
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1166 | if (iter->base.remove) { |
|
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1167 | iter->base.remove = false; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1168 | struct cx_list_s *list = iter->src_handle; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1169 | char *node = iter->elem_handle; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1170 | iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); |
|
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1171 | iter->index--; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1172 | cx_invoke_destructor(list, node + ll->loc_data); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1173 | cx_linked_list_remove(&ll->begin, &ll->end, |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1174 | ll->loc_prev, ll->loc_next, node); |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1175 | list->collection.size--; |
|
1387
9bdd053820b7
the elem_count member of an iterator was not updated after removing an element flagged by cxIteratorFlagRemoval() - fixes #728
Mike Becker <universe@uap-core.de>
parents:
1367
diff
changeset
|
1176 | iter->elem_count--; |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1177 | cxFree(list->collection.allocator, node); |
|
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1178 | } else { |
|
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1179 | iter->index--; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1180 | char *node = iter->elem_handle; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1181 | iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); |
|
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1182 | } |
|
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1183 | } |
|
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1184 | |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1185 | static void *cx_ll_iter_current(const void *it) { |
|
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1186 | const struct cx_iterator_s *iter = it; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1187 | const cx_linked_list *ll = iter->src_handle; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1188 | char *node = iter->elem_handle; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1189 | return node + ll->loc_data; |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1190 | } |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1191 | |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1192 | static CxIterator cx_ll_iterator( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1193 | const struct cx_list_s *list, |
|
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1194 | size_t index, |
|
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
1195 | bool backwards |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1196 | ) { |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1197 | CxIterator iter; |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1198 | iter.index = index; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1199 | iter.src_handle = (void*)list; |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1200 | iter.elem_handle = cx_ll_node_at((const cx_linked_list *) list, index); |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1201 | iter.elem_size = list->collection.elem_size; |
|
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1202 | iter.elem_count = list->collection.size; |
|
854
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1203 | iter.base.valid = cx_ll_iter_valid; |
|
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1204 | iter.base.current = cx_ll_iter_current; |
|
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1205 | iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next; |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1206 | iter.base.allow_remove = true; |
|
854
fe0d69d72bcd
fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents:
853
diff
changeset
|
1207 | iter.base.remove = false; |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1208 | return iter; |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1209 | } |
|
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1210 | |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1211 | static int cx_ll_insert_iter( |
|
853
d4baf4dd55c3
simplify iterator structures
Mike Becker <universe@uap-core.de>
parents:
850
diff
changeset
|
1212 | CxIterator *iter, |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1213 | const void *elem, |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1214 | int prepend |
|
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1215 | ) { |
|
1429
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1216 | struct cx_list_s *list = iter->src_handle; |
|
6e0c3a8a914a
remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents:
1426
diff
changeset
|
1217 | cx_linked_list *ll = iter->src_handle; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1218 | void *node = iter->elem_handle; |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1219 | if (node != NULL) { |
|
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1220 | assert(prepend >= 0 && prepend <= 1); |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1221 | void *choice[2] = {node, CX_LL_PTR(node, ll->loc_prev)}; |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1222 | int result = cx_ll_insert_at(list, choice[prepend], elem); |
|
874
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1223 | if (result == 0) { |
|
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1224 | iter->elem_count++; |
|
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1225 | if (prepend) { |
|
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1226 | iter->index++; |
|
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1227 | } |
|
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1228 | } |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1229 | return result; |
|
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1230 | } else { |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
1231 | if (cx_ll_insert_element(list, list->collection.size, elem) == NULL) { |
|
1504
467a77a85f43
add missing coverage exclusions for linked_list.c
Mike Becker <universe@uap-core.de>
parents:
1482
diff
changeset
|
1232 | return 1; // LCOV_EXCL_LINE |
|
874
cdce47f34d48
fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents:
856
diff
changeset
|
1233 | } |
|
1316
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
1234 | iter->elem_count++; |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
1235 | iter->index = list->collection.size; |
|
c41538edfcef
add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents:
1225
diff
changeset
|
1236 | return 0; |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1237 | } |
|
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1238 | } |
|
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1239 | |
| 524 | 1240 | static void cx_ll_destructor(CxList *list) { |
| 1241 | cx_linked_list *ll = (cx_linked_list *) list; | |
| 1242 | ||
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1243 | char *node = ll->begin; |
| 524 | 1244 | while (node) { |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1245 | cx_invoke_destructor(list, node + ll->loc_data); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1246 | void *next = CX_LL_PTR(node, ll->loc_next); |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1247 | cxFree(list->collection.allocator, node); |
| 524 | 1248 | node = next; |
| 1249 | } | |
|
708
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
703
diff
changeset
|
1250 | |
|
856
6bbbf219251d
fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents:
855
diff
changeset
|
1251 | cxFree(list->collection.allocator, list); |
| 524 | 1252 | } |
| 1253 | ||
|
451
db06dda7ac4d
make cx_linked_list_class static
Mike Becker <universe@uap-core.de>
parents:
448
diff
changeset
|
1254 | static cx_list_class cx_linked_list_class = { |
| 524 | 1255 | cx_ll_destructor, |
|
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
1256 | cx_ll_insert_element, |
|
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
1257 | cx_ll_insert_array, |
|
879
9c24a4eb5ac9
implement optimized sorted insert for linked lists - resolves #415
Mike Becker <universe@uap-core.de>
parents:
876
diff
changeset
|
1258 | cx_ll_insert_sorted, |
|
1419
e46406fd1b3c
add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents:
1387
diff
changeset
|
1259 | cx_ll_insert_unique, |
|
499
3dc9075df822
add cxListInsertAfter() and cxListInsertBefore()
Mike Becker <universe@uap-core.de>
parents:
495
diff
changeset
|
1260 | cx_ll_insert_iter, |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1261 | cx_ll_remove, |
|
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
1262 | cx_ll_clear, |
|
653
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
1263 | cx_ll_swap, |
|
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
1264 | cx_ll_at, |
|
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
1265 | cx_ll_find_remove, |
|
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
487
diff
changeset
|
1266 | cx_ll_sort, |
| 490 | 1267 | cx_ll_compare, |
|
494
6ce8cfa10a96
add iterator interface + linked list iterator
Mike Becker <universe@uap-core.de>
parents:
490
diff
changeset
|
1268 | cx_ll_reverse, |
|
1482
6769cb72521b
implement a new allocation strategy for array lists and add cxListReserve() and cxListShrink()
Mike Becker <universe@uap-core.de>
parents:
1435
diff
changeset
|
1269 | NULL, // no overallocation supported |
|
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
1270 | cx_ll_iterator, |
|
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1271 | }; |
|
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1272 | |
|
670
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
1273 | CxList *cxLinkedListCreate( |
|
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
880
diff
changeset
|
1274 | const CxAllocator *allocator, |
|
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
1275 | cx_compare_func comparator, |
|
855
35bcb3216c0d
fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents:
854
diff
changeset
|
1276 | size_t elem_size |
|
481
eef025d82a34
add several new linked list functions
Mike Becker <universe@uap-core.de>
parents:
480
diff
changeset
|
1277 | ) { |
|
670
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
1278 | if (allocator == NULL) { |
|
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
1279 | allocator = cxDefaultAllocator; |
|
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
1280 | } |
|
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
1281 | |
|
503
a89857072ace
add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
1282 | cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); |
|
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
524
diff
changeset
|
1283 | if (list == NULL) return NULL; |
|
1367
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1284 | list->loc_prev = 0; |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1285 | list->loc_next = sizeof(void*); |
|
6b3d52dd176e
change linked_list.c to allow custom data in nodes + implement cx_kv_list_set_key()
Mike Becker <universe@uap-core.de>
parents:
1360
diff
changeset
|
1286 | list->loc_data = sizeof(void*)*2; |
|
1532
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1287 | list->loc_extra = -1; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1288 | list->extra_data_len = 0; |
|
1111
78eeeb950883
remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents:
1065
diff
changeset
|
1289 | cx_list_init((CxList*)list, &cx_linked_list_class, |
|
78eeeb950883
remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents:
1065
diff
changeset
|
1290 | allocator, comparator, elem_size); |
|
667
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
1291 | |
|
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
499
diff
changeset
|
1292 | return (CxList *) list; |
|
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
1293 | } |
|
1532
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1294 | |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1295 | void cx_linked_list_extra_data(cx_linked_list *list, size_t len) { |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1296 | list->extra_data_len = len; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1297 | |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1298 | off_t loc_extra = list->loc_data + list->base.collection.elem_size; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1299 | size_t alignment = alignof(void*); |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1300 | size_t padding = alignment - (loc_extra % alignment); |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1301 | list->loc_extra = loc_extra + padding; |
|
313fd460d264
fix missing memory alignment for linked list node extra data
Mike Becker <universe@uap-core.de>
parents:
1504
diff
changeset
|
1302 | } |