Work around OpenBSD 4.0 tdelete() bug.
[gnulib.git] / lib / search.in.h
1 /* A GNU-like <search.h>.
2
3    Copyright (C) 2007-2008 Free Software Foundation, Inc.
4
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.
9
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.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _GL_SEARCH_H
19
20 /* The include_next requires a split double-inclusion guard.  */
21 #if @HAVE_SEARCH_H@
22 # @INCLUDE_NEXT@ @NEXT_SEARCH_H@
23 #endif
24
25 #ifndef _GL_SEARCH_H
26 #define _GL_SEARCH_H
27
28
29 /* The definition of GL_LINK_WARNING is copied here.  */
30
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36
37 #if @GNULIB_TSEARCH@
38 # if @REPLACE_TSEARCH@
39 #  define tsearch rpl_tsearch
40 #  define tfind rpl_tfind
41 #  define tdelete rpl_tdelete
42 #  define twalk rpl_twalk
43 # endif
44 # if !@HAVE_TSEARCH@ || @REPLACE_TSEARCH@
45
46 /* See <http://www.opengroup.org/susv3xbd/search.h.html>,
47        <http://www.opengroup.org/susv3xsh/tsearch.html>
48    for details.  */
49
50 #  if !@HAVE_TSEARCH@
51 typedef enum
52 {
53   preorder,
54   postorder,
55   endorder,
56   leaf
57 }
58 VISIT;
59 #  endif
60
61 /* Searches an element in the tree *VROOTP that compares equal to KEY.
62    If one is found, it is returned.  Otherwise, a new element equal to KEY
63    is inserted in the tree and is returned.  */
64 extern void * tsearch (const void *key, void **vrootp,
65                        int (*compar) (const void *, const void *));
66
67 /* Searches an element in the tree *VROOTP that compares equal to KEY.
68    If one is found, it is returned.  Otherwise, NULL is returned.  */
69 extern void * tfind (const void *key, void *const *vrootp,
70                      int (*compar) (const void *, const void *));
71
72 /* Searches an element in the tree *VROOTP that compares equal to KEY.
73    If one is found, it is removed from the tree, and its parent node is
74    returned.  Otherwise, NULL is returned.  */
75 extern void * tdelete (const void *key, void **vrootp,
76                        int (*compar) (const void *, const void *));
77
78 /* Perform a depth-first, left-to-right traversal of the tree VROOT.
79    The ACTION function is called:
80      - for non-leaf nodes: 3 times, before the left subtree traversal,
81        after the left subtree traversal but before the right subtree traversal,
82        and after the right subtree traversal,
83      - for leaf nodes: once.
84    The arguments passed to ACTION are:
85      1. the node; it can be casted to a 'const void * const *', i.e. into a
86         pointer to the key,
87      2. an indicator which visit of the node this is,
88      3. the level of the node in the tree (0 for the root).  */
89 extern void twalk (const void *vroot,
90                    void (*action) (const void *, VISIT, int));
91
92 # endif
93 #elif defined GNULIB_POSIXCHECK
94 # undef tsearch
95 # define tsearch(k,v,c) \
96     (GL_LINK_WARNING ("tsearch is unportable - " \
97                       "use gnulib module tsearch for portability"), \
98      tsearch (k, v, c))
99 # undef tfind
100 # define tfind(k,v,c) \
101     (GL_LINK_WARNING ("tfind is unportable - " \
102                       "use gnulib module tsearch for portability"), \
103      tfind (k, v, c))
104 # undef tdelete
105 # define tdelete(k,v,c) \
106     (GL_LINK_WARNING ("tdelete is unportable - " \
107                       "use gnulib module tsearch for portability"), \
108      tdelete (k, v, c))
109 # undef twalk
110 # define twalk(v,a) \
111     (GL_LINK_WARNING ("twalk is unportable - " \
112                       "use gnulib module tsearch for portability"), \
113      twalk (v, a))
114 #endif
115
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #endif /* _GL_SEARCH_H */
122 #endif /* _GL_SEARCH_H */