have bootstrap initialize git-merge-changelog if found
[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 @PRAGMA_SYSTEM_HEADER@
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 @REPLACE_TSEARCH@
40 #  define tsearch rpl_tsearch
41 #  define tfind rpl_tfind
42 #  define tdelete rpl_tdelete
43 #  define twalk rpl_twalk
44 # endif
45 # if !@HAVE_TSEARCH@ || @REPLACE_TSEARCH@
46
47 /* See <http://www.opengroup.org/susv3xbd/search.h.html>,
48        <http://www.opengroup.org/susv3xsh/tsearch.html>
49    for details.  */
50
51 #  if !@HAVE_TSEARCH@
52 typedef enum
53 {
54   preorder,
55   postorder,
56   endorder,
57   leaf
58 }
59 VISIT;
60 #  endif
61
62 /* Searches an element in the tree *VROOTP that compares equal to KEY.
63    If one is found, it is returned.  Otherwise, a new element equal to KEY
64    is inserted in the tree and is returned.  */
65 extern void * tsearch (const void *key, void **vrootp,
66                        int (*compar) (const void *, const void *));
67
68 /* Searches an element in the tree *VROOTP that compares equal to KEY.
69    If one is found, it is returned.  Otherwise, NULL is returned.  */
70 extern void * tfind (const void *key, void *const *vrootp,
71                      int (*compar) (const void *, const void *));
72
73 /* Searches an element in the tree *VROOTP that compares equal to KEY.
74    If one is found, it is removed from the tree, and its parent node is
75    returned.  Otherwise, NULL is returned.  */
76 extern void * tdelete (const void *key, void **vrootp,
77                        int (*compar) (const void *, const void *));
78
79 /* Perform a depth-first, left-to-right traversal of the tree VROOT.
80    The ACTION function is called:
81      - for non-leaf nodes: 3 times, before the left subtree traversal,
82        after the left subtree traversal but before the right subtree traversal,
83        and after the right subtree traversal,
84      - for leaf nodes: once.
85    The arguments passed to ACTION are:
86      1. the node; it can be casted to a 'const void * const *', i.e. into a
87         pointer to the key,
88      2. an indicator which visit of the node this is,
89      3. the level of the node in the tree (0 for the root).  */
90 extern void twalk (const void *vroot,
91                    void (*action) (const void *, VISIT, int));
92
93 # endif
94 #elif defined GNULIB_POSIXCHECK
95 # undef tsearch
96 # define tsearch(k,v,c) \
97     (GL_LINK_WARNING ("tsearch is unportable - " \
98                       "use gnulib module tsearch for portability"), \
99      tsearch (k, v, c))
100 # undef tfind
101 # define tfind(k,v,c) \
102     (GL_LINK_WARNING ("tfind is unportable - " \
103                       "use gnulib module tsearch for portability"), \
104      tfind (k, v, c))
105 # undef tdelete
106 # define tdelete(k,v,c) \
107     (GL_LINK_WARNING ("tdelete is unportable - " \
108                       "use gnulib module tsearch for portability"), \
109      tdelete (k, v, c))
110 # undef twalk
111 # define twalk(v,a) \
112     (GL_LINK_WARNING ("twalk is unportable - " \
113                       "use gnulib module tsearch for portability"), \
114      twalk (v, a))
115 #endif
116
117
118 #ifdef __cplusplus
119 }
120 #endif
121
122 #endif /* _GL_SEARCH_H */
123 #endif /* _GL_SEARCH_H */