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