Check-in [cc9ff9d9e4]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:cc9ff9d9e407489f2f9b1ae976c352e37de3714d
Date: 2008-11-03 23:33:26
User: stephan
Comment:fixed a div-by-0 which happend in some search cases
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to src/whhash.c

Old (74f5cc0ced0de206) New (803862366eb1b033)
1 /* Copyright (C) 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */ 1 /* Copyright (C) 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
2 /* Copyright (C) 2008 Stephan Beal (http://wanderinghorse.net/home/stephan/) */ 2 /* Copyright (C) 2008 Stephan Beal (http://wanderinghorse.net/home/stephan/) */
3 3
4 #include "whhash.h" 4 #include "whhash.h"
5 #include <stdlib.h> 5 #include <stdlib.h>
264 hidden lines
270 } 270 }
271 271
272 int 272 int
273 whhash_replace(whhash_table *h, void *k, void *v) 273 whhash_replace(whhash_table *h, void *k, void *v)
274 { 274 {
275 if( !h || !k ) return 0; | 275 if( !h || !k
| 276 || !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
| 277 ) return 0;
276 whhash_entry *e; 278 whhash_entry *e;
277 whhash_val_t hashvalue, index; 279 whhash_val_t hashvalue, index;
278 hashvalue = whhash_hash(h,k); 280 hashvalue = whhash_hash(h,k);
279 index = whhash_index(h->tablelength,hashvalue); 281 index = whhash_index(h->tablelength,hashvalue);
280 e = h->table[index]; 282 e = h->table[index];
18 hidden lines
299 } 301 }
300 302
301 int 303 int
302 whhash_insert(whhash_table *h, void *k, void *v) 304 whhash_insert(whhash_table *h, void *k, void *v)
303 { 305 {
304 if( ! h || !k ) return 0; | 306 if( ! h || !k
| 307 || !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
| 308 ) return 0;
305 #if 0 309 #if 0
306 /* Stephan Beal, 13 Feb 2008: now simply replaces the value of existing entries. 310 /* Stephan Beal, 13 Feb 2008: now simply replaces the value of existing entries.
307 311
308 A week or three later: profiling has shown that this nearly doubles the insertion 312 A week or three later: profiling has shown that this nearly doubles the insertion
309 time. One app was spending almost 1% of its time in this code. 313 time. One app was spending almost 1% of its time in this code.
25 hidden lines
335 } 339 }
336 340
337 static whhash_entry * whhash_search_entry(whhash_table *h, 341 static whhash_entry * whhash_search_entry(whhash_table *h,
338 void const *k) 342 void const *k)
339 { 343 {
340 if( !h || !k ) return 0; | 344 if( !h || !k
| 345 || !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
| 346 ) return 0;
341 ++h->stats.searches; 347 ++h->stats.searches;
342 whhash_entry * e = 0; 348 whhash_entry * e = 0;
343 whhash_val_t hashvalue, index; 349 whhash_val_t hashvalue, index;
344 hashvalue = whhash_hash(h,k); 350 hashvalue = whhash_hash(h,k);
345 index = whhash_index(h->tablelength,hashvalue); 351 index = whhash_index(h->tablelength,hashvalue);
25 hidden lines
371 return e ? e->v : 0; 377 return e ? e->v : 0;
372 } 378 }
373 379
374 void * whhash_take(whhash_table *h, void const *k) 380 void * whhash_take(whhash_table *h, void const *k)
375 { 381 {
> 382 if( !h || !k
> 383 || !h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
> 384 ) return 0;
> 385
376 /* TODO: consider compacting the table when the load factor drops enough, 386 /* TODO: consider compacting the table when the load factor drops enough,
377 * or provide a 'compact' method. */ 387 * or provide a 'compact' method. */
378 whhash_entry *e; 388 whhash_entry *e;
379 whhash_entry **pE; 389 whhash_entry **pE;
380 void *v; 390 void *v;
400 hidden lines
781 791
782 int 792 int
783 whhash_iter_search(whhash_iter *itr, 793 whhash_iter_search(whhash_iter *itr,
784 void *k) 794 void *k)
785 { 795 {
786 if( ! itr || !itr->h || !k ) return 0; | 796 if( ! itr || !itr->h || !k
| 797 || !itr->h->tablelength /* avoid a possible /-by-0 in whhash_index()*/
| 798 ) return 0;
787 whhash_entry *e, *parent; 799 whhash_entry *e, *parent;
788 unsigned int hashvalue, index; 800 unsigned int hashvalue, index;
789 whhash_table *h = itr->h; 801 whhash_table *h = itr->h;
790 hashvalue = whhash_hash(h,k); 802 hashvalue = whhash_hash(h,k);
791 index = whhash_index(h->tablelength,hashvalue); 803 index = whhash_index(h->tablelength,hashvalue);
96 hidden lines
888 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 900 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
889 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 901 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
890 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 902 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
891 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 903 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
892 */ 904 */