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