Appends el and returns a pointer to the stored copy, which stays valid until xar_reset. Returns null if a chunk could not be allocated.
#import "Xar";
A growable array that allocates in chunks, so its elements never move.
Procedures#
Removes element index by copying the last element over it and shrinking the count. The order changes and no memory is freed.
array: Xar(int, 2, 4);
for 1..4 xar_add(*array, it);
xar_unordered_remove(*array, 0);
assert(array.count == 3 && array[0] == 4);
xar_reset(*array);↗Frees every chunk and empties the array, so it can be used again. All pointers into it are invalid afterwards. The elements are not destroyed first.
An iterator positioned at element index, for walking with xar_get, xar_next and xar_prev; cheaper than indexing each step.
A pointer to the element an iterator is at. Fails an assert when the iterator is past the end or was never positioned.
Moves the iterator to the following element and returns a pointer to it, or null at the end (the iterator then stays where it was).
Moves the iterator to the preceding element and returns a pointer to it, or null at the start (the iterator then stays where it was).
Lets a Xar be used in a for loop over its elements in order: it is the element (a pointer with for *) and it_index its index.
Sorts a Xar in place with a three-way comparator (negative, zero or positive). Despite the name it is a heap sort, not a quicksort.
array: Xar(int, 2, 4);
for value: int.[3, 1, 2] xar_add(*array, value);
quick_sort(array, (a: int, b: int) -> int { return a - b; });
assert(array[0] == 1 && array[2] == 3);
xar_reset(*array);↗The same heap sort as quick_sort (unstable, O(n log n)); the name is kept so that code written for either works.
The same heap sort as quick_sort (unstable, O(n log n)); the name is kept so that code written for either works.
Types#
Xar(Element_Type, first_chunk_size_shift, max_chunks): a growable array whose elements never move.