This source file includes following definitions.
- hashtable_iterator
- hashtable_iterator_advance
- hashtable_iterator_remove
- hashtable_iterator_search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 #include "private/ftdm_core.h"
35 #include "hashtable.h"
36 #include "hashtable_private.h"
37 #include "hashtable_itr.h"
38 #include <stdlib.h>
39
40
41
42
43 struct hashtable_itr *
44 hashtable_iterator(struct hashtable *h)
45 {
46 unsigned int i, tablelength;
47 struct hashtable_itr *itr = ftdm_malloc(sizeof(struct hashtable_itr));
48 if (NULL == itr) return NULL;
49 itr->h = h;
50 itr->e = NULL;
51 itr->parent = NULL;
52 tablelength = h->tablelength;
53 itr->index = tablelength;
54 if (0 == h->entrycount) return itr;
55
56 for (i = 0; i < tablelength; i++)
57 {
58 if (NULL != h->table[i])
59 {
60 itr->e = h->table[i];
61 itr->index = i;
62 break;
63 }
64 }
65 return itr;
66 }
67
68
69
70
71
72 int
73 hashtable_iterator_advance(struct hashtable_itr *itr)
74 {
75 unsigned int j,tablelength;
76 struct entry **table;
77 struct entry *next;
78 if (NULL == itr->e) return 0;
79
80 next = itr->e->next;
81 if (NULL != next)
82 {
83 itr->parent = itr->e;
84 itr->e = next;
85 return -1;
86 }
87 tablelength = itr->h->tablelength;
88 itr->parent = NULL;
89 if (tablelength <= (j = ++(itr->index)))
90 {
91 itr->e = NULL;
92 return 0;
93 }
94 table = itr->h->table;
95 while (NULL == (next = table[j]))
96 {
97 if (++j >= tablelength)
98 {
99 itr->index = tablelength;
100 itr->e = NULL;
101 return 0;
102 }
103 }
104 itr->index = j;
105 itr->e = next;
106 return -1;
107 }
108
109
110
111
112
113
114
115
116
117 int
118 hashtable_iterator_remove(struct hashtable_itr *itr)
119 {
120 struct entry *remember_e, *remember_parent;
121 int ret;
122
123
124 if (NULL == (itr->parent))
125 {
126
127 itr->h->table[itr->index] = itr->e->next;
128 } else {
129
130 itr->parent->next = itr->e->next;
131 }
132
133 remember_e = itr->e;
134 itr->h->entrycount--;
135 freekey(remember_e->k);
136
137
138 remember_parent = itr->parent;
139 ret = hashtable_iterator_advance(itr);
140 if (itr->parent == remember_e) { itr->parent = remember_parent; }
141 ftdm_safe_free(remember_e);
142 return ret;
143 }
144
145
146 int
147 hashtable_iterator_search(struct hashtable_itr *itr,
148 struct hashtable *h, void *k)
149 {
150 struct entry *e, *parent;
151 unsigned int hashvalue, index;
152
153 hashvalue = hash(h,k);
154 index = indexFor(h->tablelength,hashvalue);
155
156 e = h->table[index];
157 parent = NULL;
158 while (NULL != e)
159 {
160
161 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
162 {
163 itr->index = index;
164 itr->e = e;
165 itr->parent = parent;
166 itr->h = h;
167 return -1;
168 }
169 parent = e;
170 e = e->next;
171 }
172 return 0;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186