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