1 /* Copyright (C) 2002, 2004 Christopher Clark  <firstname.lastname@cl.cam.ac.uk> */
2 
3 #include "hashtable.h"
4 #define HASHTABLE_INDEXFOR
5 #include "hashtable_private.h"
6 #include "hashtable_itr.h"
7 #include <stdlib.h> /* defines NULL */
8 
9 /*****************************************************************************/
10 /* hashtable_iterator    - iterator constructor */
11 
12 struct hashtable_itr *
hashtable_iterator(struct hashtable * h)13 hashtable_iterator(struct hashtable *h)
14 {
15     unsigned int i, tablelength;
16     struct hashtable_itr *itr = (struct hashtable_itr *)
17         malloc(sizeof(struct hashtable_itr));
18     if (NULL == itr) return NULL;
19     itr->h = h;
20     itr->e = NULL;
21     itr->parent = NULL;
22     tablelength = h->tablelength;
23     itr->index = tablelength;
24     if (0 == h->entrycount) return itr;
25 
26     for (i = 0; i < tablelength; i++)
27     {
28         if (NULL != h->table[i])
29         {
30             itr->e = h->table[i];
31             itr->index = i;
32             break;
33         }
34     }
35     return itr;
36 }
37 
38 /*****************************************************************************/
39 /* key      - return the key of the (key,value) pair at the current position */
40 /* value    - return the value of the (key,value) pair at the current position */
41 
42 void *
hashtable_iterator_key(struct hashtable_itr * i)43 hashtable_iterator_key(struct hashtable_itr *i)
44 { return i->e->k; }
45 
46 void *
hashtable_iterator_value(struct hashtable_itr * i)47 hashtable_iterator_value(struct hashtable_itr *i)
48 { return i->e->v; }
49 
50 /*****************************************************************************/
51 /* advance - advance the iterator to the next element
52  *           returns zero if advanced to end of table */
53 
54 int
hashtable_iterator_advance(struct hashtable_itr * itr)55 hashtable_iterator_advance(struct hashtable_itr *itr)
56 {
57     unsigned int j,tablelength;
58     struct entry **table;
59     struct entry *next;
60     if (NULL == itr->e) return 0; /* stupidity check */
61 
62     next = itr->e->next;
63     if (NULL != next)
64     {
65         itr->parent = itr->e;
66         itr->e = next;
67         return -1;
68     }
69     tablelength = itr->h->tablelength;
70     itr->parent = NULL;
71     if (tablelength <= (j = ++(itr->index)))
72     {
73         itr->e = NULL;
74         return 0;
75     }
76     table = itr->h->table;
77     while (NULL == (next = table[j]))
78     {
79         if (++j >= tablelength)
80         {
81             itr->index = tablelength;
82             itr->e = NULL;
83             return 0;
84         }
85     }
86     itr->index = j;
87     itr->e = next;
88     return -1;
89 }
90 
91 /*****************************************************************************/
92 /* remove - remove the entry at the current iterator position
93  *          and advance the iterator, if there is a successive
94  *          element.
95  *          If you want the value, read it before you remove:
96  *          beware memory leaks if you don't.
97  *          Returns zero if end of iteration. */
98 
99 int
hashtable_iterator_remove(struct hashtable_itr * itr)100 hashtable_iterator_remove(struct hashtable_itr *itr)
101 {
102     struct entry *remember_e, *remember_parent;
103     int ret;
104 
105     /* Do the removal */
106     if (NULL == (itr->parent))
107     {
108         /* element is head of a chain */
109         itr->h->table[itr->index] = itr->e->next;
110     } else {
111         /* element is mid-chain */
112         itr->parent->next = itr->e->next;
113     }
114     /* itr->e is now outside the hashtable */
115     remember_e = itr->e;
116     itr->h->entrycount--;
117     freekey(remember_e->k);
118 
119     /* Advance the iterator, correcting the parent */
120     remember_parent = itr->parent;
121     ret = hashtable_iterator_advance(itr);
122     if (itr->parent == remember_e) { itr->parent = remember_parent; }
123     free(remember_e);
124     return ret;
125 }
126 
127 /*****************************************************************************/
128 int /* returns zero if not found */
hashtable_iterator_search(struct hashtable_itr * itr,struct hashtable * h,void * k)129 hashtable_iterator_search(struct hashtable_itr *itr,
130                           struct hashtable *h, void *k)
131 {
132     struct entry *e, *parent;
133     unsigned int hashvalue, index;
134 
135     hashvalue = hash(h,k);
136     index = indexFor(h->tablelength,hashvalue);
137 
138     e = h->table[index];
139     parent = NULL;
140     while (NULL != e)
141     {
142         /* Check hash value to short circuit heavier comparison */
143         if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
144         {
145             itr->index = index;
146             itr->e = e;
147             itr->parent = parent;
148             itr->h = h;
149             return -1;
150         }
151         parent = e;
152         e = e->next;
153     }
154     return 0;
155 }
156 
157 
158 /*
159  * Copyright (c) 2002, 2004, Christopher Clark
160  * All rights reserved.
161  *
162  * Redistribution and use in source and binary forms, with or without
163  * modification, are permitted provided that the following conditions
164  * are met:
165  *
166  * * Redistributions of source code must retain the above copyright
167  * notice, this list of conditions and the following disclaimer.
168  *
169  * * Redistributions in binary form must reproduce the above copyright
170  * notice, this list of conditions and the following disclaimer in the
171  * documentation and/or other materials provided with the distribution.
172  *
173  * * Neither the name of the original author; nor the names of any contributors
174  * may be used to endorse or promote products derived from this software
175  * without specific prior written permission.
176  *
177  *
178  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
180  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
181  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
182  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
183  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
184  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
185  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
186  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
187  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
188  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
189 */
190