1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-15 21:24:48 +00:00
janet/src/core/array.c

268 lines
9.1 KiB
C
Raw Normal View History

2017-11-01 21:53:43 +00:00
/*
* Copyright (c) 2018 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.
*/
2018-09-06 02:18:42 +00:00
#include <janet/janet.h>
#include "gc.h"
#include <string.h>
2017-11-01 21:53:43 +00:00
2018-04-30 00:13:04 +00:00
/* Initializes an array */
2018-09-06 02:18:42 +00:00
JanetArray *janet_array_init(JanetArray *array, int32_t capacity) {
Janet *data = NULL;
if (capacity > 0) {
2018-09-06 02:18:42 +00:00
data = (Janet *) malloc(sizeof(Janet) * capacity);
if (NULL == data) {
2018-09-06 02:18:42 +00:00
JANET_OUT_OF_MEMORY;
}
2017-11-01 21:53:43 +00:00
}
array->count = 0;
array->capacity = capacity;
array->data = data;
return array;
}
2018-09-06 02:18:42 +00:00
void janet_array_deinit(JanetArray *array) {
free(array->data);
}
/* Creates a new array */
2018-09-06 02:18:42 +00:00
JanetArray *janet_array(int32_t capacity) {
JanetArray *array = janet_gcalloc(JANET_MEMORY_ARRAY, sizeof(JanetArray));
return janet_array_init(array, capacity);
}
/* 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));
array->capacity = n;
array->count = n;
2018-09-06 02:18:42 +00:00
array->data = malloc(sizeof(Janet) * n);
if (!array->data) {
2018-09-06 02:18:42 +00:00
JANET_OUT_OF_MEMORY;
}
2018-09-06 02:18:42 +00:00
memcpy(array->data, elements, sizeof(Janet) * n);
return array;
}
2017-11-01 21:53:43 +00:00
/* Ensure the array has enough capacity for elements */
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;
capacity *= growth;
2018-09-06 02:18:42 +00:00
newData = 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
}
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) {
int32_t i;
janet_array_ensure(array, count, 1);
for (i = array->count; i < count; i++) {
2018-09-06 02:18:42 +00:00
array->data[i] = janet_wrap_nil();
}
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) {
int32_t newcount = array->count + 1;
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 */
static Janet cfun_new(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 1);
int32_t cap = janet_getinteger(argv, 0);
JanetArray *array = janet_array(cap);
return janet_wrap_array(array);
}
static Janet cfun_pop(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 1);
JanetArray *array = janet_getarray(argv, 0);
return janet_array_pop(array);
2018-01-18 22:25:45 +00:00
}
static Janet cfun_peek(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 1);
JanetArray *array = janet_getarray(argv, 0);
return janet_array_peek(array);
2018-01-18 22:25:45 +00:00
}
static Janet cfun_push(int32_t argc, Janet *argv) {
janet_arity(argc, 1, -1);
JanetArray *array = janet_getarray(argv, 0);
int32_t newcount = array->count - 1 + argc;
janet_array_ensure(array, newcount, 2);
if (argc > 1) memcpy(array->data + array->count, argv + 1, (argc - 1) * sizeof(Janet));
2018-01-18 22:25:45 +00:00
array->count = newcount;
return argv[0];
2018-01-18 22:25:45 +00:00
}
static Janet cfun_ensure(int32_t argc, Janet *argv) {
janet_arity(argc, 3, 3);
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");
janet_array_ensure(array, newcount, growth);
return argv[0];
2018-01-18 22:25:45 +00:00
}
static Janet cfun_slice(int32_t argc, Janet *argv) {
JanetRange range = janet_getslice(argc, argv);
JanetView view = janet_getindexed(argv, 0);
JanetArray *array = janet_array(range.end - range.start);
memcpy(array->data, view.items + range.start, sizeof(Janet) * (range.end - range.start));
array->count = range.end - range.start;
return janet_wrap_array(array);
}
static Janet cfun_concat(int32_t argc, Janet *argv) {
int32_t i;
janet_arity(argc, 1, -1);
JanetArray *array = janet_getarray(argv, 0);
for (i = 1; i < argc; i++) {
switch (janet_type(argv[i])) {
default:
janet_array_push(array, argv[i]);
break;
2018-09-06 02:18:42 +00:00
case JANET_ARRAY:
case JANET_TUPLE:
{
int32_t j, len;
2018-09-06 02:18:42 +00:00
const Janet *vals;
janet_indexed_view(argv[i], &vals, &len);
for (j = 0; j < len; j++)
2018-09-06 02:18:42 +00:00
janet_array_push(array, vals[j]);
}
break;
}
}
return janet_wrap_array(array);
}
static Janet cfun_insert(int32_t argc, Janet *argv) {
2018-11-25 20:58:50 +00:00
size_t chunksize, restsize;
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) {
at = array->count + at + 1;
}
if (at < 0 || at > array->count)
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);
janet_array_ensure(array, array->count + argc - 2, 2);
memmove(array->data + at + argc - 2,
2018-11-25 20:58:50 +00:00
array->data + at,
restsize);
memcpy(array->data + at, argv + 2, chunksize);
array->count += (argc - 2);
return janet_wrap_array(array);
2018-11-25 20:58:50 +00:00
}
2018-09-06 02:18:42 +00:00
static const JanetReg cfuns[] = {
{"array/new", cfun_new,
"(array/new capacity)\n\n"
"Creates a new empty array with a preallocated capacity. The same as "
"(array) but can be more efficient if the maximum size of an array is known."
},
{"array/pop", cfun_pop,
"(array/pop arr)\n\n"
"Remove the last element of the array and return it. If the array is empty, will return nil. Modifies "
"the input array."
},
{"array/peek", cfun_peek,
"(array/peek arr)\n\n"
"Returns the last element of the array. Does not modify the array."
},
{"array/push", cfun_push,
"(array/push arr x)\n\n"
"Insert an element in the end of an array. Modifies the input array and returns it."
},
{"array/ensure", cfun_ensure,
"(array/ensure arr capacity)\n\n"
"Ensures that the memory backing the array has enough memory for capacity "
"items. Capacity must be an integer. 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."
},
{"array/slice", cfun_slice,
"(array/slice arrtup [, start=0 [, end=(length arrtup)]])\n\n"
"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 "
"end of the array. By default, start is 0 and end is the length of the array. "
"Returns a new array."
},
{"array/concat", cfun_concat,
"(array/concat arr & parts)\n\n"
"Concatenates a variadic number of arrays (and tuples) into the first argument "
"which must an array. If any of the parts are arrays or tuples, their elements will "
"be inserted into the array. Otherwise, each part in parts will be appended to arr in order. "
"Return the modified array arr."
},
{"array/insert", cfun_insert,
"(array/insert arr at & xs)\n\n"
2018-11-25 20:58:50 +00:00
"Insert all of xs into array arr at index at. at should be an integer "
"0 and the length of the array. A negative value for at will index from "
"the end of the array, such that inserting at -1 appends to the array. "
"Returns the array."
},
{NULL, NULL, NULL}
};
2018-01-18 22:25:45 +00:00
/* Load the array module */
void janet_lib_array(JanetTable *env) {
2018-09-06 02:18:42 +00:00
janet_cfuns(env, NULL, cfuns);
2018-01-18 22:25:45 +00:00
}