2015-05-07 15:58:04 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2018-02-21 10:11:39 +00:00
|
|
|
Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved.
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
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"
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_oop_oop_t expand_bucket (moo_t* moo, moo_oop_oop_t oldbuc)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
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;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
oldsz = MOO_OBJ_GET_SIZE(oldbuc);
|
2015-12-22 07:49:28 +00:00
|
|
|
|
|
|
|
/* 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;
|
2015-12-22 07:49:28 +00:00
|
|
|
|
|
|
|
inc = oldsz / 128;
|
2017-01-09 09:54:49 +00:00
|
|
|
inc_max = MOO_OBJ_SIZE_MAX - oldsz;
|
2015-12-22 07:49:28 +00:00
|
|
|
if (inc > inc_max)
|
|
|
|
{
|
|
|
|
if (inc_max > 0) inc = inc_max;
|
|
|
|
else
|
|
|
|
{
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EOOMEM);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-12-22 07:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newsz = oldsz + inc;
|
|
|
|
}
|
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&oldbuc);
|
2018-10-11 14:03:16 +00:00
|
|
|
newbuc = (moo_oop_oop_t)moo_instantiate(moo, moo->_array, MOO_NULL, newsz);
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolat (moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!newbuc) return MOO_NULL;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
while (oldsz > 0)
|
|
|
|
{
|
2018-12-22 08:37:18 +00:00
|
|
|
oldsz = oldsz - 1;
|
|
|
|
ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(oldbuc, oldsz);
|
2017-01-09 09:54:49 +00:00
|
|
|
if ((moo_oop_t)ass != moo->_nil)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
key = (moo_oop_char_t)ass->key;
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
index = moo_hash_oochars(MOO_OBJ_GET_CHAR_SLOT(key), MOO_OBJ_GET_SIZE(key)) % newsz;
|
2018-12-22 08:37:18 +00:00
|
|
|
while (MOO_OBJ_GET_OOP_VAL(newbuc, index) != moo->_nil) index = (index + 1) % newsz;
|
2018-12-04 15:06:56 +00:00
|
|
|
|
2018-12-22 08:37:18 +00:00
|
|
|
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(newbuc, index), (moo_oop_t)ass);
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:51:37 +00:00
|
|
|
return newbuc;
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
static moo_oop_association_t find_or_upsert (moo_t* moo, moo_oop_dic_t dic, moo_oop_char_t key, moo_oop_t value)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_ooi_t tally;
|
|
|
|
moo_oow_t hv, index;
|
|
|
|
moo_oop_association_t ass;
|
2018-12-31 16:47:54 +00:00
|
|
|
moo_oow_t volat_count = 0;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
/* the builtin dictionary is not a generic dictionary.
|
|
|
|
* it accepts only a symbol or something similar as a key. */
|
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(key));
|
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-07 15:58:04 +00:00
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
hv = moo_hash_oochars(MOO_OBJ_GET_CHAR_SLOT(key), MOO_OBJ_GET_SIZE(key));
|
2017-01-09 09:54:49 +00:00
|
|
|
index = hv % MOO_OBJ_GET_SIZE(dic->bucket);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2015-11-20 09:05:55 +00:00
|
|
|
/* find */
|
2018-12-22 08:37:18 +00:00
|
|
|
while ((moo_oop_t)(ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, index)) != moo->_nil)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
|
2017-02-07 17:40:34 +00:00
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(ass->key));
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
if (MOO_OBJ_GET_CLASS(key) == MOO_OBJ_GET_CLASS(ass->key) &&
|
|
|
|
MOO_OBJ_GET_SIZE(key) == MOO_OBJ_GET_SIZE(ass->key) &&
|
2018-12-12 15:05:18 +00:00
|
|
|
moo_equal_oochars(MOO_OBJ_GET_CHAR_SLOT(key), MOO_OBJ_GET_CHAR_SLOT(ass->key), MOO_OBJ_GET_SIZE(key)))
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
/* the value of MOO_NULL indicates no insertion or update. */
|
2018-12-23 16:54:17 +00:00
|
|
|
if (value) MOO_STORE_OOP (moo, &ass->value, value); /* update */
|
2015-06-04 18:34:37 +00:00
|
|
|
return ass;
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
index = (index + 1) % MOO_OBJ_GET_SIZE(dic->bucket);
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 08:39:18 +00:00
|
|
|
if (!value)
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
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. */
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOENT);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 09:05:55 +00:00
|
|
|
/* 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)
|
2015-11-20 09:05:55 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EDFULL);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-11-20 09:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 16:47:54 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&dic); volat_count++;
|
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&key); volat_count++;
|
|
|
|
moo_pushvolat (moo, &value); volat_count++;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
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))
|
2015-05-07 15:58:04 +00:00
|
|
|
{
|
2018-12-31 16:47:54 +00:00
|
|
|
moo_oop_oop_t tmp;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
/* TODO: make the growth policy configurable instead of growing
|
2015-11-20 09:05:55 +00:00
|
|
|
it just before it gets full. The polcy can be grow it
|
2015-05-07 15:58:04 +00:00
|
|
|
if it's 70% full */
|
|
|
|
|
2015-11-20 09:05:55 +00:00
|
|
|
/* enlarge the bucket before it gets full to
|
2015-05-07 15:58:04 +00:00
|
|
|
* 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. */
|
2018-12-31 16:47:54 +00:00
|
|
|
tmp = expand_bucket(moo, dic->bucket);
|
|
|
|
if (!tmp) goto oops;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2018-12-31 16:47:54 +00:00
|
|
|
MOO_STORE_OOP (moo, (moo_oop_t*)&dic->bucket, (moo_oop_t)tmp);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
/* recalculate the index for the expanded bucket */
|
2017-01-09 09:54:49 +00:00
|
|
|
index = hv % MOO_OBJ_GET_SIZE(dic->bucket);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2018-12-22 08:37:18 +00:00
|
|
|
while (MOO_OBJ_GET_OOP_VAL(dic->bucket, index) != moo->_nil)
|
2017-01-09 09:54:49 +00:00
|
|
|
index = (index + 1) % MOO_OBJ_GET_SIZE(dic->bucket);
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new assocation of a key and a value since
|
|
|
|
* the key isn't found in the root dictionary */
|
2018-12-04 15:06:56 +00:00
|
|
|
ass = (moo_oop_association_t)moo_instantiate(moo, moo->_association, MOO_NULL, 0);
|
2015-05-11 16:08:09 +00:00
|
|
|
if (!ass) goto oops;
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2018-12-23 16:54:17 +00:00
|
|
|
MOO_STORE_OOP (moo, &ass->key, (moo_oop_t)key);
|
|
|
|
MOO_STORE_OOP (moo, &ass->value, value);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2015-11-20 09:05:55 +00:00
|
|
|
/* 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);
|
2018-12-08 15:35:26 +00:00
|
|
|
dic->tally = MOO_SMOOI_TO_OOP(tally + 1); /* no need to use MOO_STORE_OOP as the value is not a pointer object */
|
2018-12-22 08:37:18 +00:00
|
|
|
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(dic->bucket, index), (moo_oop_t)ass);
|
2015-05-07 15:58:04 +00:00
|
|
|
|
2018-12-31 16:47:54 +00:00
|
|
|
moo_popvolats (moo, volat_count);
|
2015-06-04 18:34:37 +00:00
|
|
|
return ass;
|
2015-05-07 15:58:04 +00:00
|
|
|
|
|
|
|
oops:
|
2018-12-31 16:47:54 +00:00
|
|
|
moo_popvolats (moo, volat_count);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-05-07 15:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:34:31 +00:00
|
|
|
moo_oop_association_t moo_lookupdic_noseterr (moo_t* moo, moo_oop_dic_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
|
|
|
|
2019-03-06 01:58:51 +00:00
|
|
|
index = moo_hash_oochars(name->ptr, name->len) % MOO_OBJ_GET_SIZE(dic->bucket);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2018-12-22 08:37:18 +00:00
|
|
|
while ((moo_oop_t)(ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, index)) != moo->_nil)
|
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);
|
2017-02-07 17:40:34 +00:00
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(ass->key));
|
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) &&
|
2018-12-12 15:05:18 +00:00
|
|
|
moo_equal_oochars(name->ptr, MOO_OBJ_GET_CHAR_SLOT(ass->key), 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 */
|
2018-12-10 13:34:31 +00:00
|
|
|
|
|
|
|
/* moo_seterrXXX() is not called here. the dictionary lookup is very frequent
|
|
|
|
* and so is lookup failure. for instance, moo_findmethod() calls this over
|
|
|
|
* a class chain. there might be a failure at each class level. it's waste to
|
|
|
|
* set the error information whenever the failure occurs.
|
|
|
|
* the caller of this function must set the error information upon failure */
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2018-12-10 13:34:31 +00:00
|
|
|
static MOO_INLINE moo_oop_association_t lookup (moo_t* moo, moo_oop_dic_t dic, const moo_oocs_t* name)
|
|
|
|
{
|
|
|
|
moo_oop_association_t ass = moo_lookupdic_noseterr(moo, dic, name);
|
|
|
|
if (!ass) moo_seterrbfmt(moo, MOO_ENOENT, "unable to find %.*js in a dictionary", name->len, name->ptr);
|
|
|
|
return ass;
|
|
|
|
}
|
|
|
|
|
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)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
|
2018-10-11 14:03:16 +00:00
|
|
|
return find_or_upsert(moo, (moo_oop_dic_t)moo->sysdic, (moo_oop_char_t)key, value);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_association_t moo_getatsysdic (moo_t* moo, moo_oop_t key)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);
|
2018-10-11 14:03:16 +00:00
|
|
|
return find_or_upsert(moo, (moo_oop_dic_t)moo->sysdic, (moo_oop_char_t)key, MOO_NULL);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_association_t moo_lookupsysdic (moo_t* moo, const moo_oocs_t* name)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2018-10-11 14:03:16 +00:00
|
|
|
return lookup(moo, (moo_oop_dic_t)moo->sysdic, name);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_association_t moo_putatdic (moo_t* moo, moo_oop_dic_t dic, moo_oop_t key, moo_oop_t value)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2017-02-07 17:40:34 +00:00
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(key));
|
2018-10-11 14:03:16 +00:00
|
|
|
return find_or_upsert(moo, dic, (moo_oop_char_t)key, value);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_association_t moo_getatdic (moo_t* moo, moo_oop_dic_t dic, moo_oop_t key)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2017-02-07 17:40:34 +00:00
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,key) == moo->_symbol); */
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(key));
|
2018-10-11 14:03:16 +00:00
|
|
|
return find_or_upsert(moo, dic, (moo_oop_char_t)key, MOO_NULL);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_association_t moo_lookupdic (moo_t* moo, moo_oop_dic_t dic, const moo_oocs_t* name)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2018-10-11 13:02:21 +00:00
|
|
|
return lookup(moo, dic, name);
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
int moo_deletedic (moo_t* moo, moo_oop_dic_t dic, const moo_oocs_t* name)
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
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:11:36 +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);
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
tally = MOO_OOP_TO_SMOOI(dic->tally);
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
bs = MOO_OBJ_GET_SIZE(dic->bucket);
|
2019-03-06 01:58:51 +00:00
|
|
|
hv = moo_hash_oochars(name->ptr, name->len) % bs;
|
2017-01-09 09:11:36 +00:00
|
|
|
index = hv % bs;
|
|
|
|
|
|
|
|
/* find */
|
2018-12-22 08:37:18 +00:00
|
|
|
while ((moo_oop_t)(ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, index)) != moo->_nil)
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
|
2017-02-07 17:40:34 +00:00
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(ass->key));
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
if (name->len == MOO_OBJ_GET_SIZE(ass->key) &&
|
2018-12-12 15:05:18 +00:00
|
|
|
moo_equal_oochars(name->ptr, MOO_OBJ_GET_CHAR_SLOT(ass->key), name->len))
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (index + 1) % bs;
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOENT);
|
2017-01-09 09:11:36 +00:00
|
|
|
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 */
|
2018-12-22 08:37:18 +00:00
|
|
|
if ((moo_oop_t)(ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, y)) == moo->_nil) break;
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2018-12-22 08:37:18 +00:00
|
|
|
/* otherwise get the natural hash index for the data in the slot at
|
2017-01-09 09:11:36 +00:00
|
|
|
* the current hash index */
|
2019-03-06 01:58:51 +00:00
|
|
|
z = moo_hash_oochars(MOO_OBJ_GET_CHAR_SLOT(ass->key), MOO_OBJ_GET_SIZE(ass->key)) % bs;
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
/* move an element if necesary */
|
2018-12-26 08:29:20 +00:00
|
|
|
if ((y > x && (z <= x || z > y)) || (y < x && (z <= x && z > y)))
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
2018-12-22 08:37:18 +00:00
|
|
|
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(dic->bucket, x), MOO_OBJ_GET_OOP_VAL(dic->bucket, y));
|
2017-01-09 09:11:36 +00:00
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 08:33:52 +00:00
|
|
|
MOO_OBJ_SET_OOP_VAL (dic->bucket, x, moo->_nil); /* the value is nil. no MOO_STORE_OOP */
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
tally--;
|
2017-01-09 09:54:49 +00:00
|
|
|
dic->tally = MOO_SMOOI_TO_OOP(tally);
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:03:16 +00:00
|
|
|
int moo_walkdic (moo_t* moo, moo_oop_dic_t dic, moo_dic_walker_t walker, void* ctx)
|
|
|
|
{
|
|
|
|
moo_oow_t index, count;
|
|
|
|
moo_oop_association_t ass;
|
|
|
|
|
2019-10-10 15:06:08 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&dic); /* in case the walker function triggers GC */
|
2018-10-11 14:03:16 +00:00
|
|
|
count = MOO_OBJ_GET_SIZE(dic->bucket);
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
2018-12-22 08:37:18 +00:00
|
|
|
ass = (moo_oop_association_t)MOO_OBJ_GET_OOP_VAL(dic->bucket, index);
|
2018-10-11 14:03:16 +00:00
|
|
|
if ((moo_oop_t)ass != moo->_nil)
|
|
|
|
{
|
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,ass) == moo->_association);
|
|
|
|
/*MOO_ASSERT (moo, MOO_CLASSOF(moo,ass->key) == moo->_symbol);*/
|
|
|
|
MOO_ASSERT (moo, MOO_OBJ_IS_CHAR_POINTER(ass->key));
|
|
|
|
|
2019-10-07 16:50:17 +00:00
|
|
|
if (walker(moo, dic, ass, ctx) <= -1)
|
|
|
|
{
|
|
|
|
moo_popvolat (moo);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-11 14:03:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:50:17 +00:00
|
|
|
moo_popvolat (moo);
|
2018-10-11 14:03:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_dic_t moo_makedic (moo_t* moo, moo_oop_class_t _class, moo_oow_t size)
|
2015-05-28 16:51:37 +00:00
|
|
|
{
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_dic_t dic;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_t tmp;
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,_class) == moo->_class);
|
2017-05-20 02:27:48 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASS_SPEC_NAMED_INSTVARS(MOO_OOP_TO_SMOOI(_class->spec)) >= MOO_DIC_NAMED_INSTVARS);
|
2015-05-31 16:44:56 +00:00
|
|
|
|
2018-10-11 14:03:16 +00:00
|
|
|
dic = (moo_oop_dic_t)moo_instantiate(moo, _class, MOO_NULL, 0);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!dic) return MOO_NULL;
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&dic);
|
2018-10-11 14:03:16 +00:00
|
|
|
tmp = moo_instantiate(moo, moo->_array, MOO_NULL, size);
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolat (moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!tmp) return MOO_NULL;
|
2015-05-28 16:51:37 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
dic->tally = MOO_SMOOI_TO_OOP(0);
|
2018-12-31 16:47:54 +00:00
|
|
|
MOO_STORE_OOP (moo, (moo_oop_t*)&dic->bucket, tmp);
|
2015-05-31 18:43:37 +00:00
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(dic) == MOO_CLASS_SPEC_NAMED_INSTVARS(MOO_OOP_TO_SMOOI(_class->spec)));
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(dic->bucket) == size);
|
2015-05-31 18:43:37 +00:00
|
|
|
|
2015-06-04 18:34:37 +00:00
|
|
|
return dic;
|
2015-05-28 16:51:37 +00:00
|
|
|
}
|
2017-05-20 02:27:48 +00:00
|
|
|
|
|
|
|
moo_oop_nsdic_t moo_makensdic (moo_t* moo, moo_oop_class_t _class, moo_oow_t size)
|
|
|
|
{
|
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,_class) == moo->_class);
|
|
|
|
MOO_ASSERT (moo, MOO_CLASS_SPEC_NAMED_INSTVARS(MOO_OOP_TO_SMOOI(_class->spec)) >= MOO_NSDIC_NAMED_INSTVARS);
|
2018-10-11 14:03:16 +00:00
|
|
|
return (moo_oop_nsdic_t)moo_makedic(moo, _class, size);
|
2017-05-20 02:27:48 +00:00
|
|
|
}
|