2017-11-01 21:53:43 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 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-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2019-01-29 23:18:14 +00:00
|
|
|
#include <math.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Initialize a table */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *janet_table_init(JanetTable *table, int32_t capacity) {
|
|
|
|
JanetKV *data;
|
|
|
|
capacity = janet_tablen(capacity);
|
2018-01-05 21:17:55 +00:00
|
|
|
if (capacity) {
|
2018-09-06 02:18:42 +00:00
|
|
|
data = (JanetKV *) janet_memalloc_empty(capacity);
|
2018-01-05 21:17:55 +00:00
|
|
|
if (NULL == data) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
table->data = data;
|
|
|
|
table->capacity = capacity;
|
|
|
|
} else {
|
|
|
|
table->data = NULL;
|
|
|
|
table->capacity = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
table->count = 0;
|
|
|
|
table->deleted = 0;
|
2018-03-10 18:34:46 +00:00
|
|
|
table->proto = NULL;
|
2017-11-01 21:53:43 +00:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Deinitialize a table */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_table_deinit(JanetTable *table) {
|
2017-11-06 03:05:47 +00:00
|
|
|
free(table->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new table */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetTable *janet_table(int32_t capacity) {
|
|
|
|
JanetTable *table = janet_gcalloc(JANET_MEMORY_TABLE, sizeof(JanetTable));
|
|
|
|
return janet_table_init(table, capacity);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Find the bucket that contains the given key. Will also return
|
|
|
|
* bucket where key should go if not in the table. */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetKV *janet_table_find(JanetTable *t, Janet key) {
|
2018-09-09 16:13:32 +00:00
|
|
|
return (JanetKV *) janet_dict_find(t->data, t->capacity, key);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize the dictionary table. */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_table_rehash(JanetTable *t, int32_t size) {
|
|
|
|
JanetKV *olddata = t->data;
|
|
|
|
JanetKV *newdata = (JanetKV *) janet_memalloc_empty(size);
|
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
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, oldcapacity;
|
2017-11-01 21:53:43 +00:00
|
|
|
oldcapacity = t->capacity;
|
|
|
|
t->data = newdata;
|
|
|
|
t->capacity = size;
|
|
|
|
t->deleted = 0;
|
2018-01-05 21:17:55 +00:00
|
|
|
for (i = 0; i < oldcapacity; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetKV *kv = olddata + i;
|
|
|
|
if (!janet_checktype(kv->key, JANET_NIL)) {
|
|
|
|
JanetKV *newkv = janet_table_find(t, kv->key);
|
2018-01-05 21:17:55 +00:00
|
|
|
*newkv = *kv;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(olddata);
|
|
|
|
}
|
|
|
|
|
2018-03-10 18:34:46 +00:00
|
|
|
/* Get a value out of the table */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_table_get(JanetTable *t, Janet key) {
|
|
|
|
JanetKV *bucket = janet_table_find(t, key);
|
|
|
|
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
2018-03-10 18:34:46 +00:00
|
|
|
return bucket->value;
|
|
|
|
/* Check prototypes */
|
|
|
|
{
|
|
|
|
int i;
|
2018-09-06 02:18:42 +00:00
|
|
|
for (i = JANET_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) {
|
|
|
|
bucket = janet_table_find(t, key);
|
|
|
|
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
2018-03-10 18:34:46 +00:00
|
|
|
return bucket->value;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2018-03-10 18:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value out of the table. Don't check prototype tables. */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_table_rawget(JanetTable *t, Janet key) {
|
|
|
|
JanetKV *bucket = janet_table_find(t, key);
|
|
|
|
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL))
|
2018-01-05 21:17:55 +00:00
|
|
|
return bucket->value;
|
2017-11-01 21:53:43 +00:00
|
|
|
else
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove an entry from the dictionary. Return the value that
|
|
|
|
* was removed. */
|
2018-09-06 02:18:42 +00:00
|
|
|
Janet janet_table_remove(JanetTable *t, Janet key) {
|
|
|
|
JanetKV *bucket = janet_table_find(t, key);
|
|
|
|
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) {
|
|
|
|
Janet ret = bucket->key;
|
2017-11-01 21:53:43 +00:00
|
|
|
t->count--;
|
|
|
|
t->deleted++;
|
2018-09-06 02:18:42 +00:00
|
|
|
bucket->key = janet_wrap_nil();
|
|
|
|
bucket->value = janet_wrap_false();
|
2017-11-01 21:53:43 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_nil();
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value into the object */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_table_put(JanetTable *t, Janet key, Janet value) {
|
|
|
|
if (janet_checktype(key, JANET_NIL)) return;
|
2019-01-28 16:50:33 +00:00
|
|
|
if (janet_checktype(key, JANET_NUMBER) && isnan(janet_unwrap_number(key))) return;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (janet_checktype(value, JANET_NIL)) {
|
|
|
|
janet_table_remove(t, key);
|
2017-11-01 21:53:43 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetKV *bucket = janet_table_find(t, key);
|
|
|
|
if (NULL != bucket && !janet_checktype(bucket->key, JANET_NIL)) {
|
2018-01-05 21:17:55 +00:00
|
|
|
bucket->value = value;
|
2017-11-01 21:53:43 +00:00
|
|
|
} else {
|
2018-01-05 21:17:55 +00:00
|
|
|
if (NULL == bucket || 2 * (t->count + t->deleted + 1) > t->capacity) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_table_rehash(t, janet_tablen(2 * t->count + 2));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
bucket = janet_table_find(t, key);
|
2019-03-13 18:50:25 +00:00
|
|
|
if (janet_checktype(bucket->value, JANET_BOOLEAN))
|
2017-11-01 21:53:43 +00:00
|
|
|
--t->deleted;
|
2018-01-05 21:17:55 +00:00
|
|
|
bucket->key = key;
|
|
|
|
bucket->value = value;
|
2017-11-01 21:53:43 +00:00
|
|
|
++t->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear a table */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_table_clear(JanetTable *t) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t capacity = t->capacity;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetKV *data = t->data;
|
|
|
|
janet_memempty(data, capacity);
|
2017-11-01 21:53:43 +00:00
|
|
|
t->count = 0;
|
|
|
|
t->deleted = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Convert table to struct */
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetKV *janet_table_to_struct(JanetTable *t) {
|
|
|
|
JanetKV *st = janet_struct_begin(t->count);
|
|
|
|
JanetKV *kv = t->data;
|
|
|
|
JanetKV *end = t->data + t->capacity;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (kv < end) {
|
2018-09-06 02:18:42 +00:00
|
|
|
if (!janet_checktype(kv->key, JANET_NIL))
|
|
|
|
janet_struct_put(st, kv->key, kv->value);
|
2018-01-05 21:17:55 +00:00
|
|
|
kv++;
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_struct_end(st);
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
/* Merge a table or struct into a table */
|
2018-09-06 02:18:42 +00:00
|
|
|
static void janet_table_mergekv(JanetTable *table, const JanetKV *kvs, int32_t cap) {
|
2018-01-14 17:10:45 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < cap; i++) {
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetKV *kv = kvs + i;
|
|
|
|
if (!janet_checktype(kv->key, JANET_NIL)) {
|
|
|
|
janet_table_put(table, kv->key, kv->value);
|
2018-01-14 17:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge a table other into another table */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_table_merge_table(JanetTable *table, JanetTable *other) {
|
|
|
|
janet_table_mergekv(table, other->data, other->capacity);
|
2018-01-14 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge a struct into a table */
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_table_merge_struct(JanetTable *table, const JanetKV *other) {
|
|
|
|
janet_table_mergekv(table, other, janet_struct_capacity(other));
|
2018-01-14 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:01:58 +00:00
|
|
|
/* C Functions */
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_table_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);
|
|
|
|
return janet_wrap_table(janet_table(cap));
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_table_getproto(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
|
|
|
JanetTable *t = janet_gettable(argv, 0);
|
|
|
|
return t->proto
|
2019-02-20 01:51:34 +00:00
|
|
|
? janet_wrap_table(t->proto)
|
|
|
|
: janet_wrap_nil();
|
2018-05-08 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_table_setproto(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetTable *table = janet_gettable(argv, 0);
|
|
|
|
JanetTable *proto = NULL;
|
|
|
|
if (!janet_checktype(argv[1], JANET_NIL)) {
|
|
|
|
proto = janet_gettable(argv, 1);
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
|
|
|
table->proto = proto;
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-05-08 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_table_tostruct(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
|
|
|
JanetTable *t = janet_gettable(argv, 0);
|
|
|
|
return janet_wrap_struct(janet_table_to_struct(t));
|
2018-03-18 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static Janet cfun_table_rawget(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2019-01-06 01:09:03 +00:00
|
|
|
JanetTable *table = janet_gettable(argv, 0);
|
|
|
|
return janet_table_rawget(table, argv[1]);
|
2018-05-08 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg table_cfuns[] = {
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"table/new", cfun_table_new,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table/new capacity)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Creates a new empty table with pre-allocated memory "
|
|
|
|
"for capacity entries. This means that if one knows the number of "
|
|
|
|
"entries going to go in a table on creation, extra memory allocation "
|
|
|
|
"can be avoided. Returns the new table.")
|
2018-11-16 21:24:10 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"table/to-struct", cfun_table_tostruct,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table/to-struct tab)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Convert a table to a struct. Returns a new struct. This function "
|
|
|
|
"does not take into account prototype tables.")
|
2018-11-16 21:24:10 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"table/getproto", cfun_table_getproto,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table/getproto tab)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Get the prototype table of a table. Returns nil if a table "
|
|
|
|
"has no prototype, otherwise returns the prototype.")
|
2018-11-16 21:24:10 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"table/setproto", cfun_table_setproto,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table/setproto tab proto)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Set the prototype of a table. Returns the original table tab.")
|
2018-11-16 21:24:10 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
2019-01-24 05:15:58 +00:00
|
|
|
"table/rawget", cfun_table_rawget,
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("(table/rawget tab key)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Gets a value from a table without looking at the prototype table. "
|
|
|
|
"If a table tab does not contain t directly, the function will return "
|
|
|
|
"nil without checking the prototype. Returns the value in the table.")
|
2018-11-16 21:24:10 +00:00
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-03-18 18:01:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Load the table module */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_table(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, table_cfuns);
|
2018-03-18 18:01:58 +00:00
|
|
|
}
|