update from main archive 960921
[platform/upstream/glibc.git] / misc / search.h
1 /* search.h -- declarations for System V style searching functions.
2 Copyright (C) 1995, 1996 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
17 not, 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 /* Prototype structure for a linked-list data structure.
32    This is the type used by the `insque' and `remque' functions.  */
33
34 struct qelem
35   {
36     struct qelem *q_forw;
37     struct qelem *q_back;
38     char q_data[1];
39   };
40
41
42 /* Insert ELEM into a doubly-linked list, after PREV.  */
43 extern void insque __P ((struct qelem *__elem, struct qelem *__prev));
44
45 /* Unlink ELEM from the doubly-linked list that it is in.  */
46 extern void remque __P ((struct qelem *__elem));
47
48
49 /* For use with hsearch(3).  */
50 #ifndef __COMPAR_FN_T
51 #define __COMPAR_FN_T
52 typedef int (*__compar_fn_t) __P ((__const __ptr_t, __const __ptr_t));
53 #endif
54
55 /* Action which shall be performed in the call the hsearch.  */
56 typedef enum
57   {
58     FIND,
59     ENTER
60   }
61 ACTION;
62
63 typedef struct entry
64   {
65     char *key;
66     char *data;
67   }
68 ENTRY;
69
70 /* Opaque type for internal use.  */
71 struct _ENTRY;
72
73 /* Data type for reentrant functions.  */
74 struct hsearch_data
75   {
76     struct _ENTRY *table;
77     unsigned int size;
78     unsigned int filled;
79   };
80
81 /* Family of hash table handling functions.  The functions also have
82    reentrant counterparts ending with _r.  */
83 extern ENTRY *hsearch __P ((ENTRY __item, ACTION __action));
84 extern int hcreate __P ((unsigned int __nel));
85 extern void hdestroy __P ((void));
86
87 extern int hsearch_r __P ((ENTRY __item, ACTION __action, ENTRY **__retval,
88                            struct hsearch_data *__htab));
89 extern int hcreate_r __P ((unsigned int __nel, struct hsearch_data *htab));
90 extern void hdestroy_r __P ((struct hsearch_data *htab));
91
92
93 /* The tsearch routines are very interesting. They make many
94    assumptions about the compiler.  It assumpts that the first field
95    in node must be the "key" field, which points to the datum.
96    Everything depends on that.  */
97 /* For tsearch */
98 typedef enum
99 {
100   preorder,
101   postorder,
102   endorder,
103   leaf
104 }
105 VISIT;
106
107 extern void *tsearch __P ((__const void * __key, void **__rootp,
108                            __compar_fn_t compar));
109 extern void *__tsearch __P ((__const void * __key, void **__rootp,
110                              __compar_fn_t compar));
111
112 extern void *tfind __P ((__const void * __key, __const void ** __rootp,
113                          __compar_fn_t compar));
114 extern void *__tfind __P ((__const void * __key, __const void ** __rootp,
115                            __compar_fn_t compar));
116
117 extern void *tdelete __P ((__const void * __key, void ** __rootp,
118                            __compar_fn_t compar));
119 extern void *__tdelete __P ((__const void * __key, void ** __rootp,
120                              __compar_fn_t compar));
121
122 #ifndef __ACTION_FN_T
123 #define __ACTION_FN_T
124 typedef void (*__action_fn_t) __P ((__const void *__nodep,
125                                     __const VISIT __value,
126                                     __const int __level));
127 #endif
128
129 extern void twalk __P ((__const void * __root, __action_fn_t action));
130
131 extern void __twalk __P ((__const void * __root, __action_fn_t action));
132
133
134 /* Perform linear search for KEY by comparing by COMPAR in an array
135    [BASE,BASE+NMEMB*SIZE).  */
136 extern void * lfind __P ((__const void *__key, __const void *__base,
137                           size_t *__nmemb, size_t __size,
138                           __compar_fn_t __compar));
139
140 /* Perform linear search for KEY by comparing by COMPAR function in
141    array [BASE,BASE+NMEMB*SIZE) and insert entry if not found.  */
142 extern void * lsearch __P ((__const void *__key, void *__base, size_t *__nmemb,
143                             size_t __size, __compar_fn_t __compar));
144
145 __END_DECLS
146
147 #endif /* search.h */