1 2 3 /* Copyright (C) 2002, 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */ 4 // Portions Copyright (C) 2000-2019 The Xastir Group 5 6 7 #ifndef __HASHTABLE_ITR_CWC22__ 8 #define __HASHTABLE_ITR_CWC22__ 9 #include "hashtable.h" 10 #include "hashtable_private.h" /* needed to enable inlining */ 11 12 /*****************************************************************************/ 13 /* This struct is only concrete here to allow the inlining of two of the 14 * accessor functions. */ 15 struct hashtable_itr 16 { 17 struct hashtable *h; 18 struct entry *e; 19 struct entry *parent; 20 unsigned int index; 21 }; 22 23 24 /*****************************************************************************/ 25 /* hashtable_iterator 26 */ 27 28 struct hashtable_itr * 29 hashtable_iterator(struct hashtable *h); 30 31 #if 0 32 // BZZZZT! it is very, very wrong to be inlining this this way. 33 // If one calls hashtable_iterator on a hash table from which everything 34 // has been deleted, the iterator has a null for i->e. 35 // It is not good to require the caller to check the internals of the iterator 36 // structure just to be sure there are no null pointers inside. 37 // For whatever reason, these are defined again in the hashtable_iterator.c 38 // file, not inlined. I have modified the ones in hashtable_iterator so they 39 // actually check for nulls and don't try to dereference them. 40 /*****************************************************************************/ 41 /* hashtable_iterator_key 42 * - return the value of the (key,value) pair at the current position */ 43 44 extern inline void * 45 hashtable_iterator_key(struct hashtable_itr *i) 46 { 47 return i->e->k; 48 } 49 50 /*****************************************************************************/ 51 /* value - return the value of the (key,value) pair at the current position */ 52 53 extern inline void * 54 hashtable_iterator_value(struct hashtable_itr *i) 55 { 56 return i->e->v; 57 } 58 #else 59 // SO instead of inlining, just declare. No need to be "extern" 60 // The ones in the .c file check their arguments and return nulls if they 61 // can't comply with the request. Much nicer for the calling routine to 62 // check a return value than to monkey with the internals of the struct. 63 void * hashtable_iterator_key(struct hashtable_itr *i); 64 void * hashtable_iterator_value(struct hashtable_itr *i); 65 #endif 66 67 /*****************************************************************************/ 68 /* advance - advance the iterator to the next element 69 * returns zero if advanced to end of table */ 70 71 int 72 hashtable_iterator_advance(struct hashtable_itr *itr); 73 74 /*****************************************************************************/ 75 /* remove - remove current element and advance the iterator to the next element 76 * NB: if you need the value to free it, read it before 77 * removing. ie: beware memory leaks! 78 * returns zero if advanced to end of table */ 79 80 int 81 hashtable_iterator_remove(struct hashtable_itr *itr); 82 83 /*****************************************************************************/ 84 /* search - overwrite the supplied iterator, to point to the entry 85 * matching the supplied key. 86 h points to the hashtable to be searched. 87 * returns zero if not found. */ 88 int 89 hashtable_iterator_search(struct hashtable_itr *itr, 90 struct hashtable *h, void *k); 91 92 #define DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \ 93 int fnname (struct hashtable_itr *i, struct hashtable *h, keytype *k) \ 94 { \ 95 return (hashtable_iterator_search(i,h,k)); \ 96 } 97 98 99 100 #endif /* __HASHTABLE_ITR_CWC22__*/ 101 102 /* 103 * Copyright (C) 2002, 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> 104 * 105 * Permission is hereby granted, free of charge, to any person obtaining a copy 106 * of this software and associated documentation files (the "Software"), to 107 * deal in the Software without restriction, including without limitation the 108 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 109 * sell copies of the Software, and to permit persons to whom the Software is 110 * furnished to do so, subject to the following conditions: 111 * 112 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 113 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 114 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 115 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 116 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 117 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 118 * */ 119 120 121