2017-11-01 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
#include <dst/dst.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2017-11-01 21:53:43 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
#define dst_table_maphash(cap, hash) ((uint32_t)(hash) & (cap - 1))
|
2017-11-29 20:17:56 +00:00
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Initialize a table */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstTable *dst_table_init(DstTable *table, int32_t capacity) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *data;
|
|
|
|
capacity = dst_tablen(capacity);
|
|
|
|
if (capacity) {
|
|
|
|
data = (DstKV *) dst_memalloc_empty(capacity);
|
|
|
|
if (NULL == data) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
void dst_table_deinit(DstTable *table) {
|
|
|
|
free(table->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new table */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstTable *dst_table(int32_t capacity) {
|
2017-12-21 04:03:34 +00:00
|
|
|
DstTable *table = dst_gcalloc(DST_MEMORY_TABLE, sizeof(DstTable));
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_table_init(table, capacity);
|
|
|
|
}
|
|
|
|
|
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-01-17 14:58:32 +00:00
|
|
|
DstKV *dst_table_find(DstTable *t, Dst key) {
|
2017-11-29 20:17:56 +00:00
|
|
|
int32_t index = dst_table_maphash(t->capacity, dst_hash(key));
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
|
|
|
DstKV *first_bucket = NULL;
|
|
|
|
/* Higher half */
|
|
|
|
for (i = index; i < t->capacity; i++) {
|
|
|
|
DstKV *kv = t->data + i;
|
|
|
|
if (dst_checktype(kv->key, DST_NIL)) {
|
|
|
|
if (dst_checktype(kv->value, DST_NIL)) {
|
|
|
|
return kv;
|
|
|
|
} else if (NULL == first_bucket) {
|
|
|
|
first_bucket = kv;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
} else if (dst_equals(kv->key, key)) {
|
|
|
|
return t->data + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Lower half */
|
|
|
|
for (i = 0; i < index; i++) {
|
|
|
|
DstKV *kv = t->data + i;
|
|
|
|
if (dst_checktype(kv->key, DST_NIL)) {
|
|
|
|
if (dst_checktype(kv->value, DST_NIL)) {
|
|
|
|
return kv;
|
|
|
|
} else if (NULL == first_bucket) {
|
|
|
|
first_bucket = kv;
|
|
|
|
}
|
|
|
|
} else if (dst_equals(kv->key, key)) {
|
|
|
|
return t->data + i;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
return first_bucket;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize the dictionary table. */
|
2017-11-28 23:27:55 +00:00
|
|
|
static void dst_table_rehash(DstTable *t, int32_t size) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *olddata = t->data;
|
|
|
|
DstKV *newdata = (DstKV *) dst_memalloc_empty(size);
|
2017-11-01 21:53:43 +00:00
|
|
|
if (NULL == newdata) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
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++) {
|
|
|
|
DstKV *kv = olddata + i;
|
|
|
|
if (!dst_checktype(kv->key, DST_NIL)) {
|
|
|
|
DstKV *newkv = dst_table_find(t, kv->key);
|
|
|
|
*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-01-06 16:09:15 +00:00
|
|
|
Dst dst_table_get(DstTable *t, Dst key) {
|
2018-03-10 18:34:46 +00:00
|
|
|
DstKV *bucket = dst_table_find(t, key);
|
|
|
|
if (NULL != bucket && !dst_checktype(bucket->key, DST_NIL))
|
|
|
|
return bucket->value;
|
|
|
|
/* Check prototypes */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = DST_MAX_PROTO_DEPTH, t = t->proto; t && i; t = t->proto, --i) {
|
|
|
|
bucket = dst_table_find(t, key);
|
|
|
|
if (NULL != bucket && !dst_checktype(bucket->key, DST_NIL))
|
|
|
|
return bucket->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value out of the table. Don't check prototype tables. */
|
|
|
|
Dst dst_table_rawget(DstTable *t, Dst key) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *bucket = dst_table_find(t, key);
|
|
|
|
if (NULL != bucket && !dst_checktype(bucket->key, DST_NIL))
|
|
|
|
return bucket->value;
|
2017-11-01 21:53:43 +00:00
|
|
|
else
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove an entry from the dictionary. Return the value that
|
|
|
|
* was removed. */
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst dst_table_remove(DstTable *t, Dst key) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *bucket = dst_table_find(t, key);
|
|
|
|
if (NULL != bucket && !dst_checktype(bucket->key, DST_NIL)) {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst ret = bucket->key;
|
2017-11-01 21:53:43 +00:00
|
|
|
t->count--;
|
|
|
|
t->deleted++;
|
2018-01-05 21:17:55 +00:00
|
|
|
bucket->key = dst_wrap_nil();
|
|
|
|
bucket->value = dst_wrap_false();
|
2017-11-01 21:53:43 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value into the object */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dst_table_put(DstTable *t, Dst key, Dst value) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(key, DST_NIL)) return;
|
|
|
|
if (dst_checktype(value, DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
dst_table_remove(t, key);
|
|
|
|
} else {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *bucket = dst_table_find(t, key);
|
|
|
|
if (NULL != bucket && !dst_checktype(bucket->key, DST_NIL)) {
|
|
|
|
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) {
|
|
|
|
dst_table_rehash(t, dst_tablen(2 * t->count + 2));
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
bucket = dst_table_find(t, key);
|
2018-01-05 21:17:55 +00:00
|
|
|
if (dst_checktype(bucket->value, DST_FALSE))
|
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 */
|
|
|
|
void dst_table_clear(DstTable *t) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t capacity = t->capacity;
|
2018-01-05 21:17:55 +00:00
|
|
|
DstKV *data = t->data;
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_memempty(data, capacity);
|
2017-11-01 21:53:43 +00:00
|
|
|
t->count = 0;
|
|
|
|
t->deleted = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Find next key in an object. Returns NULL if no next key. */
|
|
|
|
const DstKV *dst_table_next(DstTable *t, const DstKV *kv) {
|
|
|
|
DstKV *end = t->data + t->capacity;
|
|
|
|
kv = (kv == NULL) ? t->data : kv + 1;
|
|
|
|
while (kv < end) {
|
|
|
|
if (!dst_checktype(kv->key, DST_NIL))
|
|
|
|
return kv;
|
|
|
|
kv++;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
return NULL;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
|
|
|
|
/* Convert table to struct */
|
2018-01-05 21:17:55 +00:00
|
|
|
const DstKV *dst_table_to_struct(DstTable *t) {
|
|
|
|
DstKV *st = dst_struct_begin(t->count);
|
|
|
|
DstKV *kv = t->data;
|
|
|
|
DstKV *end = t->data + t->capacity;
|
|
|
|
while (kv < end) {
|
|
|
|
if (!dst_checktype(kv->key, DST_NIL))
|
|
|
|
dst_struct_put(st, kv->key, kv->value);
|
|
|
|
kv++;
|
2017-11-21 02:39:44 +00:00
|
|
|
}
|
|
|
|
return dst_struct_end(st);
|
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
|
2018-01-14 17:10:45 +00:00
|
|
|
/* Merge a table or struct into a table */
|
|
|
|
static void dst_table_mergekv(DstTable *table, const DstKV *kvs, int32_t cap) {
|
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < cap; i++) {
|
|
|
|
const DstKV *kv = kvs + i;
|
|
|
|
if (!dst_checktype(kv->key, DST_NIL)) {
|
|
|
|
dst_table_put(table, kv->key, kv->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge a table other into another table */
|
|
|
|
void dst_table_merge_table(DstTable *table, DstTable *other) {
|
|
|
|
dst_table_mergekv(table, other->data, other->capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge a struct into a table */
|
|
|
|
void dst_table_merge_struct(DstTable *table, const DstKV *other) {
|
|
|
|
dst_table_mergekv(table, other, dst_struct_capacity(other));
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:01:58 +00:00
|
|
|
/* C Functions */
|
|
|
|
|
|
|
|
static int cfun_new(DstArgs args) {
|
|
|
|
DstTable *t;
|
|
|
|
int32_t cap;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_INTEGER(cap, args, 0);
|
2018-05-09 21:01:58 +00:00
|
|
|
t = dst_table(cap);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_TABLE(args, t);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:40:28 +00:00
|
|
|
static int cfun_getproto(DstArgs args) {
|
|
|
|
DstTable *t;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_TABLE(t, args, 0);
|
|
|
|
DST_RETURN(args, t->proto
|
2018-05-08 23:40:28 +00:00
|
|
|
? dst_wrap_table(t->proto)
|
|
|
|
: dst_wrap_nil());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_setproto(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DstTable *table, *proto;
|
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_ARG_TABLE(table, args, 0);
|
|
|
|
if (dst_checktype(args.v[1], DST_NIL)) {
|
|
|
|
proto = NULL;
|
|
|
|
} else {
|
|
|
|
DST_ARG_TABLE(proto, args, 1);
|
|
|
|
}
|
|
|
|
table->proto = proto;
|
|
|
|
DST_RETURN_TABLE(args, table);
|
2018-05-08 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 18:01:58 +00:00
|
|
|
static int cfun_tostruct(DstArgs args) {
|
|
|
|
DstTable *t;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_TABLE(t, args, 0);
|
|
|
|
DST_RETURN_STRUCT(args, dst_table_to_struct(t));
|
2018-03-18 18:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 23:40:28 +00:00
|
|
|
static int cfun_rawget(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DstTable *table;
|
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_ARG_TABLE(table, args, 0);
|
|
|
|
DST_RETURN(args, dst_table_rawget(table, args.v[1]));
|
2018-05-08 23:40:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 18:01:58 +00:00
|
|
|
static const DstReg cfuns[] = {
|
2018-05-09 21:01:58 +00:00
|
|
|
{"table.new", cfun_new},
|
2018-05-08 23:40:28 +00:00
|
|
|
{"table.to-struct", cfun_tostruct},
|
|
|
|
{"table.getproto", cfun_getproto},
|
|
|
|
{"table.setproto", cfun_setproto},
|
|
|
|
{"table.rawget", cfun_rawget},
|
2018-03-18 18:01:58 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Load the table module */
|
|
|
|
int dst_lib_table(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
|
|
|
dst_env_cfuns(env, cfuns);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
#undef dst_table_maphash
|