summaryrefslogtreecommitdiff
path: root/src/game/bg_lib.c
diff options
context:
space:
mode:
authorM. Kristall <mkpdev@gmail.com>2009-10-19 07:09:55 +0000
committerTim Angus <tim@ngus.net>2013-01-03 00:16:56 +0000
commit4bede371a0f04d5edf3bd28f503ccf11cc9c6f47 (patch)
treed2d2dfa738432e3f13893902d5392903cbd13a13 /src/game/bg_lib.c
parentbe79439b479902f5b464511e95c4c5455071e8fa (diff)
* Use binary searching instead of linear searching for many static arrays
Diffstat (limited to 'src/game/bg_lib.c')
-rw-r--r--src/game/bg_lib.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/game/bg_lib.c b/src/game/bg_lib.c
index 1ed9c6b3..7a3afdfb 100644
--- a/src/game/bg_lib.c
+++ b/src/game/bg_lib.c
@@ -2450,4 +2450,26 @@ int sscanf( const char *buffer, const char *fmt, ... )
return count;
}
+void *bsearch( const void *key, const void *base, size_t nmemb, size_t size,
+ cmp_t *compar )
+{
+ size_t low = 0, high = nmemb, mid;
+ int comp;
+ void *ptr;
+
+ while( low <= high )
+ {
+ mid = low + (high - low) / 2;
+ ptr = (void *)((char *)base + ( mid * size ));
+ comp = compar (key, ptr);
+ if( comp < 0 )
+ high = mid - 1;
+ else if( comp > 0 )
+ low = mid + 1;
+ else
+ return ptr;
+ }
+ return NULL;
+}
+
#endif