-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
218 lines (183 loc) · 5.99 KB
/
Copy patharray.c
File metadata and controls
218 lines (183 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* $SchulteIT: array.c 15189 2025-10-27 05:41:45Z schulte $ */
/* $JDTAUS: array.c 9639 2026-07-22 06:07:19Z schulte $ */
/*
* Copyright (c) 2018 - 2026 Christian Schulte <cs@schulte.it>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_HOST_H
#include "host.h"
#endif
#ifdef MULTI_THREADED
#include "thread.h"
#endif
#include "array.h"
#include "heap.h"
#include <string.h>
struct Array {
void **items;
size_t size;
size_t offset;
size_t capacity;
#ifdef MULTI_THREADED
mtx_t mtx;
#endif
};
void Array_grow(struct Array *restrict const);
void Array_shrink(struct Array *restrict const);
struct Array *Array_new(const size_t c) {
struct Array *restrict const a = heap_malloc(sizeof(struct Array));
a->size = 0;
a->offset = 0;
a->capacity = ((c | !c) + 1) & ~1U;
a->items = heap_calloc(a->capacity, sizeof(void *));
#ifdef MULTI_THREADED
mutex_init(&a->mtx);
#endif
return a;
}
inline void Array_delete(struct Array *restrict const a,
void (*i_delete)(void *restrict const)) {
if (i_delete)
for (size_t i = a->size; i-- > 0;)
i_delete(a->items[a->offset + i]);
#ifdef MULTI_THREADED
mutex_destroy(&a->mtx);
#endif
heap_free(a->items);
heap_free(a);
}
inline struct Array *Array_copy(const struct Array *restrict const a,
void *(*i_copy)(void *restrict const)) {
struct Array *restrict const copy = Array_new(a->size);
if (i_copy)
for (size_t i = a->size; i-- > 0;)
copy->items[i] = i_copy(a->items[a->offset + i]);
else
for (size_t i = a->size; i-- > 0;)
copy->items[i] = a->items[a->offset + i];
copy->size = a->size;
return copy;
}
inline void Array_cut(struct Array *restrict const a, const size_t i,
const size_t cnt,
void (*i_delete)(void *restrict const)) {
if (i_delete) {
size_t j = i;
while (j != 0)
i_delete(a->items[a->offset + i - j--]);
j = a->size - (i + cnt);
while (j != 0)
i_delete(a->items[a->offset + a->size - j--]);
}
a->offset += i;
a->size = cnt;
Array_shrink(a);
}
inline void Array_clear(struct Array *restrict const a,
void (*i_delete)(void *restrict const)) {
if (i_delete)
for (size_t i = a->size; i-- > 0;) {
i_delete(a->items[a->offset + i]);
a->items[a->offset + i] = NULL;
}
else
for (size_t i = a->size; i-- > 0;)
a->items[a->offset + i] = NULL;
a->size = 0;
Array_shrink(a);
}
inline void Array_compact(struct Array *restrict const a) {
if (a->offset > 0) {
memmove(&a->items[0], &a->items[a->offset], sizeof(void *) * a->size);
a->offset = 0;
}
a->capacity = ((a->size | !a->size) + 1) & ~1U;
a->items = heap_reallocarray(a->items, a->capacity, sizeof(void *));
heap_trim(0);
}
inline void Array_shrink(struct Array *restrict const a) {
if (a->size < ((a->capacity - a->offset) >> 2)) {
if (a->offset > 0) {
memmove(&a->items[0], &a->items[a->offset], sizeof(void *) * a->size);
a->offset = 0;
}
a->capacity = (((a->capacity >> 1) | !(a->capacity >> 1)) + 1) & ~1U;
a->items = heap_reallocarray(a->items, a->capacity, sizeof(void *));
heap_trim(0);
}
}
inline void Array_grow(struct Array *restrict const a) {
if (a->size >= a->capacity - a->offset) {
if (a->offset > 0) {
memmove(&a->items[0], &a->items[a->offset], sizeof(void *) * a->size);
a->offset = 0;
} else {
a->capacity <<= 1;
a->items = heap_reallocarray(a->items, a->capacity, sizeof(void *));
heap_trim(0);
}
}
}
inline void Array_add_tail(struct Array *restrict const a,
void *restrict const i) {
Array_grow(a);
a->items[a->offset + a->size++] = i;
}
inline void *Array_tail(const struct Array *restrict const a) {
return a->size > 0 ? a->items[a->offset + a->size - 1] : NULL;
}
inline void Array_add_head(struct Array *restrict const a,
void *restrict const i) {
Array_grow(a);
if (a->offset == 0) {
if (a->size > 0)
memmove(&a->items[1], &a->items[0], sizeof(void *) * a->size);
a->offset++;
}
a->items[--a->offset] = i;
a->size++;
}
inline void *Array_head(const struct Array *restrict const a) {
return a->size > 0 ? a->items[a->offset] : NULL;
}
inline void *Array_remove_tail(struct Array *restrict const a) {
return Array_remove_idx(a, a->size - 1);
}
inline void *Array_remove_head(struct Array *restrict const a) {
return Array_remove_idx(a, 0);
}
inline void *Array_remove_idx(struct Array *restrict const a, const size_t i) {
void *restrict const item = a->items[i];
if (i < a->size - 1)
memmove(&a->items[a->offset + i], &a->items[a->offset + i + 1],
sizeof(void *) * (a->size - 1 - i));
a->items[a->offset + --a->size] = NULL;
return item;
}
inline const size_t Array_size(const struct Array *restrict const a) {
return a->size;
}
inline void *const *Array_items(const struct Array *restrict const a) {
return &a->items[a->offset];
}
#ifdef MULTI_THREADED
inline mtx_t *Array_mutex(struct Array *restrict const a) { return &a->mtx; }
inline void Array_lock(struct Array *restrict const a) { mutex_lock(&a->mtx); }
inline bool Array_trylock(struct Array *restrict const a) {
return mutex_trylock(&a->mtx);
}
inline void Array_unlock(struct Array *restrict const a) {
mutex_unlock(&a->mtx);
}
#endif