drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2001 September 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This is the implementation of generic hash-tables |
| 13 | ** used in SQLite. |
| 14 | ** |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 15 | ** $Id: hash.c,v 1.35 2009/04/28 15:43:45 drh Exp $ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | #include <assert.h> |
| 19 | |
| 20 | /* Turn bulk memory into a hash table object by initializing the |
| 21 | ** fields of the Hash structure. |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 22 | ** |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 23 | ** "pNew" is a pointer to the hash table that is to be initialized. |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 24 | ** "copyKey" is true if the hash table should make its own private |
| 25 | ** copy of keys and false if it should just use the supplied pointer. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 26 | */ |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 27 | void sqlite3HashInit(Hash *pNew, int copyKey){ |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 28 | assert( pNew!=0 ); |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 29 | pNew->copyKey = copyKey!=0; |
drh | 76d7f8b | 2004-06-30 22:43:21 +0000 | [diff] [blame] | 30 | pNew->first = 0; |
| 31 | pNew->count = 0; |
| 32 | pNew->htsize = 0; |
| 33 | pNew->ht = 0; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* Remove all entries from a hash table. Reclaim all memory. |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 37 | ** Call this routine to delete a hash table or to reset a hash table |
| 38 | ** to the empty state. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 39 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 40 | void sqlite3HashClear(Hash *pH){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 41 | HashElem *elem; /* For looping over all elements of the table */ |
| 42 | |
| 43 | assert( pH!=0 ); |
| 44 | elem = pH->first; |
| 45 | pH->first = 0; |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 46 | sqlite3_free(pH->ht); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 47 | pH->ht = 0; |
| 48 | pH->htsize = 0; |
| 49 | while( elem ){ |
| 50 | HashElem *next_elem = elem->next; |
drh | e2f02ba | 2009-01-09 01:12:27 +0000 | [diff] [blame] | 51 | if( pH->copyKey ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 52 | sqlite3_free(elem->pKey); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 53 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 54 | sqlite3_free(elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 55 | elem = next_elem; |
| 56 | } |
| 57 | pH->count = 0; |
| 58 | } |
| 59 | |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 60 | /* |
| 61 | ** Hash and comparison functions when the mode is SQLITE_HASH_STRING |
| 62 | */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 63 | static unsigned int strHash(const void *pKey, int nKey){ |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 64 | const char *z = (const char *)pKey; |
| 65 | int h = 0; |
drh | a83ccca | 2009-04-28 13:01:09 +0000 | [diff] [blame] | 66 | assert( nKey>0 ); |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 67 | while( nKey > 0 ){ |
| 68 | h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++]; |
| 69 | nKey--; |
| 70 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 71 | return h; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 72 | } |
| 73 | static int strCompare(const void *pKey1, int n1, const void *pKey2, int n2){ |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 74 | if( n1!=n2 ) return 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 75 | return sqlite3StrNICmp((const char*)pKey1,(const char*)pKey2,n1); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 76 | } |
| 77 | |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 78 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 79 | /* Link pNew element into the hash table pH. If pEntry!=0 then also |
| 80 | ** insert pNew into the pEntry hash bucket. |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 81 | */ |
| 82 | static void insertElement( |
| 83 | Hash *pH, /* The complete hash table */ |
| 84 | struct _ht *pEntry, /* The entry into which pNew is inserted */ |
| 85 | HashElem *pNew /* The element to be inserted */ |
| 86 | ){ |
| 87 | HashElem *pHead; /* First element already in pEntry */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 88 | if( pEntry ){ |
| 89 | pHead = pEntry->count ? pEntry->chain : 0; |
| 90 | pEntry->count++; |
| 91 | pEntry->chain = pNew; |
| 92 | }else{ |
| 93 | pHead = 0; |
| 94 | } |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 95 | if( pHead ){ |
| 96 | pNew->next = pHead; |
| 97 | pNew->prev = pHead->prev; |
| 98 | if( pHead->prev ){ pHead->prev->next = pNew; } |
| 99 | else { pH->first = pNew; } |
| 100 | pHead->prev = pNew; |
| 101 | }else{ |
| 102 | pNew->next = pH->first; |
| 103 | if( pH->first ){ pH->first->prev = pNew; } |
| 104 | pNew->prev = 0; |
| 105 | pH->first = pNew; |
| 106 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 110 | /* Resize the hash table so that it cantains "new_size" buckets. |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 111 | ** |
| 112 | ** The hash table might fail to resize if sqlite3_malloc() fails or |
| 113 | ** if the new size is the same as the prior size. |
| 114 | ** Return TRUE if the resize occurs and false if not. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 115 | */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 116 | static int rehash(Hash *pH, unsigned int new_size){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 117 | struct _ht *new_ht; /* The new hash table */ |
| 118 | HashElem *elem, *next_elem; /* For looping over existing elements */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 119 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 120 | #if SQLITE_MALLOC_SOFT_LIMIT>0 |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 121 | if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ |
| 122 | new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); |
| 123 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 124 | if( new_size==pH->htsize ) return 0; |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 125 | #endif |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 126 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 127 | /* The inability to allocates space for a larger hash table is |
| 128 | ** a performance hit but it is not a fatal error. So mark the |
| 129 | ** allocation as a benign. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 130 | */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 131 | sqlite3BeginBenignMalloc(); |
| 132 | new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); |
| 133 | sqlite3EndBenignMalloc(); |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 134 | |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 135 | if( new_ht==0 ) return 0; |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 136 | sqlite3_free(pH->ht); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 137 | pH->ht = new_ht; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 138 | pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); |
| 139 | memset(new_ht, 0, new_size*sizeof(struct _ht)); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 140 | for(elem=pH->first, pH->first=0; elem; elem = next_elem){ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 141 | unsigned int h = strHash(elem->pKey, elem->nKey) % new_size; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 142 | next_elem = elem->next; |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 143 | insertElement(pH, &new_ht[h], elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 144 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 145 | return 1; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* This function (for internal use only) locates an element in an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 149 | ** hash table that matches the given key. The hash for this key has |
| 150 | ** already been computed and is passed as the 4th parameter. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 151 | */ |
| 152 | static HashElem *findElementGivenHash( |
| 153 | const Hash *pH, /* The pH to be searched */ |
| 154 | const void *pKey, /* The key we are searching for */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 155 | int nKey, /* Bytes in key (not counting zero terminator) */ |
| 156 | unsigned int h /* The hash for this key. */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 157 | ){ |
| 158 | HashElem *elem; /* Used to loop thru the element list */ |
| 159 | int count; /* Number of elements left to test */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 160 | |
| 161 | if( pH->ht ){ |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 162 | struct _ht *pEntry = &pH->ht[h]; |
| 163 | elem = pEntry->chain; |
| 164 | count = pEntry->count; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 165 | }else{ |
| 166 | elem = pH->first; |
| 167 | count = pH->count; |
| 168 | } |
| 169 | while( count-- && ALWAYS(elem) ){ |
| 170 | if( strCompare(elem->pKey,elem->nKey,pKey,nKey)==0 ){ |
| 171 | return elem; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 172 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 173 | elem = elem->next; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 178 | /* Remove a single entry from the hash table given a pointer to that |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 179 | ** element and a hash on the element's key. |
| 180 | */ |
| 181 | static void removeElementGivenHash( |
| 182 | Hash *pH, /* The pH containing "elem" */ |
| 183 | HashElem* elem, /* The element to be removed from the pH */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 184 | unsigned int h /* Hash value for the element */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 185 | ){ |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 186 | struct _ht *pEntry; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 187 | if( elem->prev ){ |
| 188 | elem->prev->next = elem->next; |
| 189 | }else{ |
| 190 | pH->first = elem->next; |
| 191 | } |
| 192 | if( elem->next ){ |
| 193 | elem->next->prev = elem->prev; |
| 194 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 195 | if( pH->ht ){ |
| 196 | pEntry = &pH->ht[h]; |
| 197 | if( pEntry->chain==elem ){ |
| 198 | pEntry->chain = elem->next; |
| 199 | } |
| 200 | pEntry->count--; |
| 201 | assert( pEntry->count>=0 ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 202 | } |
drh | 93553ad | 2007-03-31 03:59:23 +0000 | [diff] [blame] | 203 | if( pH->copyKey ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 204 | sqlite3_free(elem->pKey); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 205 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 206 | sqlite3_free( elem ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 207 | pH->count--; |
drh | 762e584 | 2005-10-03 15:11:08 +0000 | [diff] [blame] | 208 | if( pH->count<=0 ){ |
| 209 | assert( pH->first==0 ); |
| 210 | assert( pH->count==0 ); |
| 211 | sqlite3HashClear(pH); |
| 212 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 213 | } |
| 214 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 215 | /* Attempt to locate an element of the hash table pH with a key |
danielk1977 | 7c836f0 | 2007-09-04 14:31:47 +0000 | [diff] [blame] | 216 | ** that matches pKey,nKey. Return the data for this element if it is |
| 217 | ** found, or NULL if there is no match. |
| 218 | */ |
| 219 | void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){ |
| 220 | HashElem *elem; /* The element that matches key */ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 221 | unsigned int h; /* A hash on key */ |
| 222 | |
| 223 | assert( pH!=0 ); |
| 224 | assert( pKey!=0 ); |
| 225 | assert( nKey>0 ); |
| 226 | if( pH->ht ){ |
| 227 | h = strHash(pKey, nKey) % pH->htsize; |
| 228 | }else{ |
| 229 | h = 0; |
| 230 | } |
| 231 | elem = findElementGivenHash(pH, pKey, nKey, h); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 232 | return elem ? elem->data : 0; |
| 233 | } |
| 234 | |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 235 | /* Insert an element into the hash table pH. The key is pKey,nKey |
| 236 | ** and the data is "data". |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 237 | ** |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 238 | ** If no element exists with a matching key, then a new |
| 239 | ** element is created. A copy of the key is made if the copyKey |
| 240 | ** flag is set. NULL is returned. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 241 | ** |
| 242 | ** If another element already exists with the same key, then the |
| 243 | ** new data replaces the old data and the old data is returned. |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 244 | ** The key is not copied in this instance. If a malloc fails, then |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 245 | ** the new data is returned and the hash table is unchanged. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 246 | ** |
| 247 | ** If the "data" parameter to this function is NULL, then the |
drh | 81a20f2 | 2001-10-12 17:30:04 +0000 | [diff] [blame] | 248 | ** element corresponding to "key" is removed from the hash table. |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 249 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 250 | void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 251 | unsigned int h; /* the hash of the key modulo hash table size */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 252 | HashElem *elem; /* Used to loop thru the element list */ |
| 253 | HashElem *new_elem; /* New element added to the pH */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 254 | |
| 255 | assert( pH!=0 ); |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 256 | assert( pKey!=0 ); |
| 257 | assert( nKey>0 ); |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 258 | if( pH->htsize ){ |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 259 | h = strHash(pKey, nKey) % pH->htsize; |
| 260 | }else{ |
| 261 | h = 0; |
| 262 | } |
| 263 | elem = findElementGivenHash(pH,pKey,nKey,h); |
| 264 | if( elem ){ |
| 265 | void *old_data = elem->data; |
| 266 | if( data==0 ){ |
| 267 | removeElementGivenHash(pH,elem,h); |
| 268 | }else{ |
| 269 | elem->data = data; |
| 270 | if( !pH->copyKey ){ |
| 271 | elem->pKey = (void *)pKey; |
danielk1977 | cd2543b | 2007-09-03 15:03:20 +0000 | [diff] [blame] | 272 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 273 | assert(nKey==elem->nKey); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 274 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 275 | return old_data; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 276 | } |
| 277 | if( data==0 ) return 0; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 278 | new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 279 | if( new_elem==0 ) return data; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 280 | if( pH->copyKey ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 281 | new_elem->pKey = sqlite3Malloc( nKey ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 282 | if( new_elem->pKey==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 283 | sqlite3_free(new_elem); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 284 | return data; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 285 | } |
| 286 | memcpy((void*)new_elem->pKey, pKey, nKey); |
| 287 | }else{ |
drh | 2ce1a6e | 2002-05-21 23:44:30 +0000 | [diff] [blame] | 288 | new_elem->pKey = (void*)pKey; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 289 | } |
| 290 | new_elem->nKey = nKey; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 291 | new_elem->data = data; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 292 | pH->count++; |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 293 | if( pH->count>=10 && pH->count > 2*pH->htsize ){ |
| 294 | if( rehash(pH, pH->count*2) && pH->htsize ){ |
| 295 | h = strHash(pKey, nKey) % pH->htsize; |
drh | e8cf2ca | 2004-08-20 14:08:50 +0000 | [diff] [blame] | 296 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 297 | } |
drh | 8a1e594 | 2009-04-28 15:43:45 +0000 | [diff] [blame] | 298 | if( pH->ht ){ |
| 299 | insertElement(pH, &pH->ht[h], new_elem); |
| 300 | }else{ |
| 301 | insertElement(pH, 0, new_elem); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 302 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 303 | return 0; |
| 304 | } |