2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2021-05-31 18:46:02 +00:00
|
|
|
* Copyright (c) 2021 Calvin Rose
|
2017-11-01 21:53:43 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.h"
|
2019-07-31 05:15:28 +00:00
|
|
|
#include "state.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
#include <string.h>
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2019-06-05 20:19:51 +00:00
|
|
|
/* Creates a new array */
|
|
|
|
JanetArray *janet_array(int32_t capacity) {
|
|
|
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *data = NULL;
|
2017-12-17 04:11:51 +00:00
|
|
|
if (capacity > 0) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += capacity * sizeof(Janet);
|
2021-03-23 10:00:48 +00:00
|
|
|
data = (Janet *) janet_malloc(sizeof(Janet) * (size_t) capacity);
|
2017-12-17 04:11:51 +00:00
|
|
|
if (NULL == data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
array->count = 0;
|
|
|
|
array->capacity = capacity;
|
|
|
|
array->data = data;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Creates a new array from n elements. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetArray *janet_array_n(const Janet *elements, int32_t n) {
|
|
|
|
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
|
2018-07-04 03:07:35 +00:00
|
|
|
array->capacity = n;
|
|
|
|
array->count = n;
|
2021-03-23 10:00:48 +00:00
|
|
|
array->data = janet_malloc(sizeof(Janet) * (size_t) n);
|
2018-07-04 03:07:35 +00:00
|
|
|
if (!array->data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-04 03:07:35 +00:00
|
|
|
}
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(array->data, elements, sizeof(Janet) * n);
|
2018-07-04 03:07:35 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Ensure the array has enough capacity for elements */
|
2018-11-08 03:27:06 +00:00
|
|
|
void janet_array_ensure(JanetArray *array, int32_t capacity, int32_t growth) {
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet *newData;
|
|
|
|
Janet *old = array->data;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (capacity <= array->capacity) return;
|
2019-10-11 03:59:43 +00:00
|
|
|
int64_t new_capacity = ((int64_t) capacity) * growth;
|
2019-08-19 05:19:51 +00:00
|
|
|
if (new_capacity > INT32_MAX) new_capacity = INT32_MAX;
|
|
|
|
capacity = (int32_t) new_capacity;
|
2021-03-23 10:00:48 +00:00
|
|
|
newData = janet_realloc(old, capacity * sizeof(Janet));
|
2017-11-01 21:53:43 +00:00
|
|
|
if (NULL == newData) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.next_collection += (capacity - array->capacity) * sizeof(Janet);
|
2017-11-01 21:53:43 +00:00
|
|
|
array->data = newData;
|
|
|
|
array->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the count of an array. Extend with nil if needed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_setcount(JanetArray *array, int32_t count) {
|
2017-11-29 20:17:56 +00:00
|
|
|
if (count < 0)
|
|
|
|
return;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (count > array->count) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_array_ensure(array, count, 1);
|
2018-01-05 21:17:55 +00:00
|
|
|
for (i = array->count; i < count; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
array->data[i] = janet_wrap_nil();
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
array->count = count;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a value to the top of the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_array_push(JanetArray *array, Janet x) {
|
2020-01-03 02:27:38 +00:00
|
|
|
if (array->count == INT32_MAX) {
|
|
|
|
janet_panic("array overflow");
|
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newcount = array->count + 1;
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_array_ensure(array, newcount, 2);
|
2017-11-01 21:53:43 +00:00
|
|
|
array->data[array->count] = x;
|
|
|
|
array->count = newcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop a value from the top of the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_array_pop(JanetArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[--array->count];
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look at the last value in the array */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_array_peek(JanetArray *array) {
|
2017-11-01 21:53:43 +00:00
|
|
|
if (array->count) {
|
|
|
|
return array->data[array->count - 1];
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
|
|
|
/* C Functions */
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_new(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t cap = janet_getinteger(argv, 0);
|
|
|
|
JanetArray *array = janet_array(cap);
|
|
|
|
return janet_wrap_array(array);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:02:50 +00:00
|
|
|
static Janet cfun_array_new_filled(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
int32_t count = janet_getinteger(argv, 0);
|
|
|
|
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
|
|
|
|
JanetArray *array = janet_array(count);
|
|
|
|
for (int32_t i = 0; i < count; i++) {
|
|
|
|
array->data[i] = x;
|
|
|
|
}
|
|
|
|
array->count = count;
|
|
|
|
return janet_wrap_array(array);
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:58:11 +00:00
|
|
|
static Janet cfun_array_fill(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
Janet x = (argc == 2) ? argv[1] : janet_wrap_nil();
|
|
|
|
for (int32_t i = 0; i < array->count; i++) {
|
|
|
|
array->data[i] = x;
|
|
|
|
}
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_pop(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
return janet_array_pop(array);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_peek(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
return janet_array_peek(array);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_push(int32_t argc, Janet *argv) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
2020-01-03 04:02:57 +00:00
|
|
|
if (INT32_MAX - argc + 1 <= array->count) {
|
|
|
|
janet_panic("array overflow");
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t newcount = array->count - 1 + argc;
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_array_ensure(array, newcount, 2);
|
2020-01-07 01:30:44 +00:00
|
|
|
if (argc > 1) memcpy(array->data + array->count, argv + 1, (size_t)(argc - 1) * sizeof(Janet));
|
2018-01-18 22:25:45 +00:00
|
|
|
array->count = newcount;
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_ensure(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 3);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
int32_t newcount = janet_getinteger(argv, 1);
|
|
|
|
int32_t growth = janet_getinteger(argv, 2);
|
|
|
|
if (newcount < 1) janet_panic("expected positive integer");
|
2018-11-08 03:27:06 +00:00
|
|
|
janet_array_ensure(array, newcount, growth);
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_slice(int32_t argc, Janet *argv) {
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetView view = janet_getindexed(argv, 0);
|
2019-11-05 15:41:30 +00:00
|
|
|
JanetRange range = janet_getslice(argc, argv);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetArray *array = janet_array(range.end - range.start);
|
2019-02-22 17:10:27 +00:00
|
|
|
if (array->data)
|
|
|
|
memcpy(array->data, view.items + range.start, sizeof(Janet) * (range.end - range.start));
|
2019-01-06 01:09:03 +00:00
|
|
|
array->count = range.end - range.start;
|
|
|
|
return janet_wrap_array(array);
|
2018-01-27 20:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_concat(int32_t argc, Janet *argv) {
|
2018-03-12 04:26:13 +00:00
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
switch (janet_type(argv[i])) {
|
2018-03-12 04:26:13 +00:00
|
|
|
default:
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_array_push(array, argv[i]);
|
2018-03-12 04:26:13 +00:00
|
|
|
break;
|
2018-09-06 02:18:42 +00:00
|
|
|
case JANET_ARRAY:
|
2019-02-20 01:51:34 +00:00
|
|
|
case JANET_TUPLE: {
|
2019-03-23 17:50:50 +00:00
|
|
|
int32_t j, len = 0;
|
|
|
|
const Janet *vals = NULL;
|
2019-02-20 01:51:34 +00:00
|
|
|
janet_indexed_view(argv[i], &vals, &len);
|
|
|
|
for (j = 0; j < len; j++)
|
|
|
|
janet_array_push(array, vals[j]);
|
|
|
|
}
|
|
|
|
break;
|
2018-03-12 04:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_array(array);
|
2018-03-12 04:26:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_array_insert(int32_t argc, Janet *argv) {
|
2018-11-25 20:58:50 +00:00
|
|
|
size_t chunksize, restsize;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 2, -1);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
int32_t at = janet_getinteger(argv, 1);
|
2018-11-25 20:58:50 +00:00
|
|
|
if (at < 0) {
|
2019-01-06 08:23:03 +00:00
|
|
|
at = array->count + at + 1;
|
2018-11-25 20:58:50 +00:00
|
|
|
}
|
|
|
|
if (at < 0 || at > array->count)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("insertion index %d out of range [0,%d]", at, array->count);
|
|
|
|
chunksize = (argc - 2) * sizeof(Janet);
|
2018-11-25 20:58:50 +00:00
|
|
|
restsize = (array->count - at) * sizeof(Janet);
|
2020-01-03 02:27:38 +00:00
|
|
|
if (INT32_MAX - (argc - 2) < array->count) {
|
|
|
|
janet_panic("array overflow");
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_array_ensure(array, array->count + argc - 2, 2);
|
2020-01-29 05:38:52 +00:00
|
|
|
if (restsize) {
|
|
|
|
memmove(array->data + at + argc - 2,
|
|
|
|
array->data + at,
|
|
|
|
restsize);
|
|
|
|
}
|
|
|
|
safe_memcpy(array->data + at, argv + 2, chunksize);
|
2019-01-06 01:09:03 +00:00
|
|
|
array->count += (argc - 2);
|
2019-03-08 15:17:57 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet cfun_array_remove(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
int32_t at = janet_getinteger(argv, 1);
|
|
|
|
int32_t n = 1;
|
|
|
|
if (at < 0) {
|
|
|
|
at = array->count + at + 1;
|
|
|
|
}
|
|
|
|
if (at < 0 || at > array->count)
|
|
|
|
janet_panicf("removal index %d out of range [0,%d]", at, array->count);
|
|
|
|
if (argc == 3) {
|
|
|
|
n = janet_getinteger(argv, 2);
|
|
|
|
if (n < 0)
|
|
|
|
janet_panicf("expected non-negative integer for argument n, got %v", argv[2]);
|
|
|
|
}
|
|
|
|
if (at + n > array->count) {
|
|
|
|
n = array->count - at;
|
|
|
|
}
|
|
|
|
memmove(array->data + at,
|
|
|
|
array->data + at + n,
|
|
|
|
(array->count - at - n) * sizeof(Janet));
|
|
|
|
array->count -= n;
|
|
|
|
return argv[0];
|
2018-11-25 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 22:40:48 +00:00
|
|
|
static Janet cfun_array_trim(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
if (array->count) {
|
|
|
|
if (array->count < array->capacity) {
|
2021-03-23 10:00:48 +00:00
|
|
|
Janet *newData = janet_realloc(array->data, array->count * sizeof(Janet));
|
2020-06-14 22:40:48 +00:00
|
|
|
if (NULL == newData) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
array->data = newData;
|
|
|
|
array->capacity = array->count;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
array->capacity = 0;
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(array->data);
|
2020-06-14 22:40:48 +00:00
|
|
|
array->data = NULL;
|
|
|
|
}
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:03:57 +00:00
|
|
|
static Janet cfun_array_clear(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetArray *array = janet_getarray(argv, 0);
|
|
|
|
array->count = 0;
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg array_cfuns[] = {
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/new", cfun_array_new,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/new capacity)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a new empty array with a pre-allocated capacity. The same as "
|
|
|
|
"(array) but can be more efficient if the maximum size of an array is known.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-12-18 18:02:50 +00:00
|
|
|
{
|
|
|
|
"array/new-filled", cfun_array_new_filled,
|
2019-12-19 17:58:11 +00:00
|
|
|
JDOC("(array/new-filled count &opt value)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Creates a new array of `count` elements, all set to `value`, which defaults to nil. Returns the new array.")
|
2019-12-18 18:02:50 +00:00
|
|
|
},
|
2019-12-19 17:58:11 +00:00
|
|
|
{
|
|
|
|
"array/fill", cfun_array_fill,
|
|
|
|
JDOC("(array/fill arr &opt value)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Replace all elements of an array with `value` (defaulting to nil) without changing the length of the array. "
|
2019-12-19 17:58:11 +00:00
|
|
|
"Returns the modified array.")
|
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/pop", cfun_array_pop,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/pop arr)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Remove the last element of the array and return it. If the array is empty, will return nil. Modifies "
|
|
|
|
"the input array.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/peek", cfun_array_peek,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/peek arr)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns the last element of the array. Does not modify the array.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/push", cfun_array_push,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/push arr x)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Insert an element in the end of an array. Modifies the input array and returns it.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/ensure", cfun_array_ensure,
|
2019-11-29 04:16:32 +00:00
|
|
|
JDOC("(array/ensure arr capacity growth)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Ensures that the memory backing the array is large enough for `capacity` "
|
2019-11-29 04:16:32 +00:00
|
|
|
"items at the given rate of growth. Capacity and growth must be integers. "
|
|
|
|
"If the backing capacity is already enough, then this function does nothing. "
|
|
|
|
"Otherwise, the backing memory will be reallocated so that there is enough space.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/slice", cfun_array_slice,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(array/slice arrtup &opt start end)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Takes a slice of array or tuple from `start` to `end`. The range is half open, "
|
|
|
|
"[start, end). Indexes can also be negative, indicating indexing from the "
|
|
|
|
"end of the array. By default, `start` is 0 and `end` is the length of the array. "
|
|
|
|
"Note that index -1 is synonymous with index `(length arrtup)` to allow a full "
|
2019-12-03 09:05:43 +00:00
|
|
|
"negative slice range. Returns a new array.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/concat", cfun_array_concat,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/concat arr & parts)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Concatenates a variable number of arrays (and tuples) into the first argument, "
|
2020-09-12 14:38:29 +00:00
|
|
|
"which must be an array. If any of the parts are arrays or tuples, their elements will "
|
2021-07-24 20:58:21 +00:00
|
|
|
"be inserted into the array. Otherwise, each part in `parts` will be appended to `arr` in order. "
|
|
|
|
"Return the modified array `arr`.")
|
2018-11-15 20:45:41 +00:00
|
|
|
},
|
2019-02-20 01:51:34 +00:00
|
|
|
{
|
|
|
|
"array/insert", cfun_array_insert,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(array/insert arr at & xs)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Insert all `xs` into array `arr` at index `at`. `at` should be an integer between "
|
|
|
|
"0 and the length of the array. A negative value for `at` will index backwards from "
|
2019-02-20 01:51:34 +00:00
|
|
|
"the end of the array, such that inserting at -1 appends to the array. "
|
|
|
|
"Returns the array.")
|
2018-11-25 20:58:50 +00:00
|
|
|
},
|
2019-03-08 15:17:57 +00:00
|
|
|
{
|
|
|
|
"array/remove", cfun_array_remove,
|
2019-06-08 14:30:30 +00:00
|
|
|
JDOC("(array/remove arr at &opt n)\n\n"
|
2021-07-24 20:58:21 +00:00
|
|
|
"Remove up to `n` elements starting at index `at` in array `arr`. `at` can index from "
|
|
|
|
"the end of the array with a negative index, and `n` must be a non-negative integer. "
|
|
|
|
"By default, `n` is 1. "
|
2019-03-08 15:17:57 +00:00
|
|
|
"Returns the array.")
|
|
|
|
},
|
2020-06-14 22:40:48 +00:00
|
|
|
{
|
|
|
|
"array/trim", cfun_array_trim,
|
|
|
|
JDOC("(array/trim arr)\n\n"
|
|
|
|
"Set the backing capacity of an array to its current length. Returns the modified array.")
|
|
|
|
},
|
2020-12-18 17:03:57 +00:00
|
|
|
{
|
|
|
|
"array/clear", cfun_array_clear,
|
|
|
|
JDOC("(array/clear arr)\n\n"
|
|
|
|
"Empties an array, setting it's count to 0 but does not free the backing capacity. "
|
|
|
|
"Returns the modified array.")
|
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-05-01 15:06:31 +00:00
|
|
|
};
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
/* Load the array module */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_array(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, array_cfuns);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|