1*53ee8cc1Swenshuai.xi /* Declarations for System V style searching functions. 2*53ee8cc1Swenshuai.xi Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc. 3*53ee8cc1Swenshuai.xi This file is part of the GNU C Library. 4*53ee8cc1Swenshuai.xi 5*53ee8cc1Swenshuai.xi The GNU C Library is free software; you can redistribute it and/or 6*53ee8cc1Swenshuai.xi modify it under the terms of the GNU Lesser General Public 7*53ee8cc1Swenshuai.xi License as published by the Free Software Foundation; either 8*53ee8cc1Swenshuai.xi version 2.1 of the License, or (at your option) any later version. 9*53ee8cc1Swenshuai.xi 10*53ee8cc1Swenshuai.xi The GNU C Library is distributed in the hope that it will be useful, 11*53ee8cc1Swenshuai.xi but WITHOUT ANY WARRANTY; without even the implied warranty of 12*53ee8cc1Swenshuai.xi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13*53ee8cc1Swenshuai.xi Lesser General Public License for more details. 14*53ee8cc1Swenshuai.xi 15*53ee8cc1Swenshuai.xi You should have received a copy of the GNU Lesser General Public 16*53ee8cc1Swenshuai.xi License along with the GNU C Library; if not, write to the Free 17*53ee8cc1Swenshuai.xi Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 18*53ee8cc1Swenshuai.xi 02111-1307 USA. */ 19*53ee8cc1Swenshuai.xi 20*53ee8cc1Swenshuai.xi #ifndef _SEARCH_H 21*53ee8cc1Swenshuai.xi #define _SEARCH_H 1 22*53ee8cc1Swenshuai.xi 23*53ee8cc1Swenshuai.xi #include <features.h> 24*53ee8cc1Swenshuai.xi 25*53ee8cc1Swenshuai.xi #define __need_size_t 26*53ee8cc1Swenshuai.xi #include <stddef.h> 27*53ee8cc1Swenshuai.xi 28*53ee8cc1Swenshuai.xi __BEGIN_DECLS 29*53ee8cc1Swenshuai.xi 30*53ee8cc1Swenshuai.xi #if defined __USE_SVID || defined __USE_XOPEN_EXTENDED 31*53ee8cc1Swenshuai.xi /* Prototype structure for a linked-list data structure. 32*53ee8cc1Swenshuai.xi This is the type used by the `insque' and `remque' functions. */ 33*53ee8cc1Swenshuai.xi 34*53ee8cc1Swenshuai.xi # ifdef __USE_GNU 35*53ee8cc1Swenshuai.xi struct qelem 36*53ee8cc1Swenshuai.xi { 37*53ee8cc1Swenshuai.xi struct qelem *q_forw; 38*53ee8cc1Swenshuai.xi struct qelem *q_back; 39*53ee8cc1Swenshuai.xi char q_data[1]; 40*53ee8cc1Swenshuai.xi }; 41*53ee8cc1Swenshuai.xi # endif 42*53ee8cc1Swenshuai.xi 43*53ee8cc1Swenshuai.xi 44*53ee8cc1Swenshuai.xi /* Insert ELEM into a doubly-linked list, after PREV. */ 45*53ee8cc1Swenshuai.xi extern void insque (void *__elem, void *__prev) __THROW; 46*53ee8cc1Swenshuai.xi 47*53ee8cc1Swenshuai.xi /* Unlink ELEM from the doubly-linked list that it is in. */ 48*53ee8cc1Swenshuai.xi extern void remque (void *__elem) __THROW; 49*53ee8cc1Swenshuai.xi #endif 50*53ee8cc1Swenshuai.xi 51*53ee8cc1Swenshuai.xi 52*53ee8cc1Swenshuai.xi /* For use with hsearch(3). */ 53*53ee8cc1Swenshuai.xi #ifndef __COMPAR_FN_T 54*53ee8cc1Swenshuai.xi # define __COMPAR_FN_T 55*53ee8cc1Swenshuai.xi typedef int (*__compar_fn_t) (__const void *, __const void *); 56*53ee8cc1Swenshuai.xi 57*53ee8cc1Swenshuai.xi # ifdef __USE_GNU 58*53ee8cc1Swenshuai.xi typedef __compar_fn_t comparison_fn_t; 59*53ee8cc1Swenshuai.xi # endif 60*53ee8cc1Swenshuai.xi #endif 61*53ee8cc1Swenshuai.xi 62*53ee8cc1Swenshuai.xi /* Action which shall be performed in the call the hsearch. */ 63*53ee8cc1Swenshuai.xi typedef enum 64*53ee8cc1Swenshuai.xi { 65*53ee8cc1Swenshuai.xi FIND, 66*53ee8cc1Swenshuai.xi ENTER 67*53ee8cc1Swenshuai.xi } 68*53ee8cc1Swenshuai.xi ACTION; 69*53ee8cc1Swenshuai.xi 70*53ee8cc1Swenshuai.xi typedef struct entry 71*53ee8cc1Swenshuai.xi { 72*53ee8cc1Swenshuai.xi char *key; 73*53ee8cc1Swenshuai.xi void *data; 74*53ee8cc1Swenshuai.xi } 75*53ee8cc1Swenshuai.xi ENTRY; 76*53ee8cc1Swenshuai.xi 77*53ee8cc1Swenshuai.xi /* Opaque type for internal use. */ 78*53ee8cc1Swenshuai.xi struct _ENTRY; 79*53ee8cc1Swenshuai.xi 80*53ee8cc1Swenshuai.xi /* Family of hash table handling functions. The functions also 81*53ee8cc1Swenshuai.xi have reentrant counterparts ending with _r. The non-reentrant 82*53ee8cc1Swenshuai.xi functions all work on a signle internal hashing table. */ 83*53ee8cc1Swenshuai.xi 84*53ee8cc1Swenshuai.xi /* Search for entry matching ITEM.key in internal hash table. If 85*53ee8cc1Swenshuai.xi ACTION is `FIND' return found entry or signal error by returning 86*53ee8cc1Swenshuai.xi NULL. If ACTION is `ENTER' replace existing data (if any) with 87*53ee8cc1Swenshuai.xi ITEM.data. */ 88*53ee8cc1Swenshuai.xi extern ENTRY *hsearch (ENTRY __item, ACTION __action) __THROW; 89*53ee8cc1Swenshuai.xi 90*53ee8cc1Swenshuai.xi /* Create a new hashing table which will at most contain NEL elements. */ 91*53ee8cc1Swenshuai.xi extern int hcreate (size_t __nel) __THROW; 92*53ee8cc1Swenshuai.xi 93*53ee8cc1Swenshuai.xi /* Destroy current internal hashing table. */ 94*53ee8cc1Swenshuai.xi extern void hdestroy (void) __THROW; 95*53ee8cc1Swenshuai.xi 96*53ee8cc1Swenshuai.xi #ifdef __USE_GNU 97*53ee8cc1Swenshuai.xi /* Data type for reentrant functions. */ 98*53ee8cc1Swenshuai.xi struct hsearch_data 99*53ee8cc1Swenshuai.xi { 100*53ee8cc1Swenshuai.xi struct _ENTRY *table; 101*53ee8cc1Swenshuai.xi unsigned int size; 102*53ee8cc1Swenshuai.xi unsigned int filled; 103*53ee8cc1Swenshuai.xi }; 104*53ee8cc1Swenshuai.xi 105*53ee8cc1Swenshuai.xi /* Reentrant versions which can handle multiple hashing tables at the 106*53ee8cc1Swenshuai.xi same time. */ 107*53ee8cc1Swenshuai.xi extern int hsearch_r (ENTRY __item, ACTION __action, ENTRY **__retval, 108*53ee8cc1Swenshuai.xi struct hsearch_data *__htab) __THROW; 109*53ee8cc1Swenshuai.xi extern int hcreate_r (size_t __nel, struct hsearch_data *__htab) __THROW; 110*53ee8cc1Swenshuai.xi extern void hdestroy_r (struct hsearch_data *__htab) __THROW; 111*53ee8cc1Swenshuai.xi #endif 112*53ee8cc1Swenshuai.xi 113*53ee8cc1Swenshuai.xi 114*53ee8cc1Swenshuai.xi /* The tsearch routines are very interesting. They make many 115*53ee8cc1Swenshuai.xi assumptions about the compiler. It assumes that the first field 116*53ee8cc1Swenshuai.xi in node must be the "key" field, which points to the datum. 117*53ee8cc1Swenshuai.xi Everything depends on that. */ 118*53ee8cc1Swenshuai.xi /* For tsearch */ 119*53ee8cc1Swenshuai.xi typedef enum 120*53ee8cc1Swenshuai.xi { 121*53ee8cc1Swenshuai.xi preorder, 122*53ee8cc1Swenshuai.xi postorder, 123*53ee8cc1Swenshuai.xi endorder, 124*53ee8cc1Swenshuai.xi leaf 125*53ee8cc1Swenshuai.xi } 126*53ee8cc1Swenshuai.xi VISIT; 127*53ee8cc1Swenshuai.xi 128*53ee8cc1Swenshuai.xi /* Search for an entry matching the given KEY in the tree pointed to 129*53ee8cc1Swenshuai.xi by *ROOTP and insert a new element if not found. */ 130*53ee8cc1Swenshuai.xi extern void *tsearch (__const void *__key, void **__rootp, 131*53ee8cc1Swenshuai.xi __compar_fn_t __compar); 132*53ee8cc1Swenshuai.xi 133*53ee8cc1Swenshuai.xi /* Search for an entry matching the given KEY in the tree pointed to 134*53ee8cc1Swenshuai.xi by *ROOTP. If no matching entry is available return NULL. */ 135*53ee8cc1Swenshuai.xi extern void *tfind (__const void *__key, void *__const *__rootp, 136*53ee8cc1Swenshuai.xi __compar_fn_t __compar); 137*53ee8cc1Swenshuai.xi 138*53ee8cc1Swenshuai.xi /* Remove the element matching KEY from the tree pointed to by *ROOTP. */ 139*53ee8cc1Swenshuai.xi extern void *tdelete (__const void *__restrict __key, 140*53ee8cc1Swenshuai.xi void **__restrict __rootp, 141*53ee8cc1Swenshuai.xi __compar_fn_t __compar); 142*53ee8cc1Swenshuai.xi 143*53ee8cc1Swenshuai.xi #ifndef __ACTION_FN_T 144*53ee8cc1Swenshuai.xi # define __ACTION_FN_T 145*53ee8cc1Swenshuai.xi typedef void (*__action_fn_t) (__const void *__nodep, VISIT __value, 146*53ee8cc1Swenshuai.xi int __level); 147*53ee8cc1Swenshuai.xi #endif 148*53ee8cc1Swenshuai.xi 149*53ee8cc1Swenshuai.xi /* Walk through the whole tree and call the ACTION callback for every node 150*53ee8cc1Swenshuai.xi or leaf. */ 151*53ee8cc1Swenshuai.xi extern void twalk (__const void *__root, __action_fn_t __action); 152*53ee8cc1Swenshuai.xi 153*53ee8cc1Swenshuai.xi #ifdef __USE_GNU 154*53ee8cc1Swenshuai.xi /* Callback type for function to free a tree node. If the keys are atomic 155*53ee8cc1Swenshuai.xi data this function should do nothing. */ 156*53ee8cc1Swenshuai.xi typedef void (*__free_fn_t) (void *__nodep); 157*53ee8cc1Swenshuai.xi 158*53ee8cc1Swenshuai.xi /* Destroy the whole tree, call FREEFCT for each node or leaf. */ 159*53ee8cc1Swenshuai.xi extern void tdestroy (void *__root, __free_fn_t __freefct); 160*53ee8cc1Swenshuai.xi #endif 161*53ee8cc1Swenshuai.xi 162*53ee8cc1Swenshuai.xi 163*53ee8cc1Swenshuai.xi /* Perform linear search for KEY by comparing by COMPAR in an array 164*53ee8cc1Swenshuai.xi [BASE,BASE+NMEMB*SIZE). */ 165*53ee8cc1Swenshuai.xi extern void *lfind (__const void *__key, __const void *__base, 166*53ee8cc1Swenshuai.xi size_t *__nmemb, size_t __size, __compar_fn_t __compar); 167*53ee8cc1Swenshuai.xi 168*53ee8cc1Swenshuai.xi /* Perform linear search for KEY by comparing by COMPAR function in 169*53ee8cc1Swenshuai.xi array [BASE,BASE+NMEMB*SIZE) and insert entry if not found. */ 170*53ee8cc1Swenshuai.xi extern void *lsearch (__const void *__key, void *__base, 171*53ee8cc1Swenshuai.xi size_t *__nmemb, size_t __size, __compar_fn_t __compar); 172*53ee8cc1Swenshuai.xi 173*53ee8cc1Swenshuai.xi __END_DECLS 174*53ee8cc1Swenshuai.xi 175*53ee8cc1Swenshuai.xi #endif /* search.h */ 176