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