From e6272bf33ffc120ad50fa2565b6d08d80fc8fed8 Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 30 Mar 2006 23:02:42 +0000 Subject: [PATCH] making better use of macros for speed git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@675 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libstack/osrf_hash.c | 73 ++++++++++++++---------------------------------- src/libstack/osrf_hash.h | 2 ++ src/libstack/osrf_list.c | 12 ++------ src/utils/utils.c | 28 +++++++++---------- 4 files changed, 39 insertions(+), 76 deletions(-) diff --git a/src/libstack/osrf_hash.c b/src/libstack/osrf_hash.c index 69de9e7..778c282 100644 --- a/src/libstack/osrf_hash.c +++ b/src/libstack/osrf_hash.c @@ -4,8 +4,6 @@ osrfHash* osrfNewHash() { osrfHash* hash; OSRF_MALLOC(hash, sizeof(osrfHash)); hash->hash = osrfNewList(); - hash->freeItem = NULL; - hash->size = 0; hash->keys = osrfNewStringArray(64); return hash; } @@ -13,6 +11,7 @@ osrfHash* osrfNewHash() { /* algorithm proposed by Donald E. Knuth * in The Art Of Computer Programming Volume 3 (more or less..)*/ +/* static unsigned int osrfHashMakeKey(char* str) { if(!str) return 0; unsigned int len = strlen(str); @@ -22,45 +21,21 @@ static unsigned int osrfHashMakeKey(char* str) { h = ((h << 5) ^ (h >> 27)) ^ (*str); return (h & (OSRF_HASH_LIST_SIZE-1)); } +*/ -/* -#define OSRF_HASH_MAKE_KEY(str,num) \ - do {\ - unsigned int len = strlen(str); \ - unsigned int h = len;\ - unsigned int i = 0;\ - for(i = 0; i < len; str++, i++)\ - h = ((h << 5) ^ (h >> 27)) ^ (*str);\ - *num = (h & (OSRF_HASH_LIST_SIZE-1));\ - } while(0) - */ - -/* -#define OSRF_HASH_MAKE_KEY(str,num) \ - unsigned int ___len = strlen(str);\ - unsigned int ___h = ___len;\ - unsigned int ___i = 0;\ - for(___i = 0; ___i < ___len; str++, ___i++)\ - ___h = ((___h << 5) ^ (___h >> 27)) ^ (*str);\ - num = (___h & (OSRF_HASH_LIST_SIZE-1));\ - */ - -/* -#define OSRF_HASH_MAKE_KEY(str,num,len) \ - unsigned int __i;\ - num = len;\ - for(__i = 0; __i < len; __i++, str++)\ - num = ((num << 5) ^ (num >> 27)) ^ (*str);\ - num = (num & (OSRF_HASH_LIST_SIZE-1));\ - */ +/* macro version of the above function */ +#define OSRF_HASH_MAKE_KEY(str,num) \ + do {\ + char* __k = str;\ + unsigned int __len = strlen(__k); \ + unsigned int __h = __len;\ + unsigned int __i = 0;\ + for(__i = 0; __i < __len; __k++, __i++)\ + __h = ((__h << 5) ^ (__h >> 27)) ^ (*__k);\ + num = (__h & (OSRF_HASH_LIST_SIZE-1));\ + } while(0) -/* -#define OSRF_HASH_MAKE_KEY(str,num, len) \ - num = osrfHashMakeKey(str);\ - fprintf(stderr, "made num: %d\n", num); - */ - /* returns the index of the item and points l to the sublist the item @@ -70,12 +45,8 @@ static unsigned int osrfHashFindItem( osrfHash* hash, char* key, osrfList** l, o if(!(hash && key)) return -1; - int i = osrfHashMakeKey(key); - - /* - unsigned int i; - OSRF_HASH_MAKE_KEY(key, &i); - */ + unsigned int i = 0; + OSRF_HASH_MAKE_KEY(key,i); osrfList* list = OSRF_LIST_GET_INDEX( hash->hash, i ); if( !list ) { return -1; } @@ -122,13 +93,9 @@ void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... ) { VA_LIST_TO_STRING(key); void* olditem = osrfHashRemove( hash, VA_BUF ); - int bucketkey = osrfHashMakeKey(VA_BUF); - - /* - unsigned int bucketkey; - OSRF_HASH_MAKE_KEY(VA_BUF, &bucketkey); - */ - + unsigned int bucketkey = 0; + OSRF_HASH_MAKE_KEY(VA_BUF,bucketkey); + osrfList* bucket; if( !(bucket = OSRF_LIST_GET_INDEX(hash->hash, bucketkey)) ) { bucket = osrfNewList(); @@ -270,4 +237,6 @@ void osrfHashIteratorReset( osrfHashIterator* itr ) { } - +int osrfHashIteratorHasNext( osrfHashIterator* itr ) { + return ( itr->currentIdx < itr->keys->size ) ? 1 : 0; +} diff --git a/src/libstack/osrf_hash.h b/src/libstack/osrf_hash.h index 7146e13..e5a898e 100644 --- a/src/libstack/osrf_hash.h +++ b/src/libstack/osrf_hash.h @@ -95,6 +95,8 @@ unsigned long osrfHashGetCount( osrfHash* hash ); */ osrfHashIterator* osrfNewHashIterator( osrfHash* hash ); +int osrfHashIteratorHasNext( osrfHashIterator* itr ); + /** Returns the next non-NULL item in the list, return NULL when the end of the list has been reached diff --git a/src/libstack/osrf_list.c b/src/libstack/osrf_list.c index f4f75e4..261e850 100644 --- a/src/libstack/osrf_list.c +++ b/src/libstack/osrf_list.c @@ -1,25 +1,17 @@ #include "osrf_list.h" osrfList* osrfNewList() { - //osrfList* list = safe_malloc(sizeof(osrfList)); osrfList* list; OSRF_MALLOC(list, sizeof(osrfList)); - list->size = 0; - list->freeItem = NULL; list->arrsize = OSRF_LIST_DEFAULT_SIZE; - //list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); OSRF_MALLOC(list->arrlist, list->arrsize * sizeof(void*)); return list; } osrfList* osrfNewListSize( unsigned int size ) { - //osrfList* list = safe_malloc(sizeof(osrfList)); osrfList* list; OSRF_MALLOC(list, sizeof(osrfList)); - list->size = 0; - list->freeItem = NULL; list->arrsize = size; - //list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); OSRF_MALLOC( list->arrlist, list->arrsize * sizeof(void*) ); return list; } @@ -61,8 +53,8 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ) { void* olditem = osrfListRemove( list, position ); list->arrlist[position] = item; - if( list->size == 0 || list->size <= position ) - list->size = position + 1; + //if( list->size == 0 || list->size <= position ) + if( list->size <= position ) list->size = position + 1; return olditem; } diff --git a/src/utils/utils.c b/src/utils/utils.c index 1d347bb..e2943ed 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -309,41 +309,41 @@ char* uescape( const char* string, int size, int full_escape ) { if(full_escape) { switch(c) { case '"': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, '"'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, '"'); break; case '\b': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, 'b'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, 'b'); break; case '\f': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, 'f'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, 'f'); break; case '\t': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, 't'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, 't'); break; case '\n': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, 'n'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, 'n'); break; case '\r': - buffer_add_char(buf, '\\'); - buffer_add_char(buf, 'r'); + OSRF_BUFFER_ADD_CHAR(buf, '\\'); + OSRF_BUFFER_ADD_CHAR(buf, 'r'); break; default: - buffer_add_char(buf, c); + OSRF_BUFFER_ADD_CHAR(buf, c); } } else { - buffer_add_char(buf, c); + OSRF_BUFFER_ADD_CHAR(buf, c); } } -- 2.11.0