mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-03-20 01:11:18 +00:00
[ruamoko] Add some stdlib function wrappers
For now, just bsearch (normal and fuzzy), qsort, and prefixsum (not in C's stdlib that I know of, but I think having native implementations of float and int prefix sums will be useful.
This commit is contained in:
parent
d98c92a5c4
commit
5ca792c40e
7 changed files with 205 additions and 0 deletions
|
@ -55,6 +55,8 @@ void RUA_Script_Init (progs_t *pr, int secure);
|
|||
|
||||
void RUA_Set_Init (progs_t *pr, int secure);
|
||||
|
||||
void RUA_Stdlib_Init (struct progs_s *pr, int secure);
|
||||
|
||||
void RUA_String_Init (struct progs_s *pr, int secure);
|
||||
|
||||
void RUA_QFile_Init (struct progs_s *pr, int secure);
|
||||
|
|
|
@ -23,4 +23,5 @@ libs_ruamoko_libQFruamoko_la_SOURCES= \
|
|||
libs/ruamoko/rua_runtime.c \
|
||||
libs/ruamoko/rua_script.c \
|
||||
libs/ruamoko/rua_set.c \
|
||||
libs/ruamoko/rua_stdlib.c \
|
||||
libs/ruamoko/rua_string.c
|
||||
|
|
172
libs/ruamoko/rua_stdlib.c
Normal file
172
libs/ruamoko/rua_stdlib.c
Normal file
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
bi_stdlib.c
|
||||
|
||||
QuakeC stdlib api
|
||||
|
||||
Copyright (C) 2021 Bill Currie
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2021/5/31
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
|
||||
See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to:
|
||||
|
||||
Free Software Foundation, Inc.
|
||||
59 Temple Place - Suite 330
|
||||
Boston, MA 02111-1307, USA
|
||||
|
||||
*/
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
#define _GNU_SOURCE // for qsort_r
|
||||
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "compat.h"
|
||||
#include "qfalloca.h"
|
||||
|
||||
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#include "QF/fbsearch.h"
|
||||
#include "QF/progs.h"
|
||||
|
||||
#include "rua_internal.h"
|
||||
|
||||
typedef struct {
|
||||
progs_t *pr;
|
||||
func_t func;
|
||||
} function_t;
|
||||
|
||||
static int
|
||||
int_compare (const void *_a, const void *_b)
|
||||
{
|
||||
const int *a = _a;
|
||||
const int *b = _b;
|
||||
return *a - *b;
|
||||
}
|
||||
|
||||
static int
|
||||
rua_compare (const void *a, const void *b, void *_f)
|
||||
{
|
||||
function_t *f = _f;
|
||||
|
||||
PR_RESET_PARAMS (f->pr);
|
||||
P_POINTER (f->pr, 0) = PR_SetPointer (f->pr, a);
|
||||
P_POINTER (f->pr, 1) = PR_SetPointer (f->pr, b);
|
||||
f->pr->pr_argc = 2;
|
||||
PR_ExecuteProgram (f->pr, f->func);
|
||||
return R_INT (f->pr);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_bsearch (progs_t *pr)
|
||||
{
|
||||
const void *key = P_GPOINTER (pr, 0);
|
||||
const void *array = P_GPOINTER (pr, 1);
|
||||
size_t nmemb = P_INT (pr, 2);
|
||||
size_t size = P_INT (pr, 3) * sizeof (pr_int_t);
|
||||
func_t cmp = P_FUNCTION (pr, 4);
|
||||
void *p = 0;
|
||||
|
||||
if (!cmp) {
|
||||
p = bsearch (key, array, nmemb, size, int_compare);
|
||||
} else {
|
||||
function_t func = { pr, cmp };
|
||||
p = bsearch_r (key, array, nmemb, size, rua_compare, &func);
|
||||
}
|
||||
RETURN_POINTER (pr, p);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_fbsearch (progs_t *pr)
|
||||
{
|
||||
const void *key = P_GPOINTER (pr, 0);
|
||||
const void *array = P_GPOINTER (pr, 1);
|
||||
size_t nmemb = P_INT (pr, 2);
|
||||
size_t size = P_INT (pr, 3) * sizeof (pr_int_t);
|
||||
func_t cmp = P_FUNCTION (pr, 4);
|
||||
void *p = 0;
|
||||
|
||||
if (!cmp) {
|
||||
p = fbsearch (key, array, nmemb, size, int_compare);
|
||||
} else {
|
||||
function_t func = { pr, cmp };
|
||||
p = fbsearch_r (key, array, nmemb, size, rua_compare, &func);
|
||||
}
|
||||
RETURN_POINTER (pr, p);
|
||||
}
|
||||
|
||||
static void
|
||||
bi_qsort (progs_t *pr)
|
||||
{
|
||||
void *array = P_GPOINTER (pr, 0);
|
||||
size_t nmemb = P_INT (pr, 1);
|
||||
size_t size = P_INT (pr, 2) * sizeof (pr_int_t);
|
||||
func_t cmp = P_FUNCTION (pr, 3);
|
||||
|
||||
if (!cmp) {
|
||||
qsort (array, nmemb, size, int_compare);
|
||||
} else {
|
||||
function_t func = { pr, cmp };
|
||||
qsort_r (array, nmemb, size, rua_compare, &func);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bi_prefixsumi (progs_t *pr)
|
||||
{
|
||||
int *array = (int *) P_GPOINTER (pr, 0);
|
||||
int count = P_INT (pr, 1);
|
||||
|
||||
for (int i = 1; i < count; i++) {
|
||||
array[i] += array[i - 1];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
bi_prefixsumf (progs_t *pr)
|
||||
{
|
||||
float *array = (float *) P_GPOINTER (pr, 0);
|
||||
int count = P_INT (pr, 1);
|
||||
|
||||
for (int i = 1; i < count; i++) {
|
||||
array[i] += array[i - 1];
|
||||
}
|
||||
}
|
||||
|
||||
static builtin_t builtins[] = {
|
||||
{"bsearch", bi_bsearch, -1},
|
||||
{"fbsearch", bi_fbsearch, -1},
|
||||
{"qsort", bi_qsort, -1},
|
||||
{"prefixsum|^ii", bi_prefixsumi, -1},
|
||||
{"prefixsum|^fi", bi_prefixsumf, -1},
|
||||
{0}
|
||||
};
|
||||
|
||||
void
|
||||
RUA_Stdlib_Init (progs_t *pr, int secure)
|
||||
{
|
||||
PR_RegisterBuiltins (pr, builtins);
|
||||
}
|
|
@ -16,6 +16,7 @@ ruamoko_include = \
|
|||
ruamoko/include/server.h \
|
||||
ruamoko/include/sound.h \
|
||||
ruamoko/include/script.h \
|
||||
ruamoko/include/stdlib.h \
|
||||
ruamoko/include/string.h \
|
||||
ruamoko/include/sv_sound.h \
|
||||
ruamoko/include/system.h \
|
||||
|
|
17
ruamoko/include/stdlib.h
Normal file
17
ruamoko/include/stdlib.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef __stdlib_h
|
||||
#define __stdlib_h
|
||||
|
||||
//FIXME add const to qfcc?
|
||||
// If compare is nil, the value pointed to by key and the first member of the
|
||||
// elements in the array are treated as ints, making for fast searches when the
|
||||
// first member of the element is the key and is a simple int
|
||||
void *bsearch (void *key, void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b));
|
||||
void *fbsearch (void *key, void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b));
|
||||
void *qsort (void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b));
|
||||
@overload void prefixsum (int *array, int count);
|
||||
@overload void prefixsum (float *array, int count);
|
||||
|
||||
#endif//__stdlib_h
|
|
@ -17,6 +17,7 @@ ruamoko_lib_libr_a_SOURCES=\
|
|||
ruamoko/lib/qfs.r \
|
||||
ruamoko/lib/script.r \
|
||||
ruamoko/lib/sound.r \
|
||||
ruamoko/lib/stdlib.r \
|
||||
ruamoko/lib/string.r \
|
||||
ruamoko/lib/math.r \
|
||||
ruamoko/lib/types.r \
|
||||
|
|
11
ruamoko/lib/stdlib.r
Normal file
11
ruamoko/lib/stdlib.r
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
void *bsearch (void *key, void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b)) = #0;
|
||||
void *fbsearch (void *key, void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b)) = #0;
|
||||
void *qsort (void *array, int nmemb, int size,
|
||||
int (*compare) (void *a, void *b)) = #0;
|
||||
|
||||
void prefixsum (int *array, int count) = #0;
|
||||
void prefixsum (float *array, int count) = #0;
|
Loading…
Reference in a new issue