summaryrefslogtreecommitdiffstats
path: root/src/ptrarray.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2019-05-19 00:20:51 +0200
committerGravatar Nikias Bassen2019-05-19 00:20:51 +0200
commit08c6143b870167ad29f9c20a298e0f75c986d0ea (patch)
tree079c595bf106a0f93add63a77730a2c8dde2ab7a /src/ptrarray.c
parent7e9ecf2f3f902f3e688e68b1d272f9a4b35540c7 (diff)
downloadlibplist-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.c42
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 @@
* ptrarray.c
* simple pointer array implementation
*
- * Copyright (c) 2011 Nikias Bassen, All Rights Reserved.
+ * Copyright (c) 2011-2019 Nikias Bassen, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ptrarray.h"
+#include <string.h>
ptrarray_t *ptr_array_new(int capacity)
{
@@ -39,22 +40,51 @@ void ptr_array_free(ptrarray_t *pa)
free(pa);
}
-void ptr_array_add(ptrarray_t *pa, void *data)
+void ptr_array_insert(ptrarray_t *pa, void *data, long array_index)
{
if (!pa || !pa->pdata || !data) return;
- size_t remaining = pa->capacity-pa->len;
+ long remaining = pa->capacity-pa->len;
if (remaining == 0) {
pa->pdata = realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step));
pa->capacity += pa->capacity_step;
}
- pa->pdata[pa->len] = data;
+ if (array_index < 0 || array_index >= pa->len) {
+ pa->pdata[pa->len] = data;
+ } else {
+ memmove(&pa->pdata[array_index+1], &pa->pdata[array_index], (pa->len-array_index) * sizeof(void*));
+ pa->pdata[array_index] = data;
+ }
pa->len++;
}
-void* ptr_array_index(ptrarray_t *pa, size_t array_index)
+void ptr_array_add(ptrarray_t *pa, void *data)
+{
+ ptr_array_insert(pa, data, -1);
+}
+
+void ptr_array_remove(ptrarray_t *pa, long array_index)
+{
+ if (!pa || !pa->pdata || array_index < 0) return;
+ if (pa->len == 0 || array_index >= pa->len) return;
+ if (pa->len == 1) {
+ pa->pdata[0] = NULL;
+ } else {
+ memmove(&pa->pdata[array_index], &pa->pdata[array_index+1], (pa->len-array_index-1) * sizeof(void*));
+ }
+ pa->len--;
+}
+
+void ptr_array_set(ptrarray_t *pa, void *data, long array_index)
+{
+ if (!pa || !pa->pdata || array_index < 0) return;
+ if (pa->len == 0 || array_index >= pa->len) return;
+ pa->pdata[array_index] = data;
+}
+
+void* ptr_array_index(ptrarray_t *pa, long array_index)
{
if (!pa) return NULL;
- if (array_index >= pa->len) {
+ if (array_index < 0 || array_index >= pa->len) {
return NULL;
}
return pa->pdata[array_index];