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