2 .\" Copyright (c) 1999 The NetBSD Foundation, Inc.
3 .\" All rights reserved.
5 .\" This code is derived from software contributed to The NetBSD Foundation
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
17 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 .\" POSSIBILITY OF SUCH DAMAGE.
29 .\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.7 2008/07/06 17:03:37 danger Exp $
30 .\" $DragonFly: src/lib/libc/stdlib/hcreate.3,v 1.5 2006/05/26 19:39:37 swildner Exp $
39 .Nd manage hash search table
45 .Fn hcreate "size_t nel"
49 .Fn hsearch "ENTRY item" "ACTION action"
56 functions manage hash search tables.
60 function allocates sufficient space for the table, and the application should
61 ensure it is called before
66 argument is an estimate of the maximum
67 number of entries that the table should contain.
68 This number may be adjusted upward by the
69 algorithm in order to obtain certain mathematically favorable circumstances.
73 function disposes of the search table, and may be followed by another call to
77 the data can no longer be considered accessible.
82 for each comparison key in the search table
83 but not the data item associated with the key.
87 function is a hash-table search routine.
88 It returns a pointer into a hash table
89 indicating the location at which an entry can be found.
92 argument is a structure of type
96 header) containing two pointers:
98 points to the comparison key (a
104 points to any other data to be associated with
106 The comparison function used by
113 member of an enumeration type
115 indicating the disposition of the entry if it cannot be
120 should be inserted in the table at an
123 indicates that no entry should be made.
124 Unsuccessful resolution is
125 indicated by the return of a
129 The comparison key (passed to
133 must be allocated using
145 function returns 0 if the table creation failed and the global variable
147 is set to indicate the error;
148 otherwise, a non-zero value is returned.
152 function does not return a value.
158 pointer if either the
164 could not be found or the
168 and the table is full.
170 The following example reads in strings followed by two numbers
171 and stores them in a hash table, discarding duplicates.
172 It then reads in strings and finds the matching entry in the hash
173 table and prints it out.
180 struct info { /* This is the info stored in the table */
181 int age, room; /* other than the key. */
184 #define NUM_EMPL 5000 /* # of elements in search table. */
189 char str[BUFSIZ]; /* Space to read string */
190 struct info info_space[NUM_EMPL]; /* Space to store employee info. */
191 struct info *info_ptr = info_space; /* Next space in info_space. */
193 ENTRY *found_item; /* Name to look for in table. */
194 char name_to_find[30];
197 /* Create table; no error checking is performed. */
198 (void) hcreate(NUM_EMPL);
200 while (scanf("%s%d%d", str, &info_ptr->age,
201 &info_ptr->room) != EOF && i++ < NUM_EMPL) {
202 /* Put information in structure, and structure in item. */
203 item.key = strdup(str);
204 item.data = info_ptr;
206 /* Put item into table. */
207 (void) hsearch(item, ENTER);
211 item.key = name_to_find;
212 while (scanf("%s", item.key) != EOF) {
213 if ((found_item = hsearch(item, FIND)) != NULL) {
214 /* If item is in the table. */
215 (void)printf("found %s, age = %d, room = %d\en",
217 ((struct info *)found_item->data)->age,
218 ((struct info *)found_item->data)->room);
220 (void)printf("no such employee %s\en", name_to_find);
231 functions may fail if:
234 Insufficient storage space is available.
236 A table already exists.
258 functions first appeared in
261 The interface permits the use of only one hash table at a time.