From dfc5fdfdea1e82245449acd3c3fda12e5a4ae815 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 5 Nov 2012 09:49:54 +0000 Subject: [PATCH] implemented binary symbol search in awk modules --- qse/configure | 2 +- qse/configure.ac | 2 +- qse/lib/awk/std.c | 8 +++----- qse/mod/awk/mpi.c | 32 ++++++++++++++++++++++---------- qse/mod/awk/sys.c | 40 ++++++++++++++++++++++++++++------------ qse/mod/awk/uci.c | 18 ++++++++++++------ 6 files changed, 67 insertions(+), 35 deletions(-) diff --git a/qse/configure b/qse/configure index 9851ddf5..6f9a8e6f 100755 --- a/qse/configure +++ b/qse/configure @@ -16991,7 +16991,7 @@ _ACEOF LIBS="-lltdl $LIBS" else - as_fn_error $? "installed libltdl is too old" "$LINENO" 5 + as_fn_error $? "installed libltdl is too old. install a newer libltdl or specify --with-included-ltdl" "$LINENO" 5 fi LDFLAGS="$save_LDFLAGS" diff --git a/qse/configure.ac b/qse/configure.ac index a8dcffca..db3d0413 100644 --- a/qse/configure.ac +++ b/qse/configure.ac @@ -55,7 +55,7 @@ if test "x$with_included_ltdl" != "xyes"; then CFLAGS="$CFLAGS $LTDLINCL" LDFLAGS="$LDFLAGS $LIBLTDL" AC_CHECK_LIB([ltdl], [lt_dladvise_init], [], - [AC_MSG_ERROR([installed libltdl is too old])]) + [AC_MSG_ERROR([installed libltdl is too old. install a newer libltdl or specify --with-included-ltdl])]) LDFLAGS="$save_LDFLAGS" CFLAGS="$save_CFLAGS" fi diff --git a/qse/lib/awk/std.c b/qse/lib/awk/std.c index 1d710045..d5336367 100644 --- a/qse/lib/awk/std.c +++ b/qse/lib/awk/std.c @@ -2500,13 +2500,11 @@ static int add_globals (qse_awk_t* awk) xtn->gbl_argc = add_global (awk, QSE_T("ARGC"), 4); xtn->gbl_argv = add_global (awk, QSE_T("ARGV"), 4); - - if (xtn->gbl_argc <= -1 || xtn->gbl_argv <= -1) return -1; - xtn->gbl_environ = add_global (awk, QSE_T("ENVIRON"), 7); - if (xtn->gbl_environ <= -1) return -1; - return 0; + return (xtn->gbl_argc <= -1 || + xtn->gbl_argv <= -1 || + xtn->gbl_environ <= -1)? -1: 0; } struct fnctab_t diff --git a/qse/mod/awk/mpi.c b/qse/mod/awk/mpi.c index 4a82ca73..7632f097 100644 --- a/qse/mod/awk/mpi.c +++ b/qse/mod/awk/mpi.c @@ -174,28 +174,40 @@ static inttab_t inttab[] = static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym) { qse_cstr_t ea; - int i; + int left, right, mid, n; -/* TODO: binary search or something better */ - for (i = 0; i < QSE_COUNTOF(fnctab); i++) + left = 0; right = QSE_COUNTOF(fnctab) - 1; + + while (left <= right) { - if (qse_strcmp (fnctab[i].name, name) == 0) + mid = (left + right) / 2; + + n = qse_strcmp (fnctab[mid].name, name); + if (n > 0) right = mid - 1; + else if (n < 0) left = mid + 1; + else { sym->type = QSE_AWK_MOD_FNC; - sym->u.fnc = fnctab[i].info; + sym->u.fnc = fnctab[mid].info; return 0; } - } + } - for (i = 0; i < QSE_COUNTOF(inttab); i++) + left = 0; right = QSE_COUNTOF(inttab) - 1; + while (left <= right) { - if (qse_strcmp (inttab[i].name, name) == 0) + mid = (left + right) / 2; + + n = qse_strcmp (inttab[mid].name, name); + if (n > 0) right = mid - 1; + else if (n < 0) left = mid + 1; + else { sym->type = QSE_AWK_MOD_INT; - sym->u.in = inttab[i].info; + sym->u.in = inttab[mid].info; return 0; } - } + } ea.ptr = name; ea.len = qse_strlen(name); diff --git a/qse/mod/awk/sys.c b/qse/mod/awk/sys.c index bc648b70..990e59c6 100644 --- a/qse/mod/awk/sys.c +++ b/qse/mod/awk/sys.c @@ -454,6 +454,8 @@ struct inttab_t static fnctab_t fnctab[] = { + /* keep this table sorted for binary search in query(). */ + { QSE_T("fork"), { { 0, 0, QSE_NULL }, fnc_fork, 0 } }, { QSE_T("getegid"), { { 0, 0, QSE_NULL }, fnc_getegid, 0 } }, { QSE_T("getenv"), { { 1, 1, QSE_NULL }, fnc_getenv, 0 } }, @@ -498,14 +500,16 @@ static fnctab_t fnctab[] = static inttab_t inttab[] = { + /* keep this table sorted for binary search in query(). */ + { QSE_T("SIGABRT"), { SIGABRT } }, { QSE_T("SIGALRM"), { SIGALRM } }, { QSE_T("SIGHUP"), { SIGHUP } }, { QSE_T("SIGINT"), { SIGINT } }, { QSE_T("SIGKILL"), { SIGKILL } }, + { QSE_T("SIGQUIT"), { SIGQUIT } }, { QSE_T("SIGSEGV"), { SIGSEGV } }, - { QSE_T("SIGTERM"), { SIGTERM } }, - { QSE_T("SIGQUIT"), { SIGQUIT } } + { QSE_T("SIGTERM"), { SIGTERM } } /* { QSE_T("WNOHANG"), { WNOHANG } }, @@ -515,28 +519,40 @@ static inttab_t inttab[] = static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym) { qse_cstr_t ea; - int i; + int left, right, mid, n; -/* TODO: binary search or something better */ - for (i = 0; i < QSE_COUNTOF(fnctab); i++) + left = 0; right = QSE_COUNTOF(fnctab) - 1; + + while (left <= right) { - if (qse_strcmp (fnctab[i].name, name) == 0) + mid = (left + right) / 2; + + n = qse_strcmp (fnctab[mid].name, name); + if (n > 0) right = mid - 1; + else if (n < 0) left = mid + 1; + else { sym->type = QSE_AWK_MOD_FNC; - sym->u.fnc = fnctab[i].info; + sym->u.fnc = fnctab[mid].info; return 0; } - } + } - for (i = 0; i < QSE_COUNTOF(inttab); i++) + left = 0; right = QSE_COUNTOF(inttab) - 1; + while (left <= right) { - if (qse_strcmp (inttab[i].name, name) == 0) + mid = (left + right) / 2; + + n = qse_strcmp (inttab[mid].name, name); + if (n > 0) right = mid - 1; + else if (n < 0) left = mid + 1; + else { sym->type = QSE_AWK_MOD_INT; - sym->u.in = inttab[i].info; + sym->u.in = inttab[mid].info; return 0; } - } + } ea.ptr = name; ea.len = qse_strlen(name); diff --git a/qse/mod/awk/uci.c b/qse/mod/awk/uci.c index 90f6bc68..bd08c200 100644 --- a/qse/mod/awk/uci.c +++ b/qse/mod/awk/uci.c @@ -1233,18 +1233,24 @@ static fnctab_t fnctab[] = static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym) { qse_cstr_t ea; - int i; + int left, right, mid, n; -/* TODO: binary search */ - for (i = 0; i < QSE_COUNTOF(fnctab); i++) + left = 0; right = QSE_COUNTOF(fnctab) - 1; + + while (left <= right) { - if (qse_strcmp (fnctab[i].name, name) == 0) + mid = (left + right) / 2; + + n = qse_strcmp (fnctab[mid].name, name); + if (n > 0) right = mid - 1; + else if (n < 0) left = mid + 1; + else { sym->type = QSE_AWK_MOD_FNC; - sym->u.fnc = fnctab[i].info; + sym->u.fnc = fnctab[mid].info; return 0; } - } + } ea.ptr = name; ea.len = qse_strlen(name);