213 lines
7.9 KiB
HTML
213 lines
7.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>hcreate</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="hcreate"></a> <a name="tag_03_264"></a><!-- hcreate -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_264_01"></a>NAME</h4>
|
|
|
|
<blockquote>hcreate, hdestroy, hsearch - manage hash search table</blockquote>
|
|
|
|
<h4><a name="tag_03_264_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
|
|
"[Option Start]" border="0"> #include <<a href="../basedefs/search.h.html">search.h</a>><br>
|
|
<br>
|
|
int hcreate(size_t</tt> <i>nel</i><tt>);<br>
|
|
void hdestroy(void);<br>
|
|
ENTRY *hsearch(ENTRY</tt> <i>item</i><tt>, ACTION</tt> <i>action</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]"
|
|
border="0"></tt></code></div>
|
|
|
|
<tt><br>
|
|
</tt></blockquote>
|
|
|
|
<h4><a name="tag_03_264_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>hcreate</i>(), <i>hdestroy</i>(), and <i>hsearch</i>() functions shall manage hash search tables.</p>
|
|
|
|
<p>The <i>hcreate</i>() function shall allocate sufficient space for the table, and the application shall ensure it is called
|
|
before <i>hsearch</i>() is used. The <i>nel</i> argument is an estimate of the maximum number of entries that the table shall
|
|
contain. This number may be adjusted upward by the algorithm in order to obtain certain mathematically favorable circumstances.</p>
|
|
|
|
<p>The <i>hdestroy</i>() function shall dispose of the search table, and may be followed by another call to <i>hcreate</i>(). After
|
|
the call to <i>hdestroy</i>(), the data can no longer be considered accessible.</p>
|
|
|
|
<p>The <i>hsearch</i>() function is a hash-table search routine. It shall return a pointer into a hash table indicating the
|
|
location at which an entry can be found. The <i>item</i> argument is a structure of type <b>ENTRY</b> (defined in the <a href=
|
|
"../basedefs/search.h.html"><i><search.h></i></a> header) containing two pointers: <i>item.key</i> points to the comparison
|
|
key (a <b>char *</b>), and <i>item.data</i> (a <b>void *</b>) points to any other data to be associated with that key. The
|
|
comparison function used by <i>hsearch</i>() is <a href="../functions/strcmp.html"><i>strcmp</i>()</a>. The <i>action</i> argument
|
|
is a member of an enumeration type <b>ACTION</b> indicating the disposition of the entry if it cannot be found in the table. ENTER
|
|
indicates that the item should be inserted in the table at an appropriate point. FIND indicates that no entry should be made.
|
|
Unsuccessful resolution is indicated by the return of a null pointer.</p>
|
|
|
|
<p>These functions need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>hcreate</i>() function shall return 0 if it cannot allocate sufficient space for the table; otherwise, it shall return
|
|
non-zero.</p>
|
|
|
|
<p>The <i>hdestroy</i>() function shall not return a value.</p>
|
|
|
|
<p>The <i>hsearch</i>() function shall return a null pointer if either the action is FIND and the item could not be found or the
|
|
action is ENTER and the table is full.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>hcreate</i>() and <i>hsearch</i>() functions may fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[ENOMEM]</dt>
|
|
|
|
<dd>Insufficient storage space is available.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_264_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The following example reads in strings followed by two numbers and stores them in a hash table, discarding duplicates. It then
|
|
reads in strings and finds the matching entry in the hash table and prints it out.</p>
|
|
|
|
<pre>
|
|
<tt>#include <stdio.h>
|
|
#include <search.h>
|
|
#include <string.h>
|
|
<br>
|
|
struct info { /* This is the info stored in the table */
|
|
int age, room; /* other than the key. */
|
|
};
|
|
<br>
|
|
#define NUM_EMPL 5000 /* # of elements in search table. */
|
|
|
|
<br>
|
|
int main(void)
|
|
{
|
|
char string_space[NUM_EMPL*20]; /* Space to store strings. */
|
|
struct info info_space[NUM_EMPL]; /* Space to store employee info. */
|
|
char *str_ptr = string_space; /* Next space in string_space. */
|
|
struct info *info_ptr = info_space;
|
|
/* Next space in info_space. */
|
|
ENTRY item;
|
|
ENTRY *found_item; /* Name to look for in table. */
|
|
char name_to_find[30];
|
|
<br>
|
|
int i = 0;
|
|
<br>
|
|
/* Create table; no error checking is performed. */
|
|
(void) hcreate(NUM_EMPL);
|
|
while (scanf("%s%d%d", str_ptr, &info_ptr->age,
|
|
&info_ptr->room) != EOF && i++ < NUM_EMPL) {
|
|
<br>
|
|
/* Put information in structure, and structure in item. */
|
|
item.key = str_ptr;
|
|
item.data = info_ptr;
|
|
str_ptr += strlen(str_ptr) + 1;
|
|
info_ptr++;
|
|
<br>
|
|
/* Put item into table. */
|
|
(void) hsearch(item, ENTER);
|
|
}
|
|
<br>
|
|
/* Access table. */
|
|
item.key = name_to_find;
|
|
while (scanf("%s", item.key) != EOF) {
|
|
if ((found_item = hsearch(item, FIND)) != NULL) {
|
|
<br>
|
|
/* If item is in the table. */
|
|
(void)printf("found %s, age = %d, room = %d\n",
|
|
found_item->key,
|
|
((struct info *)found_item->data)->age,
|
|
((struct info *)found_item->data)->room);
|
|
} else
|
|
(void)printf("no such employee %s\n", name_to_find);
|
|
}
|
|
return 0;
|
|
}
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>hcreate</i>() and <i>hsearch</i>() functions may use <a href="../functions/malloc.html"><i>malloc</i>()</a> to allocate
|
|
space.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="bsearch.html"><i>bsearch</i>()</a> , <a href="lsearch.html"><i>lsearch</i>()</a> , <a href=
|
|
"malloc.html"><i>malloc</i>()</a> , <a href="strcmp.html"><i>strcmp</i>()</a> , <a href="tsearch.html"><i>tsearch</i>()</a> , the
|
|
Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/search.h.html"><i><search.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_264_12"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p>
|
|
|
|
<p>A note indicating that this function need not be reentrant is added to the DESCRIPTION.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|