diff options
author | David Walter Seikel | 2012-01-23 23:30:42 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-23 23:30:42 +1000 |
commit | 825a3d837a33f226c879cd02ad15c3fba57e8b2c (patch) | |
tree | 75f57bd9c4253508d338dc79ba8e57a7abc42255 /libraries/eina/src/lib/eina_inarray.c | |
parent | Add ability to disable the test harness, or the Lua compile test. (diff) | |
download | SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.zip SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.gz SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.bz2 SledjHamr-825a3d837a33f226c879cd02ad15c3fba57e8b2c.tar.xz |
Update the EFL to what I'm actually using, coz I'm using some stuff not yet released.
Diffstat (limited to '')
-rw-r--r-- | libraries/eina/src/lib/eina_inarray.c | 777 |
1 files changed, 777 insertions, 0 deletions
diff --git a/libraries/eina/src/lib/eina_inarray.c b/libraries/eina/src/lib/eina_inarray.c new file mode 100644 index 0000000..dc95bc6 --- /dev/null +++ b/libraries/eina/src/lib/eina_inarray.c | |||
@@ -0,0 +1,777 @@ | |||
1 | /* EINA - EFL data type library | ||
2 | * Copyright (C) 2012 - ProFUSION embedded systems | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library; | ||
16 | * if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include "config.h" | ||
21 | #endif | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | #include "eina_config.h" | ||
27 | #include "eina_private.h" | ||
28 | #include "eina_error.h" | ||
29 | #include "eina_log.h" | ||
30 | |||
31 | /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */ | ||
32 | #include "eina_safety_checks.h" | ||
33 | #include "eina_inarray.h" | ||
34 | |||
35 | /*============================================================================* | ||
36 | * Local * | ||
37 | *============================================================================*/ | ||
38 | |||
39 | /** | ||
40 | * @cond LOCAL | ||
41 | */ | ||
42 | |||
43 | static const char EINA_MAGIC_INARRAY_STR[] = "Eina Inline Array"; | ||
44 | static const char EINA_MAGIC_INARRAY_ITERATOR_STR[] = "Eina Inline Array Iterator"; | ||
45 | static const char EINA_MAGIC_INARRAY_ACCESSOR_STR[] = "Eina Inline Array Accessor"; | ||
46 | |||
47 | typedef struct _Eina_Iterator_Inarray Eina_Iterator_Inarray; | ||
48 | typedef struct _Eina_Accessor_Inarray Eina_Accessor_Inarray; | ||
49 | |||
50 | struct _Eina_Iterator_Inarray | ||
51 | { | ||
52 | Eina_Iterator iterator; | ||
53 | const Eina_Inarray *array; | ||
54 | unsigned int pos; | ||
55 | EINA_MAGIC | ||
56 | }; | ||
57 | |||
58 | struct _Eina_Accessor_Inarray | ||
59 | { | ||
60 | Eina_Accessor accessor; | ||
61 | const Eina_Inarray *array; | ||
62 | EINA_MAGIC | ||
63 | }; | ||
64 | |||
65 | static int _eina_inarray_log_dom = -1; | ||
66 | |||
67 | #ifdef ERR | ||
68 | #undef ERR | ||
69 | #endif | ||
70 | #define ERR(...) EINA_LOG_DOM_ERR(_eina_inarray_log_dom, __VA_ARGS__) | ||
71 | |||
72 | #ifdef DBG | ||
73 | #undef DBG | ||
74 | #endif | ||
75 | #define DBG(...) EINA_LOG_DOM_DBG(_eina_inarray_log_dom, __VA_ARGS__) | ||
76 | |||
77 | #define EINA_MAGIC_CHECK_INARRAY(d, ...) \ | ||
78 | do \ | ||
79 | { \ | ||
80 | if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_INARRAY)) \ | ||
81 | { \ | ||
82 | EINA_MAGIC_FAIL(d, EINA_MAGIC_INARRAY); \ | ||
83 | return __VA_ARGS__; \ | ||
84 | } \ | ||
85 | eina_error_set(0); \ | ||
86 | } \ | ||
87 | while(0) | ||
88 | |||
89 | #define EINA_MAGIC_CHECK_INARRAY_ITERATOR(d, ...) \ | ||
90 | do \ | ||
91 | { \ | ||
92 | if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_INARRAY_ITERATOR)) \ | ||
93 | { \ | ||
94 | EINA_MAGIC_FAIL(d, EINA_MAGIC_INARRAY_ITERATOR); \ | ||
95 | return __VA_ARGS__; \ | ||
96 | } \ | ||
97 | eina_error_set(0); \ | ||
98 | } \ | ||
99 | while(0) | ||
100 | |||
101 | #define EINA_MAGIC_CHECK_INARRAY_ACCESSOR(d, ...) \ | ||
102 | do \ | ||
103 | { \ | ||
104 | if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_INARRAY_ACCESSOR)) \ | ||
105 | { \ | ||
106 | EINA_MAGIC_FAIL(d, EINA_MAGIC_INARRAY_ACCESSOR); \ | ||
107 | return __VA_ARGS__; \ | ||
108 | } \ | ||
109 | eina_error_set(0); \ | ||
110 | } \ | ||
111 | while(0) | ||
112 | |||
113 | static void | ||
114 | _eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) | ||
115 | { | ||
116 | EINA_MAGIC_SET(array, EINA_MAGIC_INARRAY); | ||
117 | array->member_size = member_size; | ||
118 | array->len = 0; | ||
119 | array->max = 0; | ||
120 | array->step = (step > 0) ? step : 32; | ||
121 | array->members = NULL; | ||
122 | } | ||
123 | |||
124 | static Eina_Bool | ||
125 | _eina_inarray_resize(Eina_Inarray *array, unsigned int new_size) | ||
126 | { | ||
127 | unsigned int new_max; | ||
128 | void *tmp; | ||
129 | |||
130 | if (new_size < array->max) | ||
131 | return EINA_TRUE; | ||
132 | |||
133 | if (new_size % array->step == 0) | ||
134 | new_max = new_size; | ||
135 | else | ||
136 | new_max = ((new_size / array->step) + 1) * array->step; | ||
137 | |||
138 | tmp = realloc(array->members, new_max * array->member_size); | ||
139 | if ((!tmp) && (new_max > 0)) | ||
140 | { | ||
141 | eina_error_set(EINA_ERROR_OUT_OF_MEMORY); | ||
142 | return EINA_FALSE; | ||
143 | } | ||
144 | |||
145 | array->members = tmp; | ||
146 | array->max = new_max; | ||
147 | return EINA_TRUE; | ||
148 | } | ||
149 | |||
150 | static inline void * | ||
151 | _eina_inarray_get(const Eina_Inarray *array, unsigned int position) | ||
152 | { | ||
153 | unsigned int offset = position * array->member_size; | ||
154 | return (unsigned char *)array->members + offset; | ||
155 | } | ||
156 | |||
157 | static int | ||
158 | _eina_inarray_search(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare) | ||
159 | { | ||
160 | const unsigned char *start, *found; | ||
161 | start = array->members; | ||
162 | found = bsearch(data, start, array->len, array->member_size, compare); | ||
163 | if (!found) | ||
164 | return -1; | ||
165 | return (found - start) / array->member_size; | ||
166 | } | ||
167 | |||
168 | static unsigned int | ||
169 | _eina_inarray_search_sorted_near(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare, int *cmp) | ||
170 | { | ||
171 | unsigned int start, last, middle; | ||
172 | |||
173 | if (array->len == 0) | ||
174 | { | ||
175 | *cmp = -1; | ||
176 | return 0; | ||
177 | } | ||
178 | else if (array->len == 1) | ||
179 | { | ||
180 | *cmp = compare(data, array->members); | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | start = 0; | ||
185 | last = array->len - 1; /* inclusive */ | ||
186 | do | ||
187 | { | ||
188 | void *p; | ||
189 | middle = start + (last - start) / 2; /* avoid overflow */ | ||
190 | p = _eina_inarray_get(array, middle); | ||
191 | *cmp = compare(data, p); | ||
192 | if (*cmp == 0) | ||
193 | return middle; | ||
194 | else if (*cmp > 0) | ||
195 | start = middle + 1; | ||
196 | else if (middle > 0) | ||
197 | last = middle - 1; | ||
198 | else | ||
199 | break; | ||
200 | } | ||
201 | while (start <= last); | ||
202 | return middle; | ||
203 | } | ||
204 | |||
205 | |||
206 | static Eina_Bool | ||
207 | _eina_inarray_iterator_next(Eina_Iterator_Inarray *it, void **data) | ||
208 | { | ||
209 | EINA_MAGIC_CHECK_INARRAY_ITERATOR(it, EINA_FALSE); | ||
210 | |||
211 | if (it->pos >= it->array->len) | ||
212 | return EINA_FALSE; | ||
213 | |||
214 | *data = _eina_inarray_get(it->array, it->pos); | ||
215 | it->pos++; | ||
216 | |||
217 | return EINA_TRUE; | ||
218 | } | ||
219 | |||
220 | static Eina_Bool | ||
221 | _eina_inarray_iterator_prev(Eina_Iterator_Inarray *it, void **data) | ||
222 | { | ||
223 | EINA_MAGIC_CHECK_INARRAY_ITERATOR(it, EINA_FALSE); | ||
224 | |||
225 | if (it->pos == 0) | ||
226 | return EINA_FALSE; | ||
227 | |||
228 | it->pos--; | ||
229 | *data = _eina_inarray_get(it->array, it->pos); | ||
230 | |||
231 | return EINA_TRUE; | ||
232 | } | ||
233 | |||
234 | static Eina_Inarray * | ||
235 | _eina_inarray_iterator_get_container(Eina_Iterator_Inarray *it) | ||
236 | { | ||
237 | EINA_MAGIC_CHECK_INARRAY_ITERATOR(it, NULL); | ||
238 | return (Eina_Inarray *)it->array; | ||
239 | } | ||
240 | |||
241 | static void | ||
242 | _eina_inarray_iterator_free(Eina_Iterator_Inarray *it) | ||
243 | { | ||
244 | EINA_MAGIC_CHECK_INARRAY_ITERATOR(it); | ||
245 | MAGIC_FREE(it); | ||
246 | } | ||
247 | |||
248 | static Eina_Bool | ||
249 | _eina_inarray_accessor_get_at(Eina_Accessor_Inarray *it, unsigned int pos, void **data) | ||
250 | { | ||
251 | EINA_MAGIC_CHECK_INARRAY_ACCESSOR(it, EINA_FALSE); | ||
252 | |||
253 | if (pos >= it->array->len) | ||
254 | return EINA_FALSE; | ||
255 | |||
256 | *data = _eina_inarray_get(it->array, pos); | ||
257 | return EINA_TRUE; | ||
258 | } | ||
259 | |||
260 | static Eina_Inarray * | ||
261 | _eina_inarray_accessor_get_container(Eina_Accessor_Inarray *it) | ||
262 | { | ||
263 | EINA_MAGIC_CHECK_INARRAY_ACCESSOR(it, NULL); | ||
264 | return (Eina_Inarray *)it->array; | ||
265 | } | ||
266 | |||
267 | static void | ||
268 | _eina_inarray_accessor_free(Eina_Accessor_Inarray *it) | ||
269 | { | ||
270 | EINA_MAGIC_CHECK_INARRAY_ACCESSOR(it); | ||
271 | MAGIC_FREE(it); | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * @endcond | ||
276 | */ | ||
277 | |||
278 | |||
279 | /*============================================================================* | ||
280 | * Global * | ||
281 | *============================================================================*/ | ||
282 | |||
283 | /** | ||
284 | * @internal | ||
285 | * @brief Initialize the inline array module. | ||
286 | * | ||
287 | * @return #EINA_TRUE on success, #EINA_FALSE on failure. | ||
288 | * | ||
289 | * This function sets up the inline array module of Eina. It is called | ||
290 | * by eina_init(). | ||
291 | * | ||
292 | * @see eina_init() | ||
293 | */ | ||
294 | Eina_Bool | ||
295 | eina_inarray_init(void) | ||
296 | { | ||
297 | _eina_inarray_log_dom = eina_log_domain_register("eina_inarray", | ||
298 | EINA_LOG_COLOR_DEFAULT); | ||
299 | if (_eina_inarray_log_dom < 0) | ||
300 | { | ||
301 | EINA_LOG_ERR("Could not register log domain: eina_inarray"); | ||
302 | return EINA_FALSE; | ||
303 | } | ||
304 | |||
305 | #define EMS(n) eina_magic_string_static_set(n, n ## _STR) | ||
306 | EMS(EINA_MAGIC_INARRAY); | ||
307 | EMS(EINA_MAGIC_INARRAY_ITERATOR); | ||
308 | EMS(EINA_MAGIC_INARRAY_ACCESSOR); | ||
309 | #undef EMS | ||
310 | |||
311 | return EINA_TRUE; | ||
312 | } | ||
313 | |||
314 | /** | ||
315 | * @internal | ||
316 | * @brief Shut down the inline array module. | ||
317 | * | ||
318 | * @return #EINA_TRUE on success, #EINA_FALSE on failure. | ||
319 | * | ||
320 | * This function shuts down the inline array module set up by | ||
321 | * eina_inarray_init(). It is called by eina_shutdown(). | ||
322 | * | ||
323 | * @see eina_shutdown() | ||
324 | */ | ||
325 | Eina_Bool | ||
326 | eina_inarray_shutdown(void) | ||
327 | { | ||
328 | eina_log_domain_unregister(_eina_inarray_log_dom); | ||
329 | _eina_inarray_log_dom = -1; | ||
330 | return EINA_TRUE; | ||
331 | } | ||
332 | |||
333 | /*============================================================================* | ||
334 | * API * | ||
335 | *============================================================================*/ | ||
336 | EAPI Eina_Inarray * | ||
337 | eina_inarray_new(unsigned int member_size, unsigned int step) | ||
338 | { | ||
339 | Eina_Inarray *ret; | ||
340 | |||
341 | EINA_SAFETY_ON_TRUE_RETURN_VAL(member_size == 0, NULL); | ||
342 | |||
343 | ret = malloc(sizeof(*ret)); | ||
344 | if (!ret) | ||
345 | { | ||
346 | eina_error_set(EINA_ERROR_OUT_OF_MEMORY); | ||
347 | return NULL; | ||
348 | } | ||
349 | eina_error_set(0); | ||
350 | _eina_inarray_setup(ret, member_size, step); | ||
351 | return ret; | ||
352 | } | ||
353 | |||
354 | EAPI void | ||
355 | eina_inarray_free(Eina_Inarray *array) | ||
356 | { | ||
357 | EINA_MAGIC_CHECK_INARRAY(array); | ||
358 | free(array->members); | ||
359 | free(array); | ||
360 | } | ||
361 | |||
362 | EAPI void | ||
363 | eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) | ||
364 | { | ||
365 | EINA_SAFETY_ON_NULL_RETURN(array); | ||
366 | EINA_SAFETY_ON_TRUE_RETURN(member_size == 0); | ||
367 | _eina_inarray_setup(array, member_size, step); | ||
368 | } | ||
369 | |||
370 | EAPI void | ||
371 | eina_inarray_flush(Eina_Inarray *array) | ||
372 | { | ||
373 | EINA_MAGIC_CHECK_INARRAY(array); | ||
374 | free(array->members); | ||
375 | array->len = 0; | ||
376 | array->max = 0; | ||
377 | array->members = NULL; | ||
378 | } | ||
379 | |||
380 | EAPI int | ||
381 | eina_inarray_append(Eina_Inarray *array, const void *data) | ||
382 | { | ||
383 | void *p; | ||
384 | |||
385 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
386 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
387 | |||
388 | if (!_eina_inarray_resize(array, array->len + 1)) | ||
389 | return -1; | ||
390 | |||
391 | p = _eina_inarray_get(array, array->len); | ||
392 | memcpy(p, data, array->member_size); | ||
393 | |||
394 | array->len++; | ||
395 | return array->len - 1; | ||
396 | } | ||
397 | |||
398 | EAPI int | ||
399 | eina_inarray_insert(Eina_Inarray *array, const void *data, Eina_Compare_Cb compare) | ||
400 | { | ||
401 | const unsigned char *itr, *itr_end; | ||
402 | unsigned int sz; | ||
403 | |||
404 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
405 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
406 | EINA_SAFETY_ON_NULL_RETURN_VAL(compare, -1); | ||
407 | |||
408 | sz = array->member_size; | ||
409 | itr = array->members; | ||
410 | itr_end = itr + array->len * sz; | ||
411 | for (; itr < itr_end; itr += sz) | ||
412 | { | ||
413 | unsigned int offset, position; | ||
414 | int cmp = compare(itr, data); | ||
415 | if (cmp <= 0) | ||
416 | continue; | ||
417 | |||
418 | offset = itr - (unsigned char *)array->members; | ||
419 | position = offset / sz; | ||
420 | if (!eina_inarray_insert_at(array, position, data)) | ||
421 | return -1; | ||
422 | return position; | ||
423 | } | ||
424 | return eina_inarray_append(array, data); | ||
425 | } | ||
426 | |||
427 | EAPI int | ||
428 | eina_inarray_insert_sorted(Eina_Inarray *array, const void *data, Eina_Compare_Cb compare) | ||
429 | { | ||
430 | unsigned int pos; | ||
431 | int cmp; | ||
432 | |||
433 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
434 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
435 | EINA_SAFETY_ON_NULL_RETURN_VAL(compare, -1); | ||
436 | |||
437 | pos = _eina_inarray_search_sorted_near(array, data, compare, &cmp); | ||
438 | if (cmp > 0) | ||
439 | pos++; | ||
440 | |||
441 | if (!eina_inarray_insert_at(array, pos, data)) | ||
442 | return -1; | ||
443 | return pos; | ||
444 | } | ||
445 | |||
446 | EAPI int | ||
447 | eina_inarray_remove(Eina_Inarray *array, const void *data) | ||
448 | { | ||
449 | const unsigned char *itr, *itr_end; | ||
450 | unsigned int position, sz; | ||
451 | |||
452 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
453 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
454 | |||
455 | sz = array->member_size; | ||
456 | if ((data >= array->members) && | ||
457 | (data < _eina_inarray_get(array, array->len))) | ||
458 | { | ||
459 | unsigned int offset = ((unsigned char *)data - | ||
460 | (unsigned char *)array->members); | ||
461 | position = offset / sz; | ||
462 | goto found; | ||
463 | } | ||
464 | |||
465 | itr = array->members; | ||
466 | itr_end = itr + array->len * sz; | ||
467 | for (; itr < itr_end; itr += sz) | ||
468 | { | ||
469 | if (memcmp(data, itr, sz) == 0) | ||
470 | { | ||
471 | unsigned int offset = itr - (unsigned char *)array->members; | ||
472 | position = offset / sz; | ||
473 | goto found; | ||
474 | } | ||
475 | } | ||
476 | return -1; | ||
477 | |||
478 | found: | ||
479 | if (!eina_inarray_remove_at(array, position)) | ||
480 | return -1; | ||
481 | return position; | ||
482 | } | ||
483 | |||
484 | EAPI int | ||
485 | eina_inarray_pop(Eina_Inarray *array) | ||
486 | { | ||
487 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
488 | EINA_SAFETY_ON_TRUE_RETURN_VAL(array->len == 0, -1); | ||
489 | if (!_eina_inarray_resize(array, array->len - 1)) | ||
490 | return -1; | ||
491 | array->len--; | ||
492 | return array->len + 1; | ||
493 | } | ||
494 | |||
495 | EAPI void * | ||
496 | eina_inarray_nth(const Eina_Inarray *array, unsigned int position) | ||
497 | { | ||
498 | EINA_MAGIC_CHECK_INARRAY(array, NULL); | ||
499 | EINA_SAFETY_ON_TRUE_RETURN_VAL(position >= array->len, NULL); | ||
500 | return _eina_inarray_get(array, position); | ||
501 | } | ||
502 | |||
503 | EAPI Eina_Bool | ||
504 | eina_inarray_insert_at(Eina_Inarray *array, unsigned int position, const void *data) | ||
505 | { | ||
506 | unsigned int sz; | ||
507 | unsigned char *p; | ||
508 | |||
509 | EINA_MAGIC_CHECK_INARRAY(array, EINA_FALSE); | ||
510 | EINA_SAFETY_ON_TRUE_RETURN_VAL(position > array->len, EINA_FALSE); | ||
511 | |||
512 | if (!_eina_inarray_resize(array, array->len + 1)) | ||
513 | return EINA_FALSE; | ||
514 | |||
515 | p = _eina_inarray_get(array, position); | ||
516 | sz = array->member_size; | ||
517 | if (array->len > position) | ||
518 | memmove(p + sz, p, (array->len - position) * sz); | ||
519 | memcpy(p, data, sz); | ||
520 | |||
521 | array->len++; | ||
522 | return EINA_TRUE; | ||
523 | } | ||
524 | |||
525 | EAPI void * | ||
526 | eina_inarray_alloc_at(Eina_Inarray *array, unsigned int position, unsigned int member_count) | ||
527 | { | ||
528 | unsigned int sz; | ||
529 | unsigned char *p; | ||
530 | |||
531 | EINA_MAGIC_CHECK_INARRAY(array, NULL); | ||
532 | EINA_SAFETY_ON_TRUE_RETURN_VAL(position > array->len, NULL); | ||
533 | EINA_SAFETY_ON_TRUE_RETURN_VAL(member_count == 0, NULL); | ||
534 | |||
535 | if (!_eina_inarray_resize(array, array->len + member_count)) | ||
536 | return NULL; | ||
537 | |||
538 | p = _eina_inarray_get(array, position); | ||
539 | sz = array->member_size; | ||
540 | if (array->len > position) | ||
541 | memmove(p + member_count * sz, p, (array->len - position) * sz); | ||
542 | |||
543 | array->len += member_count; | ||
544 | return p; | ||
545 | } | ||
546 | |||
547 | EAPI Eina_Bool | ||
548 | eina_inarray_replace_at(Eina_Inarray *array, unsigned int position, const void *data) | ||
549 | { | ||
550 | unsigned char *p; | ||
551 | |||
552 | EINA_MAGIC_CHECK_INARRAY(array, EINA_FALSE); | ||
553 | EINA_SAFETY_ON_TRUE_RETURN_VAL(position >= array->len, EINA_FALSE); | ||
554 | |||
555 | p = _eina_inarray_get(array, position); | ||
556 | memcpy(p, data, array->member_size); | ||
557 | |||
558 | return EINA_TRUE; | ||
559 | } | ||
560 | |||
561 | EAPI Eina_Bool | ||
562 | eina_inarray_remove_at(Eina_Inarray *array, unsigned int position) | ||
563 | { | ||
564 | EINA_MAGIC_CHECK_INARRAY(array, EINA_FALSE); | ||
565 | EINA_SAFETY_ON_TRUE_RETURN_VAL(position >= array->len, EINA_FALSE); | ||
566 | |||
567 | if (position + 1 < array->len) | ||
568 | { | ||
569 | unsigned int sz = array->member_size; | ||
570 | unsigned char *p = _eina_inarray_get(array, position); | ||
571 | memmove(p, p + sz, (array->len - position - 1) * sz); | ||
572 | } | ||
573 | |||
574 | _eina_inarray_resize(array, array->len - 1); | ||
575 | array->len--; | ||
576 | return EINA_TRUE; | ||
577 | } | ||
578 | |||
579 | EAPI void | ||
580 | eina_inarray_reverse(Eina_Inarray *array) | ||
581 | { | ||
582 | size_t sz; | ||
583 | unsigned char *fwd, *rev, *fwd_end; | ||
584 | void *tmp; | ||
585 | |||
586 | EINA_MAGIC_CHECK_INARRAY(array); | ||
587 | |||
588 | if (array->len < 2) | ||
589 | return; | ||
590 | |||
591 | sz = array->member_size; | ||
592 | |||
593 | tmp = alloca(sz); | ||
594 | EINA_SAFETY_ON_NULL_RETURN(tmp); | ||
595 | |||
596 | fwd = array->members; | ||
597 | fwd_end = fwd + (array->len / 2) * sz; | ||
598 | |||
599 | rev = fwd + (array->len - 1) * sz; | ||
600 | |||
601 | for (; fwd < fwd_end; fwd += sz, rev -= sz) | ||
602 | { | ||
603 | memcpy(tmp, fwd, sz); | ||
604 | memcpy(fwd, rev, sz); | ||
605 | memcpy(rev, tmp, sz); | ||
606 | } | ||
607 | } | ||
608 | |||
609 | EAPI void | ||
610 | eina_inarray_sort(Eina_Inarray *array, Eina_Compare_Cb compare) | ||
611 | { | ||
612 | EINA_MAGIC_CHECK_INARRAY(array); | ||
613 | EINA_SAFETY_ON_NULL_RETURN(compare); | ||
614 | qsort(array->members, array->len, array->member_size, compare); | ||
615 | } | ||
616 | |||
617 | EAPI int | ||
618 | eina_inarray_search(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare) | ||
619 | { | ||
620 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
621 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
622 | EINA_SAFETY_ON_NULL_RETURN_VAL(compare, -1); | ||
623 | return _eina_inarray_search(array, data, compare); | ||
624 | } | ||
625 | |||
626 | EAPI int | ||
627 | eina_inarray_search_sorted(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare) | ||
628 | { | ||
629 | unsigned int pos; | ||
630 | int cmp; | ||
631 | |||
632 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
633 | EINA_SAFETY_ON_NULL_RETURN_VAL(data, -1); | ||
634 | EINA_SAFETY_ON_NULL_RETURN_VAL(compare, -1); | ||
635 | |||
636 | pos = _eina_inarray_search_sorted_near(array, data, compare, &cmp); | ||
637 | if (cmp == 0) | ||
638 | return pos; | ||
639 | return -1; | ||
640 | } | ||
641 | |||
642 | EAPI Eina_Bool | ||
643 | eina_inarray_foreach(const Eina_Inarray *array, Eina_Each_Cb function, const void *user_data) | ||
644 | { | ||
645 | unsigned char *itr, *itr_end; | ||
646 | unsigned int sz; | ||
647 | Eina_Bool ret = EINA_TRUE; | ||
648 | |||
649 | EINA_MAGIC_CHECK_INARRAY(array, EINA_FALSE); | ||
650 | EINA_SAFETY_ON_NULL_RETURN_VAL(function, EINA_FALSE); | ||
651 | |||
652 | sz = array->member_size; | ||
653 | itr = array->members; | ||
654 | itr_end = itr + array->len * sz; | ||
655 | for (; (itr < itr_end) && (ret); itr += sz) | ||
656 | ret = function(array, itr, (void *)user_data); | ||
657 | return ret; | ||
658 | } | ||
659 | |||
660 | EAPI int | ||
661 | eina_inarray_foreach_remove(Eina_Inarray *array, Eina_Each_Cb match, const void *user_data) | ||
662 | { | ||
663 | unsigned int i = 0, count = 0; | ||
664 | |||
665 | EINA_MAGIC_CHECK_INARRAY(array, -1); | ||
666 | EINA_SAFETY_ON_NULL_RETURN_VAL(match, -1); | ||
667 | |||
668 | while (i < array->len) | ||
669 | { | ||
670 | void *p = _eina_inarray_get(array, i); | ||
671 | if (match(array, p, (void *)user_data) == EINA_FALSE) | ||
672 | { | ||
673 | i++; | ||
674 | continue; | ||
675 | } | ||
676 | |||
677 | eina_inarray_remove_at(array, i); | ||
678 | count++; | ||
679 | } | ||
680 | |||
681 | return count; | ||
682 | } | ||
683 | |||
684 | EAPI unsigned int | ||
685 | eina_inarray_count(const Eina_Inarray *array) | ||
686 | { | ||
687 | EINA_MAGIC_CHECK_INARRAY(array, 0); | ||
688 | return array->len; | ||
689 | } | ||
690 | |||
691 | EAPI Eina_Iterator * | ||
692 | eina_inarray_iterator_new(const Eina_Inarray *array) | ||
693 | { | ||
694 | Eina_Iterator_Inarray *it; | ||
695 | |||
696 | EINA_MAGIC_CHECK_INARRAY(array, NULL); | ||
697 | |||
698 | eina_error_set(0); | ||
699 | it = calloc(1, sizeof(Eina_Iterator_Inarray)); | ||
700 | if (!it) | ||
701 | { | ||
702 | eina_error_set(EINA_ERROR_OUT_OF_MEMORY); | ||
703 | return NULL; | ||
704 | } | ||
705 | |||
706 | EINA_MAGIC_SET(it, EINA_MAGIC_INARRAY_ITERATOR); | ||
707 | EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR); | ||
708 | |||
709 | it->array = array; | ||
710 | |||
711 | it->iterator.version = EINA_ITERATOR_VERSION; | ||
712 | it->iterator.next = FUNC_ITERATOR_NEXT(_eina_inarray_iterator_next); | ||
713 | it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER | ||
714 | (_eina_inarray_iterator_get_container); | ||
715 | it->iterator.free = FUNC_ITERATOR_FREE(_eina_inarray_iterator_free); | ||
716 | |||
717 | return &it->iterator; | ||
718 | } | ||
719 | |||
720 | EAPI Eina_Iterator * | ||
721 | eina_inarray_iterator_reversed_new(const Eina_Inarray *array) | ||
722 | { | ||
723 | Eina_Iterator_Inarray *it; | ||
724 | |||
725 | EINA_MAGIC_CHECK_INARRAY(array, NULL); | ||
726 | |||
727 | eina_error_set(0); | ||
728 | it = calloc(1, sizeof(Eina_Iterator_Inarray)); | ||
729 | if (!it) | ||
730 | { | ||
731 | eina_error_set(EINA_ERROR_OUT_OF_MEMORY); | ||
732 | return NULL; | ||
733 | } | ||
734 | |||
735 | EINA_MAGIC_SET(it, EINA_MAGIC_INARRAY_ITERATOR); | ||
736 | EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR); | ||
737 | |||
738 | it->array = array; | ||
739 | it->pos = array->len; | ||
740 | |||
741 | it->iterator.version = EINA_ITERATOR_VERSION; | ||
742 | it->iterator.next = FUNC_ITERATOR_NEXT(_eina_inarray_iterator_prev); | ||
743 | it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER | ||
744 | (_eina_inarray_iterator_get_container); | ||
745 | it->iterator.free = FUNC_ITERATOR_FREE(_eina_inarray_iterator_free); | ||
746 | |||
747 | return &it->iterator; | ||
748 | } | ||
749 | |||
750 | EAPI Eina_Accessor * | ||
751 | eina_inarray_accessor_new(const Eina_Inarray *array) | ||
752 | { | ||
753 | Eina_Accessor_Inarray *ac; | ||
754 | |||
755 | EINA_MAGIC_CHECK_INARRAY(array, NULL); | ||
756 | |||
757 | eina_error_set(0); | ||
758 | ac = calloc(1, sizeof(Eina_Accessor_Inarray)); | ||
759 | if (!ac) | ||
760 | { | ||
761 | eina_error_set(EINA_ERROR_OUT_OF_MEMORY); | ||
762 | return NULL; | ||
763 | } | ||
764 | |||
765 | EINA_MAGIC_SET(ac, EINA_MAGIC_INARRAY_ACCESSOR); | ||
766 | EINA_MAGIC_SET(&ac->accessor, EINA_MAGIC_ACCESSOR); | ||
767 | |||
768 | ac->array = array; | ||
769 | |||
770 | ac->accessor.version = EINA_ACCESSOR_VERSION; | ||
771 | ac->accessor.get_at = FUNC_ACCESSOR_GET_AT(_eina_inarray_accessor_get_at); | ||
772 | ac->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER | ||
773 | (_eina_inarray_accessor_get_container); | ||
774 | ac->accessor.free = FUNC_ACCESSOR_FREE(_eina_inarray_accessor_free); | ||
775 | |||
776 | return &ac->accessor; | ||
777 | } | ||