1 /* A GNU-like <search.h>.
3 Copyright (C) 2007-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _@GUARD_PREFIX@_SEARCH_H
21 @PRAGMA_SYSTEM_HEADER@
25 /* The include_next requires a split double-inclusion guard. */
27 # @INCLUDE_NEXT@ @NEXT_SEARCH_H@
30 #ifndef _@GUARD_PREFIX@_SEARCH_H
31 #define _@GUARD_PREFIX@_SEARCH_H
33 /* This file uses GNULIB_POSIXCHECK, HAVE_RAW_DECL_*. */
34 #if !_GL_CONFIG_H_INCLUDED
35 #error "Please include config.h first."
39 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
41 /* The definition of _GL_ARG_NONNULL is copied here. */
43 /* The definition of _GL_WARN_ON_USE is copied here. */
49 #if !GNULIB_defined_lsearch_fn_types
50 typedef int (*_gl_lsearch_compar_fn
) (const void *, const void *);
51 # define GNULIB_defined_lsearch_fn_types 1
57 #if @GNULIB_MDA_LFIND@
58 /* On native Windows, map 'lfind' to '_lfind', so that -loldnames is not
59 required. In C++ with GNULIB_NAMESPACE, avoid differences between
60 platforms by defining GNULIB_NAMESPACE::lfind always. */
61 # if defined _WIN32 && !defined __CYGWIN__
62 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
66 _GL_CXXALIAS_MDA (lfind
, void *,
67 (const void *key
, const void *base
, unsigned int *nmemb
,
69 _gl_lsearch_compar_fn compar
));
71 _GL_CXXALIAS_SYS (lfind
, void *,
72 (const void *key
, const void *base
, size_t *nmemb
,
74 _gl_lsearch_compar_fn compar
));
76 _GL_CXXALIASWARN (lfind
);
79 #if @GNULIB_MDA_LSEARCH@
80 /* On native Windows, map 'lsearch' to '_lsearch', so that -loldnames is not
81 required. In C++ with GNULIB_NAMESPACE, avoid differences between
82 platforms by defining GNULIB_NAMESPACE::lsearch always. */
83 # if defined _WIN32 && !defined __CYGWIN__
84 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
86 # define lsearch _lsearch
88 _GL_CXXALIAS_MDA (lsearch
, void *,
89 (const void *key
, void *base
, unsigned int *nmemb
,
91 _gl_lsearch_compar_fn compar
));
93 _GL_CXXALIAS_SYS (lsearch
, void *,
94 (const void *key
, void *base
, size_t *nmemb
,
96 _gl_lsearch_compar_fn compar
));
98 _GL_CXXALIASWARN (lsearch
);
103 # if @REPLACE_TSEARCH@
104 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
105 # define tsearch rpl_tsearch
106 # define tfind rpl_tfind
107 # define tdelete rpl_tdelete
111 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
112 # define twalk rpl_twalk
116 /* See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/search.h.html>
117 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/tsearch.html>
120 # if !@HAVE_TYPE_VISIT@
121 # if !GNULIB_defined_search_types
130 # define GNULIB_defined_search_types 1
137 # if !GNULIB_defined_search_fn_types
138 typedef int (*_gl_search_compar_fn
) (const void *, const void *);
139 typedef void (*_gl_search_action_fn
) (const void *, VISIT
, int);
140 # define GNULIB_defined_search_fn_types 1
146 /* Searches an element in the tree *VROOTP that compares equal to KEY.
147 If one is found, it is returned. Otherwise, a new element equal to KEY
148 is inserted in the tree and is returned. */
149 # if @REPLACE_TSEARCH@
150 _GL_FUNCDECL_RPL (tsearch
, void *,
151 (const void *key
, void **vrootp
,
152 _gl_search_compar_fn compar
)
153 _GL_ARG_NONNULL ((1, 2, 3)));
154 _GL_CXXALIAS_RPL (tsearch
, void *,
155 (const void *key
, void **vrootp
,
156 _gl_search_compar_fn compar
));
159 _GL_FUNCDECL_SYS (tsearch
, void *,
160 (const void *key
, void **vrootp
,
161 _gl_search_compar_fn compar
)
162 _GL_ARG_NONNULL ((1, 2, 3)));
164 _GL_CXXALIAS_SYS (tsearch
, void *,
165 (const void *key
, void **vrootp
,
166 _gl_search_compar_fn compar
));
169 _GL_CXXALIASWARN (tsearch
);
172 /* Searches an element in the tree *VROOTP that compares equal to KEY.
173 If one is found, it is returned. Otherwise, NULL is returned. */
174 # if @REPLACE_TSEARCH@
175 _GL_FUNCDECL_RPL (tfind
, void *,
176 (const void *key
, void *const *vrootp
,
177 _gl_search_compar_fn compar
)
178 _GL_ARG_NONNULL ((1, 2, 3)));
179 _GL_CXXALIAS_RPL (tfind
, void *,
180 (const void *key
, void *const *vrootp
,
181 _gl_search_compar_fn compar
));
184 _GL_FUNCDECL_SYS (tfind
, void *,
185 (const void *key
, void *const *vrootp
,
186 _gl_search_compar_fn compar
)
187 _GL_ARG_NONNULL ((1, 2, 3)));
189 /* Need to cast, because on Cygwin 1.5.x systems, the second parameter is
191 _GL_CXXALIAS_SYS_CAST (tfind
, void *,
192 (const void *key
, void *const *vrootp
,
193 _gl_search_compar_fn compar
));
196 _GL_CXXALIASWARN (tfind
);
199 /* Searches an element in the tree *VROOTP that compares equal to KEY.
200 If one is found, it is removed from the tree, and its parent node is
201 returned. Otherwise, NULL is returned. */
202 # if @REPLACE_TSEARCH@
203 _GL_FUNCDECL_RPL (tdelete
, void *,
204 (const void *restrict key
, void **restrict vrootp
,
205 _gl_search_compar_fn compar
)
206 _GL_ARG_NONNULL ((1, 2, 3)));
207 _GL_CXXALIAS_RPL (tdelete
, void *,
208 (const void *restrict key
, void **restrict vrootp
,
209 _gl_search_compar_fn compar
));
212 _GL_FUNCDECL_SYS (tdelete
, void *,
213 (const void *restrict key
, void **restrict vrootp
,
214 _gl_search_compar_fn compar
)
215 _GL_ARG_NONNULL ((1, 2, 3)));
217 _GL_CXXALIAS_SYS (tdelete
, void *,
218 (const void *restrict key
, void **restrict vrootp
,
219 _gl_search_compar_fn compar
));
222 _GL_CXXALIASWARN (tdelete
);
225 /* Perform a depth-first, left-to-right traversal of the tree VROOT.
226 The ACTION function is called:
227 - for non-leaf nodes: 3 times, before the left subtree traversal,
228 after the left subtree traversal but before the right subtree traversal,
229 and after the right subtree traversal,
230 - for leaf nodes: once.
231 The arguments passed to ACTION are:
232 1. the node; it can be cast to a 'const void * const *', i.e. into a
234 2. an indicator which visit of the node this is,
235 3. the level of the node in the tree (0 for the root). */
237 _GL_FUNCDECL_RPL (twalk
, void,
238 (const void *vroot
, _gl_search_action_fn action
)
239 _GL_ARG_NONNULL ((2)));
240 _GL_CXXALIAS_RPL (twalk
, void,
241 (const void *vroot
, _gl_search_action_fn action
));
244 _GL_FUNCDECL_SYS (twalk
, void,
245 (const void *vroot
, _gl_search_action_fn action
)
246 _GL_ARG_NONNULL ((2)));
248 _GL_CXXALIAS_SYS (twalk
, void,
249 (const void *vroot
, _gl_search_action_fn action
));
252 _GL_CXXALIASWARN (twalk
);
255 /* Flags used by tsearch.c. */
256 # define GNULIB_defined_tsearch (@REPLACE_TSEARCH@ || !@HAVE_TSEARCH@)
257 # define GNULIB_defined_twalk (@REPLACE_TWALK@ || !@HAVE_TWALK@)
259 #elif defined GNULIB_POSIXCHECK
261 # if HAVE_RAW_DECL_TSEARCH
262 _GL_WARN_ON_USE (tsearch
, "tsearch is unportable - "
263 "use gnulib module tsearch for portability");
266 # if HAVE_RAW_DECL_TFIND
267 _GL_WARN_ON_USE (tfind
, "tfind is unportable - "
268 "use gnulib module tsearch for portability");
271 # if HAVE_RAW_DECL_TDELETE
272 _GL_WARN_ON_USE (tdelete
, "tdelete is unportable - "
273 "use gnulib module tsearch for portability");
276 # if HAVE_RAW_DECL_TWALK
277 _GL_WARN_ON_USE (twalk
, "twalk is unportable - "
278 "use gnulib module tsearch for portability");
283 #endif /* _@GUARD_PREFIX@_SEARCH_H */
284 #endif /* _@GUARD_PREFIX@_SEARCH_H */