clarify the purpose of this project
[nyanglibc.git] / include / search.h
blobce6b2ab5682e30db1db6ff557d51096a04911a39
1 /* Declarations for System V style searching functions.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef _SEARCH_H
20 #define _SEARCH_H 1
22 #include <features.h>
24 #define __need_size_t
25 #include <stddef.h>
27 __BEGIN_DECLS
29 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
30 /* Prototype structure for a linked-list data structure.
31 This is the type used by the `insque' and `remque' functions. */
33 # ifdef __USE_GNU
34 struct qelem
36 struct qelem *q_forw;
37 struct qelem *q_back;
38 char q_data[1];
40 # endif
43 /* Insert ELEM into a doubly-linked list, after PREV. */
44 extern void insque (void *__elem, void *__prev) __THROW;
46 /* Unlink ELEM from the doubly-linked list that it is in. */
47 extern void remque (void *__elem) __THROW;
48 #endif
51 /* For use with hsearch(3). */
52 #ifndef __COMPAR_FN_T
53 # define __COMPAR_FN_T
54 typedef int (*__compar_fn_t) (const void *, const void *);
56 # ifdef __USE_GNU
57 typedef __compar_fn_t comparison_fn_t;
58 # endif
59 #endif
61 /* Action which shall be performed in the call the hsearch. */
62 typedef enum
64 FIND,
65 ENTER
67 ACTION;
69 typedef struct entry
71 char *key;
72 void *data;
74 ENTRY;
76 /* Opaque type for internal use. */
77 struct _ENTRY;
79 /* Family of hash table handling functions. The functions also
80 have reentrant counterparts ending with _r. The non-reentrant
81 functions all work on a signle internal hashing table. */
83 /* Search for entry matching ITEM.key in internal hash table. If
84 ACTION is `FIND' return found entry or signal error by returning
85 NULL. If ACTION is `ENTER' replace existing data (if any) with
86 ITEM.data. */
87 extern ENTRY *hsearch (ENTRY __item, ACTION __action) __THROW;
89 /* Create a new hashing table which will at most contain NEL elements. */
90 extern int hcreate (size_t __nel) __THROW;
92 /* Destroy current internal hashing table. */
93 extern void hdestroy (void) __THROW;
95 #ifdef __USE_GNU
96 /* Data type for reentrant functions. */
97 struct hsearch_data
99 struct _ENTRY *table;
100 unsigned int size;
101 unsigned int filled;
104 /* Reentrant versions which can handle multiple hashing tables at the
105 same time. */
106 extern int hsearch_r (ENTRY __item, ACTION __action, ENTRY **__retval,
107 struct hsearch_data *__htab) __THROW;
108 extern int hcreate_r (size_t __nel, struct hsearch_data *__htab) __THROW;
109 extern void hdestroy_r (struct hsearch_data *__htab) __THROW;
110 #endif
113 /* The tsearch routines are very interesting. They make many
114 assumptions about the compiler. It assumes that the first field
115 in node must be the "key" field, which points to the datum.
116 Everything depends on that. */
117 /* For tsearch */
118 typedef enum
120 preorder,
121 postorder,
122 endorder,
123 leaf
125 VISIT;
127 /* Search for an entry matching the given KEY in the tree pointed to
128 by *ROOTP and insert a new element if not found. */
129 extern void *tsearch (const void *__key, void **__rootp,
130 __compar_fn_t __compar);
132 /* Search for an entry matching the given KEY in the tree pointed to
133 by *ROOTP. If no matching entry is available return NULL. */
134 extern void *tfind (const void *__key, void *const *__rootp,
135 __compar_fn_t __compar);
137 /* Remove the element matching KEY from the tree pointed to by *ROOTP. */
138 extern void *tdelete (const void *__restrict __key,
139 void **__restrict __rootp,
140 __compar_fn_t __compar);
142 #ifndef __ACTION_FN_T
143 # define __ACTION_FN_T
144 typedef void (*__action_fn_t) (const void *__nodep, VISIT __value,
145 int __level);
146 #endif
148 /* Walk through the whole tree and call the ACTION callback for every node
149 or leaf. */
150 extern void twalk (const void *__root, __action_fn_t __action);
152 #ifdef __USE_GNU
153 /* Like twalk, but pass down a closure parameter instead of the
154 level. */
155 extern void twalk_r (const void *__root,
156 void (*) (const void *__nodep, VISIT __value,
157 void *__closure),
158 void *__closure);
160 /* Callback type for function to free a tree node. If the keys are atomic
161 data this function should do nothing. */
162 typedef void (*__free_fn_t) (void *__nodep);
164 /* Destroy the whole tree, call FREEFCT for each node or leaf. */
165 extern void tdestroy (void *__root, __free_fn_t __freefct);
166 #endif
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);
174 /* Perform linear search for KEY by comparing by COMPAR function in
175 array [BASE,BASE+NMEMB*SIZE) and insert entry if not found. */
176 extern void *lsearch (const void *__key, void *__base,
177 size_t *__nmemb, size_t __size, __compar_fn_t __compar);
179 __END_DECLS
181 #endif /* search.h */