added hawk-map.h which uses either htb or rbt depending on HAWK_MAP_IS_HTB or HAWK_MAP_IS_RBT

the hawk's map value is now dependent on the above change. at compile time, you can choose HTB or RBT.
fixed a bug of not resetting FNR in the standard console read handlers
This commit is contained in:
hyung-hwan 2020-03-15 17:39:15 +00:00
parent 4121ef9b3f
commit 6cd9cafd33
15 changed files with 491 additions and 266 deletions

View File

@ -122,18 +122,6 @@ struct arg_t
/* ========================================================================= */
static void dprint (const hawk_ooch_t* fmt, ...)
{
if (app_debug)
{
va_list ap;
va_start (ap, fmt);
// TODO: output..
//hawk_sio_putstrvf (HAWK_STDERR, fmt, ap);
va_end (ap);
}
}
/* ---------------------------------------------------------------------- */
typedef struct sig_state_t sig_state_t;
@ -328,29 +316,30 @@ static void unset_intr_run (void)
static hawk_htb_walk_t print_awk_value (hawk_htb_t* map, hawk_htb_pair_t* pair, void* arg)
{
hawk_rtx_t* rtx = (hawk_rtx_t*)arg;
hawk_t* hawk = hawk_rtx_gethawk(rtx);
hawk_ooch_t* str;
hawk_oow_t len;
hawk_errinf_t oerrinf;
hawk_rtx_geterrinf (rtx, &oerrinf);
str = hawk_rtx_valtooocstrdup(rtx, HAWK_HTB_VPTR(pair), &len);
if (str == HAWK_NULL)
str = hawk_rtx_getvaloocstr(rtx, HAWK_HTB_VPTR(pair), &len);
if (HAWK_UNLIKELY(!str))
{
if (hawk_rtx_geterrnum(rtx) == HAWK_EVALTOSTR)
{
dprint (HAWK_T("%.*s = [not printable]\n"), HAWK_HTB_KLEN(pair), HAWK_HTB_KPTR(pair));
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("%.*js = [not printable]\n"), HAWK_HTB_KLEN(pair), HAWK_HTB_KPTR(pair));
hawk_rtx_seterrinf (rtx, &oerrinf);
}
else
{
dprint (HAWK_T("***OUT OF MEMORY***\n"));
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("***OUT OF MEMORY***\n"));
}
}
else
{
dprint (HAWK_T("%.*s = %.*s\n"), HAWK_HTB_KLEN(pair), HAWK_HTB_KPTR(pair), len, str);
hawk_rtx_freemem (rtx, str);
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("%.*js = %.*js\n"), HAWK_HTB_KLEN(pair), HAWK_HTB_KPTR(pair), len, str);
hawk_rtx_freevaloocstr (rtx, HAWK_HTB_VPTR(pair), str);
}
return HAWK_HTB_WALK_FORWARD;
@ -434,36 +423,39 @@ static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, arg_t* arg)
static void dprint_return (hawk_rtx_t* rtx, hawk_val_t* ret)
{
hawk_t* hawk = hawk_rtx_gethawk(rtx);
hawk_oow_t len;
hawk_ooch_t* str;
if (hawk_rtx_isnilval(rtx, ret))
{
dprint (HAWK_T("[RETURN] - ***nil***\n"));
hawk_logfmt (hawk, HAWK_LOG_STDERR,HAWK_T("[RETURN] - ***nil***\n"));
}
else
{
str = hawk_rtx_valtooocstrdup (rtx, ret, &len);
if (str == HAWK_NULL)
{
dprint (HAWK_T("[RETURN] - ***OUT OF MEMORY***\n"));
hawk_logfmt (hawk, HAWK_LOG_STDERR,HAWK_T("[RETURN] - ***OUT OF MEMORY***\n"));
}
else
{
dprint (HAWK_T("[RETURN] - [%.*js]\n"), len, str);
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("[RETURN] - [%.*js]\n"), len, str);
hawk_freemem (hawk_rtx_gethawk(rtx), str);
}
}
dprint (HAWK_T("[NAMED VARIABLES]\n"));
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("[NAMED VARIABLES]\n"));
hawk_htb_walk (hawk_rtx_getnvmap(rtx), print_awk_value, rtx);
dprint (HAWK_T("[END NAMED VARIABLES]\n"));
hawk_logfmt (hawk, HAWK_LOG_STDERR, HAWK_T("[END NAMED VARIABLES]\n"));
}
#if defined(ENABLE_CALLBACK)
static void on_statement (hawk_rtx_t* rtx, hawk_nde_t* nde)
{
dprint (HAWK_T("running %d at line %zu\n"), (int)nde->type, (hawk_oow_t)nde->loc.line);
hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_LOG_STDERR, HAWK_T("running %d at line %zu\n"), (int)nde->type, (hawk_oow_t)nde->loc.line);
}
#endif

View File

@ -1151,7 +1151,7 @@ Hawk::Value::IndexIterator Hawk::Value::getFirstIndex (Index* idx) const
{
HAWK_ASSERT (this->val != HAWK_NULL);
if (HAWK_RTX_GETVALTYPE (this->run->rtx, this->val) != HAWK_VAL_MAP) return IndexIterator::END;
if (HAWK_RTX_GETVALTYPE(this->run->rtx, this->val) != HAWK_VAL_MAP) return IndexIterator::END;
HAWK_ASSERT (this->run != HAWK_NULL);
@ -1166,16 +1166,15 @@ Hawk::Value::IndexIterator Hawk::Value::getFirstIndex (Index* idx) const
return itr;
}
Hawk::Value::IndexIterator Hawk::Value::getNextIndex (
Index* idx, const IndexIterator& curitr) const
Hawk::Value::IndexIterator Hawk::Value::getNextIndex (Index* idx, const IndexIterator& curitr) const
{
HAWK_ASSERT (val != HAWK_NULL);
if (HAWK_RTX_GETVALTYPE (this->run->rtx, val) != HAWK_VAL_MAP) return IndexIterator::END;
if (HAWK_RTX_GETVALTYPE(this->run->rtx, val) != HAWK_VAL_MAP) return IndexIterator::END;
HAWK_ASSERT (this->run != HAWK_NULL);
Hawk::Value::IndexIterator itr (curitr);
Hawk::Value::IndexIterator itr(curitr);
hawk_val_map_itr_t* iptr;
iptr = hawk_rtx_getnextmapvalitr(this->run->rtx, this->val, &itr);
@ -1273,7 +1272,7 @@ int Hawk::Run::setGlobal (int id, hawk_int_t v)
{
HAWK_ASSERT (this->rtx != HAWK_NULL);
hawk_val_t* tmp = hawk_rtx_makeintval (this->rtx, v);
hawk_val_t* tmp = hawk_rtx_makeintval(this->rtx, v);
if (HAWK_UNLIKELY(!tmp)) return -1;
hawk_rtx_refupval (this->rtx, tmp);
@ -2201,15 +2200,15 @@ int Hawk::findGlobal (const hawk_uch_t* name, bool inc_builtins)
int Hawk::setGlobal (int id, const Value& v)
{
HAWK_ASSERT (this->hawk != HAWK_NULL);
HAWK_ASSERT (runctx.rtx != HAWK_NULL);
HAWK_ASSERT (this->runctx.rtx != HAWK_NULL);
if (v.run != &runctx)
if (v.run != &this->runctx)
{
this->setError (HAWK_EINVAL);
return -1;
}
int n = runctx.setGlobal (id, v);
int n = this->runctx.setGlobal(id, v);
if (n <= -1) this->retrieveError ();
return n;
}

View File

@ -945,26 +945,22 @@ public:
///
IndexIterator ()
{
this->pair = HAWK_NULL;
this->buckno = 0;
hawk_init_map_itr (this, 0);
}
protected:
IndexIterator (hawk_htb_pair_t* pair, hawk_oow_t buckno)
{
this->pair = pair;
this->buckno = buckno;
}
public:
bool operator== (const IndexIterator& ii) const
bool operator== (const IndexIterator& ii) const
{
// the most common use of this operator is to test it against END
#if defined(HAWK_MAP_IS_HTB)
return this->pair == ii.pair && this->buckno == ii.buckno;
#else
return this->pair == ii.pair && this->_prev == ii._prev;
#endif
}
bool operator!= (const IndexIterator& ii) const
bool operator!= (const IndexIterator& ii) const
{
return !operator== (ii);
return !this->operator==(ii);
}
};

View File

@ -878,8 +878,8 @@ int HawkStd::open_console_in (Console& io)
hawk_val_t* v_argc, * v_argv, * v_pair;
hawk_int_t i_argc;
const hawk_ooch_t* file;
hawk_htb_t* map;
hawk_htb_pair_t* pair;
hawk_map_t* map;
hawk_map_pair_t* pair;
hawk_ooch_t ibuf[128];
hawk_oow_t ibuflen;
hawk_oocs_t as;
@ -933,7 +933,7 @@ nextfile:
}
ibuflen = hawk_int_to_oocstr(this->runarg_index + 1, 10, HAWK_NULL, ibuf, HAWK_COUNTOF(ibuf));
pair = hawk_htb_search(map, ibuf, ibuflen);
pair = hawk_map_search(map, ibuf, ibuflen);
if (!pair)
{
if (this->runarg_count <= 0) goto console_open_stdin;
@ -1123,6 +1123,7 @@ hawk_ooi_t HawkStd::readConsole (Console& io, hawk_ooch_t* data, hawk_oow_t size
}
if (sio) hawk_sio_close (sio);
((Run*)io)->setGlobal (HAWK_GBL_FNR, (hawk_int_t)0);
}
return nn;
@ -1147,6 +1148,7 @@ hawk_ooi_t HawkStd::readConsoleBytes (Console& io, hawk_bch_t* data, hawk_oow_t
}
if (sio) hawk_sio_close (sio);
((Run*)io)->setGlobal (HAWK_GBL_FNR, (hawk_int_t)0);
}
return nn;

View File

@ -1880,11 +1880,11 @@ static HAWK_INLINE int __fnc_asort (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi,
if (!hawk_rtx_getfirstmapvalitr(rtx, a0_val, &itr)) goto done; /* map empty */
msz = hawk_htb_getsize(((hawk_val_map_t*)a0_val)->map);
msz = hawk_map_getsize(((hawk_val_map_t*)a0_val)->map);
HAWK_ASSERT (msz > 0);
va = (hawk_val_t**)hawk_rtx_allocmem(rtx, msz * HAWK_SIZEOF(*va));
if (!va) return -1;
if (HAWK_UNLIKELY(!va)) return -1;
i = 0;
if (sort_keys)
{
@ -1892,7 +1892,7 @@ static HAWK_INLINE int __fnc_asort (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi,
{
const hawk_oocs_t* key = HAWK_VAL_MAP_ITR_KEY(&itr);
va[i] = hawk_rtx_makestrvalwithoocs(rtx, key);
if (!va[i])
if (HAWK_UNLIKELY(!va[i]))
{
while (i > 0)
{

View File

@ -256,6 +256,14 @@ struct hawk_htb_t
hawk_htb_pair_t** bucket;
};
struct hawk_htb_itr_t
{
hawk_htb_pair_t* pair;
hawk_oow_t buckno;
};
typedef struct hawk_htb_itr_t hawk_htb_itr_t;
/**
* The HAWK_HTB_COPIER_SIMPLE macros defines a copier that remembers the
* pointer and length of data in a pair.
@ -595,13 +603,17 @@ HAWK_EXPORT void hawk_htb_walk (
void* ctx /**< pointer to user-specific data */
);
HAWK_EXPORT void hawk_init_htb_itr (
hawk_htb_itr_t* itr
);
/**
* The hawk_htb_getfirstpair() function returns the pointer to the first pair
* in a hash table.
*/
HAWK_EXPORT hawk_htb_pair_t* hawk_htb_getfirstpair (
hawk_htb_t* htb, /**< hash table */
hawk_oow_t* buckno /**< bucket number */
hawk_htb_t* htb, /**< hash table */
hawk_htb_itr_t* itr /**< iterator*/
);
/**
@ -610,8 +622,7 @@ HAWK_EXPORT hawk_htb_pair_t* hawk_htb_getfirstpair (
*/
HAWK_EXPORT hawk_htb_pair_t* hawk_htb_getnextpair (
hawk_htb_t* htb, /**< hash table */
hawk_htb_pair_t* pair, /**< current pair */
hawk_oow_t* buckno /**< bucket number */
hawk_htb_itr_t* itr /**< iterator*/
);
/**

166
hawk/lib/hawk-map.h Normal file
View File

@ -0,0 +1,166 @@
/*
* $Id$
*
Copyright (c) 2006-2019 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.
*/
#ifndef _HAWK_MAP_H_
#define _HAWK_MAP_H_
/*
* it is a convenience header file to switch easily between a red-black tree
* and a hash table. You must define either HAWK_MAP_IS_HTB or HAWK_MAP_IS_RBT
* before including this file.
*/
#if defined(HAWK_MAP_IS_HTB)
# include <hawk-htb.h>
# define HAWK_MAP_STYLE_DEFAULT HAWK_HTB_STYLE_DEFAULT
# define HAWK_MAP_STYLE_INLINE_COPIERS HAWK_HTB_STYLE_INLINE_COPIERS
# define HAWK_MAP_STYLE_INLINE_KEY_COPIER HAWK_HTB_STYLE_INLINE_KEY_COPIER
# define HAWK_MAP_STYLE_INLINE_VALUE_COPIER HAWK_HTB_STYLE_INLINE_VALUE_COPIER
# define hawk_getmapstyle(kind) hawk_gethtbstyle(kind)
# define hawk_map_open(mmgr,ext,capa,factor,ks,vs) hawk_htb_open(mmgr,ext,capa,factor,ks,vs)
# define hawk_map_close(map) hawk_htb_close(map)
# define hawk_map_init(map,mmgr,capa,factor,ks,vs) hawk_htb_init(map,mmgr,capa,factor,ks,vs)
# define hawk_map_fini(map) hawk_htb_fini(map)
# define hawk_map_getxtn(map) hawk_htb_getxtn(map)
# define hawk_map_getsize(map) hawk_htb_getsize(map)
# define hawk_map_getcapa(map) hawk_htb_getcapa(map)
# define hawk_map_getstyle(map) hawk_htb_getstyle(map)
# define hawk_map_setstyle(map,cbs) hawk_htb_setstyle(map,cbs)
# define hawk_map_search(map,kptr,klen) hawk_htb_search(map,kptr,klen)
# define hawk_map_upsert(map,kptr,klen,vptr,vlen) hawk_htb_upsert(map,kptr,klen,vptr,vlen)
# define hawk_map_ensert(map,kptr,klen,vptr,vlen) hawk_htb_ensert(map,kptr,klen,vptr,vlen)
# define hawk_map_insert(map,kptr,klen,vptr,vlen) hawk_htb_insert(map,kptr,klen,vptr,vlen)
# define hawk_map_update(map,kptr,klen,vptr,vlen) hawk_htb_update(map,kptr,klen,vptr,vlen)
# define hawk_map_cbsert(map,kptr,klen,cb,ctx) hawk_htb_cbsert(map,kptr,klen,cb,ctx)
# define hawk_map_delete(map,kptr,klen) hawk_htb_delete(map,kptr,klen)
# define hawk_map_clear(map) hawk_htb_clear(map)
# define hawk_init_map_itr(itr,dir) hawk_init_htb_itr(itr) /* dir not used. not supported */
# define hawk_map_getfirstpair(map,itr) hawk_htb_getfirstpair(map,itr)
# define hawk_map_getnextpair(map,itr) hawk_htb_getnextpair(map,itr)
# define hawk_map_walk(map,walker,ctx) hawk_htb_walk(map,walker,ctx)
# define HAWK_MAP_WALK_STOP HAWK_HTB_WALK_STOP
# define HAWK_MAP_WALK_FORWARD HAWK_HTB_WALK_FORWARD
# define hawk_map_walk_t hawk_htb_walk_t
# define HAWK_MAP_KEY HAWK_HTB_KEY
# define HAWK_MAP_VAL HAWK_HTB_VAL
# define hawk_map_id_t hawk_htb_id_t
# define hawk_map_t hawk_htb_t
# define hawk_map_pair_t hawk_htb_pair_t
# define hawk_map_style_t hawk_htb_style_t
# define hawk_map_cbserter_t hawk_htb_cbserter_t
# define hawk_map_itr_t hawk_htb_itr_t
# define hawk_map_walker_t hawk_htb_walker_t
# define HAWK_MAP_COPIER_SIMPLE HAWK_HTB_COPIER_SIMPLE
# define HAWK_MAP_COPIER_INLINE HAWK_HTB_COPIER_INLINE
# define HAWK_MAP_COPIER_DEFAULT HAWK_HTB_COPIER_DEFAULT
# define HAWK_MAP_FREEER_DEFAULT HAWK_HTB_FREEER_DEFAULT
# define HAWK_MAP_COMPER_DEFAULT HAWK_HTB_COMPER_DEFAULT
# define HAWK_MAP_KEEPER_DEFAULT HAWK_HTB_KEEPER_DEFAULT
# define HAWK_MAP_SIZER_DEFAULT HAWK_HTB_SIZER_DEFAULT
# define HAWK_MAP_HASHER_DEFAULT HAWK_HTB_HASHER_DEFAULT
# define HAWK_MAP_SIZE(map) HAWK_HTB_SIZE(map)
# define HAWK_MAP_KCOPIER(map) HAWK_HTB_KCOPIER(map)
# define HAWK_MAP_VCOPIER(map) HAWK_HTB_VCOPIER(map)
# define HAWK_MAP_KFREEER(map) HAWK_HTB_KFREEER(map)
# define HAWK_MAP_VFREEER(map) HAWK_HTB_VFREEER(map)
# define HAWK_MAP_COMPER(map) HAWK_HTB_COMPER(map)
# define HAWK_MAP_KEEPER(map) HAWK_HTB_KEEPER(map)
# define HAWK_MAP_KSCALE(map) HAWK_HTB_KSCALE(map)
# define HAWK_MAP_VSCALE(map) HAWK_HTB_VSCALE(map)
# define HAWK_MAP_KPTL(p) HAWK_HTB_KPTL(p)
# define HAWK_MAP_VPTL(p) HAWK_HTB_VPTL(p)
# define HAWK_MAP_KPTR(p) HAWK_HTB_KPTR(p)
# define HAWK_MAP_KLEN(p) HAWK_HTB_KLEN(p)
# define HAWK_MAP_VPTR(p) HAWK_HTB_VPTR(p)
# define HAWK_MAP_VLEN(p) HAWK_HTB_VLEN(p)
#elif defined(HAWK_MAP_IS_RBT)
# include <hawk-rbt.h>
# define HAWK_MAP_STYLE_DEFAULT HAWK_RBT_STYLE_DEFAULT
# define HAWK_MAP_STYLE_INLINE_COPIERS HAWK_RBT_STYLE_INLINE_COPIERS
# define HAWK_MAP_STYLE_INLINE_KEY_COPIER HAWK_RBT_STYLE_INLINE_KEY_COPIER
# define HAWK_MAP_STYLE_INLINE_VALUE_COPIER HAWK_RBT_STYLE_INLINE_VALUE_COPIER
# define hawk_getmapstyle(kind) hawk_getrbtstyle(kind)
# define hawk_map_open(mmgr,ext,capa,factor,ks,vs) hawk_rbt_open(mmgr,ext,ks,vs)
# define hawk_map_close(map) hawk_rbt_close(map)
# define hawk_map_init(map,mmgr,capa,factor,ks,vs) hawk_rbt_init(map,mmgr,ks,vs)
# define hawk_map_fini(map) hawk_rbt_fini(map)
# define hawk_map_getxtn(map) hawk_rbt_getxtn(map)
# define hawk_map_getsize(map) hawk_rbt_getsize(map)
# define hawk_map_getcapa(map) hawk_rbt_getsize(map)
# define hawk_map_getstyle(map) hawk_rbt_getstyle(map)
# define hawk_map_setstyle(map,cbs) hawk_rbt_setstyle(map,cbs)
# define hawk_map_search(map,kptr,klen) hawk_rbt_search(map,kptr,klen)
# define hawk_map_upsert(map,kptr,klen,vptr,vlen) hawk_rbt_upsert(map,kptr,klen,vptr,vlen)
# define hawk_map_ensert(map,kptr,klen,vptr,vlen) hawk_rbt_ensert(map,kptr,klen,vptr,vlen)
# define hawk_map_insert(map,kptr,klen,vptr,vlen) hawk_rbt_insert(map,kptr,klen,vptr,vlen)
# define hawk_map_update(map,kptr,klen,vptr,vlen) hawk_rbt_update(map,kptr,klen,vptr,vlen)
# define hawk_map_cbsert(map,kptr,klen,cb,ctx) hawk_rbt_cbsert(map,kptr,klen,cb,ctx)
# define hawk_map_delete(map,kptr,klen) hawk_rbt_delete(map,kptr,klen)
# define hawk_map_clear(map) hawk_rbt_clear(map)
# define hawk_init_map_itr(itr,dir) hawk_init_rbt_itr(itr,dir)
# define hawk_map_getfirstpair(map,itr) hawk_rbt_getfirstpair(map,itr)
# define hawk_map_getnextpair(map,itr) hawk_rbt_getnextpair(map,itr)
# define hawk_map_walk(map,walker,ctx) hawk_rbt_walk(map,walker,ctx)
# define HAWK_MAP_WALK_STOP HAWK_RBT_WALK_STOP
# define HAWK_MAP_WALK_FORWARD HAWK_RBT_WALK_FORWARD
# define hawk_map_walk_t hawk_rbt_walk_t
# define HAWK_MAP_KEY HAWK_RBT_KEY
# define HAWK_MAP_VAL HAWK_RBT_VAL
# define hawk_map_id_t hawk_rbt_id_t
# define hawk_map_t hawk_rbt_t
# define hawk_map_pair_t hawk_rbt_pair_t
# define hawk_map_style_t hawk_rbt_style_t
# define hawk_map_cbserter_t hawk_rbt_cbserter_t
# define hawk_map_itr_t hawk_rbt_itr_t
# define hawk_map_walker_t hawk_rbt_walker_t
# define HAWK_MAP_COPIER_SIMPLE HAWK_RBT_COPIER_SIMPLE
# define HAWK_MAP_COPIER_INLINE HAWK_RBT_COPIER_INLINE
# define HAWK_MAP_COPIER_DEFAULT HAWK_RBT_COPIER_DEFAULT
# define HAWK_MAP_FREEER_DEFAULT HAWK_RBT_FREEER_DEFAULT
# define HAWK_MAP_COMPER_DEFAULT HAWK_RBT_COMPER_DEFAULT
# define HAWK_MAP_KEEPER_DEFAULT HAWK_RBT_KEEPER_DEFAULT
# define HAWK_MAP_SIZER_DEFAULT (HAWK_NULL)
# define HAWK_MAP_HASHER_DEFAULT (HAWK_NULL)
# define HAWK_MAP_SIZE(map) HAWK_RBT_SIZE(map)
# define HAWK_MAP_KCOPIER(map) HAWK_RBT_KCOPIER(map)
# define HAWK_MAP_VCOPIER(map) HAWK_RBT_VCOPIER(map)
# define HAWK_MAP_KFREEER(map) HAWK_RBT_KFREEER(map)
# define HAWK_MAP_VFREEER(map) HAWK_RBT_VFREEER(map)
# define HAWK_MAP_COMPER(map) HAWK_RBT_COMPER(map)
# define HAWK_MAP_KEEPER(map) HAWK_RBT_KEEPER(map)
# define HAWK_MAP_KSCALE(map) HAWK_RBT_KSCALE(map)
# define HAWK_MAP_VSCALE(map) HAWK_RBT_VSCALE(map)
# define HAWK_MAP_KPTL(p) HAWK_RBT_KPTL(p)
# define HAWK_MAP_VPTL(p) HAWK_RBT_VPTL(p)
# define HAWK_MAP_KPTR(p) HAWK_RBT_KPTR(p)
# define HAWK_MAP_KLEN(p) HAWK_RBT_KLEN(p)
# define HAWK_MAP_VPTR(p) HAWK_RBT_VPTR(p)
# define HAWK_MAP_VLEN(p) HAWK_RBT_VLEN(p)
#else
# error define HAWK_MAP_IS_HTB or HAWK_MAP_IS_RBT before including this file
#endif
#endif

View File

@ -243,6 +243,17 @@ struct hawk_rbt_t
hawk_rbt_pair_t* root; /**< root pair */
};
struct hawk_rbt_itr_t
{
hawk_rbt_pair_t* pair;
hawk_rbt_pair_t* _prev;
int _dir; /* 0 or 1 */
int _state;
};
typedef struct hawk_rbt_itr_t hawk_rbt_itr_t;
/**
* The HAWK_RBT_COPIER_SIMPLE macros defines a copier that remembers the
* pointer and length of data in a pair.
@ -379,9 +390,9 @@ HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_search (
*/
HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_upsert (
hawk_rbt_t* rbt, /**< red-black tree */
void* kptr, /**< key pointer */
void* kptr, /**< key pointer */
hawk_oow_t klen, /**< key length */
void* vptr, /**< value pointer */
void* vptr, /**< value pointer */
hawk_oow_t vlen /**< value length */
);
@ -393,9 +404,9 @@ HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_upsert (
*/
HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_ensert (
hawk_rbt_t* rbt, /**< red-black tree */
void* kptr, /**< key pointer */
void* kptr, /**< key pointer */
hawk_oow_t klen, /**< key length */
void* vptr, /**< value pointer */
void* vptr, /**< value pointer */
hawk_oow_t vlen /**< value length */
);
@ -407,9 +418,9 @@ HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_ensert (
*/
HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_insert (
hawk_rbt_t* rbt, /**< red-black tree */
void* kptr, /**< key pointer */
void* kptr, /**< key pointer */
hawk_oow_t klen, /**< key length */
void* vptr, /**< value pointer */
void* vptr, /**< value pointer */
hawk_oow_t vlen /**< value length */
);
@ -527,7 +538,7 @@ HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_cbsert (
*/
HAWK_EXPORT int hawk_rbt_delete (
hawk_rbt_t* rbt, /**< red-black tree */
const void* kptr, /**< key pointer */
const void* kptr, /**< key pointer */
hawk_oow_t klen /**< key size */
);
@ -538,6 +549,21 @@ HAWK_EXPORT void hawk_rbt_clear (
hawk_rbt_t* rbt /**< red-black tree */
);
HAWK_EXPORT void hawk_init_rbt_itr (
hawk_rbt_itr_t* itr,
int dir /* 0 or 1 */
);
HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_getfirstpair (
hawk_rbt_t* rbt,
hawk_rbt_itr_t* itr
);
HAWK_EXPORT hawk_rbt_pair_t* hawk_rbt_getnextpair (
hawk_rbt_t* rbt,
hawk_rbt_itr_t* itr
);
/**
* The hawk_rbt_walk() function traverses a red-black tree in preorder
* from the leftmost child.

View File

@ -30,9 +30,12 @@
#include <hawk-cmn.h>
#include <hawk-ecs.h>
#include <hawk-gem.h>
#include <hawk-htb.h>
#include <hawk-tre.h>
#include <hawk-utl.h>
#include <hawk-htb.h> /* for rtx->named */
#define HAWK_MAP_IS_RBT
#include <hawk-map.h>
#include <stdarg.h>
/** \file
@ -238,7 +241,7 @@ struct hawk_val_map_t
* integers switch to map dynamically once the
* non-integral index is seen.
*/
hawk_htb_t* map;
hawk_map_t* map;
};
typedef struct hawk_val_map_t hawk_val_map_t;
@ -276,26 +279,19 @@ typedef struct hawk_val_ref_t hawk_val_ref_t;
/**
* The hawk_val_map_itr_t type defines the iterator to map value fields.
*/
struct hawk_val_map_itr_t
{
hawk_htb_pair_t* pair;
hawk_oow_t buckno;
};
typedef struct hawk_val_map_itr_t hawk_val_map_itr_t;
typedef hawk_map_itr_t hawk_val_map_itr_t;
/**
* The #HAWK_VAL_MAP_ITR_KEY macro get the pointer to the key part
* of a map value.
*/
#define HAWK_VAL_MAP_ITR_KEY(itr) \
((const hawk_oocs_t*)HAWK_HTB_KPTL((itr)->pair))
#define HAWK_VAL_MAP_ITR_KEY(itr) ((const hawk_oocs_t*)HAWK_MAP_KPTL((itr)->pair))
/**
* The #HAWK_VAL_MAP_ITR_VAL macro get the pointer to the value part
* of a map value.
*/
#define HAWK_VAL_MAP_ITR_VAL(itr) \
((const hawk_val_t*)HAWK_HTB_VPTR((itr)->pair))
#define HAWK_VAL_MAP_ITR_VAL(itr) ((const hawk_val_t*)HAWK_MAP_VPTR((itr)->pair))
/**

View File

@ -663,7 +663,14 @@ void hawk_htb_walk (hawk_htb_t* htb, walker_t walker, void* ctx)
}
}
pair_t* hawk_htb_getfirstpair (hawk_htb_t* htb, hawk_oow_t* buckno)
void hawk_init_htb_itr (hawk_htb_itr_t* itr)
{
itr->pair = HAWK_NULL;
itr->buckno = 0;
}
pair_t* hawk_htb_getfirstpair (hawk_htb_t* htb, hawk_htb_itr_t* itr)
{
hawk_oow_t i;
pair_t* pair;
@ -671,9 +678,10 @@ pair_t* hawk_htb_getfirstpair (hawk_htb_t* htb, hawk_oow_t* buckno)
for (i = 0; i < htb->capa; i++)
{
pair = htb->bucket[i];
if (pair != HAWK_NULL)
if (pair)
{
*buckno = i;
itr->buckno = i;
itr->pair = pair;
return pair;
}
}
@ -681,24 +689,26 @@ pair_t* hawk_htb_getfirstpair (hawk_htb_t* htb, hawk_oow_t* buckno)
return HAWK_NULL;
}
pair_t* hawk_htb_getnextpair (hawk_htb_t* htb, pair_t* pair, hawk_oow_t* buckno)
pair_t* hawk_htb_getnextpair (hawk_htb_t* htb, hawk_htb_itr_t* itr)
{
hawk_oow_t i;
pair_t* next;
pair_t* pair;
next = NEXT(pair);
if (next != HAWK_NULL)
pair = NEXT(itr->pair);
if (pair)
{
/* no change in bucket number */
return next;
itr->pair = pair;
return pair;
}
for (i = (*buckno)+1; i < htb->capa; i++)
for (i = itr->buckno + 1; i < htb->capa; i++)
{
pair = htb->bucket[i];
if (pair != HAWK_NULL)
if (pair)
{
*buckno = i;
itr->buckno = i;
itr->pair = pair;
return pair;
}
}

View File

@ -579,10 +579,10 @@ static int parse (hawk_t* awk)
* o therwise, the error detection will get delay until run-time. */
hawk_htb_pair_t* p;
hawk_oow_t buckno;
hawk_htb_itr_t itr;
p = hawk_htb_getfirstpair(awk->parse.funs, &buckno);
while (p != HAWK_NULL)
p = hawk_htb_getfirstpair(awk->parse.funs, &itr);
while (p)
{
if (hawk_htb_search(awk->tree.funs, HAWK_HTB_KPTR(p), HAWK_HTB_KLEN(p)) == HAWK_NULL)
{
@ -595,7 +595,7 @@ static int parse (hawk_t* awk)
goto oops;
}
p = hawk_htb_getnextpair (awk->parse.funs, p, &buckno);
p = hawk_htb_getnextpair(awk->parse.funs, &itr);
}
}

View File

@ -892,10 +892,24 @@ static HAWK_INLINE hawk_rbt_walk_t walk_recursively (
}
#endif
static HAWK_INLINE void walk (hawk_rbt_t* rbt, walker_t walker, void* ctx, int l, int r)
void hawk_init_rbt_itr (hawk_rbt_itr_t* itr, int dir)
{
hawk_rbt_pair_t* x_cur = rbt->root;
hawk_rbt_pair_t* prev = rbt->root->parent;
itr->pair = HAWK_NULL;
itr->_prev = HAWK_NULL;
itr->_dir = dir;
itr->_state = 0;
}
static hawk_rbt_pair_t* get_next_pair (hawk_rbt_t* rbt, hawk_rbt_itr_t* itr)
{
hawk_rbt_pair_t* x_cur = itr->pair;
hawk_rbt_pair_t* prev = itr->_prev;
int l = !!itr->_dir;
int r = !itr->_dir;
if (itr->_state == 1) goto resume_1;
if (itr->_state == 2) goto resume_2;
while (x_cur && !IS_NIL(rbt,x_cur))
{
@ -911,8 +925,13 @@ static HAWK_INLINE void walk (hawk_rbt_t* rbt, walker_t walker, void* ctx, int l
}
else
{
if (walker (rbt, x_cur, ctx) == HAWK_RBT_WALK_STOP) break;
/*if (walker(rbt, x_cur, ctx) == HAWK_RBT_WALK_STOP) break; */
itr->pair = x_cur;
itr->_prev = prev;
itr->_state = 1;
return x_cur;
resume_1:
if (!IS_NIL(rbt,x_cur->child[r]))
{
/* go down to the right node if exists */
@ -931,8 +950,13 @@ static HAWK_INLINE void walk (hawk_rbt_t* rbt, walker_t walker, void* ctx, int l
{
/* the left child has been already traversed */
if (walker (rbt, x_cur, ctx) == HAWK_RBT_WALK_STOP) break;
/*if (walker(rbt, x_cur, ctx) == HAWK_RBT_WALK_STOP) break;*/
itr->pair = x_cur;
itr->_prev = prev;
itr->_state = 2;
return x_cur;
resume_2:
if (!IS_NIL(rbt,x_cur->child[r]))
{
/* go down to the right node if it exists */
@ -955,18 +979,52 @@ static HAWK_INLINE void walk (hawk_rbt_t* rbt, walker_t walker, void* ctx, int l
x_cur = x_cur->parent;
}
}
return HAWK_NULL;
}
hawk_rbt_pair_t* hawk_rbt_getfirstpair (hawk_rbt_t* rbt, hawk_rbt_itr_t* itr)
{
itr->pair = rbt->root;
itr->_prev = rbt->root->parent;
itr->_state = 0;
return get_next_pair(rbt, itr);
}
hawk_rbt_pair_t* hawk_rbt_getnextpair (hawk_rbt_t* rbt, hawk_rbt_itr_t* itr)
{
return get_next_pair(rbt, itr);
}
void hawk_rbt_walk (hawk_rbt_t* rbt, walker_t walker, void* ctx)
{
walk (rbt, walker, ctx, LEFT, RIGHT);
hawk_rbt_itr_t itr;
hawk_rbt_pair_t* pair;
itr._dir = 0;
pair = hawk_rbt_getfirstpair(rbt, &itr);
while (pair)
{
if (walker(rbt, pair, ctx) == HAWK_RBT_WALK_STOP) break;
pair = hawk_rbt_getnextpair(rbt, &itr);
}
}
void hawk_rbt_rwalk (hawk_rbt_t* rbt, walker_t walker, void* ctx)
{
walk (rbt, walker, ctx, RIGHT, LEFT);
hawk_rbt_itr_t itr;
hawk_rbt_pair_t* pair;
itr._dir = 1;
pair = hawk_rbt_getfirstpair(rbt, &itr);
while (pair)
{
if (walker(rbt, pair, ctx) == HAWK_RBT_WALK_STOP) break;
pair = hawk_rbt_getnextpair(rbt, &itr);
}
}
int hawk_rbt_dflcomp (const hawk_rbt_t* rbt, const void* kptr1, hawk_oow_t klen1, const void* kptr2, hawk_oow_t klen2)
{
hawk_oow_t min;

View File

@ -1107,7 +1107,7 @@ oops_13:
oops_12:
hawk_htb_close (rtx->named);
oops_11:
hawk_becs_fini (&rtx->fnc.oout);
hawk_ooecs_fini (&rtx->fnc.oout);
oops_10:
hawk_becs_fini (&rtx->fnc.bout);
oops_9:
@ -1284,7 +1284,7 @@ static int update_fnr (hawk_rtx_t* rtx, hawk_int_t fnr, hawk_int_t nr)
else
{
tmp2 = hawk_rtx_makeintval(rtx, nr);
if (!tmp2) { n = -1; goto oops; }
if (HAWK_UNLIKELY(!tmp2)) { n = -1; goto oops; }
hawk_rtx_refupval (rtx, tmp2);
}
@ -2360,13 +2360,13 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
{
hawk_val_t* val;
if (nde->init != HAWK_NULL)
if (nde->init)
{
HAWK_ASSERT (nde->init->next == HAWK_NULL);
ON_STATEMENT (rtx, nde->init);
val = eval_expression(rtx,nde->init);
if (val == HAWK_NULL) return -1;
if (HAWK_UNLIKELY(!val)) return -1;
hawk_rtx_refupval (rtx, val);
hawk_rtx_refdownval (rtx, val);
@ -2374,7 +2374,7 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
while (1)
{
if (nde->test != HAWK_NULL)
if (nde->test)
{
hawk_val_t* test;
@ -2384,12 +2384,12 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
ON_STATEMENT (rtx, nde->test);
test = eval_expression (rtx, nde->test);
if (test == HAWK_NULL) return -1;
if (HAWK_UNLIKELY(!test)) return -1;
hawk_rtx_refupval (rtx, test);
if (hawk_rtx_valtobool(rtx, test))
{
if (run_statement(rtx,nde->body) == -1)
if (run_statement(rtx,nde->body) <= -1)
{
hawk_rtx_refdownval (rtx, test);
return -1;
@ -2405,7 +2405,7 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
}
else
{
if (run_statement(rtx,nde->body) == -1) return -1;
if (run_statement(rtx,nde->body) <= -1) return -1;
}
if (rtx->exit_level == EXIT_BREAK)
@ -2425,7 +2425,7 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
ON_STATEMENT (rtx, nde->incr);
val = eval_expression (rtx, nde->incr);
if (val == HAWK_NULL) return -1;
if (HAWK_UNLIKELY(!val)) return -1;
hawk_rtx_refupval (rtx, val);
hawk_rtx_refdownval (rtx, val);
@ -2435,68 +2435,15 @@ static int run_for (hawk_rtx_t* rtx, hawk_nde_for_t* nde)
return 0;
}
struct foreach_walker_t
{
hawk_rtx_t* rtx;
hawk_nde_t* var;
hawk_nde_t* body;
int ret;
};
static hawk_htb_walk_t walk_foreach (hawk_htb_t* map, hawk_htb_pair_t* pair, void* arg)
{
struct foreach_walker_t* w = (struct foreach_walker_t*)arg;
hawk_val_t* str;
str = (hawk_val_t*)hawk_rtx_makestrvalwithoochars(w->rtx, HAWK_HTB_KPTR(pair), HAWK_HTB_KLEN(pair));
if (str == HAWK_NULL)
{
ADJERR_LOC (w->rtx, &w->var->loc);
w->ret = -1;
return HAWK_HTB_WALK_STOP;
}
hawk_rtx_refupval (w->rtx, str);
if (do_assignment(w->rtx, w->var, str) == HAWK_NULL)
{
hawk_rtx_refdownval (w->rtx, str);
w->ret = -1;
return HAWK_HTB_WALK_STOP;
}
if (run_statement(w->rtx, w->body) == -1)
{
hawk_rtx_refdownval (w->rtx, str);
w->ret = -1;
return HAWK_HTB_WALK_STOP;
}
hawk_rtx_refdownval (w->rtx, str);
if (w->rtx->exit_level == EXIT_BREAK)
{
w->rtx->exit_level = EXIT_NONE;
return HAWK_HTB_WALK_STOP;
}
else if (w->rtx->exit_level == EXIT_CONTINUE)
{
w->rtx->exit_level = EXIT_NONE;
}
else if (w->rtx->exit_level != EXIT_NONE)
{
return HAWK_HTB_WALK_STOP;
}
return HAWK_HTB_WALK_FORWARD;
}
static int run_foreach (hawk_rtx_t* rtx, hawk_nde_foreach_t* nde)
{
hawk_nde_exp_t* test;
hawk_val_t* rv;
hawk_htb_t* map;
struct foreach_walker_t walker;
hawk_map_t* map;
hawk_map_pair_t* pair;
hawk_map_itr_t itr;
hawk_val_type_t rvtype;
int ret;
test = (hawk_nde_exp_t*)nde->test;
HAWK_ASSERT (test->type == HAWK_NDE_EXP_BIN && test->opcode == HAWK_BINOP_IN);
@ -2506,32 +2453,69 @@ static int run_foreach (hawk_rtx_t* rtx, hawk_nde_foreach_t* nde)
HAWK_ASSERT (test->right->next == HAWK_NULL);
rv = eval_expression(rtx, test->right);
if (rv == HAWK_NULL) return -1;
if (HAWK_UNLIKELY(!rv)) return -1;
ret = 0;
hawk_rtx_refupval (rtx, rv);
rvtype = HAWK_RTX_GETVALTYPE(rtx, rv);
if (rvtype == HAWK_VAL_NIL)
{
/* just return without excuting the loop body */
hawk_rtx_refdownval (rtx, rv);
return 0;
goto done;
}
else if (rvtype != HAWK_VAL_MAP)
{
hawk_rtx_refdownval (rtx, rv);
hawk_rtx_seterrnum (rtx, &test->right->loc, HAWK_ENOTMAPIN);
return -1;
ret = -1;
goto done;
}
map = ((hawk_val_map_t*)rv)->map;
walker.rtx = rtx;
walker.var = test->left;
walker.body = nde->body;
walker.ret = 0;
hawk_htb_walk (map, walk_foreach, &walker);
hawk_init_map_itr (&itr, 0);
pair = hawk_map_getfirstpair(map, &itr);
while (pair)
{
hawk_val_t* str;
str = (hawk_val_t*)hawk_rtx_makestrvalwithoochars(rtx, HAWK_HTB_KPTR(pair), HAWK_HTB_KLEN(pair));
if (HAWK_UNLIKELY(!str))
{
ADJERR_LOC (rtx, &test->left->loc);
ret = -1;
goto done;
}
hawk_rtx_refupval (rtx, str);
if (!do_assignment(rtx, test->left, str) || run_statement(rtx, nde->body) <= -1)
{
hawk_rtx_refdownval (rtx, str);
ret = -1;
goto done;
}
hawk_rtx_refdownval (rtx, str);
if (rtx->exit_level == EXIT_BREAK)
{
rtx->exit_level = EXIT_NONE;
goto done;
}
else if (rtx->exit_level == EXIT_CONTINUE)
{
rtx->exit_level = EXIT_NONE;
}
else if (rtx->exit_level != EXIT_NONE)
{
goto done;
}
pair = hawk_map_getnextpair(map, &itr);
}
done:
hawk_rtx_refdownval (rtx, rv);
return walker.ret;
return ret;
}
static int run_break (hawk_rtx_t* run, hawk_nde_break_t* nde)
@ -2548,7 +2532,7 @@ static int run_continue (hawk_rtx_t* rtx, hawk_nde_continue_t* nde)
static int run_return (hawk_rtx_t* rtx, hawk_nde_return_t* nde)
{
if (nde->val != HAWK_NULL)
if (nde->val)
{
hawk_val_t* val;
@ -2557,7 +2541,7 @@ static int run_return (hawk_rtx_t* rtx, hawk_nde_return_t* nde)
HAWK_ASSERT (nde->val->next == HAWK_NULL);
val = eval_expression (rtx, nde->val);
if (val == HAWK_NULL) return -1;
if (HAWK_UNLIKELY(!val)) return -1;
if (!(rtx->hawk->opt.trait & HAWK_FLEXMAP))
{
@ -2592,8 +2576,8 @@ static int run_exit (hawk_rtx_t* rtx, hawk_nde_exit_t* nde)
* by the parser first of all */
HAWK_ASSERT (nde->val->next == HAWK_NULL);
val = eval_expression (rtx, nde->val);
if (val == HAWK_NULL) return -1;
val = eval_expression(rtx, nde->val);
if (HAWK_UNLIKELY(!val)) return -1;
hawk_rtx_refdownval (rtx, RTX_STACK_RETVAL_GBL(rtx));
RTX_STACK_RETVAL_GBL(rtx) = val; /* global return value */
@ -2641,8 +2625,8 @@ static int run_nextinfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
return -1;
}
n = hawk_rtx_nextio_read (rtx, HAWK_IN_CONSOLE, HAWK_T(""));
if (n == -1)
n = hawk_rtx_nextio_read(rtx, HAWK_IN_CONSOLE, HAWK_T(""));
if (n <= -1)
{
ADJERR_LOC (rtx, &nde->loc);
return -1;
@ -2656,7 +2640,7 @@ static int run_nextinfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
}
/* FNR resets to 0, NR remains the same */
if (update_fnr (rtx, 0, rtx->gbl.nr) == -1)
if (update_fnr(rtx, 0, rtx->gbl.nr) <= -1)
{
ADJERR_LOC (rtx, &nde->loc);
return -1;
@ -2674,7 +2658,7 @@ static int run_nextoutfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
/* nextofile can be called from BEGIN and END block unlike nextfile */
n = hawk_rtx_nextio_write (rtx, HAWK_OUT_CONSOLE, HAWK_T(""));
if (n == -1)
if (n <= -1)
{
/* adjust the error line */
ADJERR_LOC (rtx, &nde->loc);
@ -2698,7 +2682,7 @@ static int run_nextfile (hawk_rtx_t* rtx, hawk_nde_nextfile_t* nde)
return (nde->out)? run_nextoutfile(rtx, nde): run_nextinfile(rtx, nde);
}
static int delete_indexed (hawk_rtx_t* rtx, hawk_htb_t* map, hawk_nde_var_t* var)
static int delete_indexed (hawk_rtx_t* rtx, hawk_map_t* map, hawk_nde_var_t* var)
{
hawk_ooch_t* iptr;
hawk_oow_t ilen;
@ -2710,9 +2694,9 @@ static int delete_indexed (hawk_rtx_t* rtx, hawk_htb_t* map, hawk_nde_var_t* var
* delete x[20,"abc"]; */
ilen = HAWK_COUNTOF(idxbuf);
iptr = idxnde_to_str(rtx, var->idx, idxbuf, &ilen);
if (!iptr) return -1;
if (HAWK_UNLIKELY(!iptr)) return -1;
hawk_htb_delete (map, iptr, ilen);
hawk_map_delete (map, iptr, ilen);
if (iptr != idxbuf) hawk_rtx_freemem (rtx, iptr);
return 0;
@ -2737,7 +2721,7 @@ static int run_delete_named (hawk_rtx_t* rtx, hawk_nde_var_t* var)
* create a map and assign it to the variable */
tmp = hawk_rtx_makemapval(rtx);
if (tmp == HAWK_NULL)
if (HAWK_UNLIKELY(!tmp))
{
/* adjust error line */
ADJERR_LOC (rtx, &var->loc);
@ -2745,7 +2729,7 @@ static int run_delete_named (hawk_rtx_t* rtx, hawk_nde_var_t* var)
}
pair = hawk_htb_upsert(rtx->named, var->id.name.ptr, var->id.name.len, tmp, 0);
if (pair == HAWK_NULL)
if (HAWK_UNLIKELY(!pair))
{
hawk_rtx_refupval (rtx, tmp);
hawk_rtx_refdownval (rtx, tmp);
@ -2760,7 +2744,7 @@ static int run_delete_named (hawk_rtx_t* rtx, hawk_nde_var_t* var)
else
{
hawk_val_t* val;
hawk_htb_t* map;
hawk_map_t* map;
val = (hawk_val_t*)HAWK_HTB_VPTR(pair);
HAWK_ASSERT (val != HAWK_NULL);
@ -2778,7 +2762,7 @@ static int run_delete_named (hawk_rtx_t* rtx, hawk_nde_var_t* var)
}
else
{
hawk_htb_clear (map);
hawk_map_clear (map);
}
}
@ -2818,7 +2802,7 @@ static int run_delete_unnamed (hawk_rtx_t* rtx, hawk_nde_var_t* var)
* create a map and assign it to the variable */
tmp = hawk_rtx_makemapval(rtx);
if (tmp == HAWK_NULL)
if (HAWK_UNLIKELY(!tmp))
{
ADJERR_LOC (rtx, &var->loc);
return -1;
@ -2859,7 +2843,7 @@ static int run_delete_unnamed (hawk_rtx_t* rtx, hawk_nde_var_t* var)
}
else
{
hawk_htb_t* map;
hawk_map_t* map;
if (vtype != HAWK_VAL_MAP)
{
@ -2876,7 +2860,7 @@ static int run_delete_unnamed (hawk_rtx_t* rtx, hawk_nde_var_t* var)
}
else
{
hawk_htb_clear (map);
hawk_map_clear (map);
}
}
@ -3871,7 +3855,7 @@ retry:
hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("**** index str=>%s, map->ref=%d, map->type=%d\n"), str, (int)map->ref, (int)map->type);
#endif
if (hawk_htb_upsert(map->map, str, len, val, 0) == HAWK_NULL)
if (hawk_map_upsert(map->map, str, len, val, 0) == HAWK_NULL)
{
if (str != idxbuf) hawk_rtx_freemem (rtx, str);
ADJERR_LOC (rtx, &var->loc);
@ -4193,10 +4177,10 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
else if (rvtype == HAWK_VAL_MAP)
{
hawk_val_t* res;
hawk_htb_t* map;
hawk_map_t* map;
map = ((hawk_val_map_t*)rv)->map;
res = (hawk_htb_search(map, str, len) == HAWK_NULL)? HAWK_VAL_ZERO: HAWK_VAL_ONE;
res = (hawk_map_search(map, str, len) == HAWK_NULL)? HAWK_VAL_ZERO: HAWK_VAL_ONE;
if (str != idxbuf) hawk_rtx_freemem (rtx, str);
hawk_rtx_refdownval (rtx, rv);
@ -6502,7 +6486,7 @@ static int get_reference (hawk_rtx_t* rtx, hawk_nde_t* nde, hawk_val_t*** ref)
static hawk_val_t** get_reference_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* nde, hawk_val_t** val)
{
hawk_htb_pair_t* pair;
hawk_map_pair_t* pair;
hawk_ooch_t* str;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
@ -6515,8 +6499,8 @@ static hawk_val_t** get_reference_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* nde,
{
hawk_val_t* tmp;
tmp = hawk_rtx_makemapval (rtx);
if (tmp == HAWK_NULL)
tmp = hawk_rtx_makemapval(rtx);
if (HAWK_UNLIKELY(!tmp))
{
ADJERR_LOC (rtx, &nde->loc);
return HAWK_NULL;
@ -6536,13 +6520,13 @@ static hawk_val_t** get_reference_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* nde,
len = HAWK_COUNTOF(idxbuf);
str = idxnde_to_str(rtx, nde->idx, idxbuf, &len);
if (str == HAWK_NULL) return HAWK_NULL;
if (HAWK_UNLIKELY(!str)) return HAWK_NULL;
pair = hawk_htb_search((*(hawk_val_map_t**)val)->map, str, len);
if (pair == HAWK_NULL)
pair = hawk_map_search((*(hawk_val_map_t**)val)->map, str, len);
if (!pair)
{
pair = hawk_htb_upsert((*(hawk_val_map_t**)val)->map, str, len, hawk_val_nil, 0);
if (pair == HAWK_NULL)
pair = hawk_map_upsert((*(hawk_val_map_t**)val)->map, str, len, hawk_val_nil, 0);
if (HAWK_UNLIKELY(!pair))
{
if (str != idxbuf) hawk_rtx_freemem (rtx, str);
ADJERR_LOC (rtx, &nde->loc);
@ -6654,7 +6638,7 @@ static hawk_val_t* eval_arg (hawk_rtx_t* rtx, hawk_nde_t* nde)
static hawk_val_t* eval_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* nde, hawk_val_t** val)
{
hawk_htb_pair_t* pair;
hawk_map_pair_t* pair;
hawk_ooch_t* str;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
@ -6690,7 +6674,7 @@ static hawk_val_t* eval_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* nde, hawk_val_
str = idxnde_to_str(rtx, nde->idx, idxbuf, &len);
if (str == HAWK_NULL) return HAWK_NULL;
pair = hawk_htb_search((*(hawk_val_map_t**)val)->map, str, len);
pair = hawk_map_search((*(hawk_val_map_t**)val)->map, str, len);
if (str != idxbuf) hawk_rtx_freemem (rtx, str);
return (pair == HAWK_NULL)? hawk_val_nil: (hawk_val_t*)HAWK_HTB_VPTR(pair);

View File

@ -2044,8 +2044,8 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod)
hawk_val_t* v_argc, * v_argv, * v_pair;
hawk_int_t i_argc;
const hawk_ooch_t* file;
hawk_htb_t* map;
hawk_htb_pair_t* pair;
hawk_map_t* map;
hawk_map_pair_t* pair;
hawk_ooch_t ibuf[128];
hawk_oow_t ibuflen;
hawk_oocs_t as;
@ -2100,7 +2100,7 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod)
ibuflen = hawk_int_to_oocstr(rxtn->c.in.index + 1, 10, HAWK_NULL, ibuf, HAWK_COUNTOF(ibuf));
pair = hawk_htb_search(map, ibuf, ibuflen);
pair = hawk_map_search(map, ibuf, ibuflen);
if (!pair)
{
/* the key doesn't exist any more */
@ -2337,8 +2337,10 @@ static hawk_ooi_t awk_rio_console (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_rio
}
if (sio) hawk_sio_close (sio);
}
/* reset FNR to 0 here since the caller doesn't know that the file has changed. */
hawk_rtx_setgbl(rtx, HAWK_GBL_FNR, hawk_rtx_makeintval(rtx, 0));
}
return nn;
}
@ -2361,6 +2363,7 @@ static hawk_ooi_t awk_rio_console (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_rio
}
if (sio) hawk_sio_close (sio);
hawk_rtx_setgbl(rtx, HAWK_GBL_FNR, hawk_rtx_makeintval(rtx, 0));
}
return nn;
@ -2459,7 +2462,7 @@ static int build_argcv (hawk_rtx_t* rtx, int argc_id, int argv_id, const hawk_oo
hawk_rtx_refupval (rtx, v_tmp);
key_len = hawk_copy_oocstr(key, HAWK_COUNTOF(key), HAWK_T("0"));
if (hawk_htb_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL)
if (hawk_map_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL)
{
/* if the assignment operation fails, decrements
* the reference of v_tmp to free it */
@ -2490,7 +2493,7 @@ static int build_argcv (hawk_rtx_t* rtx, int argc_id, int argv_id, const hawk_oo
hawk_rtx_refupval (rtx, v_tmp);
if (hawk_htb_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL)
if (hawk_map_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL)
{
hawk_rtx_refdownval (rtx, v_tmp);
hawk_rtx_refdownval (rtx, v_argv);
@ -2603,7 +2606,7 @@ static int build_environ (hawk_rtx_t* rtx, int gbl_id, env_char_t* envarr[])
/* the string in ENVIRON should be a numeric value if
* it can be converted to a number.
/*v_tmp = hawk_rtx_makenstrvalwithoocstr(rtx, vptr);*/
*v_tmp = hawk_rtx_makenstrvalwithoocstr(rtx, vptr);*/
v_tmp = hawk_rtx_makenumorstrvalwithoochars(rtx, vptr, vlen);
if (HAWK_UNLIKELY(!v_tmp))
{
@ -2622,7 +2625,7 @@ static int build_environ (hawk_rtx_t* rtx, int gbl_id, env_char_t* envarr[])
* it has successfully been assigned into ARGV. */
hawk_rtx_refupval (rtx, v_tmp);
if (hawk_htb_upsert(((hawk_val_map_t*)v_env)->map, kptr, klen, v_tmp, 0) == HAWK_NULL)
if (hawk_map_upsert(((hawk_val_map_t*)v_env)->map, kptr, klen, v_tmp, 0) == HAWK_NULL)
{
/* if the assignment operation fails, decrements
* the reference of v_tmp to free it */

View File

@ -511,9 +511,9 @@ hawk_val_t* hawk_rtx_makerexval (hawk_rtx_t* rtx, const hawk_oocs_t* str, hawk_t
return (hawk_val_t*)val;
}
static void free_mapval (hawk_htb_t* map, void* dptr, hawk_oow_t dlen)
static void free_mapval (hawk_map_t* map, void* dptr, hawk_oow_t dlen)
{
hawk_rtx_t* rtx = *(hawk_rtx_t**)hawk_htb_getxtn(map);
hawk_rtx_t* rtx = *(hawk_rtx_t**)hawk_map_getxtn(map);
#if defined(DEBUG_VAL)
hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("refdown in map free - [%O]\n"), dptr);
@ -522,9 +522,9 @@ static void free_mapval (hawk_htb_t* map, void* dptr, hawk_oow_t dlen)
hawk_rtx_refdownval (rtx, dptr);
}
static void same_mapval (hawk_htb_t* map, void* dptr, hawk_oow_t dlen)
static void same_mapval (hawk_map_t* map, void* dptr, hawk_oow_t dlen)
{
hawk_rtx_t* run = *(hawk_rtx_t**)hawk_htb_getxtn(map);
hawk_rtx_t* run = *(hawk_rtx_t**)hawk_map_getxtn(map);
#if defined(DEBUG_VAL)
hawk_logfmt (hawk_rtx_gethawk(rtx), HAWK_T("refdown nofree in map free - [%O]\n"), dptr);
#endif
@ -533,46 +533,28 @@ static void same_mapval (hawk_htb_t* map, void* dptr, hawk_oow_t dlen)
hawk_val_t* hawk_rtx_makemapval (hawk_rtx_t* rtx)
{
static hawk_htb_style_t style =
static hawk_map_style_t style =
{
/* the key is copied inline into a pair and is freed when the pair
* is destroyed. not setting copier for a value means that the pointer
* to the data allocated somewhere else is remembered in a pair. but
* freeing the actual value is handled by free_mapval and same_mapval */
{
HAWK_HTB_COPIER_INLINE,
HAWK_HTB_COPIER_DEFAULT
HAWK_MAP_COPIER_INLINE,
HAWK_MAP_COPIER_DEFAULT
},
{
HAWK_HTB_FREEER_DEFAULT,
HAWK_MAP_FREEER_DEFAULT,
free_mapval
},
HAWK_HTB_COMPER_DEFAULT,
HAWK_MAP_COMPER_DEFAULT,
same_mapval,
HAWK_HTB_SIZER_DEFAULT,
HAWK_HTB_HASHER_DEFAULT
HAWK_MAP_SIZER_DEFAULT,
HAWK_MAP_HASHER_DEFAULT
};
hawk_val_map_t* val;
/* CHECK */
/*
val = (hawk_val_map_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_map_t));
if (!val) return HAWK_NULL;
val->type = HAWK_VAL_MAP;
val->ref = 0;
val->stat = 0;
val->nstr = 0;
val->fcb = 0;
val->map = hawk_htb_open(hawk_rtx_getgem(rtx), 256, 70, free_mapval, same_mapval);
if (val->map == HAWK_NULL)
{
hawk_rtx_freemem (rtx, val);
return HAWK_NULL;
}
*/
val = (hawk_val_map_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_map_t) + HAWK_SIZEOF(hawk_htb_t) + HAWK_SIZEOF(rtx));
val = (hawk_val_map_t*)hawk_rtx_callocmem(rtx, HAWK_SIZEOF(hawk_val_map_t) + HAWK_SIZEOF(hawk_map_t) + HAWK_SIZEOF(rtx));
if (!val) return HAWK_NULL;
val->v_type = HAWK_VAL_MAP;
@ -580,16 +562,15 @@ hawk_val_t* hawk_rtx_makemapval (hawk_rtx_t* rtx)
val->stat = 0;
val->nstr = 0;
val->fcb = 0;
val->map = (hawk_htb_t*)(val + 1);
val->map = (hawk_map_t*)(val + 1);
if (hawk_htb_init(val->map, hawk_rtx_getgem(rtx), 256, 70, HAWK_SIZEOF(hawk_ooch_t), 1) <= -1)
if (hawk_map_init(val->map, hawk_rtx_getgem(rtx), 256, 70, HAWK_SIZEOF(hawk_ooch_t), 1) <= -1)
{
hawk_rtx_freemem (rtx, val);
return HAWK_NULL;
}
*(hawk_rtx_t**)hawk_htb_getxtn(val->map) = rtx;
hawk_htb_setstyle (val->map, &style);
/* END CHECK */
*(hawk_rtx_t**)hawk_map_getxtn(val->map) = rtx;
hawk_map_setstyle (val->map, &style);
return (hawk_val_t*)val;
}
@ -664,7 +645,7 @@ hawk_val_t* hawk_rtx_setmapvalfld (
{
HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, map) == HAWK_VAL_MAP);
if (hawk_htb_upsert (((hawk_val_map_t*)map)->map, (hawk_ooch_t*)kptr, klen, v, 0) == HAWK_NULL) return HAWK_NULL;
if (hawk_map_upsert (((hawk_val_map_t*)map)->map, (hawk_ooch_t*)kptr, klen, v, 0) == HAWK_NULL) return HAWK_NULL;
/* the value is passed in by an external party. we can't refup()
* and refdown() the value if htb_upsert() fails. that way, the value
@ -677,11 +658,11 @@ hawk_val_t* hawk_rtx_setmapvalfld (
hawk_val_t* hawk_rtx_getmapvalfld (hawk_rtx_t* rtx, hawk_val_t* map, const hawk_ooch_t* kptr, hawk_oow_t klen)
{
hawk_htb_pair_t* pair;
hawk_map_pair_t* pair;
HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, map) == HAWK_VAL_MAP);
pair = hawk_htb_search(((hawk_val_map_t*)map)->map, kptr, klen);
pair = hawk_map_search(((hawk_val_map_t*)map)->map, kptr, klen);
if (!pair)
{
/* the given key is not found in the map.
@ -693,20 +674,21 @@ hawk_val_t* hawk_rtx_getmapvalfld (hawk_rtx_t* rtx, hawk_val_t* map, const hawk_
return HAWK_NULL;
}
return HAWK_HTB_VPTR(pair);
return HAWK_MAP_VPTR(pair);
}
hawk_val_map_itr_t* hawk_rtx_getfirstmapvalitr (hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr)
{
HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, map) == HAWK_VAL_MAP);
itr->pair = hawk_htb_getfirstpair (((hawk_val_map_t*)map)->map, &itr->buckno);
HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, map) == HAWK_VAL_MAP);
hawk_init_map_itr (itr, 0); /* override the caller provided direction to 0 */
itr->pair = hawk_map_getfirstpair(((hawk_val_map_t*)map)->map, itr);
return itr->pair? itr: HAWK_NULL;
}
hawk_val_map_itr_t* hawk_rtx_getnextmapvalitr (hawk_rtx_t* rtx, hawk_val_t* map, hawk_val_map_itr_t* itr)
{
HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, map) == HAWK_VAL_MAP);
itr->pair = hawk_htb_getnextpair (((hawk_val_map_t*)map)->map, itr->pair, &itr->buckno);
HAWK_ASSERT (HAWK_RTX_GETVALTYPE(rtx, map) == HAWK_VAL_MAP);
itr->pair = hawk_map_getnextpair(((hawk_val_map_t*)map)->map, itr);
return itr->pair? itr: HAWK_NULL;
}
@ -864,7 +846,7 @@ void hawk_rtx_freeval (hawk_rtx_t* rtx, hawk_val_t* val, int cache)
case HAWK_VAL_MAP:
{
hawk_htb_fini (((hawk_val_map_t*)val)->map);
hawk_map_fini (((hawk_val_map_t*)val)->map);
hawk_rtx_freemem (rtx, val);
break;
}
@ -1013,7 +995,7 @@ int hawk_rtx_valtobool (hawk_rtx_t* rtx, const hawk_val_t* val)
return 1;
case HAWK_VAL_MAP:
/* true if the map size is greater than 0. false if not */
return HAWK_HTB_SIZE(((hawk_val_map_t*)val)->map) > 0;
return HAWK_MAP_SIZE(((hawk_val_map_t*)val)->map) > 0;
case HAWK_VAL_REF:
return val_ref_to_bool(rtx, (hawk_val_ref_t*)val);
}
@ -1785,7 +1767,7 @@ int hawk_rtx_valtonum (hawk_rtx_t* rtx, const hawk_val_t* v, hawk_int_t* l, hawk
case HAWK_VAL_MAP:
if (rtx->hawk->opt.trait & HAWK_FLEXMAP)
{
*l = HAWK_HTB_SIZE(((hawk_val_map_t*)v)->map);
*l = HAWK_MAP_SIZE(((hawk_val_map_t*)v)->map);
return 0; /* long */
}
goto invalid;
@ -2117,17 +2099,17 @@ int hawk_rtx_setrefval (hawk_rtx_t* rtx, hawk_val_ref_t* ref, hawk_val_t* val)
#define hawk_errputstrf hawk_errputstrf
static hawk_htb_walk_t print_pair (hawk_htb_t* map, hawk_htb_pair_t* pair, void* arg)
static hawk_map_walk_t print_pair (hawk_map_t* map, hawk_map_pair_t* pair, void* arg)
{
hawk_rtx_t* run = (hawk_rtx_t*)arg;
HAWK_ASSERT (run == *(hawk_rtx_t**)hawk_htb_getxtn(map));
HAWK_ASSERT (run == *(hawk_rtx_t**)hawk_map_getxtn(map));
hawk_errputstrf (HAWK_T(" %.*s=>"), HAWK_HTB_KLEN(pair), HAWK_HTB_KPTR(pair));
hawk_dprintval ((hawk_rtx_t*)arg, HAWK_HTB_VPTR(pair));
hawk_errputstrf (HAWK_T(" %.*s=>"), HAWK_MAP_KLEN(pair), HAWK_MAP_KPTR(pair));
hawk_dprintval ((hawk_rtx_t*)arg, HAWK_MAP_VPTR(pair));
hawk_errputstrf (HAWK_T(" "));
return HAWK_HTB_WALK_FORWARD;
return HAWK_MAP_WALK_FORWARD;
}
void hawk_dprintval (hawk_rtx_t* run, hawk_val_t* val)
@ -2168,7 +2150,7 @@ void hawk_dprintval (hawk_rtx_t* run, hawk_val_t* val)
case HAWK_VAL_MAP:
hawk_errputstrf (HAWK_T("MAP["));
hawk_htb_walk (((hawk_val_map_t*)val)->map, print_pair, run);
hawk_map_walk (((hawk_val_map_t*)val)->map, print_pair, run);
hawk_errputstrf (HAWK_T("]"));
break;