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-11-01 21:53:43 +00:00
|
|
|
|
2017-11-29 20:17:56 +00:00
|
|
|
#define dst_table_maphash(cap, hash) (((uint32_t)(hash) % (cap)) & 0xFFFFFFFE)
|
|
|
|
|
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) {
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue *data;
|
2017-11-01 21:53:43 +00:00
|
|
|
if (capacity < 2) capacity = 2;
|
2017-11-29 20:17:56 +00:00
|
|
|
data = (DstValue *) dst_memalloc_empty(capacity);
|
2017-11-01 21:53:43 +00:00
|
|
|
if (NULL == data) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
table->data = data;
|
|
|
|
table->capacity = capacity;
|
|
|
|
table->count = 0;
|
|
|
|
table->deleted = 0;
|
|
|
|
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-11-06 03:05:47 +00:00
|
|
|
DstTable *table = dst_alloc(DST_MEMORY_TABLE, sizeof(DstTable));
|
|
|
|
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. */
|
|
|
|
static DstValue *dst_table_find(DstTable *t, DstValue key) {
|
2017-11-29 20:17:56 +00:00
|
|
|
int32_t index = dst_table_maphash(t->capacity, dst_hash(key));
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i, j;
|
|
|
|
int32_t start[2], end[2];
|
2017-11-01 21:53:43 +00:00
|
|
|
start[0] = index; end[0] = t->capacity;
|
|
|
|
start[1] = 0; end[1] = index;
|
|
|
|
for (j = 0; j < 2; ++j) {
|
|
|
|
for (i = start[j]; i < end[j]; i += 2) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(t->data[i], DST_NIL)) {
|
|
|
|
if (dst_checktype(t->data[i + 1], DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Empty */
|
|
|
|
return t->data + i;
|
|
|
|
}
|
|
|
|
} else if (dst_equals(t->data[i], key)) {
|
|
|
|
return t->data + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize the dictionary table. */
|
2017-11-28 23:27:55 +00:00
|
|
|
static void dst_table_rehash(DstTable *t, int32_t size) {
|
2017-11-01 21:53:43 +00:00
|
|
|
DstValue *olddata = t->data;
|
2017-11-29 20:17:56 +00:00
|
|
|
DstValue *newdata = (DstValue *) 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;
|
|
|
|
for (i = 0; i < oldcapacity; i += 2) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (!dst_checktype(olddata[i], DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
DstValue *bucket = dst_table_find(t, olddata[i]);
|
|
|
|
bucket[0] = olddata[i];
|
|
|
|
bucket[1] = olddata[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(olddata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a value out of the object */
|
|
|
|
DstValue dst_table_get(DstTable *t, DstValue key) {
|
|
|
|
DstValue *bucket = dst_table_find(t, key);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (NULL != bucket && !dst_checktype(bucket[0], DST_NIL))
|
2017-11-01 21:53:43 +00:00
|
|
|
return bucket[1];
|
|
|
|
else
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove an entry from the dictionary. Return the value that
|
|
|
|
* was removed. */
|
|
|
|
DstValue dst_table_remove(DstTable *t, DstValue key) {
|
|
|
|
DstValue *bucket = dst_table_find(t, key);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (NULL != bucket && !dst_checktype(bucket[0], DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
DstValue ret = bucket[1];
|
|
|
|
t->count--;
|
|
|
|
t->deleted++;
|
2017-11-28 23:27:55 +00:00
|
|
|
bucket[0] = dst_wrap_nil();
|
|
|
|
bucket[1] = dst_wrap_false();
|
2017-11-01 21:53:43 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a value into the object */
|
2017-11-06 03:05:47 +00:00
|
|
|
void dst_table_put(DstTable *t, DstValue key, DstValue 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 {
|
|
|
|
DstValue *bucket = dst_table_find(t, key);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (NULL != bucket && !dst_checktype(bucket[0], DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
bucket[1] = value;
|
|
|
|
} else {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (NULL == bucket || 4 * (t->count + t->deleted) >= t->capacity) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_table_rehash(t, 4 * t->count + 6);
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
bucket = dst_table_find(t, key);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(bucket[1], DST_FALSE))
|
2017-11-01 21:53:43 +00:00
|
|
|
--t->deleted;
|
|
|
|
bucket[0] = key;
|
|
|
|
bucket[1] = value;
|
|
|
|
++t->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear a table */
|
|
|
|
void dst_table_clear(DstTable *t) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t capacity = t->capacity;
|
2017-11-01 21:53:43 +00:00
|
|
|
DstValue *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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find next key in an object. Returns nil if no next key. */
|
|
|
|
DstValue dst_table_next(DstTable *t, DstValue key) {
|
|
|
|
const DstValue *bucket, *end;
|
|
|
|
end = t->data + t->capacity;
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(key, DST_NIL)) {
|
2017-11-01 21:53:43 +00:00
|
|
|
bucket = t->data;
|
|
|
|
} else {
|
|
|
|
bucket = dst_table_find(t, key);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (NULL == bucket || dst_checktype(bucket[0], DST_NIL))
|
2017-11-01 21:53:43 +00:00
|
|
|
return dst_wrap_nil();
|
|
|
|
bucket += 2;
|
|
|
|
}
|
|
|
|
for (; bucket < end; bucket += 2) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (!dst_checktype(bucket[0], DST_NIL))
|
2017-11-01 21:53:43 +00:00
|
|
|
return bucket[0];
|
|
|
|
}
|
|
|
|
return dst_wrap_nil();
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
|
|
|
|
/* Convert table to struct */
|
|
|
|
const DstValue *dst_table_to_struct(DstTable *t) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2017-11-25 04:17:04 +00:00
|
|
|
DstValue *st = dst_struct_begin(t->count);
|
2017-12-17 04:11:51 +00:00
|
|
|
for (i = 0; i < t->capacity; i += 2) {
|
2017-11-28 23:27:55 +00:00
|
|
|
if (!dst_checktype(t->data[i], DST_NIL))
|
2017-11-21 02:39:44 +00:00
|
|
|
dst_struct_put(st, t->data[i], t->data[i + 1]);
|
|
|
|
}
|
|
|
|
return dst_struct_end(st);
|
|
|
|
}
|
2017-11-29 20:17:56 +00:00
|
|
|
|
|
|
|
#undef dst_table_maphash
|