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