/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 | 113 | { |
26 | 113 | ptrarray_t *pa = (ptrarray_t*)malloc(sizeof(ptrarray_t)); |
27 | 113 | pa->pdata = (void**)malloc(sizeof(void*) * capacity); |
28 | 113 | pa->capacity = capacity; |
29 | 113 | pa->capacity_step = (capacity > 4096) ? 4096 : capacity; |
30 | 113 | pa->len = 0; |
31 | 113 | return pa; |
32 | 113 | } |
33 | | |
34 | | void ptr_array_free(ptrarray_t *pa) |
35 | 53.3k | { |
36 | 53.3k | if (!pa) return; |
37 | 113 | if (pa->pdata) { |
38 | 113 | free(pa->pdata); |
39 | 113 | } |
40 | 113 | free(pa); |
41 | 113 | } |
42 | | |
43 | | void ptr_array_insert(ptrarray_t *pa, void *data, long array_index) |
44 | 24.0k | { |
45 | 24.0k | if (!pa || !pa->pdata) return; |
46 | 24.0k | long remaining = pa->capacity-pa->len; |
47 | 24.0k | if (remaining == 0) { |
48 | 99 | pa->pdata = (void**)realloc(pa->pdata, sizeof(void*) * (pa->capacity + pa->capacity_step)); |
49 | 99 | pa->capacity += pa->capacity_step; |
50 | 99 | } |
51 | 24.0k | if (array_index < 0 || array_index >= pa->len) { |
52 | 24.0k | pa->pdata[pa->len] = data; |
53 | 24.0k | } else { |
54 | 0 | memmove(&pa->pdata[array_index+1], &pa->pdata[array_index], (pa->len-array_index) * sizeof(void*)); |
55 | 0 | pa->pdata[array_index] = data; |
56 | 0 | } |
57 | 24.0k | pa->len++; |
58 | 24.0k | } |
59 | | |
60 | | void ptr_array_add(ptrarray_t *pa, void *data) |
61 | 11.4k | { |
62 | 11.4k | ptr_array_insert(pa, data, -1); |
63 | 11.4k | } |
64 | | |
65 | | void ptr_array_remove(ptrarray_t *pa, long array_index) |
66 | 0 | { |
67 | 0 | if (!pa || !pa->pdata || array_index < 0) return; |
68 | 0 | if (pa->len == 0 || array_index >= pa->len) return; |
69 | 0 | if (pa->len == 1) { |
70 | 0 | pa->pdata[0] = NULL; |
71 | 0 | } else { |
72 | 0 | memmove(&pa->pdata[array_index], &pa->pdata[array_index+1], (pa->len-array_index-1) * sizeof(void*)); |
73 | 0 | } |
74 | 0 | pa->len--; |
75 | 0 | } |
76 | | |
77 | | void ptr_array_set(ptrarray_t *pa, void *data, long array_index) |
78 | 0 | { |
79 | 0 | if (!pa || !pa->pdata || array_index < 0) return; |
80 | 0 | if (pa->len == 0 || array_index >= pa->len) return; |
81 | 0 | pa->pdata[array_index] = data; |
82 | 0 | } |
83 | | |
84 | | void* ptr_array_index(ptrarray_t *pa, long array_index) |
85 | 0 | { |
86 | 0 | if (!pa) return NULL; |
87 | 0 | if (array_index < 0 || array_index >= pa->len) { |
88 | 0 | return NULL; |
89 | 0 | } |
90 | 0 | return pa->pdata[array_index]; |
91 | 0 | } |
92 | | |
93 | | long ptr_array_size(ptrarray_t *pa) |
94 | 0 | { |
95 | 0 | return pa->len; |
96 | 0 | } |