Janet 1.4.0-655d4b3 Documentation
(Other Versions: 1.4.0 1.3.1)

Array C API

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.