1*1f5207b7SJohn Levon /* Copyright (C) 2002, 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
2*1f5207b7SJohn Levon 
3*1f5207b7SJohn Levon #ifndef __HASHTABLE_ITR_CWC22__
4*1f5207b7SJohn Levon #define __HASHTABLE_ITR_CWC22__
5*1f5207b7SJohn Levon #include "hashtable.h"
6*1f5207b7SJohn Levon #include "hashtable_private.h" /* needed to enable inlining */
7*1f5207b7SJohn Levon 
8*1f5207b7SJohn Levon /*****************************************************************************/
9*1f5207b7SJohn Levon /* This struct is only concrete here to allow the inlining of two of the
10*1f5207b7SJohn Levon  * accessor functions. */
11*1f5207b7SJohn Levon struct hashtable_itr
12*1f5207b7SJohn Levon {
13*1f5207b7SJohn Levon     struct hashtable *h;
14*1f5207b7SJohn Levon     struct entry *e;
15*1f5207b7SJohn Levon     struct entry *parent;
16*1f5207b7SJohn Levon     unsigned int index;
17*1f5207b7SJohn Levon };
18*1f5207b7SJohn Levon 
19*1f5207b7SJohn Levon 
20*1f5207b7SJohn Levon /*****************************************************************************/
21*1f5207b7SJohn Levon /* hashtable_iterator
22*1f5207b7SJohn Levon  */
23*1f5207b7SJohn Levon 
24*1f5207b7SJohn Levon struct hashtable_itr *
25*1f5207b7SJohn Levon hashtable_iterator(struct hashtable *h);
26*1f5207b7SJohn Levon 
27*1f5207b7SJohn Levon /*****************************************************************************/
28*1f5207b7SJohn Levon /* hashtable_iterator_key
29*1f5207b7SJohn Levon  * - return the value of the (key,value) pair at the current position */
30*1f5207b7SJohn Levon 
31*1f5207b7SJohn Levon extern inline void *
hashtable_iterator_key(struct hashtable_itr * i)32*1f5207b7SJohn Levon hashtable_iterator_key(struct hashtable_itr *i)
33*1f5207b7SJohn Levon {
34*1f5207b7SJohn Levon     return i->e->k;
35*1f5207b7SJohn Levon }
36*1f5207b7SJohn Levon 
37*1f5207b7SJohn Levon /*****************************************************************************/
38*1f5207b7SJohn Levon /* value - return the value of the (key,value) pair at the current position */
39*1f5207b7SJohn Levon 
40*1f5207b7SJohn Levon extern inline void *
hashtable_iterator_value(struct hashtable_itr * i)41*1f5207b7SJohn Levon hashtable_iterator_value(struct hashtable_itr *i)
42*1f5207b7SJohn Levon {
43*1f5207b7SJohn Levon     return i->e->v;
44*1f5207b7SJohn Levon }
45*1f5207b7SJohn Levon 
46*1f5207b7SJohn Levon /*****************************************************************************/
47*1f5207b7SJohn Levon /* advance - advance the iterator to the next element
48*1f5207b7SJohn Levon  *           returns zero if advanced to end of table */
49*1f5207b7SJohn Levon 
50*1f5207b7SJohn Levon int
51*1f5207b7SJohn Levon hashtable_iterator_advance(struct hashtable_itr *itr);
52*1f5207b7SJohn Levon 
53*1f5207b7SJohn Levon /*****************************************************************************/
54*1f5207b7SJohn Levon /* remove - remove current element and advance the iterator to the next element
55*1f5207b7SJohn Levon  *          NB: if you need the value to free it, read it before
56*1f5207b7SJohn Levon  *          removing. ie: beware memory leaks!
57*1f5207b7SJohn Levon  *          returns zero if advanced to end of table */
58*1f5207b7SJohn Levon 
59*1f5207b7SJohn Levon int
60*1f5207b7SJohn Levon hashtable_iterator_remove(struct hashtable_itr *itr);
61*1f5207b7SJohn Levon 
62*1f5207b7SJohn Levon /*****************************************************************************/
63*1f5207b7SJohn Levon /* search - overwrite the supplied iterator, to point to the entry
64*1f5207b7SJohn Levon  *          matching the supplied key.
65*1f5207b7SJohn Levon             h points to the hashtable to be searched.
66*1f5207b7SJohn Levon  *          returns zero if not found. */
67*1f5207b7SJohn Levon int
68*1f5207b7SJohn Levon hashtable_iterator_search(struct hashtable_itr *itr,
69*1f5207b7SJohn Levon                           struct hashtable *h, void *k);
70*1f5207b7SJohn Levon 
71*1f5207b7SJohn Levon #define DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \
72*1f5207b7SJohn Levon int fnname (struct hashtable_itr *i, struct hashtable *h, keytype *k) \
73*1f5207b7SJohn Levon { \
74*1f5207b7SJohn Levon     return (hashtable_iterator_search(i,h,k)); \
75*1f5207b7SJohn Levon }
76*1f5207b7SJohn Levon 
77*1f5207b7SJohn Levon 
78*1f5207b7SJohn Levon 
79*1f5207b7SJohn Levon #endif /* __HASHTABLE_ITR_CWC22__*/
80*1f5207b7SJohn Levon 
81*1f5207b7SJohn Levon /*
82*1f5207b7SJohn Levon  * Copyright (c) 2002, 2004, Christopher Clark
83*1f5207b7SJohn Levon  * All rights reserved.
84*1f5207b7SJohn Levon  *
85*1f5207b7SJohn Levon  * Redistribution and use in source and binary forms, with or without
86*1f5207b7SJohn Levon  * modification, are permitted provided that the following conditions
87*1f5207b7SJohn Levon  * are met:
88*1f5207b7SJohn Levon  *
89*1f5207b7SJohn Levon  * * Redistributions of source code must retain the above copyright
90*1f5207b7SJohn Levon  * notice, this list of conditions and the following disclaimer.
91*1f5207b7SJohn Levon  *
92*1f5207b7SJohn Levon  * * Redistributions in binary form must reproduce the above copyright
93*1f5207b7SJohn Levon  * notice, this list of conditions and the following disclaimer in the
94*1f5207b7SJohn Levon  * documentation and/or other materials provided with the distribution.
95*1f5207b7SJohn Levon  *
96*1f5207b7SJohn Levon  * * Neither the name of the original author; nor the names of any contributors
97*1f5207b7SJohn Levon  * may be used to endorse or promote products derived from this software
98*1f5207b7SJohn Levon  * without specific prior written permission.
99*1f5207b7SJohn Levon  *
100*1f5207b7SJohn Levon  *
101*1f5207b7SJohn Levon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
102*1f5207b7SJohn Levon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
103*1f5207b7SJohn Levon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
104*1f5207b7SJohn Levon  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
105*1f5207b7SJohn Levon  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
106*1f5207b7SJohn Levon  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
107*1f5207b7SJohn Levon  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
108*1f5207b7SJohn Levon  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
109*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
110*1f5207b7SJohn Levon  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
111*1f5207b7SJohn Levon  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112*1f5207b7SJohn Levon */
113