Janet 1.12.2-b91fe8b Documentation
(Other Versions: 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

Array C API

As with most data structures, arrays are exposed directly to the programmer in the C API. Users are free to set and get elements in the array, event change the count value of the array to change it's size. It is recommended to use the provided API functions to mutate the array aside from setting a single element, though. For getting and setting values in the array, use array->data[index] directly.

Definition

/* A dynamic array type. */
struct JanetArray {
    JanetGCObject gc;
    int32_t count;
    int32_t capacity;
    Janet *data;
};
typedef struct JanetArray JanetArray;

Functions

JANET_API JanetArray *janet_array(int32_t capacity);

Creates a new, empty array with enough preallocated space for capacity elements.

JANET_API JanetArray *janet_array_n(const Janet *elements, int32_t n);

Creates a new array and fills it with n elements. The elements are copied into the array via memcpy.

JANET_API void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth);

Ensure that an array has enough space for capacity elements. If not, resize the backing memory to capacity * growth slots. In most cases, growth should be 1 or 2.

JANET_API void janet_array_setcount(JanetArray *array, int32_t count);

Set the length of a janet array by setting array->count = count. If count is greater than the current array->count, also lengthens the array by appending nils. Do not pass a negative value to count.

JANET_API void janet_array_push(JanetArray *array, Janet x);

Insert a value at the end of an array. May panic if the array runs out of capacity.

JANET_API Janet janet_array_pop(JanetArray *array);

Remove an element from the end of the array. Returns the element, or nil if the array is empty.

JANET_API Janet janet_array_peek(JanetArray *array);

Returns the last element in the array, not modifying the array.