[Assorted-commits] SF.net SVN: assorted: [353] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-10 18:44:40
|
Revision: 353 http://assorted.svn.sourceforge.net/assorted/?rev=353&view=rev Author: yangzhang Date: 2008-02-10 10:44:43 -0800 (Sun, 10 Feb 2008) Log Message: ----------- added hash functions Modified Paths: -------------- cpp-commons/trunk/src/commons/cppcommons.cpp Added Paths: ----------- cpp-commons/trunk/src/commons/hash.h Modified: cpp-commons/trunk/src/commons/cppcommons.cpp =================================================================== --- cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:37:27 UTC (rev 352) +++ cpp-commons/trunk/src/commons/cppcommons.cpp 2008-02-10 18:44:43 UTC (rev 353) @@ -26,6 +26,7 @@ #include "commons/check.h" #include "commons/cpuid.h" #include "commons/files.h" +#include "commons/hash.h" #include "commons/strings.h" #include "commons/time.h" #include "commons/threads.h" Added: cpp-commons/trunk/src/commons/hash.h =================================================================== --- cpp-commons/trunk/src/commons/hash.h (rev 0) +++ cpp-commons/trunk/src/commons/hash.h 2008-02-10 18:44:43 UTC (rev 353) @@ -0,0 +1,45 @@ +#ifndef _COMMONS_HASH_H +#define _COMMONS_HASH_H + +namespace commons +{ + + /** + * From libstdc++ 4.1 __stl_hash_string. + */ + inline size_t + hash_stl(const char* s) + { + unsigned long h = 0; + for ( ; *s; ++s) + h = 5 * h + *s; + return size_t(h); + } + + /** + * From http://www.cse.yorku.ca/~oz/hash.html. + */ + inline size_t + hash_djb2(const char* s) + { + unsigned long h = 5381; + for (; *s; ++s) + h = ((h << 5) + h) + *s; + return size_t(h); + } + + /** + * From Sun JDK6 String.hashCode. + */ + inline size_t + hash_java(const char* s) + { + unsigned long h = 0; + for (; *s; ++s) + h = 31 * h + x; + return size_t(h); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |