11f5207bJohn Levon/* Copyright (C) 2002, 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
21f5207bJohn Levon
31f5207bJohn Levon#ifndef __HASHTABLE_ITR_CWC22__
41f5207bJohn Levon#define __HASHTABLE_ITR_CWC22__
51f5207bJohn Levon#include "hashtable.h"
61f5207bJohn Levon#include "hashtable_private.h" /* needed to enable inlining */
71f5207bJohn Levon
81f5207bJohn Levon/*****************************************************************************/
91f5207bJohn Levon/* This struct is only concrete here to allow the inlining of two of the
101f5207bJohn Levon * accessor functions. */
111f5207bJohn Levonstruct hashtable_itr
121f5207bJohn Levon{
131f5207bJohn Levon    struct hashtable *h;
141f5207bJohn Levon    struct entry *e;
151f5207bJohn Levon    struct entry *parent;
161f5207bJohn Levon    unsigned int index;
171f5207bJohn Levon};
181f5207bJohn Levon
191f5207bJohn Levon
201f5207bJohn Levon/*****************************************************************************/
211f5207bJohn Levon/* hashtable_iterator
221f5207bJohn Levon */
231f5207bJohn Levon
241f5207bJohn Levonstruct hashtable_itr *
251f5207bJohn Levonhashtable_iterator(struct hashtable *h);
261f5207bJohn Levon
271f5207bJohn Levon/*****************************************************************************/
281f5207bJohn Levon/* hashtable_iterator_key
291f5207bJohn Levon * - return the value of the (key,value) pair at the current position */
301f5207bJohn Levon
311f5207bJohn Levonextern inline void *
321f5207bJohn Levonhashtable_iterator_key(struct hashtable_itr *i)
331f5207bJohn Levon{
341f5207bJohn Levon    return i->e->k;
351f5207bJohn Levon}
361f5207bJohn Levon
371f5207bJohn Levon/*****************************************************************************/
381f5207bJohn Levon/* value - return the value of the (key,value) pair at the current position */
391f5207bJohn Levon
401f5207bJohn Levonextern inline void *
411f5207bJohn Levonhashtable_iterator_value(struct hashtable_itr *i)
421f5207bJohn Levon{
431f5207bJohn Levon    return i->e->v;
441f5207bJohn Levon}
451f5207bJohn Levon
461f5207bJohn Levon/*****************************************************************************/
471f5207bJohn Levon/* advance - advance the iterator to the next element
481f5207bJohn Levon *           returns zero if advanced to end of table */
491f5207bJohn Levon
501f5207bJohn Levonint
511f5207bJohn Levonhashtable_iterator_advance(struct hashtable_itr *itr);
521f5207bJohn Levon
531f5207bJohn Levon/*****************************************************************************/
541f5207bJohn Levon/* remove - remove current element and advance the iterator to the next element
551f5207bJohn Levon *          NB: if you need the value to free it, read it before
561f5207bJohn Levon *          removing. ie: beware memory leaks!
571f5207bJohn Levon *          returns zero if advanced to end of table */
581f5207bJohn Levon
591f5207bJohn Levonint
601f5207bJohn Levonhashtable_iterator_remove(struct hashtable_itr *itr);
611f5207bJohn Levon
621f5207bJohn Levon/*****************************************************************************/
631f5207bJohn Levon/* search - overwrite the supplied iterator, to point to the entry
641f5207bJohn Levon *          matching the supplied key.
651f5207bJohn Levon            h points to the hashtable to be searched.
661f5207bJohn Levon *          returns zero if not found. */
671f5207bJohn Levonint
681f5207bJohn Levonhashtable_iterator_search(struct hashtable_itr *itr,
691f5207bJohn Levon                          struct hashtable *h, void *k);
701f5207bJohn Levon
711f5207bJohn Levon#define DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \
721f5207bJohn Levonint fnname (struct hashtable_itr *i, struct hashtable *h, keytype *k) \
731f5207bJohn Levon{ \
741f5207bJohn Levon    return (hashtable_iterator_search(i,h,k)); \
751f5207bJohn Levon}
761f5207bJohn Levon
771f5207bJohn Levon
781f5207bJohn Levon
791f5207bJohn Levon#endif /* __HASHTABLE_ITR_CWC22__*/
801f5207bJohn Levon
811f5207bJohn Levon/*
821f5207bJohn Levon * Copyright (c) 2002, 2004, Christopher Clark
831f5207bJohn Levon * All rights reserved.
841f5207bJohn Levon *
851f5207bJohn Levon * Redistribution and use in source and binary forms, with or without
861f5207bJohn Levon * modification, are permitted provided that the following conditions
871f5207bJohn Levon * are met:
881f5207bJohn Levon *
891f5207bJohn Levon * * Redistributions of source code must retain the above copyright
901f5207bJohn Levon * notice, this list of conditions and the following disclaimer.
911f5207bJohn Levon *
921f5207bJohn Levon * * Redistributions in binary form must reproduce the above copyright
931f5207bJohn Levon * notice, this list of conditions and the following disclaimer in the
941f5207bJohn Levon * documentation and/or other materials provided with the distribution.
951f5207bJohn Levon *
961f5207bJohn Levon * * Neither the name of the original author; nor the names of any contributors
971f5207bJohn Levon * may be used to endorse or promote products derived from this software
981f5207bJohn Levon * without specific prior written permission.
991f5207bJohn Levon *
1001f5207bJohn Levon *
1011f5207bJohn Levon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1021f5207bJohn Levon * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1031f5207bJohn Levon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1041f5207bJohn Levon * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
1051f5207bJohn Levon * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1061f5207bJohn Levon * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1071f5207bJohn Levon * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1081f5207bJohn Levon * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1091f5207bJohn Levon * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1101f5207bJohn Levon * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1111f5207bJohn Levon * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1121f5207bJohn Levon*/
113