Path: blob/main/contrib/libdiff/include/arraylist.h
35065 views
/* Auto-reallocating array for arbitrary member types. */1/*2* Copyright (c) 2020 Neels Hofmeyr <[email protected]>3*4* Permission to use, copy, modify, and distribute this software for any5* purpose with or without fee is hereby granted, provided that the above6* copyright notice and this permission notice appear in all copies.7*8* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES9* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF10* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR11* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES12* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN13* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF14* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.15*/1617/* Usage:18*19* ARRAYLIST(any_type_t) list;20* // OR21* typedef ARRAYLIST(any_type_t) any_type_list_t;22* any_type_list_t list;23*24* // pass the number of (at first unused) members to add on each realloc:25* ARRAYLIST_INIT(list, 128);26* any_type_t *x;27* while (bar) {28* // This enlarges the allocated array as needed;29* // list.head may change due to realloc:30* ARRAYLIST_ADD(x, list);31* if (!x)32* return ENOMEM;33* *x = random_foo_value;34* }35* for (i = 0; i < list.len; i++)36* printf("%s", foo_to_str(list.head[i]));37* ARRAYLIST_FREE(list);38*/39#define ARRAYLIST(MEMBER_TYPE) \40struct { \41MEMBER_TYPE *head; \42MEMBER_TYPE *p; \43unsigned int len; \44unsigned int allocated; \45unsigned int alloc_blocksize; \46}4748#define ARRAYLIST_INIT(ARRAY_LIST, ALLOC_BLOCKSIZE) do { \49(ARRAY_LIST).head = NULL; \50(ARRAY_LIST).len = 0; \51(ARRAY_LIST).allocated = 0; \52(ARRAY_LIST).alloc_blocksize = ALLOC_BLOCKSIZE; \53} while(0)5455#define ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST) do { \56if ((ARRAY_LIST).len && !(ARRAY_LIST).allocated) { \57NEW_ITEM_P = NULL; \58break; \59} \60if ((ARRAY_LIST).head == NULL \61|| (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \62(ARRAY_LIST).p = recallocarray((ARRAY_LIST).head, \63(ARRAY_LIST).len, \64(ARRAY_LIST).allocated + \65((ARRAY_LIST).allocated ? \66(ARRAY_LIST).allocated / 2 : \67(ARRAY_LIST).alloc_blocksize ? \68(ARRAY_LIST).alloc_blocksize : 8), \69sizeof(*(ARRAY_LIST).head)); \70if ((ARRAY_LIST).p == NULL) { \71NEW_ITEM_P = NULL; \72break; \73} \74(ARRAY_LIST).allocated += \75(ARRAY_LIST).allocated ? \76(ARRAY_LIST).allocated / 2 : \77(ARRAY_LIST).alloc_blocksize ? \78(ARRAY_LIST).alloc_blocksize : 8, \79(ARRAY_LIST).head = (ARRAY_LIST).p; \80(ARRAY_LIST).p = NULL; \81}; \82if ((ARRAY_LIST).head == NULL \83|| (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \84NEW_ITEM_P = NULL; \85break; \86} \87(NEW_ITEM_P) = &(ARRAY_LIST).head[(ARRAY_LIST).len]; \88(ARRAY_LIST).len++; \89} while (0)9091#define ARRAYLIST_INSERT(NEW_ITEM_P, ARRAY_LIST, AT_IDX) do { \92int _at_idx = (AT_IDX); \93ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST); \94if ((NEW_ITEM_P) \95&& _at_idx >= 0 \96&& _at_idx < (ARRAY_LIST).len) { \97memmove(&(ARRAY_LIST).head[_at_idx + 1], \98&(ARRAY_LIST).head[_at_idx], \99((ARRAY_LIST).len - 1 - _at_idx) \100* sizeof(*(ARRAY_LIST).head)); \101(NEW_ITEM_P) = &(ARRAY_LIST).head[_at_idx]; \102}; \103} while (0)104105#define ARRAYLIST_CLEAR(ARRAY_LIST) \106(ARRAY_LIST).len = 0107108#define ARRAYLIST_FREE(ARRAY_LIST) \109do { \110if ((ARRAY_LIST).head && (ARRAY_LIST).allocated) \111free((ARRAY_LIST).head); \112ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \113} while(0)114115#define ARRAYLIST_FOREACH(ITEM_P, ARRAY_LIST) \116for ((ITEM_P) = (ARRAY_LIST).head; \117(ITEM_P) - (ARRAY_LIST).head < (ARRAY_LIST).len; \118(ITEM_P)++)119120#define ARRAYLIST_IDX(ITEM_P, ARRAY_LIST) ((ITEM_P) - (ARRAY_LIST).head)121122123