Update.
[platform/upstream/glibc.git] / misc / search.h
1 /* Declarations for System V style searching functions.
2    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #ifndef _SEARCH_H
21 #define _SEARCH_H 1
22
23 #include <sys/cdefs.h>
24
25 #define __need_size_t
26 #define __need_NULL
27 #include <stddef.h>
28
29 __BEGIN_DECLS
30
31 #if defined(__USE_SVID) || defined(__USE_XOPEN_EXTENDED)
32 /* Prototype structure for a linked-list data structure.
33    This is the type used by the `insque' and `remque' functions.  */
34
35 struct qelem
36   {
37     struct qelem *q_forw;
38     struct qelem *q_back;
39     char q_data[1];
40   };
41
42
43 /* Insert ELEM into a doubly-linked list, after PREV.  */
44 extern void insque __P ((void *__elem, void *__prev));
45
46 /* Unlink ELEM from the doubly-linked list that it is in.  */
47 extern void remque __P ((void *__elem));
48 #endif
49
50
51 /* For use with hsearch(3).  */
52 #ifndef __COMPAR_FN_T
53 #define __COMPAR_FN_T
54 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
55 #endif
56
57 /* Action which shall be performed in the call the hsearch.  */
58 typedef enum
59   {
60     FIND,
61     ENTER
62   }
63 ACTION;
64
65 typedef struct entry
66   {
67     char *key;
68     char *data;
69   }
70 ENTRY;
71
72 /* Opaque type for internal use.  */
73 struct _ENTRY;
74
75 /* Data type for reentrant functions.  */
76 struct hsearch_data
77   {
78     struct _ENTRY *table;
79     unsigned int size;
80     unsigned int filled;
81   };
82
83 /* Family of hash table handling functions.  The functions also have
84    reentrant counterparts ending with _r.  */
85 extern ENTRY *hsearch __P ((ENTRY __item, ACTION __action));
86 extern int hcreate __P ((size_t __nel));
87 extern void hdestroy __P ((void));
88
89 extern int hsearch_r __P ((ENTRY __item, ACTION __action, ENTRY **__retval,
90                            struct hsearch_data *__htab));
91 extern int hcreate_r __P ((size_t __nel, struct hsearch_data *htab));
92 extern void hdestroy_r __P ((struct hsearch_data *htab));
93
94
95 /* The tsearch routines are very interesting. They make many
96    assumptions about the compiler.  It assumes that the first field
97    in node must be the "key" field, which points to the datum.
98    Everything depends on that.  */
99 /* For tsearch */
100 typedef enum
101 {
102   preorder,
103   postorder,
104   endorder,
105   leaf
106 }
107 VISIT;
108
109 /* Search for an entry matching the given KEY in the tree pointed to
110    by *ROOTP and insert a new element if not found.  */
111 extern void *tsearch __P ((__const void * __key, void **__rootp,
112                            __compar_fn_t compar));
113 extern void *__tsearch __P ((__const void * __key, void **__rootp,
114                              __compar_fn_t compar));
115
116 /* Search for an entry matching the given KEY in the tree pointed to
117    by *ROOTP.  If no matching entry is available return NULL.  */
118 extern void *tfind __P ((__const void * __key, void *__const * __rootp,
119                          __compar_fn_t compar));
120 extern void *__tfind __P ((__const void * __key, void *__const * __rootp,
121                            __compar_fn_t compar));
122
123 /* Remove the element matching KEY from the tree pointed to by *ROOTP.  */
124 extern void *tdelete __P ((__const void * __key, void ** __rootp,
125                            __compar_fn_t compar));
126 extern void *__tdelete __P ((__const void * __key, void ** __rootp,
127                              __compar_fn_t compar));
128
129 #ifndef __ACTION_FN_T
130 #define __ACTION_FN_T
131 typedef void (*__action_fn_t) __P ((__const void *__nodep,
132                                     VISIT __value,
133                                     int __level));
134 #endif
135
136 /* Walk through the whole tree and call the ACTION callback for every node
137    or leaf.  */
138 extern void twalk __P ((__const void * __root, __action_fn_t action));
139
140 extern void __twalk __P ((__const void * __root, __action_fn_t action));
141
142 #ifdef __USE_GNU
143 /* Callback type for function to free a tree node.  If the keys are atomic
144    data this function should do nothing.  */
145 typedef void (*__free_fn_t) __P ((void *__nodep));
146
147 /* Destroy the whole tree, call FREEFCT for each node or leaf.  */
148 extern void __tdestroy __P ((void *__root, __free_fn_t freefct));
149 extern void tdestroy __P ((void *__root, __free_fn_t freefct));
150 #endif
151
152
153 /* Perform linear search for KEY by comparing by COMPAR in an array
154    [BASE,BASE+NMEMB*SIZE).  */
155 extern void * lfind __P ((__const void *__key, __const void *__base,
156                           size_t *__nmemb, size_t __size,
157                           __compar_fn_t __compar));
158
159 /* Perform linear search for KEY by comparing by COMPAR function in
160    array [BASE,BASE+NMEMB*SIZE) and insert entry if not found.  */
161 extern void * lsearch __P ((__const void *__key, void *__base, size_t *__nmemb,
162                             size_t __size, __compar_fn_t __compar));
163
164 __END_DECLS
165
166 #endif /* search.h */