1 /* A GNU-like <search.h>.
3 Copyright (C) 2007-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU 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
34 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
36 /* The definition of _GL_ARG_NONNULL is copied here. */
38 /* The definition of _GL_WARN_ON_USE is copied here. */
41 #if defined _WIN32 && !defined __CYGWIN__
46 #if defined _WIN32 && !defined __CYGWIN__
48 # define lsearch _lsearch
53 # if @REPLACE_TSEARCH@
54 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
55 # define tsearch rpl_tsearch
56 # define tfind rpl_tfind
57 # define tdelete rpl_tdelete
58 # define twalk rpl_twalk
62 /* See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/search.h.html>
63 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/tsearch.html>
66 # if !@HAVE_TYPE_VISIT@
67 # if !GNULIB_defined_search_types
76 # define GNULIB_defined_search_types 1
83 # if !GNULIB_defined_search_fn_types
84 typedef int (*_gl_search_compar_fn
) (const void *, const void *);
85 typedef void (*_gl_search_action_fn
) (const void *, VISIT
, int);
86 # define GNULIB_defined_search_fn_types 1
92 /* Searches an element in the tree *VROOTP that compares equal to KEY.
93 If one is found, it is returned. Otherwise, a new element equal to KEY
94 is inserted in the tree and is returned. */
95 # if @REPLACE_TSEARCH@
96 _GL_FUNCDECL_RPL (tsearch
, void *,
97 (const void *key
, void **vrootp
,
98 _gl_search_compar_fn compar
)
99 _GL_ARG_NONNULL ((1, 2, 3)));
100 _GL_CXXALIAS_RPL (tsearch
, void *,
101 (const void *key
, void **vrootp
,
102 _gl_search_compar_fn compar
));
105 _GL_FUNCDECL_SYS (tsearch
, void *,
106 (const void *key
, void **vrootp
,
107 _gl_search_compar_fn compar
)
108 _GL_ARG_NONNULL ((1, 2, 3)));
110 _GL_CXXALIAS_SYS (tsearch
, void *,
111 (const void *key
, void **vrootp
,
112 _gl_search_compar_fn compar
));
114 _GL_CXXALIASWARN (tsearch
);
116 /* Searches an element in the tree *VROOTP that compares equal to KEY.
117 If one is found, it is returned. Otherwise, NULL is returned. */
118 # if @REPLACE_TSEARCH@
119 _GL_FUNCDECL_RPL (tfind
, void *,
120 (const void *key
, void *const *vrootp
,
121 _gl_search_compar_fn compar
)
122 _GL_ARG_NONNULL ((1, 2, 3)));
123 _GL_CXXALIAS_RPL (tfind
, void *,
124 (const void *key
, void *const *vrootp
,
125 _gl_search_compar_fn compar
));
128 _GL_FUNCDECL_SYS (tfind
, void *,
129 (const void *key
, void *const *vrootp
,
130 _gl_search_compar_fn compar
)
131 _GL_ARG_NONNULL ((1, 2, 3)));
133 /* Need to cast, because on Cygwin 1.5.x systems, the second parameter is
135 _GL_CXXALIAS_SYS_CAST (tfind
, void *,
136 (const void *key
, void *const *vrootp
,
137 _gl_search_compar_fn compar
));
139 _GL_CXXALIASWARN (tfind
);
141 /* Searches an element in the tree *VROOTP that compares equal to KEY.
142 If one is found, it is removed from the tree, and its parent node is
143 returned. Otherwise, NULL is returned. */
144 # if @REPLACE_TSEARCH@
145 _GL_FUNCDECL_RPL (tdelete
, void *,
146 (const void *restrict key
, void **restrict vrootp
,
147 _gl_search_compar_fn compar
)
148 _GL_ARG_NONNULL ((1, 2, 3)));
149 _GL_CXXALIAS_RPL (tdelete
, void *,
150 (const void *restrict key
, void **restrict vrootp
,
151 _gl_search_compar_fn compar
));
154 _GL_FUNCDECL_SYS (tdelete
, void *,
155 (const void *restrict key
, void **restrict vrootp
,
156 _gl_search_compar_fn compar
)
157 _GL_ARG_NONNULL ((1, 2, 3)));
159 _GL_CXXALIAS_SYS (tdelete
, void *,
160 (const void *restrict key
, void **restrict vrootp
,
161 _gl_search_compar_fn compar
));
163 _GL_CXXALIASWARN (tdelete
);
165 /* Perform a depth-first, left-to-right traversal of the tree VROOT.
166 The ACTION function is called:
167 - for non-leaf nodes: 3 times, before the left subtree traversal,
168 after the left subtree traversal but before the right subtree traversal,
169 and after the right subtree traversal,
170 - for leaf nodes: once.
171 The arguments passed to ACTION are:
172 1. the node; it can be cast to a 'const void * const *', i.e. into a
174 2. an indicator which visit of the node this is,
175 3. the level of the node in the tree (0 for the root). */
176 # if @REPLACE_TSEARCH@
177 _GL_FUNCDECL_RPL (twalk
, void,
178 (const void *vroot
, _gl_search_action_fn action
)
179 _GL_ARG_NONNULL ((2)));
180 _GL_CXXALIAS_RPL (twalk
, void,
181 (const void *vroot
, _gl_search_action_fn action
));
184 _GL_FUNCDECL_SYS (twalk
, void,
185 (const void *vroot
, _gl_search_action_fn action
)
186 _GL_ARG_NONNULL ((2)));
188 _GL_CXXALIAS_SYS (twalk
, void,
189 (const void *vroot
, _gl_search_action_fn action
));
191 _GL_CXXALIASWARN (twalk
);
193 /* Flags used by tsearch.c. */
194 # define GNULIB_defined_tsearch (@REPLACE_TSEARCH@ || !@HAVE_TSEARCH@)
195 # define GNULIB_defined_twalk (@REPLACE_TSEARCH@ || !@HAVE_TWALK@)
197 #elif defined GNULIB_POSIXCHECK
199 # if HAVE_RAW_DECL_TSEARCH
200 _GL_WARN_ON_USE (tsearch
, "tsearch is unportable - "
201 "use gnulib module tsearch for portability");
204 # if HAVE_RAW_DECL_TFIND
205 _GL_WARN_ON_USE (tfind
, "tfind is unportable - "
206 "use gnulib module tsearch for portability");
209 # if HAVE_RAW_DECL_TDELETE
210 _GL_WARN_ON_USE (tdelete
, "tdelete is unportable - "
211 "use gnulib module tsearch for portability");
214 # if HAVE_RAW_DECL_TWALK
215 _GL_WARN_ON_USE (twalk
, "twalk is unportable - "
216 "use gnulib module tsearch for portability");
221 #endif /* _@GUARD_PREFIX@_SEARCH_H */
222 #endif /* _@GUARD_PREFIX@_SEARCH_H */