cpp
[m6w6/ext-psi] / src / plist.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "plist.h"
29
30 struct psi_plist {
31 size_t size;
32 size_t count;
33 void (*dtor)(void *);
34 void *list[0];
35 };
36
37 #define PLIST_ELE(l, i) (((char *)(l)->list) + (l)->size * (i))
38 #define PLIST_CPY(list, dest, src) do { \
39 if (list->size == sizeof(void *)) { \
40 *(void **)(dest) = *(void **)(src); \
41 } else { \
42 memcpy(dest, src, list->size); \
43 } \
44 } while (0)
45 /* !!! adjust list->count prior reduction */
46 #define PLIST_MOV_REDUCE(l, i) PLIST_MOV_REDUCE_EX(l, i, 1)
47 #define PLIST_MOV_REDUCE_EX(l, i, n) memmove(PLIST_ELE(l, i), PLIST_ELE(l, i + n), (l)->size * ((l)->count - i))
48 /* !!! adjust list->count after expansion */
49 #define PLIST_MOV_EXPAND(l, i) PLIST_MOV_EXPAND_EX(l, i, 1)
50 #define PLIST_MOV_EXPAND_EX(l, i, n) memmove(PLIST_ELE(l, i + n), PLIST_ELE(l, i), (l)->size * ((l)->count - i))
51
52 struct psi_plist *psi_plist_init(void (*dtor)(void *)) {
53 return psi_plist_init_ex(0, dtor);
54 }
55 struct psi_plist *psi_plist_init_ex(size_t size, void (*dtor)(void *)) {
56 struct psi_plist *list;
57
58 if (!size) {
59 size = sizeof(void*);
60 }
61
62 list = calloc(1, sizeof(*list) + size);
63 list->size = size;
64 list->dtor = dtor;
65
66 return list;
67 }
68
69 void psi_plist_free(struct psi_plist *list) {
70 size_t i;
71
72 if (list->dtor) for (i = 0; i < list->count; ++i) {
73 list->dtor(PLIST_ELE(list, i));
74 }
75 free(list);
76 }
77
78 struct psi_plist *psi_plist_copy(struct psi_plist *list, void (*ctor)(void *))
79 {
80 size_t i;
81 struct psi_plist *copy = calloc(1, sizeof(*list) + list->size + list->count * list->size);
82
83 *copy = *list;
84 if (list->count) {
85 memcpy(copy->list, list->list, list->size * list->count);
86 }
87 if (ctor) {
88 for (i = 0; i < list->count; ++i) {
89 void *ptr = PLIST_ELE(copy, i);
90 ctor(ptr);
91 }
92 }
93 return copy;
94 }
95
96 size_t psi_plist_count(struct psi_plist *list) {
97 return list ? list->count : 0;
98 }
99
100 void **psi_plist_eles(struct psi_plist *list) {
101 return list ? list->list : NULL;
102 }
103
104 struct psi_plist *psi_plist_add(struct psi_plist *list, void *ptr) {
105 if (list && list->count) {
106 list = realloc(list, sizeof(*list) + list->size + list->count * list->size);
107 }
108 if (list) {
109 PLIST_CPY(list, PLIST_ELE(list, list->count++), ptr);
110 }
111 return list;
112 }
113
114 bool psi_plist_get(struct psi_plist *list, size_t index, void *ptr) {
115 if (list && list->count > index) {
116 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
117 return true;
118 }
119 return false;
120 }
121
122 bool psi_plist_del(struct psi_plist *list, size_t index, void *ptr) {
123 if (list && list->count > index) {
124 if (ptr) {
125 PLIST_CPY(list, ptr, PLIST_ELE(list, index));
126 }
127 if (--list->count > index) {
128 PLIST_MOV_REDUCE(list, index);
129 }
130 return true;
131 }
132 return false;
133 }
134
135 bool psi_plist_del_range(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
136 if (list) {
137 size_t offset_end = offset_start + num_eles - 1;
138
139 if (list->count <= offset_end) {
140 offset_end = list->count - 1;
141 }
142 if (offset_end >= offset_start) {
143 num_eles = 1 + offset_end - offset_start;
144
145 if (eles) {
146 memcpy(eles, PLIST_ELE(list, offset_start), num_eles * list->size);
147 }
148
149 if ((list->count -= num_eles)) {
150 PLIST_MOV_REDUCE_EX(list, offset_start, num_eles);
151 }
152 return true;
153 }
154 }
155 return false;
156 }
157
158 struct psi_plist *psi_plist_ins(struct psi_plist *list, size_t index, void *ptr) {
159 size_t new_count = MAX(list->count + 1, index);
160
161 if (list && new_count) {
162 list = realloc(list, sizeof(*list) + list->size + new_count * list->size);
163 }
164 if (list) {
165 PLIST_MOV_EXPAND(list, index);
166 PLIST_CPY(list, PLIST_ELE(list, index), ptr);
167 list->count = new_count;
168 }
169 return list;
170 }
171
172 struct psi_plist *psi_plist_ins_range(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
173 size_t new_count = MAX(offset_start, list->count) + num_eles;
174
175 if (list && new_count) {
176 list = realloc(list, sizeof(*list) + list->size + new_count * list->size);
177 }
178 if (list) {
179 size_t e;
180
181 PLIST_MOV_EXPAND_EX(list, offset_start, num_eles);
182 for (e = 0; e < num_eles; ++e) {
183 PLIST_CPY(list, PLIST_ELE(list, offset_start + e), &eles[e]);
184 }
185 list->count = new_count;
186 }
187 return list;
188 }
189
190 bool psi_plist_shift(struct psi_plist *list, void *ptr) {
191 if (list && list->count) {
192 if (ptr) {
193 PLIST_CPY(list, ptr, PLIST_ELE(list, 0));
194 }
195 if (--list->count) {
196 PLIST_MOV_REDUCE(list, 0);
197 }
198 return true;
199 }
200 return false;
201 }
202
203 bool psi_plist_pop(struct psi_plist *list, void *ptr) {
204 if (list && list->count) {
205 --list->count;
206 if (ptr) {
207 PLIST_CPY(list, ptr, PLIST_ELE(list, list->count));
208 }
209 return true;
210 }
211 return false;
212 }
213
214 bool psi_plist_top(struct psi_plist *list, void *ptr) {
215 if (list && list->count) {
216 PLIST_CPY(list, ptr, PLIST_ELE(list, list->count - 1));
217 return true;
218 }
219 return false;
220 }
221
222
223 static void swp_ptr(void *a, void *b) {
224 void **_a = a, **_b = b, *_c;
225
226 _c = *_b;
227 *_b = *_a;
228 *_a = _c;
229 }
230
231 void psi_plist_sort(struct psi_plist *list, compare_func_t cmp, swap_func_t swp) {
232 if (!swp && list->size == sizeof(void *)) {
233 swp = swp_ptr;
234 }
235 assert(swp);
236 zend_insert_sort((void *) list->list, list->count, list->size, cmp, swp);
237 }