moo/stix/lib/dic.c

355 lines
11 KiB
C
Raw Normal View History

/*
* $Id$
*
Copyright (c) 2014-2016 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2017-01-09 09:54:49 +00:00
#include "moo-prv.h"
2017-01-09 09:54:49 +00:00
static moo_oop_oop_t expand_bucket (moo_t* moo, moo_oop_oop_t oldbuc)
{
2017-01-09 09:54:49 +00:00
moo_oop_oop_t newbuc;
moo_oow_t oldsz, newsz, index;
moo_oop_association_t ass;
moo_oop_char_t key;
2017-01-09 09:54:49 +00:00
oldsz = MOO_OBJ_GET_SIZE(oldbuc);
/* TODO: better growth policy? */
if (oldsz < 5000) newsz = oldsz + oldsz;
else if (oldsz < 50000) newsz = oldsz + (oldsz / 2);
else if (oldsz < 100000) newsz = oldsz + (oldsz / 4);
else if (oldsz < 200000) newsz = oldsz + (oldsz / 8);
else if (oldsz < 400000) newsz = oldsz + (oldsz / 16);
else if (oldsz < 800000) newsz = oldsz + (oldsz / 32);
else if (oldsz < 1600000) newsz = oldsz + (oldsz / 64);
else
{
2017-01-09 09:54:49 +00:00
moo_oow_t inc, inc_max;
inc = oldsz / 128;
2017-01-09 09:54:49 +00:00
inc_max = MOO_OBJ_SIZE_MAX - oldsz;
if (inc > inc_max)
{
if (inc_max > 0) inc = inc_max;
else
{
2017-01-09 09:54:49 +00:00
moo->errnum = MOO_EOOMEM;
return MOO_NULL;
}
}
newsz = oldsz + inc;
}
2017-01-09 09:54:49 +00:00
moo_pushtmp (moo, (moo_oop_t*)&oldbuc);
newbuc = (moo_oop_oop_t)moo_instantiate (moo, moo->_array, MOO_NULL, newsz);
moo_poptmp (moo);
if (!newbuc) return MOO_NULL;
while (oldsz > 0)
{
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)oldbuc->slot[--oldsz];
if ((moo_oop_t)ass != moo->_nil)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
2017-01-09 09:54:49 +00:00
key = (moo_oop_char_t)ass->key;
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == (moo_oop_t)moo->_symbol);
2017-01-09 09:54:49 +00:00
index = moo_hashoochars(key->slot, MOO_OBJ_GET_SIZE(key)) % newsz;
while (newbuc->slot[index] != moo->_nil) index = (index + 1) % newsz;
newbuc->slot[index] = (moo_oop_t)ass;
}
}
return newbuc;
}
2017-01-09 09:54:49 +00:00
static moo_oop_association_t find_or_upsert (moo_t* moo, moo_oop_set_t dic, moo_oop_char_t key, moo_oop_t value)
{
2017-01-09 09:54:49 +00:00
moo_ooi_t tally;
moo_oow_t hv, index;
moo_oop_association_t ass;
moo_oow_t tmp_count = 0;
/* the system dictionary is not a generic dictionary.
* it accepts only a symbol as a key. */
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->tally) == moo->_small_integer);
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->bucket) == moo->_array);
2017-01-09 09:54:49 +00:00
hv = moo_hashoochars(key->slot, MOO_OBJ_GET_SIZE(key));
index = hv % MOO_OBJ_GET_SIZE(dic->bucket);
/* find */
2017-01-09 09:54:49 +00:00
while (dic->bucket->slot[index] != moo->_nil)
{
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)dic->bucket->slot[index];
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);
2017-01-09 09:54:49 +00:00
if (MOO_OBJ_GET_SIZE(key) == MOO_OBJ_GET_SIZE(ass->key) &&
moo_equaloochars (key->slot, ((moo_oop_char_t)ass->key)->slot, MOO_OBJ_GET_SIZE(key)))
{
2017-01-09 09:54:49 +00:00
/* the value of MOO_NULL indicates no insertion or update. */
if (value) ass->value = value; /* update */
2015-06-04 18:34:37 +00:00
return ass;
}
2017-01-09 09:54:49 +00:00
index = (index + 1) % MOO_OBJ_GET_SIZE(dic->bucket);
}
if (!value)
{
2017-01-09 09:54:49 +00:00
/* when value is MOO_NULL, perform no insertion.
* the value of MOO_NULL indicates no insertion or update. */
moo->errnum = MOO_ENOENT;
return MOO_NULL;
}
/* the key is not found. insert it. */
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(dic->tally));
tally = MOO_OOP_TO_SMOOI(dic->tally);
if (tally >= MOO_SMOOI_MAX)
{
/* this built-in dictionary is not allowed to hold more than
2017-01-09 09:54:49 +00:00
* MOO_SMOOI_MAX items for efficiency sake */
moo->errnum = MOO_EDFULL;
return MOO_NULL;
}
2017-01-09 09:54:49 +00:00
moo_pushtmp (moo, (moo_oop_t*)&dic); tmp_count++;
moo_pushtmp (moo, (moo_oop_t*)&key); tmp_count++;
moo_pushtmp (moo, &value); tmp_count++;
2017-01-09 09:54:49 +00:00
/* no conversion to moo_oow_t is necessary for tally + 1.
* the maximum value of tally is checked to be MOO_SMOOI_MAX - 1.
* tally + 1 can produce at most MOO_SMOOI_MAX. above all,
* MOO_SMOOI_MAX is way smaller than MOO_TYPE_MAX(moo_ooi_t). */
if (tally + 1 >= MOO_OBJ_GET_SIZE(dic->bucket))
{
2017-01-09 09:54:49 +00:00
moo_oop_oop_t bucket;
/* TODO: make the growth policy configurable instead of growing
it just before it gets full. The polcy can be grow it
if it's 70% full */
/* enlarge the bucket before it gets full to
* make sure that it has at least one free slot left
* after having added a new symbol. this is to help
* traversal end at a _nil slot if no entry is found. */
2017-01-09 09:54:49 +00:00
bucket = expand_bucket (moo, dic->bucket);
if (!bucket) goto oops;
dic->bucket = bucket;
/* recalculate the index for the expanded bucket */
2017-01-09 09:54:49 +00:00
index = hv % MOO_OBJ_GET_SIZE(dic->bucket);
2017-01-09 09:54:49 +00:00
while (dic->bucket->slot[index] != moo->_nil)
index = (index + 1) % MOO_OBJ_GET_SIZE(dic->bucket);
}
/* create a new assocation of a key and a value since
* the key isn't found in the root dictionary */
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)moo_instantiate (moo, moo->_association, MOO_NULL, 0);
if (!ass) goto oops;
2017-01-09 09:54:49 +00:00
ass->key = (moo_oop_t)key;
ass->value = value;
/* the current tally must be less than the maximum value. otherwise,
* it overflows after increment below */
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, tally < MOO_SMOOI_MAX);
dic->tally = MOO_SMOOI_TO_OOP(tally + 1);
dic->bucket->slot[index] = (moo_oop_t)ass;
2017-01-09 09:54:49 +00:00
moo_poptmps (moo, tmp_count);
2015-06-04 18:34:37 +00:00
return ass;
oops:
2017-01-09 09:54:49 +00:00
moo_poptmps (moo, tmp_count);
return MOO_NULL;
}
2017-01-09 09:54:49 +00:00
static moo_oop_association_t lookup (moo_t* moo, moo_oop_set_t dic, const moo_oocs_t* name)
2015-05-25 17:10:49 +00:00
{
2017-01-09 09:54:49 +00:00
/* this is special version of moo_getatsysdic() that performs
2015-05-25 17:10:49 +00:00
* lookup using a plain string specified */
2017-01-09 09:54:49 +00:00
moo_oow_t index;
moo_oop_association_t ass;
2015-05-25 17:10:49 +00:00
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->tally) == moo->_small_integer);
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->bucket) == moo->_array);
2015-05-25 17:10:49 +00:00
2017-01-09 09:54:49 +00:00
index = moo_hashoochars(name->ptr, name->len) % MOO_OBJ_GET_SIZE(dic->bucket);
2015-05-25 17:10:49 +00:00
2017-01-09 09:54:49 +00:00
while (dic->bucket->slot[index] != moo->_nil)
2015-05-25 17:10:49 +00:00
{
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)dic->bucket->slot[index];
2015-05-25 17:10:49 +00:00
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);
2015-05-25 17:10:49 +00:00
2017-01-09 09:54:49 +00:00
if (name->len == MOO_OBJ_GET_SIZE(ass->key) &&
moo_equaloochars(name->ptr, ((moo_oop_char_t)ass->key)->slot, name->len))
2015-05-25 17:10:49 +00:00
{
2015-06-04 18:34:37 +00:00
return ass;
2015-05-25 17:10:49 +00:00
}
2017-01-09 09:54:49 +00:00
index = (index + 1) % MOO_OBJ_GET_SIZE(dic->bucket);
2015-05-25 17:10:49 +00:00
}
2017-01-09 09:54:49 +00:00
/* when value is MOO_NULL, perform no insertion */
moo->errnum = MOO_ENOENT;
return MOO_NULL;
2015-05-25 17:10:49 +00:00
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_putatsysdic (moo_t* moo, moo_oop_t key, moo_oop_t value)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
return find_or_upsert (moo, moo->sysdic, (moo_oop_char_t)key, value);
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_getatsysdic (moo_t* moo, moo_oop_t key)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
return find_or_upsert (moo, moo->sysdic, (moo_oop_char_t)key, MOO_NULL);
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_lookupsysdic (moo_t* moo, const moo_oocs_t* name)
{
2017-01-09 09:54:49 +00:00
return lookup (moo, moo->sysdic, name);
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_putatdic (moo_t* moo, moo_oop_set_t dic, moo_oop_t key, moo_oop_t value)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
return find_or_upsert (moo, dic, (moo_oop_char_t)key, value);
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_getatdic (moo_t* moo, moo_oop_set_t dic, moo_oop_t key)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
return find_or_upsert (moo, dic, (moo_oop_char_t)key, MOO_NULL);
}
2017-01-09 09:54:49 +00:00
moo_oop_association_t moo_lookupdic (moo_t* moo, moo_oop_set_t dic, const moo_oocs_t* name)
{
2017-01-09 09:54:49 +00:00
return lookup (moo, dic, name);
}
2017-01-09 09:54:49 +00:00
int moo_deletedic (moo_t* moo, moo_oop_set_t dic, const moo_oocs_t* name)
{
2017-01-09 09:54:49 +00:00
moo_ooi_t tally;
moo_oow_t hv, index, bs, i, x, y, z;
moo_oop_association_t ass;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->tally) == moo->_small_integer);
MOO_ASSERT (moo, MOO_CLASSOF(moo,dic->bucket) == moo->_array);
2017-01-09 09:54:49 +00:00
tally = MOO_OOP_TO_SMOOI(dic->tally);
2017-01-09 09:54:49 +00:00
bs = MOO_OBJ_GET_SIZE(dic->bucket);
hv = moo_hashoochars(name->ptr, name->len) % bs;
index = hv % bs;
/* find */
2017-01-09 09:54:49 +00:00
while (dic->bucket->slot[index] != moo->_nil)
{
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)dic->bucket->slot[index];
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);
2017-01-09 09:54:49 +00:00
if (name->len == MOO_OBJ_GET_SIZE(ass->key) &&
moo_equaloochars(name->ptr, ((moo_oop_char_t)ass->key)->slot, name->len))
{
goto found;
}
index = (index + 1) % bs;
}
2017-01-09 09:54:49 +00:00
moo->errnum = MOO_ENOENT;
return -1;
found:
/* compact the cluster */
for (i = 0, x = index, y = index; i < tally; i++)
{
y = (y + 1) % bs;
/* done if the slot at the current index is empty */
2017-01-09 09:54:49 +00:00
if (dic->bucket->slot[y] == moo->_nil) break;
/* get the natural hash index for the data in the slot at
* the current hash index */
2017-01-09 09:54:49 +00:00
ass = (moo_oop_association_t)dic->bucket->slot[y];
z = moo_hashoochars(((moo_oop_char_t)ass->key)->slot, MOO_OBJ_GET_SIZE(ass->key)) % bs;
/* move an element if necesary */
if ((y > x && (z <= x || z > y)) ||
(y < x && (z <= x && z > y)))
{
dic->bucket->slot[x] = dic->bucket->slot[y];
x = y;
}
}
2017-01-09 09:54:49 +00:00
dic->bucket->slot[x] = moo->_nil;
tally--;
2017-01-09 09:54:49 +00:00
dic->tally = MOO_SMOOI_TO_OOP(tally);
return 0;
}
2017-01-09 09:54:49 +00:00
moo_oop_set_t moo_makedic (moo_t* moo, moo_oop_t cls, moo_oow_t size)
{
2017-01-09 09:54:49 +00:00
moo_oop_set_t dic;
moo_oop_t tmp;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,cls) == moo->_class);
2015-05-31 16:44:56 +00:00
2017-01-09 09:54:49 +00:00
dic = (moo_oop_set_t)moo_instantiate (moo, cls, MOO_NULL, 0);
if (!dic) return MOO_NULL;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(dic) == MOO_SET_NAMED_INSTVARS);
2017-01-09 09:54:49 +00:00
moo_pushtmp (moo, (moo_oop_t*)&dic);
tmp = moo_instantiate (moo, moo->_array, MOO_NULL, size);
moo_poptmp (moo);
if (!tmp) return MOO_NULL;
2017-01-09 09:54:49 +00:00
dic->tally = MOO_SMOOI_TO_OOP(0);
dic->bucket = (moo_oop_oop_t)tmp;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(dic) == MOO_SET_NAMED_INSTVARS);
MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(dic->bucket) == size);
2015-06-04 18:34:37 +00:00
return dic;
}