mirror of
https://github.com/janet-lang/janet
synced 2025-11-13 22:07:13 +00:00
Major refactor and restructure. Add CMake for anticipated windows
support.
This commit is contained in:
159
src/core/array.c
Normal file
159
src/core/array.c
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Calvin Rose
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <dst/dst.h>
|
||||
#include "gc.h"
|
||||
|
||||
/* Iniializes an array */
|
||||
DstArray *dst_array_init(DstArray *array, int32_t capacity) {
|
||||
Dst *data = NULL;
|
||||
if (capacity > 0) {
|
||||
data = (Dst *) malloc(sizeof(Dst) * capacity);
|
||||
if (NULL == data) {
|
||||
DST_OUT_OF_MEMORY;
|
||||
}
|
||||
}
|
||||
array->count = 0;
|
||||
array->capacity = capacity;
|
||||
array->data = data;
|
||||
return array;
|
||||
}
|
||||
|
||||
void dst_array_deinit(DstArray *array) {
|
||||
free(array->data);
|
||||
}
|
||||
|
||||
/* Creates a new array */
|
||||
DstArray *dst_array(int32_t capacity) {
|
||||
DstArray *array = dst_gcalloc(DST_MEMORY_ARRAY, sizeof(DstArray));
|
||||
return dst_array_init(array, capacity);
|
||||
}
|
||||
|
||||
/* Ensure the array has enough capacity for elements */
|
||||
void dst_array_ensure(DstArray *array, int32_t capacity) {
|
||||
Dst *newData;
|
||||
Dst *old = array->data;
|
||||
if (capacity <= array->capacity) return;
|
||||
newData = realloc(old, capacity * sizeof(Dst));
|
||||
if (NULL == newData) {
|
||||
DST_OUT_OF_MEMORY;
|
||||
}
|
||||
array->data = newData;
|
||||
array->capacity = capacity;
|
||||
}
|
||||
|
||||
/* Set the count of an array. Extend with nil if needed. */
|
||||
void dst_array_setcount(DstArray *array, int32_t count) {
|
||||
if (count < 0)
|
||||
return;
|
||||
if (count > array->count) {
|
||||
int32_t i;
|
||||
dst_array_ensure(array, count + 1);
|
||||
for (i = array->count; i < count; i++) {
|
||||
array->data[i] = dst_wrap_nil();
|
||||
}
|
||||
}
|
||||
array->count = count;
|
||||
}
|
||||
|
||||
/* Push a value to the top of the array */
|
||||
void dst_array_push(DstArray *array, Dst x) {
|
||||
int32_t newcount = array->count + 1;
|
||||
if (newcount >= array->capacity) {
|
||||
dst_array_ensure(array, newcount * 2);
|
||||
}
|
||||
array->data[array->count] = x;
|
||||
array->count = newcount;
|
||||
}
|
||||
|
||||
/* Pop a value from the top of the array */
|
||||
Dst dst_array_pop(DstArray *array) {
|
||||
if (array->count) {
|
||||
return array->data[--array->count];
|
||||
} else {
|
||||
return dst_wrap_nil();
|
||||
}
|
||||
}
|
||||
|
||||
/* Look at the last value in the array */
|
||||
Dst dst_array_peek(DstArray *array) {
|
||||
if (array->count) {
|
||||
return array->data[array->count - 1];
|
||||
} else {
|
||||
return dst_wrap_nil();
|
||||
}
|
||||
}
|
||||
|
||||
/* C Functions */
|
||||
|
||||
static int cfun_pop(DstArgs args) {
|
||||
if (args.n != 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
||||
return dst_return(args, dst_array_pop(dst_unwrap_array(args.v[0])));
|
||||
}
|
||||
|
||||
static int cfun_peek(DstArgs args) {
|
||||
if (args.n != 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
||||
return dst_return(args, dst_array_peek(dst_unwrap_array(args.v[0])));
|
||||
}
|
||||
|
||||
static int cfun_push(DstArgs args) {
|
||||
DstArray *array;
|
||||
int32_t newcount;
|
||||
if (args.n < 1 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
||||
array = dst_unwrap_array(args.v[0]);
|
||||
newcount = array->count - 1 + args.n;
|
||||
dst_array_ensure(array, newcount);
|
||||
if (args.n > 1) memcpy(array->data + array->count, args.v + 1, (args.n - 1) * sizeof(Dst));
|
||||
array->count = newcount;
|
||||
return dst_return(args, args.v[0]);
|
||||
}
|
||||
|
||||
static int cfun_setcount(DstArgs args) {
|
||||
int32_t newcount;
|
||||
if (args.n != 2 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
||||
if (!dst_checktype(args.v[1], DST_INTEGER)) return dst_throw(args, "expected positive integer");
|
||||
newcount = dst_unwrap_integer(args.v[1]);
|
||||
if (newcount < 0) return dst_throw(args, "expected positive integer");
|
||||
dst_array_setcount(dst_unwrap_array(args.v[0]), newcount);
|
||||
return dst_return(args, args.v[0]);
|
||||
}
|
||||
|
||||
static int cfun_ensure(DstArgs args) {
|
||||
int32_t newcount;
|
||||
if (args.n != 2 || !dst_checktype(args.v[0], DST_ARRAY)) return dst_throw(args, "expected array");
|
||||
if (!dst_checktype(args.v[1], DST_INTEGER)) return dst_throw(args, "expected positive integer");
|
||||
newcount = dst_unwrap_integer(args.v[1]);
|
||||
if (newcount < 0) return dst_throw(args, "expected positive integer");
|
||||
dst_array_ensure(dst_unwrap_array(args.v[0]), newcount);
|
||||
return dst_return(args, args.v[0]);
|
||||
}
|
||||
|
||||
/* Load the array module */
|
||||
int dst_lib_array(DstArgs args) {
|
||||
DstTable *env = dst_env_arg(args);
|
||||
dst_env_def(env, "array-pop", dst_wrap_cfunction(cfun_pop));
|
||||
dst_env_def(env, "array-peek", dst_wrap_cfunction(cfun_peek));
|
||||
dst_env_def(env, "array-push", dst_wrap_cfunction(cfun_push));
|
||||
dst_env_def(env, "array-setcount", dst_wrap_cfunction(cfun_setcount));
|
||||
dst_env_def(env, "array-ensure", dst_wrap_cfunction(cfun_ensure));
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user