1*1f5207b7SJohn Levon /* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
2*1f5207b7SJohn Levon 
3*1f5207b7SJohn Levon #include "hashtable.h"
4*1f5207b7SJohn Levon #include "hashtable_private.h"
5*1f5207b7SJohn Levon #include "hashtable_utility.h"
6*1f5207b7SJohn Levon #include <stdlib.h>
7*1f5207b7SJohn Levon #include <stdio.h>
8*1f5207b7SJohn Levon #include <string.h>
9*1f5207b7SJohn Levon 
10*1f5207b7SJohn Levon /*****************************************************************************/
11*1f5207b7SJohn Levon /* hashtable_change
12*1f5207b7SJohn Levon  *
13*1f5207b7SJohn Levon  * function to change the value associated with a key, where there already
14*1f5207b7SJohn Levon  * exists a value bound to the key in the hashtable.
15*1f5207b7SJohn Levon  * Source due to Holger Schemel.
16*1f5207b7SJohn Levon  *
17*1f5207b7SJohn Levon  *  */
18*1f5207b7SJohn Levon int
hashtable_change(struct hashtable * h,void * k,void * v)19*1f5207b7SJohn Levon hashtable_change(struct hashtable *h, void *k, void *v)
20*1f5207b7SJohn Levon {
21*1f5207b7SJohn Levon     struct entry *e;
22*1f5207b7SJohn Levon     unsigned int hashvalue, index;
23*1f5207b7SJohn Levon     hashvalue = hash(h,k);
24*1f5207b7SJohn Levon     index = indexFor(h->tablelength,hashvalue);
25*1f5207b7SJohn Levon     e = h->table[index];
26*1f5207b7SJohn Levon     while (NULL != e)
27*1f5207b7SJohn Levon     {
28*1f5207b7SJohn Levon         /* Check hash value to short circuit heavier comparison */
29*1f5207b7SJohn Levon         if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
30*1f5207b7SJohn Levon         {
31*1f5207b7SJohn Levon             free(e->v);
32*1f5207b7SJohn Levon             e->v = v;
33*1f5207b7SJohn Levon             return -1;
34*1f5207b7SJohn Levon         }
35*1f5207b7SJohn Levon         e = e->next;
36*1f5207b7SJohn Levon     }
37*1f5207b7SJohn Levon     return 0;
38*1f5207b7SJohn Levon }
39*1f5207b7SJohn Levon 
40*1f5207b7SJohn Levon /*
41*1f5207b7SJohn Levon  * Copyright (c) 2002, Christopher Clark
42*1f5207b7SJohn Levon  * All rights reserved.
43*1f5207b7SJohn Levon  *
44*1f5207b7SJohn Levon  * Redistribution and use in source and binary forms, with or without
45*1f5207b7SJohn Levon  * modification, are permitted provided that the following conditions
46*1f5207b7SJohn Levon  * are met:
47*1f5207b7SJohn Levon  *
48*1f5207b7SJohn Levon  * * Redistributions of source code must retain the above copyright
49*1f5207b7SJohn Levon  * notice, this list of conditions and the following disclaimer.
50*1f5207b7SJohn Levon  *
51*1f5207b7SJohn Levon  * * Redistributions in binary form must reproduce the above copyright
52*1f5207b7SJohn Levon  * notice, this list of conditions and the following disclaimer in the
53*1f5207b7SJohn Levon  * documentation and/or other materials provided with the distribution.
54*1f5207b7SJohn Levon  *
55*1f5207b7SJohn Levon  * * Neither the name of the original author; nor the names of any contributors
56*1f5207b7SJohn Levon  * may be used to endorse or promote products derived from this software
57*1f5207b7SJohn Levon  * without specific prior written permission.
58*1f5207b7SJohn Levon  *
59*1f5207b7SJohn Levon  *
60*1f5207b7SJohn Levon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
61*1f5207b7SJohn Levon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
62*1f5207b7SJohn Levon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
63*1f5207b7SJohn Levon  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
64*1f5207b7SJohn Levon  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
65*1f5207b7SJohn Levon  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
66*1f5207b7SJohn Levon  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
67*1f5207b7SJohn Levon  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
68*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
69*1f5207b7SJohn Levon  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
70*1f5207b7SJohn Levon  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71*1f5207b7SJohn Levon */
72