paul_list.h
IntroductionDynamic array (stretchy-buffer) macros for C/C++. DiscussionImplementation is included when PAUL_LIST_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.
Macro Definitions
list_appendAppend a value to the end of the list. #define list_append(a, v) ParametersDiscussionThis macro may reallocate the list to accommodate the new element. After growing, it stores the value at the next index and increments the list count. list_carReturn a pointer to the first element of the list (the "car"). ParametersReturn ValuePointer to the first element, or NULL if the list is NULL. list_cdrReturn a pointer to the list starting at the second element (the "cdr"). ParametersReturn ValuePointer to the second element of the list, or NULL if the list has fewer than two elements. list_clearClear all elements from the list and shrink its backing allocation. #define list_clear(a) Parameterslist_countReturn the number of elements currently stored in the list. #define list_count(a) ParametersReturn ValueThe number of elements in the list (0 if 'a' is NULL). list_freeFree the underlying buffer used by a dynamic list created with these macros. ParametersDiscussionIf 'a' is non-NULL this macro frees the internal allocation backing the list. After calling this macro the caller must not use 'a' unless it is reinitialized. The macro evaluates to 0. list_insertInsert a value at the specified index, shifting subsequent elements. #define list_insert(a, idx, v) ParametersDiscussionThis macro grows the list if necessary, shifts elements starting at 'idx' one slot to the right, stores 'v' at 'idx', and increments the stored element count. list_lastReturn a pointer to the last element in the list. ParametersReturn ValuePointer to the last element, or NULL if the list is NULL or empty. list_popRemove the last element from the list (decrement count) and possibly shrink the backing storage. ParametersDiscussionThis macro only adjusts the stored element count; it does not return the popped value. It may shrink the backing allocation when the list becomes sparse. list_pushPush a value to the front of the list (insert at index 0). Parameterslist_remove_atRemove the element at the given index, shifting subsequent elements left. #define list_remove_at(a, idx) ParametersDiscussionThe macro shifts elements after 'idx' down and adjusts the stored count. It will also attempt to shrink the backing allocation when appropriate. list_reverseReverse the order of elements in the list in-place. #define list_reverse(a) \ do { \ int len = list_count(a); \ for (int i = 0; i < len / 2; i++) { \ int j = (len - i) - 1; \ typeof(a[i]) tmp = a[i]; \ a[i] = a[j]; \ a[j] = tmp; \ } \ } while(0) Parameterslist_shiftRemove the first element of the list (shift). #define list_shift(a) Parameterslist_shuffleRandomly shuffle the elements of the list in-place using rand(). #define list_shuffle(a) \ do { \ int i, j, n = list_count(a); \ for (i = n - 1; i > 0; i--) { \ j = rand() % (i + 1); \ typeof(a[i]) tmp = a[j]; \ a[j] = a[i]; \ a[i] = tmp; \ } \ } while(0) ParametersDiscussionUses the Fisher–Yates shuffle algorithm. The caller is responsible for seeding the PRNG (e.g. srand()) if reproducible behaviour is required. |