-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathparray.c
More file actions
executable file
·454 lines (405 loc) · 10.4 KB
/
parray.c
File metadata and controls
executable file
·454 lines (405 loc) · 10.4 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*-------------------------------------------------------------------------
*
* parray.c: pointer array collection.
*
* Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
* Portions Copyright (c) 2024-2025 ZhangChen
*
* This code is released under the PostgreSQL License.
* See LICENSE-PostgreSQL.
*
* Modifications by ZhangChen licensed under the Apache License, Version 2.0;
* see LICENSE.
*
*-------------------------------------------------------------------------
*/
#include "parray.h"
/*
* struct parray - Internal structure for dynamic pointer array
*
* Members of struct parray are hidden from client code.
* This structure manages a dynamically resizable array of void pointers.
*/
struct parray
{
void **data;
size_t alloced;
size_t used;
};
/**
* pgut_malloc - Allocate memory with error reporting
*
* @size: Number of bytes to allocate
*
* Allocates the requested amount of memory. If allocation fails,
* prints an error message to stdout.
*
* Returns: Pointer to allocated memory, or NULL on failure
*/
void *
pgut_malloc(size_t size)
{
char *ret;
if ((ret = malloc(size)) == NULL)
printf("could not allocate memory (%lu bytes)",
(unsigned long) size);
return ret;
}
/**
* pgut_realloc - Reallocate memory with error reporting
*
* @p: Pointer to existing memory block (or NULL)
* @size: New size in bytes
*
* Reallocates the memory block to the specified size. If reallocation
* fails, prints an error message to stdout.
*
* Returns: Pointer to reallocated memory, or NULL on failure
*/
void *
pgut_realloc(void *p, size_t size)
{
char *ret;
if ((ret = realloc(p, size)) == NULL)
printf( "could not re-allocate memory (%lu bytes)",
(unsigned long) size);
return ret;
}
/**
* parray_new - Create a new dynamic pointer array
*
* Allocates and initializes a new parray object with an initial
* capacity of 1024 elements.
*
* Returns: Pointer to the newly created parray (never returns NULL)
*/
parray *
parray_new(void)
{
parray *a = pgut_new(parray);
a->data = NULL;
a->used = 0;
a->alloced = 0;
parray_expand(a, 1024);
return a;
}
/**
* parray_expand - Expand array capacity to specified size
*
* @array: Pointer to the parray to expand
* @newsize: New capacity (number of elements)
*
* Expands the internal storage of the parray to hold at least newsize
* elements. Newly allocated slots are initialized to NULL. Does nothing
* if current capacity is already sufficient.
*/
void
parray_expand(parray *array, size_t newsize)
{
void **p;
if (newsize <= array->alloced)
return;
p = pgut_realloc(array->data, sizeof(void *) * newsize);
memset(p + array->alloced, 0, (newsize - array->alloced) * sizeof(void *));
array->alloced = newsize;
array->data = p;
}
/**
* parray_free - Free a parray and its internal storage
*
* @array: Pointer to the parray to free (may be NULL)
*
* Releases all memory associated with the parray. Safe to call with NULL.
* Note: Does not free the elements stored in the array.
*/
void
parray_free(parray *array)
{
if (array == NULL)
return;
free(array->data);
free(array);
}
/**
* parray_append - Append an element to the end of the array
*
* @array: Pointer to the parray
* @elem: Element to append
*
* Adds the element to the end of the array, automatically expanding
* the array capacity if necessary.
*/
void
parray_append(parray *array, void *elem)
{
if (array->used + 1 > array->alloced)
parray_expand(array, array->alloced * 2);
array->data[array->used++] = elem;
}
/**
* parray_insert - Insert an element at a specific index
*
* @array: Pointer to the parray
* @index: Position at which to insert the element
* @elem: Element to insert
*
* Inserts the element at the specified index, shifting existing elements
* to make room. Automatically expands the array if necessary.
*/
void
parray_insert(parray *array, size_t index, void *elem)
{
if (array->used + 1 > array->alloced)
parray_expand(array, array->alloced * 2);
memmove(array->data + index + 1, array->data + index,
(array->alloced - index - 1) * sizeof(void *));
array->data[index] = elem;
if (array->used < index + 1)
array->used = index + 1;
else
array->used++;
}
/**
* parray_concat - Concatenate two parrays
*
* @dest: Destination parray
* @src: Source parray to append
*
* Appends a copy of all elements from src to the end of dest.
* The source array is not modified.
*
* Returns: Pointer to the destination parray
*/
parray *
parray_concat(parray *dest, const parray *src)
{
parray_expand(dest, dest->used + src->used);
memcpy(dest->data + dest->used, src->data, src->used * sizeof(void *));
dest->used += parray_num(src);
return dest;
}
/**
* parray_set - Set an element at a specific index
*
* @array: Pointer to the parray
* @index: Index at which to set the element
* @elem: Element to store
*
* Sets the element at the specified index. Automatically expands
* the array if the index exceeds current capacity.
*/
void
parray_set(parray *array, size_t index, void *elem)
{
if (index > array->alloced - 1)
parray_expand(array, index + 1);
array->data[index] = elem;
if (array->used < index + 1)
array->used = index + 1;
}
/**
* parray_get - Retrieve an element at a specific index
*
* @array: Pointer to the parray
* @index: Index of the element to retrieve
*
* Returns: The element at the specified index, or NULL if index is out of bounds
*/
void *
parray_get(const parray *array, size_t index)
{
if (index > array->alloced - 1)
return NULL;
return array->data[index];
}
/**
* parray_remove - Remove and return an element at a specific index
*
* @array: Pointer to the parray
* @index: Index of the element to remove
*
* Removes the element at the specified index and shifts remaining
* elements to fill the gap.
*
* Returns: The removed element, or NULL if index is out of bounds
*/
void *
parray_remove(parray *array, size_t index)
{
void *val;
if (index > array->used)
return NULL;
val = array->data[index];
if (index < array->alloced - 1)
memmove(array->data + index, array->data + index + 1,
(array->alloced - index - 1) * sizeof(void *));
array->used--;
return val;
}
/**
* parray_rm - Remove the first element matching a key
*
* @array: Pointer to the parray
* @key: Key to search for
* @compare: Comparison function (returns 0 on match)
*
* Searches for an element matching the key using the provided comparison
* function and removes the first match found.
*
* Returns: true if an element was removed, false otherwise
*/
bool
parray_rm(parray *array, const void *key, int(*compare)(const void *, const void *))
{
int i;
for (i = 0; i < array->used; i++)
{
if (compare(&key, &array->data[i]) == 0)
{
parray_remove(array, i);
return true;
}
}
return false;
}
/**
* parray_num - Get the number of elements in the array
*
* @array: Pointer to the parray (may be NULL)
*
* Returns: Number of elements in the array, or 0 if array is NULL
*/
size_t
parray_num(const parray *array)
{
return array!= NULL ? array->used : (size_t) 0;
}
/**
* parray_qsort - Sort the array using quicksort
*
* @array: Pointer to the parray
* @compare: Comparison function for qsort
*
* Sorts the elements in the array using the standard qsort algorithm.
*/
void
parray_qsort(parray *array, int(*compare)(const void *, const void *))
{
qsort(array->data, array->used, sizeof(void *), compare);
}
/**
* parray_walk - Apply a function to each element in the array
*
* @array: Pointer to the parray
* @action: Function to call for each element
*
* Iterates through all elements and calls the action function on each.
*/
void
parray_walk(parray *array, void (*action)(void *))
{
int i;
for (i = 0; i < array->used; i++)
action(array->data[i]);
}
/**
* parray_bsearch - Binary search for an element in a sorted array
*
* @array: Pointer to the parray (must be sorted)
* @key: Key to search for
* @compare: Comparison function for bsearch
*
* Performs binary search on a sorted array.
*
* Returns: Pointer to the matching element, or NULL if not found
*/
void *
parray_bsearch(parray *array, const void *key, int(*compare)(const void *, const void *))
{
return bsearch(&key, array->data, array->used, sizeof(void *), compare);
}
/**
* parray_bsearch_index - Binary search returning element index
*
* @array: Pointer to the parray (must be sorted)
* @key: Key to search for
* @compare: Comparison function for bsearch
*
* Performs binary search and returns the index of the found element.
*
* Returns: Index of the matching element, or -1 if not found
*/
int
parray_bsearch_index(parray *array, const void *key, int(*compare)(const void *, const void *))
{
void **elem = parray_bsearch(array, key, compare);
return elem != NULL ? elem - array->data : -1;
}
/**
* parray_contains - Check if array contains a specific element
*
* @array: Pointer to the parray
* @elem: Element to search for (pointer comparison)
*
* Searches the array for the exact pointer value.
*
* Returns: true if element is found, false otherwise
*/
bool parray_contains(parray *array, void *elem)
{
int i;
for (i = 0; i < parray_num(array); i++)
{
if (parray_get(array, i) == elem)
return true;
}
return false;
}
/**
* parray_remove_if - Remove elements matching a criterion
*
* @array: Pointer to the parray
* @criterion: Function that returns true for elements to remove
* @args: Additional arguments passed to criterion function
* @clean: Cleanup function called for each removed element
*
* Removes all elements for which the criterion function returns true.
* The cleanup function is called on each removed element before removal.
* Uses an efficient in-place removal algorithm.
*/
void
parray_remove_if(parray *array, criterion_fn criterion, void *args, cleanup_fn clean) {
int i = 0;
int j = 0;
while(j < parray_num(array)) {
void *value = array->data[j];
if(criterion(value, args)) {
clean(value);
j++;
continue;
}
if(i != j)
array->data[i] = array->data[j];
i++;
j++;
}
array->used -= j - i;
}
/**
* parray_duplicate - Copy all elements from source to destination array
*
* @src: Source parray to copy from
* @dst: Destination parray to copy to
*
* Appends all elements from the source array to the destination array.
* Note: Only copies the pointers, not the underlying data.
*/
void
parray_duplicate(parray *src, parray *dst) {
int i=0;
for ( i = 0; i < parray_num(src); i++)
{
void *elem = parray_get(src,i);
parray_append(dst,elem);
}
}