Merge lp:~paul-lucas/zorba/pjl-misc into lp:zorba

Proposed by Paul J. Lucas
Status: Merged
Approved by: Paul J. Lucas
Approved revision: 11187
Merged at revision: 11386
Proposed branch: lp:~paul-lucas/zorba/pjl-misc
Merge into: lp:zorba
Diff against target: 178 lines (+80/-33)
4 files modified
src/store/naive/json_items.h (+3/-27)
src/util/hash/hash.cpp (+30/-6)
src/util/hash/hash.h (+25/-0)
src/util/stl_util.h (+22/-0)
To merge this branch: bzr merge lp:~paul-lucas/zorba/pjl-misc
Reviewer Review Type Date Requested Status
Matthias Brantner Approve
Paul J. Lucas Approve
Review via email: mp+159290@code.launchpad.net

Commit message

Added hash_c_str() and hash<char const*>.

Description of the change

Added hash_c_str() and hash<char const*>.

To post a comment you must log in.
Revision history for this message
Paul J. Lucas (paul-lucas) :
review: Approve
Revision history for this message
Matthias Brantner (matthias-brantner) wrote :

Please use the functions for the unordered_map in json_items.h.

review: Needs Fixing
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

The attempt to merge lp:~paul-lucas/zorba/pjl-misc into lp:zorba failed. Below is the output from the failed tests.

CMake Error at /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake:275 (message):
  Validation queue job pjl-misc-2013-04-18T01-59-51.531Z is finished. The
  final status was:

  Undetermined, probably an error - please email <email address hidden> with the
  number of this job!

Error in read script: /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake

lp:~paul-lucas/zorba/pjl-misc updated
11187. By Paul J. Lucas

Side merge from bug-1131990 branch since trunk is broken.

Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :
Revision history for this message
Matthias Brantner (matthias-brantner) :
review: Approve
Revision history for this message
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job pjl-misc-2013-04-18T03-02-04.504Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/store/naive/json_items.h'
2--- src/store/naive/json_items.h 2013-04-04 01:37:05 +0000
3+++ src/store/naive/json_items.h 2013-04-18 02:12:27 +0000
4@@ -20,6 +20,7 @@
5 #include <vector>
6
7 #include <zorba/config.h>
8+#include "util/stl_util.h"
9 #include "util/unordered_map.h"
10
11 #include "diagnostics/assert.h"
12@@ -208,36 +209,11 @@
13 class SimpleJSONObject : public JSONObject
14 {
15 protected:
16- class ConstCharStarHash
17- {
18- public:
19- typedef size_t result_type;
20- size_t operator()(const char* a) const
21- {
22- size_t hash = 5381;
23- int c;
24-
25- while ((c = *a++))
26- hash = ((hash << 5) + hash) + c;
27-
28- return hash;
29- }
30- };
31-
32- class ConstCharStarComparator
33- {
34- public:
35- bool operator()(const char* a, const char* b) const
36- {
37- return strcmp(a, b) == 0;
38- }
39- };
40-
41 typedef std::unordered_map<
42 const char*,
43 csize,
44- ConstCharStarHash,
45- ConstCharStarComparator> Keys;
46+ ztd::hash<char const*>,
47+ ztd::equal_to<char const*> > Keys;
48
49 typedef std::vector<std::pair<store::Item*, store::Item*> > Pairs;
50
51
52=== modified file 'src/util/hash/hash.cpp'
53--- src/util/hash/hash.cpp 2013-02-07 17:24:36 +0000
54+++ src/util/hash/hash.cpp 2013-04-18 02:12:27 +0000
55@@ -31,15 +31,15 @@
56 ///////////////////////////////////////////////////////////////////////////////
57
58 size_t hash_bytes( void const *p, size_t len, size_t result ) {
59- unsigned char const *c = reinterpret_cast<unsigned char const*>( p );
60- unsigned char const *const end = c + len;
61+ unsigned char const *u = reinterpret_cast<unsigned char const*>( p );
62+ unsigned char const *const end = u + len;
63 #ifdef ZORBA_HASH_FN_FNV_1a
64 //
65 // FNV-1a (Fowler/Noll/Vo) hashing algorithm.
66 //
67- while ( c < end ) {
68+ while ( u < end ) {
69 result *= Hash_Prime;
70- result ^= *c++;
71+ result ^= *u++;
72 }
73 #endif /* ZORBA_HASH_FN_FNV_1a */
74 #ifdef ZORBA_HASH_FN_PJW
75@@ -61,8 +61,32 @@
76 static size_t const OneEighth = BitsInSizeT / 8;
77 static size_t const HighBits = ~( (size_t)(~0ul) >> OneEighth );
78
79- while ( c < end ) {
80- result = (result << OneEighth) + *c++;
81+ while ( u < end ) {
82+ result = (result << OneEighth) + *u++;
83+ if ( size_t temp = result & HighBits )
84+ result = (result ^ (temp >> ThreeFourths)) & ~HighBits;
85+ }
86+#endif /* ZORBA_HASH_FN_PJW */
87+ return result;
88+}
89+
90+size_t hash_c_str( char const *s, size_t result ) {
91+ unsigned char const *u = reinterpret_cast<unsigned char const*>( s );
92+#ifdef ZORBA_HASH_FN_FNV_1a
93+ while ( *u ) {
94+ result *= Hash_Prime;
95+ result ^= *u++;
96+ }
97+#endif /* ZORBA_HASH_FN_FNV_1a */
98+#ifdef ZORBA_HASH_FN_PJW
99+ static size_t const BitsInSizeT = sizeof( size_t ) *
100+ numeric_limits<unsigned char>::digits;
101+ static size_t const ThreeFourths = BitsInSizeT * 3 / 4;
102+ static size_t const OneEighth = BitsInSizeT / 8;
103+ static size_t const HighBits = ~( (size_t)(~0ul) >> OneEighth );
104+
105+ while ( *u ) {
106+ result = (result << OneEighth) + *u++;
107 if ( size_t temp = result & HighBits )
108 result = (result ^ (temp >> ThreeFourths)) & ~HighBits;
109 }
110
111=== modified file 'src/util/hash/hash.h'
112--- src/util/hash/hash.h 2013-02-07 17:24:36 +0000
113+++ src/util/hash/hash.h 2013-04-18 02:12:27 +0000
114@@ -79,6 +79,31 @@
115 return hash_bytes( &v, sizeof( v ) );
116 }
117
118+/**
119+ * Generic hash function that hashes a null-terminated C string.
120+ *
121+ * @param s A pointer to the C string to hash.
122+ * @param init The initialization value.
123+ * @return Returns the hash code for the given C string.
124+ */
125+size_t hash_c_str( char const *s, size_t init = Hash_Init );
126+
127+/**
128+ * The generic %hash unary_function class.
129+ *
130+ * @tparam T The type to hash.
131+ */
132+template<typename T>
133+struct hash : std::unary_function<T,size_t> {
134+ size_t operator()( T ) const; // not defined
135+};
136+
137+/** Specialization for <code>char const*</code>. */
138+template<> inline
139+size_t hash<char const*>::operator()( char const *s ) const {
140+ return hash_c_str( s );
141+}
142+
143 } // namespace ztd
144 } // namespace zorba
145
146
147=== modified file 'src/util/stl_util.h'
148--- src/util/stl_util.h 2013-03-22 19:06:19 +0000
149+++ src/util/stl_util.h 2013-04-18 02:12:27 +0000
150@@ -93,6 +93,28 @@
151 ///////////////////////////////////////////////////////////////////////////////
152
153 /**
154+ * Copy of std::equal_to in this namespace so we can specialize it below.
155+ */
156+template<typename T>
157+struct equal_to : std::binary_function<T,T,bool> {
158+ bool operator()( T const &a, T const &b ) const {
159+ return a == b;
160+ }
161+};
162+
163+/**
164+ * Specialization of std::equal_to for C strings.
165+ */
166+template<>
167+struct equal_to<char const*> :
168+ std::binary_function<char const*,char const*,bool>
169+{
170+ bool operator()( char const *s1, char const *s2 ) const {
171+ return std::strcmp( s1, s2 ) == 0;
172+ }
173+};
174+
175+/**
176 * Implementation of SGI's %identity extension.
177 * See: http://www.sgi.com/tech/stl/identity.html
178 */

Subscribers

People subscribed via source and target branches