Add list_pop and list_merge utilities
This commit is contained in:
parent
1c1b611bbb
commit
01ece119ed
@ -31,27 +31,52 @@
|
||||
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
|
||||
} while (0)
|
||||
|
||||
#define _increase_list_capacity(T, LP, CAP) \
|
||||
do { \
|
||||
u64 new_capacity = LP->capacity * 2; \
|
||||
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
|
||||
assert(tmp != NULL && "Failed to increase capacity"); \
|
||||
\
|
||||
memcpy(tmp, LP->items, sizeof(T) * LP->count); \
|
||||
\
|
||||
i32 deallocated = munmap(LP->items, sizeof(T) * LP->capacity); \
|
||||
assert(deallocated == 0 && "Failed to deallocate old memory"); \
|
||||
\
|
||||
LP->capacity = new_capacity; \
|
||||
LP->items = tmp; \
|
||||
} while (0)
|
||||
|
||||
#define list_append(T, LP, ITEM) \
|
||||
do { \
|
||||
if (LP->count + 1 >= LP->capacity) { \
|
||||
u64 new_capacity = LP->capacity * 2; \
|
||||
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
|
||||
assert(tmp != NULL && "Failed to increase capacity"); \
|
||||
\
|
||||
memcpy(tmp, LP->items, sizeof(T) * LP->count); \
|
||||
\
|
||||
i32 deallocated = munmap(LP->items, sizeof(T) * LP->capacity); \
|
||||
assert(deallocated == 0 && "Failed to deallocate old memory"); \
|
||||
\
|
||||
LP->capacity = new_capacity; \
|
||||
LP->items = tmp; \
|
||||
_increase_list_capacity(T, LP, LP->capacity * 2); \
|
||||
} \
|
||||
\
|
||||
LP->items[(LP->count)++] = ITEM; \
|
||||
} while (0)
|
||||
|
||||
#define list_pop(LP) (LP->count -= 1)
|
||||
|
||||
#define list_get(LP, IDX) LP->items[IDX]
|
||||
|
||||
#define list_merge(T, DST, LP1, LP2) \
|
||||
do { \
|
||||
u64 new_count = LP1->count + LP2->count; \
|
||||
u64 capacity = \
|
||||
new_count < BASE_LIST_CAPACITY ? BASE_LIST_CAPACITY : new_count * 2; \
|
||||
\
|
||||
DST = (LIST_TYPE_NAME(T) *)_create_list(new_count, capacity); \
|
||||
assert(DST != NULL && "Failed to allocate new list"); \
|
||||
\
|
||||
u64 lp1_copy_size = sizeof(T) * LP1->count; \
|
||||
u64 lp2_copy_size = sizeof(T) * LP2->count; \
|
||||
memcpy(DST->items, LP1->items, lp1_copy_size); \
|
||||
T *dst = &(DST->items[LP1->count]); \
|
||||
memcpy(dst, LP2->items, lp2_copy_size); \
|
||||
DST->capacity = capacity; \
|
||||
DST->count = new_count; \
|
||||
} while (0)
|
||||
|
||||
MAKE_LIST_TYPE(void);
|
||||
|
||||
list_void_t *_create_list(u64 size, u64 count);
|
||||
|
Loading…
Reference in New Issue
Block a user