Appends el and returns a pointer to the stored copy, or null if a chunk could not be allocated.
#import "Xar";
A growable array whose elements never move, so pointers to them stay valid while you add more.
- Xar is the type; xar_add appends and xar_reset frees.
array[i]indexes,array.countcounts, andforwalks it in order.- xar_iterator, xar_next and xar_prev walk it cheaply by hand.
- quick_sort sorts it.
array: Xar(int, 2, 8);
first := xar_add(*array, 10);
for 1..20 xar_add(*array, it * 10);
assert(array.count == 21 && first == *array[0]);
assert(array[5] == 50);
xar_reset(*array);Procedures#
Removes element index by copying the last element over it. 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. Pointers into it are invalid afterwards. The elements are not destroyed first.
Returns an iterator at element index, for walking with xar_get, xar_next and xar_prev. Cheaper than indexing at every step.
array: Xar(int, 2, 4);
for 1..3 xar_add(*array, it);
iterator := xar_iterator(*array, 1);
assert(xar_get(*array, iterator).* == 2);
xar_reset(*array);↗Returns a pointer to the element the iterator is at. Fails an assert when the iterator is past the end.
Moves the iterator to the next element and returns a pointer to it, or null at the end (the iterator then stays put).
Moves the iterator to the previous element and returns a pointer to it, or null at the start (the iterator then stays put).
array: Xar(int, 2, 4);
for 1..3 xar_add(*array, it);
iterator := xar_iterator(*array, 2);
assert(xar_prev(*array, *iterator).* == 2);
xar_reset(*array);↗Makes a Xar work in a for loop: it is the element (a pointer with for *) and it_index its index. Do not add during the loop. Reverse loops are a compile error.
Sorts in place with a three-way comparator (negative, zero or positive). Despite the name it is an unstable heap sort, O(n log n); it returns nothing and has no key form.
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 sort as quick_sort under another name, so code written for either works.
array: Xar(int, 2, 4);
for value: int.[2, 1] xar_add(*array, value);
quick_sort_simple(array, (a: int, b: int) -> int { return a - b; });
assert(array[0] == 1);
xar_reset(*array);↗The same sort as quick_sort under another name, so code written for either works.
Types#
Xar(Element_Type, first_chunk_size_shift, max_chunks) stores its elements in separately allocated chunks.
- count: s64
- chunks: [max_chunks] *Element_Type
- allocator: Allocator