78e3980ec87aa87205ca9cc207eb1e02df979ea8
[platform/upstream/coreutils.git] / lib / search_.h
1 /* For use with hsearch(3).  */
2 #ifndef __COMPAR_FN_T
3 # define __COMPAR_FN_T
4 typedef int (*__compar_fn_t) (const void *, const void *);
5
6 # ifdef __USE_GNU
7 typedef __compar_fn_t comparison_fn_t;
8 # endif
9 #endif
10
11 /* The tsearch routines are very interesting. They make many
12    assumptions about the compiler.  It assumes that the first field
13    in node must be the "key" field, which points to the datum.
14    Everything depends on that.  */
15 /* For tsearch */
16 typedef enum
17 {
18   preorder,
19   postorder,
20   endorder,
21   leaf
22 }
23 VISIT;
24
25 /* GCC 2.95 and later have "__restrict"; C99 compilers have
26    "restrict", and "configure" may have defined "restrict".  */
27 #ifndef __restrict
28 # if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
29 #  if defined restrict || 199901L <= __STDC_VERSION__
30 #   define __restrict restrict
31 #  else
32 #   define __restrict
33 #  endif
34 # endif
35 #endif
36
37 /* Search for an entry matching the given KEY in the tree pointed to
38    by *ROOTP and insert a new element if not found.  */
39 extern void *tsearch (const void *__key, void **__rootp,
40                       __compar_fn_t __compar);
41
42 /* Search for an entry matching the given KEY in the tree pointed to
43    by *ROOTP.  If no matching entry is available return NULL.  */
44 extern void *tfind (const void *__key, void *const *__rootp,
45                     __compar_fn_t __compar);
46
47 /* Remove the element matching KEY from the tree pointed to by *ROOTP.  */
48 extern void *tdelete (const void *__restrict __key,
49                       void **__restrict __rootp,
50                       __compar_fn_t __compar);
51
52 #ifndef __ACTION_FN_T
53 # define __ACTION_FN_T
54 typedef void (*__action_fn_t) (const void *__nodep, VISIT __value,
55                                int __level);
56 #endif
57
58 /* Walk through the whole tree and call the ACTION callback for every node
59    or leaf.  */
60 extern void twalk (const void *__root, __action_fn_t __action);
61
62 #ifdef __USE_GNU
63 /* Callback type for function to free a tree node.  If the keys are atomic
64    data this function should do nothing.  */
65 typedef void (*__free_fn_t) (void *__nodep);
66
67 /* Destroy the whole tree, call FREEFCT for each node or leaf.  */
68 extern void tdestroy (void *__root, __free_fn_t __freefct);
69 #endif