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