/* * category: [algorithm] * apply status: hashset, shm_table, timerqueue, sp_tbs and mod_sipphone * edit status: not * build status: * description: */ #ifndef __HASH_H__ #define __HASH_H__ #pragma once #include "config.h" #ifdef __cplusplus extern "C" { #endif #define HASH32_BUF_INIT 5381 /* * uint32_t * hash32_buf(const void *bf, size_t len, uint32_t hash) * return a 32 bit hash of the binary buffer buf (size len), * seeded with an initial hash value of hash (usually HASH32_BUF_INIT). */ static __inline unsigned int hash32_buf(const void *bf, size_t len, unsigned int hash) { const unsigned char *s = (const unsigned char*)bf; while (len-- != 0) /* "nemesi": k=257, r=r*257 */ hash = hash * 257 + *s++; return (hash * 257); } #define HASH32_STR_INIT 5381 /* * uint32_t * hash32_str(const void *bf, uint32_t hash) * return a 32 bit hash of NUL terminated ASCII string buf, * seeded with an initial hash value of hash (usually HASH32_STR_INIT). */ static __inline unsigned int hash32_str(const void *bf, unsigned int hash) { const unsigned char *s = (const unsigned char*)bf; unsigned char c; while ((c = *s++) != 0) hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */ return (hash + (hash >> 5)); } /* * unsigned int * hash32_strn(const void *bf, size_t len, unsigned int hash) * return a 32 bit hash of NUL terminated ASCII string buf up to * a maximum of len bytes, * seeded with an initial hash value of hash (usually HASH32_STR_INIT). */ static __inline unsigned int hash32_strn(const void *bf, size_t len, unsigned int hash) { const unsigned char *s = (const unsigned char *)bf; unsigned char c; //while ((c = *s++) != 0 && len-- != 0) // xkm@20160824: sonar fix while ((c = *s) != 0 && len != 0) { s++; len--; hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */ } return (hash + (hash >> 5)); } /* used for double hash */ static __inline unsigned int hash_incr(unsigned int h1, unsigned int htable_size) { return 1 + ((h1>>5)+1)%(htable_size-1); } #ifdef __cplusplus } // extern "C" { #endif #endif //__HASH_H__