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