Sun, 20 Nov 2022 16:22:50 +0100
missing const in cxListFind
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" | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
30 | #include <assert.h> |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
31 | #include <string.h> |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
32 | #include <stdint.h> |
606 | 33 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
34 | /* LOW LEVEL ARRAY LIST FUNCTIONS */ |
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 | ) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
46 | /* assert pointers */ |
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 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
51 | /* determine capacity */ |
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 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
54 | /* check if resize is required */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
55 | size_t newsize = index + elem_count; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
56 | bool needrealloc = newsize > cap; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
57 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
58 | /* reallocate if possible */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
59 | if (needrealloc) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
60 | /* a reallocator and a capacity variable must be available */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
61 | if (reallocator == NULL || capacity == NULL) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
62 | 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
|
63 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
64 | |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
65 | /* check, if we need to repair the src pointer */ |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
66 | uintptr_t targetaddr = (uintptr_t) *target; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
67 | uintptr_t srcaddr = (uintptr_t) src; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
68 | bool repairsrc = targetaddr <= srcaddr |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
69 | && srcaddr < targetaddr + cap * elem_size; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
70 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
71 | /* increase capacity linearly */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
72 | cap += 16; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
73 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
74 | /* perform reallocation */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
75 | void *newmem = reallocator->realloc( |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
76 | *target, cap, elem_size, reallocator |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
77 | ); |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
78 | if (newmem == NULL) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
79 | return CX_ARRAY_COPY_REALLOC_FAILED; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
80 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
81 | |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
82 | /* repair src pointer, if necessary */ |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
83 | if (repairsrc) { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
84 | src = ((char *) newmem) + (srcaddr - targetaddr); |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
85 | } |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
86 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
87 | /* store new pointer and capacity */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
88 | *target = newmem; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
89 | *capacity = cap; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
90 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
91 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
92 | /* determine target pointer */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
93 | char *start = *target; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
94 | start += index * elem_size; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
95 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
96 | /* copy elements and set new size */ |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
97 | 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
|
98 | *size = newsize; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
99 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
100 | /* return successfully */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
101 | return CX_ARRAY_COPY_SUCCESS; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
102 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
103 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
104 | /* HIGH LEVEL ARRAY LIST FUNCTIONS */ |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
105 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
106 | typedef struct { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
107 | struct cx_list_s base; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
108 | void *data; |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
109 | struct cx_array_reallocator_s reallocator; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
110 | } cx_array_list; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
111 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
112 | static void *cx_arl_realloc( |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
113 | void *array, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
114 | size_t capacity, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
115 | size_t elem_size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
116 | struct cx_array_reallocator_s *alloc |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
117 | ) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
118 | /* retrieve the pointer to the list allocator */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
119 | CxAllocator const *al = alloc->ptr1; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
120 | |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
121 | /* use the list allocator to reallocate the memory */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
122 | 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
|
123 | } |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
124 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
125 | 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
|
126 | cx_array_list *arl = (cx_array_list *) list; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
127 | cxFree(list->allocator, arl->data); |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
128 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
129 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
130 | static int cx_arl_add( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
131 | struct cx_list_s *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
132 | void const *elem |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
133 | ) { |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
134 | cx_array_list *arl = (cx_array_list *) list; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
135 | return cx_array_copy( |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
136 | &arl->data, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
137 | &list->size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
138 | &list->capacity, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
139 | list->size, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
140 | elem, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
141 | list->itemsize, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
142 | 1, |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
143 | &arl->reallocator |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
144 | ); |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
145 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
146 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
147 | static int cx_arl_insert( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
148 | struct cx_list_s *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
149 | size_t index, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
150 | void const *elem |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
151 | ) { |
611
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
152 | if (index > list->size) { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
153 | return 1; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
154 | } else if (index == list->size) { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
155 | return cx_arl_add(list, elem); |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
156 | } else { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
157 | cx_array_list *arl = (cx_array_list *) list; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
158 | |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
159 | /* move elements starting at index to the right */ |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
160 | if (cx_array_copy( |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
161 | &arl->data, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
162 | &list->size, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
163 | &list->capacity, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
164 | index + 1, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
165 | ((char *) arl->data) + index * list->itemsize, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
166 | list->itemsize, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
167 | list->size - index, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
168 | &arl->reallocator |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
169 | )) { |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
170 | return 1; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
171 | } |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
172 | |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
173 | /* place the element */ |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
174 | memcpy(((char *) arl->data) + index * list->itemsize, |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
175 | elem, list->itemsize); |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
176 | |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
177 | return 0; |
77efa5163ae5
#219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents:
610
diff
changeset
|
178 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
179 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
180 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
181 | static int cx_arl_insert_iter( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
182 | struct cx_iterator_s *iter, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
183 | void const *elem, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
184 | int prepend |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
185 | ) { |
619
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
186 | 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
|
187 | if (iter->index < list->size) { |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
188 | int result = cx_arl_insert( |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
189 | list, |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
190 | iter->index + 1 - prepend, |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
191 | elem |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
192 | ); |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
193 | if (result == 0 && prepend != 0) { |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
194 | iter->index++; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
195 | iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
196 | } |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
197 | return result; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
198 | } else { |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
199 | int result = cx_arl_add(list, elem); |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
200 | iter->index = list->size; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
201 | return result; |
5e58187ac707
#219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents:
616
diff
changeset
|
202 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
203 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
204 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
205 | static int cx_arl_remove( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
206 | struct cx_list_s *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
207 | size_t index |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
208 | ) { |
613
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
209 | /* out-of-bounds check */ |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
210 | if (index >= list->size) { |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
211 | return 1; |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
212 | } |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
213 | |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
214 | cx_array_list *arl = (cx_array_list *) list; |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
215 | |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
216 | /* just move the elements starting at index to the left */ |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
217 | int result = cx_array_copy( |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
218 | &arl->data, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
219 | &list->size, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
220 | &list->capacity, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
221 | index, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
222 | ((char *) arl->data) + (index + 1) * list->itemsize, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
223 | list->itemsize, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
224 | list->size - index, |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
225 | &arl->reallocator |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
226 | ); |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
227 | if (result == 0) { |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
228 | /* decrease the size */ |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
229 | list->size--; |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
230 | } |
85c08391a090
#219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents:
612
diff
changeset
|
231 | return result; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
232 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
233 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
234 | static void *cx_arl_at( |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
235 | struct cx_list_s const *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
236 | size_t index |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
237 | ) { |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
238 | if (index < list->size) { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
239 | 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
|
240 | char *space = arl->data; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
241 | return space + index * list->itemsize; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
242 | } else { |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
243 | return NULL; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
244 | } |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
245 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
246 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
247 | static size_t cx_arl_find( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
248 | struct cx_list_s const *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
249 | void const *elem |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
250 | ) { |
614
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
251 | char *cur = ((cx_array_list const *) list)->data; |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
252 | |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
253 | for (size_t i = 0; i < list->size; i++) { |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
254 | if (0 == list->cmpfunc(elem, cur)) { |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
255 | return i; |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
256 | } |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
257 | cur += list->itemsize; |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
258 | } |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
259 | |
7aaec630cf15
#219 array list: implement find
Mike Becker <universe@uap-core.de>
parents:
613
diff
changeset
|
260 | return list->size; |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
261 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
262 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
263 | static void cx_arl_sort(struct cx_list_s *list) { |
615
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
264 | qsort(((cx_array_list *) list)->data, |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
265 | list->size, |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
266 | list->itemsize, |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
267 | list->cmpfunc |
b52b66dcd44b
#219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents:
614
diff
changeset
|
268 | ); |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
269 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
270 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
271 | static int cx_arl_compare( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
272 | struct cx_list_s const *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
273 | struct cx_list_s const *other |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
274 | ) { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
275 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
276 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
277 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
278 | static void cx_arl_reverse(struct cx_list_s *list) { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
279 | |
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 | |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
282 | static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) { |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
283 | 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
|
284 | return iter->index < list->size; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
285 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
286 | |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
287 | static void *cx_arl_iter_current(struct cx_iterator_s const *iter) { |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
288 | return iter->elem_handle; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
289 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
290 | |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
291 | static void cx_arl_iter_next(struct cx_iterator_s *iter) { |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
292 | if (iter->remove) { |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
293 | iter->remove = false; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
294 | cx_arl_remove(iter->src_handle, iter->index); |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
295 | } else { |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
296 | iter->index++; |
620
f220695aded6
#219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents:
619
diff
changeset
|
297 | iter->elem_handle = |
f220695aded6
#219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents:
619
diff
changeset
|
298 | ((char *) iter->elem_handle) |
f220695aded6
#219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents:
619
diff
changeset
|
299 | + ((struct cx_list_s const *) iter->src_handle)->itemsize; |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
300 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
301 | } |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
302 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
303 | static struct cx_iterator_s cx_arl_iterator( |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
304 | struct cx_list_s *list, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
305 | size_t index |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
306 | ) { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
307 | struct cx_iterator_s iter; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
308 | |
616
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
309 | iter.index = index; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
310 | iter.src_handle = list; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
311 | iter.elem_handle = cx_arl_at(list, index); |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
312 | iter.valid = cx_arl_iter_valid; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
313 | iter.current = cx_arl_iter_current; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
314 | iter.next = cx_arl_iter_next; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
315 | iter.remove = false; |
af7d8a29fbc5
#219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents:
615
diff
changeset
|
316 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
317 | return iter; |
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 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
320 | 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
|
321 | cx_arl_destructor, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
322 | cx_arl_add, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
323 | cx_arl_insert, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
324 | cx_arl_insert_iter, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
325 | cx_arl_remove, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
326 | cx_arl_at, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
327 | cx_arl_find, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
328 | cx_arl_sort, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
329 | cx_arl_compare, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
330 | cx_arl_reverse, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
331 | cx_arl_iterator, |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
332 | }; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
333 | |
606 | 334 | CxList *cxArrayListCreate( |
335 | CxAllocator const *allocator, | |
336 | CxListComparator comparator, | |
337 | size_t item_size, | |
338 | size_t initial_capacity | |
339 | ) { | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
340 | 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
|
341 | if (list == NULL) return NULL; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
342 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
343 | list->data = cxCalloc(allocator, initial_capacity, item_size); |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
344 | if (list->data == NULL) { |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
345 | cxFree(allocator, list); |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
346 | return NULL; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
347 | } |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
348 | |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
349 | list->base.cl = &cx_array_list_class; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
350 | list->base.allocator = allocator; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
351 | list->base.cmpfunc = comparator; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
352 | list->base.itemsize = item_size; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
353 | list->base.capacity = initial_capacity; |
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
354 | |
610
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
355 | /* configure the reallocator */ |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
356 | list->reallocator.realloc = cx_arl_realloc; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
357 | list->reallocator.ptr1 = (void *) allocator; |
de5d3ee6435f
#219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents:
607
diff
changeset
|
358 | |
607
2d99e978dc34
implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents:
606
diff
changeset
|
359 | return (CxList *) list; |
606 | 360 | } |