From 35c8cfbd72a28c118ad95791ccff57bb8916d4d5 Mon Sep 17 00:00:00 2001 From: divverent Date: Sat, 19 Jan 2008 01:46:58 +0000 Subject: [PATCH] Fix bug in stringbuffers with bufstr_set not updating num_strings; add a new builtin for QC called float hash(float caseinsensitive, string str) that returns the CRC of the string (possibly after lowercasing). Using this, a persistent database can be implemented quite efficiently (for a 1MB database file, it takes about 0.1 seconds to load and 0.3 seconds to save, which is quite acceptable). From QC, I use a string buffer of 16384 strings, indexed by hash of the key, containing an info string at each index to solve the collision issue. Possibly add hash() to the stringbuffer DP extension, or make a new extension for it? git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7979 d7cf8633-e32d-0410-b094-e92efae38249 --- prvm_cmds.c | 14 +++++++++++++- prvm_cmds.h | 2 ++ svvm_cmds.c | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/prvm_cmds.c b/prvm_cmds.c index a3505fe8..c3292266 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -3916,6 +3916,7 @@ void VM_bufstr_set (void) } BufStr_Expand(stringbuffer, strindex); + stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1); if(stringbuffer->strings[strindex]) Mem_Free(stringbuffer->strings[strindex]); @@ -3964,7 +3965,7 @@ void VM_bufstr_add (void) } order = (int)PRVM_G_FLOAT(OFS_PARM2); if(order) - strindex = stringbuffer->num_strings++; + strindex = stringbuffer->num_strings; else for (strindex = 0;strindex < stringbuffer->num_strings;strindex++) if (stringbuffer->strings[strindex] == NULL) @@ -4452,6 +4453,17 @@ void VM_strncasecmp (void) } } +// #487 float(float caseinsensitive, string s, ...) hash +void VM_hash(void) +{ + float insensitive; + static char s[VM_STRINGTEMP_LENGTH]; + VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash); + insensitive = PRVM_G_FLOAT(OFS_PARM0); + VM_VarString(1, s, sizeof(s)); + PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s))); +} + void VM_wasfreed (void) { VM_SAFEPARMCOUNT(1, VM_wasfreed); diff --git a/prvm_cmds.h b/prvm_cmds.h index 85f8e876..33eedb55 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -404,6 +404,8 @@ void VM_wasfreed (void); void VM_strreplace (void); void VM_strireplace (void); +void VM_hash(void); + void VM_SetTraceGlobals(const trace_t *trace); void VM_Cmd_Init(void); diff --git a/svvm_cmds.c b/svvm_cmds.c index 45e5adcc..74e0e478 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -3296,7 +3296,7 @@ VM_SV_pointsound, // #483 void(vector origin, string sample, float volume, fl VM_strreplace, // #484 string(string search, string replace, string subject) strreplace (DP_QC_STRREPLACE) VM_strireplace, // #485 string(string search, string replace, string subject) strireplace (DP_QC_STRREPLACE) VM_SV_getsurfacepointattribute,// #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486; -NULL, // #487 +VM_hash, // #487 float(float caseinsensitive, string s, ...) hash = #487; NULL, // #488 NULL, // #489 NULL, // #490 -- 2.39.2