zorba-coders team mailing list archive
-
zorba-coders team
-
Mailing list archive
-
Message #20696
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
Paul J. Lucas has proposed merging lp:~paul-lucas/zorba/pjl-misc into lp:zorba.
Commit message:
Added hash_c_str() and hash<char const*>.
Requested reviews:
Paul J. Lucas (paul-lucas)
For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/159290
Added hash_c_str() and hash<char const*>.
--
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/159290
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/util/hash/hash.cpp'
--- src/util/hash/hash.cpp 2013-02-07 17:24:36 +0000
+++ src/util/hash/hash.cpp 2013-04-17 04:58:28 +0000
@@ -31,15 +31,15 @@
///////////////////////////////////////////////////////////////////////////////
size_t hash_bytes( void const *p, size_t len, size_t result ) {
- unsigned char const *c = reinterpret_cast<unsigned char const*>( p );
- unsigned char const *const end = c + len;
+ unsigned char const *u = reinterpret_cast<unsigned char const*>( p );
+ unsigned char const *const end = u + len;
#ifdef ZORBA_HASH_FN_FNV_1a
//
// FNV-1a (Fowler/Noll/Vo) hashing algorithm.
//
- while ( c < end ) {
+ while ( u < end ) {
result *= Hash_Prime;
- result ^= *c++;
+ result ^= *u++;
}
#endif /* ZORBA_HASH_FN_FNV_1a */
#ifdef ZORBA_HASH_FN_PJW
@@ -61,8 +61,32 @@
static size_t const OneEighth = BitsInSizeT / 8;
static size_t const HighBits = ~( (size_t)(~0ul) >> OneEighth );
- while ( c < end ) {
- result = (result << OneEighth) + *c++;
+ while ( u < end ) {
+ result = (result << OneEighth) + *u++;
+ if ( size_t temp = result & HighBits )
+ result = (result ^ (temp >> ThreeFourths)) & ~HighBits;
+ }
+#endif /* ZORBA_HASH_FN_PJW */
+ return result;
+}
+
+size_t hash_c_str( char const *s, size_t result ) {
+ unsigned char const *u = reinterpret_cast<unsigned char const*>( s );
+#ifdef ZORBA_HASH_FN_FNV_1a
+ while ( *u ) {
+ result *= Hash_Prime;
+ result ^= *u++;
+ }
+#endif /* ZORBA_HASH_FN_FNV_1a */
+#ifdef ZORBA_HASH_FN_PJW
+ static size_t const BitsInSizeT = sizeof( size_t ) *
+ numeric_limits<unsigned char>::digits;
+ static size_t const ThreeFourths = BitsInSizeT * 3 / 4;
+ static size_t const OneEighth = BitsInSizeT / 8;
+ static size_t const HighBits = ~( (size_t)(~0ul) >> OneEighth );
+
+ while ( *u ) {
+ result = (result << OneEighth) + *u++;
if ( size_t temp = result & HighBits )
result = (result ^ (temp >> ThreeFourths)) & ~HighBits;
}
=== modified file 'src/util/hash/hash.h'
--- src/util/hash/hash.h 2013-02-07 17:24:36 +0000
+++ src/util/hash/hash.h 2013-04-17 04:58:28 +0000
@@ -79,6 +79,31 @@
return hash_bytes( &v, sizeof( v ) );
}
+/**
+ * Generic hash function that hashes a null-terminated C string.
+ *
+ * @param s A pointer to the C string to hash.
+ * @param init The initialization value.
+ * @return Returns the hash code for the given C string.
+ */
+size_t hash_c_str( char const *s, size_t init = Hash_Init );
+
+/**
+ * The generic %hash unary_function class.
+ *
+ * @tparam T The type to hash.
+ */
+template<typename T>
+struct hash : std::unary_function<T,size_t> {
+ size_t operator()( T ) const; // not defined
+};
+
+/** Specialization for \c char*. */
+template<> inline
+size_t hash<char const*>::operator()( char const *s ) const {
+ return hash_c_str( s );
+}
+
} // namespace ztd
} // namespace zorba
Follow ups
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: noreply, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Zorba Build Bot, 2013-04-18
-
Re: [Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Matthias Brantner, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Zorba Build Bot, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Paul J. Lucas, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Zorba Build Bot, 2013-04-18
-
Re: [Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Zorba Build Bot, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Zorba Build Bot, 2013-04-18
-
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Paul J. Lucas, 2013-04-18
-
Re: [Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Matthias Brantner, 2013-04-17
-
Re: [Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
From: Paul J. Lucas, 2013-04-17