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