OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 22 | 2 ** 2001 September 22 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 } | 48 } |
49 pH->count = 0; | 49 pH->count = 0; |
50 } | 50 } |
51 | 51 |
52 /* | 52 /* |
53 ** The hashing function. | 53 ** The hashing function. |
54 */ | 54 */ |
55 static unsigned int strHash(const char *z){ | 55 static unsigned int strHash(const char *z){ |
56 unsigned int h = 0; | 56 unsigned int h = 0; |
57 unsigned char c; | 57 unsigned char c; |
58 while( (c = (unsigned char)*z++)!=0 ){ | 58 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/ |
59 h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; | 59 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510). |
| 60 ** 0x9e3779b1 is 2654435761 which is the closest prime number to |
| 61 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */ |
| 62 h += sqlite3UpperToLower[c]; |
| 63 h *= 0x9e3779b1; |
60 } | 64 } |
61 return h; | 65 return h; |
62 } | 66 } |
63 | 67 |
64 | 68 |
65 /* Link pNew element into the hash table pH. If pEntry!=0 then also | 69 /* Link pNew element into the hash table pH. If pEntry!=0 then also |
66 ** insert pNew into the pEntry hash bucket. | 70 ** insert pNew into the pEntry hash bucket. |
67 */ | 71 */ |
68 static void insertElement( | 72 static void insertElement( |
69 Hash *pH, /* The complete hash table */ | 73 Hash *pH, /* The complete hash table */ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 */ | 145 */ |
142 static HashElem *findElementWithHash( | 146 static HashElem *findElementWithHash( |
143 const Hash *pH, /* The pH to be searched */ | 147 const Hash *pH, /* The pH to be searched */ |
144 const char *pKey, /* The key we are searching for */ | 148 const char *pKey, /* The key we are searching for */ |
145 unsigned int *pHash /* Write the hash value here */ | 149 unsigned int *pHash /* Write the hash value here */ |
146 ){ | 150 ){ |
147 HashElem *elem; /* Used to loop thru the element list */ | 151 HashElem *elem; /* Used to loop thru the element list */ |
148 int count; /* Number of elements left to test */ | 152 int count; /* Number of elements left to test */ |
149 unsigned int h; /* The computed hash */ | 153 unsigned int h; /* The computed hash */ |
150 | 154 |
151 if( pH->ht ){ | 155 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ |
152 struct _ht *pEntry; | 156 struct _ht *pEntry; |
153 h = strHash(pKey) % pH->htsize; | 157 h = strHash(pKey) % pH->htsize; |
154 pEntry = &pH->ht[h]; | 158 pEntry = &pH->ht[h]; |
155 elem = pEntry->chain; | 159 elem = pEntry->chain; |
156 count = pEntry->count; | 160 count = pEntry->count; |
157 }else{ | 161 }else{ |
158 h = 0; | 162 h = 0; |
159 elem = pH->first; | 163 elem = pH->first; |
160 count = pH->count; | 164 count = pH->count; |
161 } | 165 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 pH->count++; | 262 pH->count++; |
259 if( pH->count>=10 && pH->count > 2*pH->htsize ){ | 263 if( pH->count>=10 && pH->count > 2*pH->htsize ){ |
260 if( rehash(pH, pH->count*2) ){ | 264 if( rehash(pH, pH->count*2) ){ |
261 assert( pH->htsize>0 ); | 265 assert( pH->htsize>0 ); |
262 h = strHash(pKey) % pH->htsize; | 266 h = strHash(pKey) % pH->htsize; |
263 } | 267 } |
264 } | 268 } |
265 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); | 269 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); |
266 return 0; | 270 return 0; |
267 } | 271 } |
OLD | NEW |