diff options
| author | 2019-05-19 00:20:51 +0200 | |
|---|---|---|
| committer | 2019-05-19 00:20:51 +0200 | |
| commit | 08c6143b870167ad29f9c20a298e0f75c986d0ea (patch) | |
| tree | 079c595bf106a0f93add63a77730a2c8dde2ab7a /src/ptrarray.c | |
| parent | 7e9ecf2f3f902f3e688e68b1d272f9a4b35540c7 (diff) | |
| download | libplist-08c6143b870167ad29f9c20a298e0f75c986d0ea.tar.gz libplist-08c6143b870167ad29f9c20a298e0f75c986d0ea.tar.bz2 | |
Add index lookup table for large PLIST_ARRAY nodes
Diffstat (limited to 'src/ptrarray.c')
| -rw-r--r-- | src/ptrarray.c | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/src/ptrarray.c b/src/ptrarray.c index eb17d28..bcffb77 100644 --- a/src/ptrarray.c +++ b/src/ptrarray.c | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | * ptrarray.c | 2 | * ptrarray.c |
| 3 | * simple pointer array implementation | 3 | * simple pointer array implementation |
| 4 | * | 4 | * |
| 5 | * Copyright (c) 2011 Nikias Bassen, All Rights Reserved. | 5 | * Copyright (c) 2011-2019 Nikias Bassen, All Rights Reserved. |
| 6 | * | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or | 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public | 8 | * modify it under the terms of the GNU Lesser General Public |
| @@ -19,6 +19,7 @@ | |||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ | 20 | */ |
| 21 | #include "ptrarray.h" | 21 | #include "ptrarray.h" |
| 22 | #include <string.h> | ||
| 22 | 23 | ||
| 23 | ptrarray_t *ptr_array_new(int capacity) | 24 | ptrarray_t *ptr_array_new(int capacity) |
| 24 | { | 25 | { |
| @@ -39,22 +40,51 @@ void ptr_array_free(ptrarray_t *pa) | |||
| 39 | free(pa); | 40 | free(pa); |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | void ptr_array_add(ptrarray_t *pa, void *data) | 43 | void ptr_array_insert(ptrarray_t *pa, void *data, long array_index) |
| 43 | { | 44 | { |
| 44 | if (!pa || !pa->pdata || !data) return; | 45 | if (!pa || !pa->pdata || !data) return; |
| 45 | size_t remaining = pa->capacity-pa->len; | 46 | long remaining = pa->capacity-pa->len; |
| 46 | if (remaining == 0) { | 47 | if (remaining == 0) { |
| 47 | pa->pdata = realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); | 48 | pa->pdata = realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); |
| 48 | pa->capacity += pa->capacity_step; | 49 | pa->capacity += pa->capacity_step; |
| 49 | } | 50 | } |
| 50 | pa->pdata[pa->len] = data; | 51 | if (array_index < 0 || array_index >= pa->len) { |
| 52 | pa->pdata[pa->len] = data; | ||
| 53 | } else { | ||
| 54 | memmove(&pa->pdata[array_index+1], &pa->pdata[array_index], (pa->len-array_index) * sizeof(void*)); | ||
| 55 | pa->pdata[array_index] = data; | ||
| 56 | } | ||
| 51 | pa->len++; | 57 | pa->len++; |
| 52 | } | 58 | } |
| 53 | 59 | ||
| 54 | void* ptr_array_index(ptrarray_t *pa, size_t array_index) | 60 | void ptr_array_add(ptrarray_t *pa, void *data) |
| 61 | { | ||
| 62 | ptr_array_insert(pa, data, -1); | ||
| 63 | } | ||
| 64 | |||
| 65 | void ptr_array_remove(ptrarray_t *pa, long array_index) | ||
| 66 | { | ||
| 67 | if (!pa || !pa->pdata || array_index < 0) return; | ||
| 68 | if (pa->len == 0 || array_index >= pa->len) return; | ||
| 69 | if (pa->len == 1) { | ||
| 70 | pa->pdata[0] = NULL; | ||
| 71 | } else { | ||
| 72 | memmove(&pa->pdata[array_index], &pa->pdata[array_index+1], (pa->len-array_index-1) * sizeof(void*)); | ||
| 73 | } | ||
| 74 | pa->len--; | ||
| 75 | } | ||
| 76 | |||
| 77 | void ptr_array_set(ptrarray_t *pa, void *data, long array_index) | ||
| 78 | { | ||
| 79 | if (!pa || !pa->pdata || array_index < 0) return; | ||
| 80 | if (pa->len == 0 || array_index >= pa->len) return; | ||
| 81 | pa->pdata[array_index] = data; | ||
| 82 | } | ||
| 83 | |||
| 84 | void* ptr_array_index(ptrarray_t *pa, long array_index) | ||
| 55 | { | 85 | { |
| 56 | if (!pa) return NULL; | 86 | if (!pa) return NULL; |
| 57 | if (array_index >= pa->len) { | 87 | if (array_index < 0 || array_index >= pa->len) { |
| 58 | return NULL; | 88 | return NULL; |
| 59 | } | 89 | } |
| 60 | return pa->pdata[array_index]; | 90 | return pa->pdata[array_index]; |
