/src/libplist/src/ptrarray.c
Line | Count | Source |
1 | | /* |
2 | | * ptrarray.c |
3 | | * simple pointer array implementation |
4 | | * |
5 | | * Copyright (c) 2011-2019 Nikias Bassen, All Rights Reserved. |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | #include "ptrarray.h" |
22 | | #include <string.h> |
23 | | |
24 | | ptrarray_t *ptr_array_new(int capacity) |
25 | 4.62k | { |
26 | 4.62k | ptrarray_t *pa = (ptrarray_t*)malloc(sizeof(ptrarray_t)); |
27 | 4.62k | pa->pdata = (void**)malloc(sizeof(void*) * capacity); |
28 | 4.62k | pa->capacity = capacity; |
29 | 4.62k | pa->capacity_step = (capacity > 4096) ? 4096 : capacity; |
30 | 4.62k | pa->len = 0; |
31 | 4.62k | return pa; |
32 | 4.62k | } |
33 | | |
34 | | void ptr_array_free(ptrarray_t *pa) |
35 | 438k | { |
36 | 438k | if (!pa) return; |
37 | 4.62k | if (pa->pdata) { |
38 | 4.62k | free(pa->pdata); |
39 | 4.62k | } |
40 | 4.62k | free(pa); |
41 | 4.62k | } |
42 | | |
43 | | int ptr_array_insert(ptrarray_t *pa, void *data, long array_index) |
44 | 113k | { |
45 | 113k | if (!pa || !pa->pdata) return -1; |
46 | 113k | long remaining = pa->capacity-pa->len; |
47 | 113k | if (remaining == 0) { |
48 | 474 | void **newdata = (void**)realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); |
49 | 474 | if (!newdata) { |
50 | 0 | return -1; |
51 | 0 | } |
52 | 474 | pa->pdata = newdata; |
53 | 474 | pa->capacity += pa->capacity_step; |
54 | 474 | } |
55 | 113k | if (array_index < 0 || array_index >= pa->len) { |
56 | 113k | pa->pdata[pa->len] = data; |
57 | 113k | } else { |
58 | 0 | memmove(&pa->pdata[array_index+1], &pa->pdata[array_index], (pa->len-array_index) * sizeof(void*)); |
59 | 0 | pa->pdata[array_index] = data; |
60 | 0 | } |
61 | 113k | pa->len++; |
62 | 113k | return 0; |
63 | 113k | } |
64 | | |
65 | | int ptr_array_add(ptrarray_t *pa, void *data) |
66 | 63.0k | { |
67 | 63.0k | return ptr_array_insert(pa, data, -1); |
68 | 63.0k | } |
69 | | |
70 | | void ptr_array_remove(ptrarray_t *pa, long array_index) |
71 | 0 | { |
72 | 0 | if (!pa || !pa->pdata || array_index < 0) return; |
73 | 0 | if (pa->len == 0 || array_index >= pa->len) return; |
74 | 0 | if (pa->len == 1) { |
75 | 0 | pa->pdata[0] = NULL; |
76 | 0 | } else { |
77 | 0 | memmove(&pa->pdata[array_index], &pa->pdata[array_index+1], (pa->len-array_index-1) * sizeof(void*)); |
78 | 0 | } |
79 | 0 | pa->len--; |
80 | 0 | } |
81 | | |
82 | | void ptr_array_set(ptrarray_t *pa, void *data, long array_index) |
83 | 4.57M | { |
84 | 4.57M | if (!pa || !pa->pdata || array_index < 0) return; |
85 | 4.57M | if (pa->len == 0 || array_index >= pa->len) return; |
86 | 4.57M | pa->pdata[array_index] = data; |
87 | 4.57M | } |
88 | | |
89 | | void* ptr_array_index(ptrarray_t *pa, long array_index) |
90 | 50.3M | { |
91 | 50.3M | if (!pa) return NULL; |
92 | 50.3M | if (array_index < 0 || array_index >= pa->len) { |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 50.3M | return pa->pdata[array_index]; |
96 | 50.3M | } |
97 | | |
98 | | long ptr_array_size(ptrarray_t *pa) |
99 | 4.59M | { |
100 | 4.59M | return pa->len; |
101 | 4.59M | } |