Tue, 16 Jan 2024 23:43:27 +0100
improve program flow - relates to #353
606 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/array_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" |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
31 | #include <assert.h> |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
32 | #include <string.h> |
606 | 33 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
34 | // LOW LEVEL ARRAY LIST FUNCTIONS |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
35 | |
612
820ee59121b4
fix typo in enum cx_array_copy_result
Mike Becker <universe@uap-core.de>
parents:
611
diff
changeset
|
36 | enum cx_array_copy_result cx_array_copy( |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
37 | void **target, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
38 | size_t *size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
39 | size_t *capacity, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
40 | size_t index, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
41 | void const *src, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
42 | size_t elem_size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
43 | size_t elem_count, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
44 | struct cx_array_reallocator_s *reallocator |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
45 | ) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
46 | // assert pointers |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
47 | assert(target != NULL); |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
48 | assert(size != NULL); |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
49 | assert(src != NULL); |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
50 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
51 | // determine capacity |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
52 | size_t cap = capacity == NULL ? *size : *capacity; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
53 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
54 | // check if resize is required |
627
cc8cbabd27cd
fix cx_array_copy() unintentionally shrinking the array
Mike Becker <universe@uap-core.de>
parents:
626
diff
changeset
|
55 | size_t minsize = index + elem_count; |
cc8cbabd27cd
fix cx_array_copy() unintentionally shrinking the array
Mike Becker <universe@uap-core.de>
parents:
626
diff
changeset
|
56 | size_t newsize = *size < minsize ? minsize : *size; |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
57 | bool needrealloc = newsize > cap; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
58 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
59 | // reallocate if possible |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
60 | if (needrealloc) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
61 | // a reallocator and a capacity variable must be available |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
62 | if (reallocator == NULL || capacity == NULL) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
63 | return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
64 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
65 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
66 | // check, if we need to repair the src pointer |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
67 | uintptr_t targetaddr = (uintptr_t) *target; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
68 | uintptr_t srcaddr = (uintptr_t) src; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
69 | bool repairsrc = targetaddr <= srcaddr |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
70 | && srcaddr < targetaddr + cap * elem_size; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
71 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
72 | // calculate new capacity (next number divisible by 16) |
625
a4c4a50c067a
fix calculation of new capacity in cx_array_copy()
Mike Becker <universe@uap-core.de>
parents:
624
diff
changeset
|
73 | cap = newsize - (newsize % 16) + 16; |
a4c4a50c067a
fix calculation of new capacity in cx_array_copy()
Mike Becker <universe@uap-core.de>
parents:
624
diff
changeset
|
74 | assert(cap > newsize); |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
75 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
76 | // perform reallocation |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
77 | void *newmem = reallocator->realloc( |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
78 | *target, cap, elem_size, reallocator |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
79 | ); |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
80 | if (newmem == NULL) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
81 | return CX_ARRAY_COPY_REALLOC_FAILED; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
82 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
83 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
84 | // repair src pointer, if necessary |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
85 | if (repairsrc) { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
86 | src = ((char *) newmem) + (srcaddr - targetaddr); |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
87 | } |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
88 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
89 | // store new pointer and capacity |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
90 | *target = newmem; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
91 | *capacity = cap; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
92 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
93 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
94 | // determine target pointer |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
95 | char *start = *target; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
96 | start += index * elem_size; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
97 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
98 | // copy elements and set new size |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
99 | memmove(start, src, elem_count * elem_size); |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
100 | *size = newsize; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
101 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
102 | // return successfully |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
103 | return CX_ARRAY_COPY_SUCCESS; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
104 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
105 | |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
106 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
735
b686d0c98c62
unify the list swap SBO sizes
Mike Becker <universe@uap-core.de>
parents:
708
diff
changeset
|
107 | #define CX_ARRAY_SWAP_SBO_SIZE 128 |
643
5700ba9154ab
#228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
108 | #endif |
807
c8d692131b1e
remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents:
804
diff
changeset
|
109 | unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE; |
804
5136f2fc32ec
add CX_DISABLE_ARRAY_LIST_SWAP_SBO flag
Mike Becker <universe@uap-core.de>
parents:
764
diff
changeset
|
110 | |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
111 | void cx_array_swap( |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
112 | void *arr, |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
113 | size_t elem_size, |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
114 | size_t idx1, |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
115 | size_t idx2 |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
116 | ) { |
660 | 117 | assert(arr != NULL); |
118 | ||
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
119 | // short circuit |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
120 | if (idx1 == idx2) return; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
121 | |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
122 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
123 | void *tmp; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
124 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
125 | // decide if we can use the local buffer |
807
c8d692131b1e
remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents:
804
diff
changeset
|
126 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
127 | tmp = malloc(elem_size); |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
128 | // we don't want to enforce error handling |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
129 | if (tmp == NULL) abort(); |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
130 | } else { |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
131 | tmp = sbo_mem; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
132 | } |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
133 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
134 | // calculate memory locations |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
135 | char *left = arr, *right = arr; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
136 | left += idx1 * elem_size; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
137 | right += idx2 * elem_size; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
138 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
139 | // three-way swap |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
140 | memcpy(tmp, left, elem_size); |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
141 | memcpy(left, right, elem_size); |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
142 | memcpy(right, tmp, elem_size); |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
143 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
144 | // free dynamic memory, if it was needed |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
145 | if (tmp != sbo_mem) { |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
146 | free(tmp); |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
147 | } |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
148 | } |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
149 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
150 | // HIGH LEVEL ARRAY LIST FUNCTIONS |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
151 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
152 | typedef struct { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
153 | struct cx_list_s base; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
154 | void *data; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
155 | size_t capacity; |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
156 | struct cx_array_reallocator_s reallocator; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
157 | } cx_array_list; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
158 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
159 | static void *cx_arl_realloc( |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
160 | void *array, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
161 | size_t capacity, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
162 | size_t elem_size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
163 | struct cx_array_reallocator_s *alloc |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
164 | ) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
165 | // retrieve the pointer to the list allocator |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
166 | CxAllocator const *al = alloc->ptr1; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
167 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
168 | // use the list allocator to reallocate the memory |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
169 | return cxRealloc(al, array, capacity * elem_size); |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
170 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
171 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
172 | static void cx_arl_destructor(struct cx_list_s *list) { |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
173 | cx_array_list *arl = (cx_array_list *) list; |
708
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
174 | |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
175 | char *ptr = arl->data; |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
176 | |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
177 | if (list->simple_destructor) { |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
178 | for (size_t i = 0; i < list->size; i++) { |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
179 | cx_invoke_simple_destructor(list, ptr); |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
180 | ptr += list->item_size; |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
181 | } |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
182 | } |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
183 | if (list->advanced_destructor) { |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
184 | for (size_t i = 0; i < list->size; i++) { |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
185 | cx_invoke_advanced_destructor(list, ptr); |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
186 | ptr += list->item_size; |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
187 | } |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
188 | } |
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
189 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
190 | cxFree(list->allocator, arl->data); |
708
1caed6c9ba68
fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents:
699
diff
changeset
|
191 | cxFree(list->allocator, list); |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
192 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
193 | |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
194 | static size_t cx_arl_insert_array( |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
195 | struct cx_list_s *list, |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
196 | size_t index, |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
197 | void const *array, |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
198 | size_t n |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
199 | ) { |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
200 | // out of bounds and special case check |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
201 | if (index > list->size || n == 0) return 0; |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
202 | |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
203 | // get a correctly typed pointer to the list |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
204 | cx_array_list *arl = (cx_array_list *) list; |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
205 | |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
206 | // do we need to move some elements? |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
207 | if (index < list->size) { |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
208 | char const *first_to_move = (char const *) arl->data; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
209 | first_to_move += index * list->item_size; |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
210 | size_t elems_to_move = list->size - index; |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
211 | size_t start_of_moved = index + n; |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
212 | |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
213 | if (CX_ARRAY_COPY_SUCCESS != cx_array_copy( |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
214 | &arl->data, |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
215 | &list->size, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
216 | &arl->capacity, |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
217 | start_of_moved, |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
218 | first_to_move, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
219 | list->item_size, |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
220 | elems_to_move, |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
221 | &arl->reallocator |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
222 | )) { |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
223 | // if moving existing elems is unsuccessful, abort |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
224 | return 0; |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
225 | } |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
226 | } |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
227 | |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
228 | // note that if we had to move the elements, the following operation |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
229 | // is guaranteed to succeed, because we have the memory already allocated |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
230 | // therefore, it is impossible to leave this function with an invalid array |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
231 | |
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
232 | // place the new elements |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
233 | if (CX_ARRAY_COPY_SUCCESS == cx_array_copy( |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
234 | &arl->data, |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
235 | &list->size, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
236 | &arl->capacity, |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
237 | index, |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
238 | array, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
239 | list->item_size, |
629
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
240 | n, |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
241 | &arl->reallocator |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
242 | )) { |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
243 | return n; |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
244 | } else { |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
245 | // array list implementation is "all or nothing" |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
246 | return 0; |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
247 | } |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
248 | } |
6c81ee4f11ad
#224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
249 | |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
250 | static int cx_arl_insert_element( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
251 | struct cx_list_s *list, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
252 | size_t index, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
253 | void const *element |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
254 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
255 | return 1 != cx_arl_insert_array(list, index, element, 1); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
256 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
257 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
258 | static int cx_arl_insert_iter( |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
259 | struct cx_mut_iterator_s *iter, |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
260 | void const *elem, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
261 | int prepend |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
262 | ) { |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
263 | struct cx_list_s *list = iter->src_handle; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
264 | if (iter->index < list->size) { |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
265 | int result = cx_arl_insert_element( |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
266 | list, |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
267 | iter->index + 1 - prepend, |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
268 | elem |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
269 | ); |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
270 | if (result == 0 && prepend != 0) { |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
271 | iter->index++; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
272 | iter->elem_handle = ((char *) iter->elem_handle) + list->item_size; |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
273 | } |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
274 | return result; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
275 | } else { |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
276 | int result = cx_arl_insert_element(list, list->size, elem); |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
277 | iter->index = list->size; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
278 | return result; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
279 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
280 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
281 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
282 | static int cx_arl_remove( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
283 | struct cx_list_s *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
284 | size_t index |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
285 | ) { |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
286 | cx_array_list *arl = (cx_array_list *) list; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
287 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
288 | // out-of-bounds check |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
289 | if (index >= list->size) { |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
290 | return 1; |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
291 | } |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
292 | |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
293 | // content destruction |
678 | 294 | cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size); |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
295 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
296 | // short-circuit removal of last element |
624
b0bdff7d8203
#219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents:
623
diff
changeset
|
297 | if (index == list->size - 1) { |
b0bdff7d8203
#219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents:
623
diff
changeset
|
298 | list->size--; |
b0bdff7d8203
#219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents:
623
diff
changeset
|
299 | return 0; |
b0bdff7d8203
#219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents:
623
diff
changeset
|
300 | } |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
301 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
302 | // just move the elements starting at index to the left |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
303 | int result = cx_array_copy( |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
304 | &arl->data, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
305 | &list->size, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
306 | &arl->capacity, |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
307 | index, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
308 | ((char *) arl->data) + (index + 1) * list->item_size, |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
309 | list->item_size, |
626
254cc61c71a0
#219: fix off-by-one bug in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents:
625
diff
changeset
|
310 | list->size - index - 1, |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
311 | &arl->reallocator |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
312 | ); |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
313 | if (result == 0) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
314 | // decrease the size |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
315 | list->size--; |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
316 | } |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
317 | return result; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
318 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
319 | |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
320 | static void cx_arl_clear(struct cx_list_s *list) { |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
321 | if (list->size == 0) return; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
322 | |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
323 | cx_array_list *arl = (cx_array_list *) list; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
324 | char *ptr = arl->data; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
325 | |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
326 | if (list->simple_destructor) { |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
327 | for (size_t i = 0; i < list->size; i++) { |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
328 | cx_invoke_simple_destructor(list, ptr); |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
329 | ptr += list->item_size; |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
330 | } |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
331 | } |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
332 | if (list->advanced_destructor) { |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
333 | for (size_t i = 0; i < list->size; i++) { |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
334 | cx_invoke_advanced_destructor(list, ptr); |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
335 | ptr += list->item_size; |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
336 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
337 | } |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
338 | |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
339 | memset(arl->data, 0, list->size * list->item_size); |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
340 | list->size = 0; |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
341 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
342 | |
647
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
343 | static int cx_arl_swap( |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
344 | struct cx_list_s *list, |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
345 | size_t i, |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
346 | size_t j |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
347 | ) { |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
348 | if (i >= list->size || j >= list->size) return 1; |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
349 | cx_array_list *arl = (cx_array_list *) list; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
350 | cx_array_swap(arl->data, list->item_size, i, j); |
647
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
351 | return 0; |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
352 | } |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
353 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
354 | static void *cx_arl_at( |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
355 | struct cx_list_s const *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
356 | size_t index |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
357 | ) { |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
358 | if (index < list->size) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
359 | cx_array_list const *arl = (cx_array_list const *) list; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
360 | char *space = arl->data; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
361 | return space + index * list->item_size; |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
362 | } else { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
363 | return NULL; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
364 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
365 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
366 | |
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
367 | static ssize_t cx_arl_find_remove( |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
368 | struct cx_list_s *list, |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
369 | void const *elem, |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
370 | bool remove |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
371 | ) { |
660 | 372 | assert(list->cmpfunc != NULL); |
699
35b2b99ee523
make list find return a negative value when elem not found
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
373 | assert(list->size < SIZE_MAX / 2); |
614
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
374 | char *cur = ((cx_array_list const *) list)->data; |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
375 | |
699
35b2b99ee523
make list find return a negative value when elem not found
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
376 | for (ssize_t i = 0; i < (ssize_t) list->size; i++) { |
614
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
377 | if (0 == list->cmpfunc(elem, cur)) { |
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
378 | if (remove) { |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
379 | if (0 == cx_arl_remove(list, i)) { |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
380 | return i; |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
381 | } else { |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
382 | return -1; |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
383 | } |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
384 | } else { |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
385 | return i; |
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
386 | } |
614
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
387 | } |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
388 | cur += list->item_size; |
614
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
389 | } |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
390 | |
699
35b2b99ee523
make list find return a negative value when elem not found
Mike Becker <universe@uap-core.de>
parents:
678
diff
changeset
|
391 | return -1; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
392 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
393 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
394 | static void cx_arl_sort(struct cx_list_s *list) { |
660 | 395 | assert(list->cmpfunc != NULL); |
615
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
396 | qsort(((cx_array_list *) list)->data, |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
397 | list->size, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
398 | list->item_size, |
615
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
399 | list->cmpfunc |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
400 | ); |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
401 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
402 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
403 | static int cx_arl_compare( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
404 | struct cx_list_s const *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
405 | struct cx_list_s const *other |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
406 | ) { |
660 | 407 | assert(list->cmpfunc != NULL); |
622
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
408 | if (list->size == other->size) { |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
409 | char const *left = ((cx_array_list const *) list)->data; |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
410 | char const *right = ((cx_array_list const *) other)->data; |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
411 | for (size_t i = 0; i < list->size; i++) { |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
412 | int d = list->cmpfunc(left, right); |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
413 | if (d != 0) { |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
414 | return d; |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
415 | } |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
416 | left += list->item_size; |
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
417 | right += other->item_size; |
622
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
418 | } |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
419 | return 0; |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
420 | } else { |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
421 | return list->size < other->size ? -1 : 1; |
3d93cd78aa20
#219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents:
620
diff
changeset
|
422 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
423 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
424 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
425 | static void cx_arl_reverse(struct cx_list_s *list) { |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
426 | if (list->size < 2) return; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
427 | void *data = ((cx_array_list const *) list)->data; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
428 | size_t half = list->size / 2; |
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
429 | for (size_t i = 0; i < half; i++) { |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
430 | cx_array_swap(data, list->item_size, i, list->size - 1 - i); |
623
21082350a590
#219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents:
622
diff
changeset
|
431 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
432 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
433 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
434 | static bool cx_arl_iter_valid(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
435 | struct cx_iterator_s const *iter = it; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
436 | struct cx_list_s const *list = iter->src_handle; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
437 | return iter->index < list->size; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
438 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
439 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
440 | static void *cx_arl_iter_current(void const *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
441 | struct cx_iterator_s const *iter = it; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
442 | return iter->elem_handle; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
443 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
444 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
445 | static void cx_arl_iter_next(void *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
446 | struct cx_iterator_base_s *itbase = it; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
447 | if (itbase->remove) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
448 | struct cx_mut_iterator_s *iter = it; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
449 | itbase->remove = false; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
450 | cx_arl_remove(iter->src_handle, iter->index); |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
451 | } else { |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
452 | struct cx_iterator_s *iter = it; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
453 | iter->index++; |
620
f220695aded6
#219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents:
619
diff
changeset
|
454 | iter->elem_handle = |
f220695aded6
#219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents:
619
diff
changeset
|
455 | ((char *) iter->elem_handle) |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
456 | + ((struct cx_list_s const *) iter->src_handle)->item_size; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
457 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
458 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
459 | |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
460 | static void cx_arl_iter_prev(void *it) { |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
461 | struct cx_iterator_base_s *itbase = it; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
462 | struct cx_mut_iterator_s *iter = it; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
463 | cx_array_list *const list = iter->src_handle; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
464 | if (itbase->remove) { |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
465 | itbase->remove = false; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
466 | cx_arl_remove(iter->src_handle, iter->index); |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
467 | } |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
468 | iter->index--; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
469 | if (iter->index < list->base.size) { |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
470 | iter->elem_handle = ((char *) list->data) |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
471 | + iter->index * list->base.item_size; |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
472 | } |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
473 | } |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
474 | |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
475 | static bool cx_arl_iter_flag_rm(void *it) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
476 | struct cx_iterator_base_s *iter = it; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
477 | if (iter->mutating) { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
478 | iter->remove = true; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
479 | return true; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
480 | } else { |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
481 | return false; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
482 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
483 | } |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
484 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
485 | static struct cx_iterator_s cx_arl_iterator( |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
486 | struct cx_list_s const *list, |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
487 | size_t index, |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
488 | bool backwards |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
489 | ) { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
490 | struct cx_iterator_s iter; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
491 | |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
492 | iter.index = index; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
493 | iter.src_handle = list; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
494 | iter.elem_handle = cx_arl_at(list, index); |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
495 | iter.base.valid = cx_arl_iter_valid; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
496 | iter.base.current = cx_arl_iter_current; |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
654
diff
changeset
|
497 | iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
498 | iter.base.flag_removal = cx_arl_iter_flag_rm; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
499 | iter.base.remove = false; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
500 | iter.base.mutating = false; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
501 | |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
502 | return iter; |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
629
diff
changeset
|
503 | } |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
504 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
505 | static cx_list_class cx_array_list_class = { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
506 | cx_arl_destructor, |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
507 | cx_arl_insert_element, |
638
eafb45eefc51
add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
508 | cx_arl_insert_array, |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
509 | cx_arl_insert_iter, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
510 | cx_arl_remove, |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
662
diff
changeset
|
511 | cx_arl_clear, |
647
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
643
diff
changeset
|
512 | cx_arl_swap, |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
513 | cx_arl_at, |
764
ccbdbd088455
add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents:
763
diff
changeset
|
514 | cx_arl_find_remove, |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
515 | cx_arl_sort, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
516 | cx_arl_compare, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
517 | cx_arl_reverse, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
518 | cx_arl_iterator, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
519 | }; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
520 | |
670
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
521 | CxList *cxArrayListCreate( |
606 | 522 | CxAllocator const *allocator, |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
523 | cx_compare_func comparator, |
606 | 524 | size_t item_size, |
525 | size_t initial_capacity | |
526 | ) { | |
670
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
527 | if (allocator == NULL) { |
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
528 | allocator = cxDefaultAllocator; |
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
529 | } |
4ad8ea3aee49
allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents:
667
diff
changeset
|
530 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
531 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
532 | if (list == NULL) return NULL; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
533 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
534 | list->base.cl = &cx_array_list_class; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
535 | list->base.allocator = allocator; |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
536 | list->capacity = initial_capacity; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
537 | |
667
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
538 | if (item_size > 0) { |
677
b09aae58bba4
refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents:
676
diff
changeset
|
539 | list->base.item_size = item_size; |
763
741a2040fa33
make cx_cmp_ptr default comparator for pointer lists - relates to #340
Mike Becker <universe@uap-core.de>
parents:
735
diff
changeset
|
540 | list->base.cmpfunc = comparator; |
667
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
541 | } else { |
678 | 542 | item_size = sizeof(void *); |
763
741a2040fa33
make cx_cmp_ptr default comparator for pointer lists - relates to #340
Mike Becker <universe@uap-core.de>
parents:
735
diff
changeset
|
543 | list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; |
667
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
544 | cxListStorePointers((CxList *) list); |
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
545 | } |
2f88a7c13a28
add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents:
666
diff
changeset
|
546 | |
676
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
547 | // allocate the array after the real item_size is known |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
548 | list->data = cxCalloc(allocator, initial_capacity, item_size); |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
549 | if (list->data == NULL) { |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
550 | cxFree(allocator, list); |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
551 | return NULL; |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
552 | } |
d0680a23d850
fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents:
670
diff
changeset
|
553 | |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
627
diff
changeset
|
554 | // configure the reallocator |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
555 | list->reallocator.realloc = cx_arl_realloc; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
556 | list->reallocator.ptr1 = (void *) allocator; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
557 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
558 | return (CxList *) list; |
606 | 559 | } |