Modules

Bucket_Array

moduleSource
macOSLinuxWindowswasm

#import "Bucket_Array";

A collection with stable element addresses, grown a fixed-size bucket at a time.

Procedures#

(array: *Bucket_Array, item: array.type) -> (Bucket_Locator, pointer: *array.type)

Stores item in the first free slot and returns its Bucket_Locator and a pointer to the stored copy.

array: Bucket_Array(int, 2);
a, pa := bucket_array_add(*array, 10);
b, pb := bucket_array_add(*array, 20);
c, pc := bucket_array_add(*array, 30); // starts a second bucket
assert(array.count == 3 && c.bucket_index == 1);

pa.* += 1; // pointers stay valid as the array grows
assert(bucket_array_find(*array, a) == 11);
bucket_array_reset(*array);
↗
(array: *$T/Bucket_Array, body: Code, flags: For_Flags)

Lets a Bucket_Array be used in a for loop over its live elements, in bucket and slot order; it_index numbers the elements visited, 0, 1, 2, ... with free slots skipped.

array: Bucket_Array(string, 2);
for string.["a", "b", "c"] bucket_array_add(*array, it);

for name, index: array {
    if name == "b" remove name;
}
assert(array.count == 2);
for name, index: array assert(name == ifx index == 0 then "a" else "c");
bucket_array_reset(*array);
↗

Types#

:: struct

A growable collection whose elements never move: Bucket_Array(Type, items_per_bucket).

array: Bucket_Array(int, 4);
for 10..14 bucket_array_add(*array, it);

for value, index: array {
    assert(value == 10 + index); // counts the elements visited, removed ones too
    if value == 11 || value == 13 remove value;
}

assert(array.count == 3);
for value, index: array assert(value == 10 + 2 * index); // 10, 12, 14: numbered afresh
bucket_array_reset(*array);
↗
Bucketstruct
:: struct

One bucket of a Bucket_Array: an occupancy flag and storage for each of items_per_bucket slots. Reachable through all_buckets; it is not meant to be created directly.

:: struct

Where an element lives: the number of its bucket and its slot in that bucket.