⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hcreate.3

📁 KPIT GNU Tools is a set of GNU development tools for Renesas microcontrollers.
💻 3
字号:
.\" $FreeBSD: src/lib/libc/stdlib/hcreate.3,v 1.2 2001/07/09 15:54:36 ru Exp $.\".Dd May 8, 2001.Os.Dt HCREATE 3.Sh NAME.Nm hcreate , hdestroy , hsearch.Nd manage hash search table.Sh LIBRARY.Lb libc.Sh SYNOPSIS.In search.h.Ft int.Fn hcreate "size_t nel".Ft void.Fn hdestroy void.Ft ENTRY *.Fn hsearch "ENTRY item" "ACTION action".Sh DESCRIPTIONThe.Fn hcreate ,.Fn hdestroy ,and.Fn hsearchfunctions manage hash search tables..PpThe.Fn hcreatefunction allocates sufficient space for the table, and the application shouldensure it is called before.Fn hsearchis used.The.Fa nelargument is an estimate of the maximumnumber of entries that the table should contain.This number may be adjusted upward by thealgorithm in order to obtain certain mathematically favorable circumstances..PpThe.Fn hdestroyfunction disposes of the search table, and may be followed by another call to.Fn hcreate .After the call to.Fn hdestroy ,the data can no longer be considered accessible..PpThe.Fn hsearchfunction is a hash-table search routine.It returns a pointer into a hash tableindicating the location at which an entry can be found.The.Fa itemargument is a structure of type.Vt ENTRY(defined in the.Aq Pa search.hheader) containing two pointers:.Fa item.keypoints to the comparison key (a.Vt "char *" ) ,and.Fa item.data(a.Vt "void *" )points to any other data to be associated withthat key.The comparison function used by.Fn hsearchis.Xr strcmp 3 .The.Fa actionargument is amember of an enumeration type.Vt ACTIONindicating the disposition of the entry if it cannot befound in the table..Dv ENTERindicates that the.Fa itemshould be inserted in the table at anappropriate point..Dv FINDindicates that no entry should be made.Unsuccessful resolution isindicated by the return of a.Dv NULLpointer..Sh RETURN VALUESThe.Fn hcreatefunction returns 0 if it cannot allocate sufficient space for the table;otherwise, it returns non-zero..PpThe.Fn hdestroyfunction does not return a value..PpThe.Fn hsearchfunction returns a.Dv NULLpointer if either the.Fa actionis.Dv FINDand the.Fa itemcould not be found or the.Fa actionis.Dv ENTERand the table is full..Sh ERRORSThe.Fn hcreateand.Fn hsearchfunctions may fail if:.Bl -tag -width Er.It Bq Er ENOMEMInsufficient storage space is available..El.Sh EXAMPLESThe following example reads in strings followed by two numbersand stores them in a hash table, discarding duplicates.It then reads in strings and finds the matching entry in the hashtable and prints it out..Bd -literal#include <stdio.h>#include <search.h>#include <string.h>struct info {			/* This is the info stored in the table */	int age, room;		/* other than the key. */};#define NUM_EMPL	5000	/* # of elements in search table. */intmain(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];	int i = 0;	/* 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) {		/* Put information in structure, and structure in item. */		item.key = str_ptr;		item.data = info_ptr;		str_ptr += strlen(str_ptr) + 1;		info_ptr++;		/* Put item into table. */		(void) hsearch(item, ENTER);	}	/* Access table. */	item.key = name_to_find;	while (scanf("%s", item.key) != EOF) {		if ((found_item = hsearch(item, FIND)) != NULL) {			/* If item is in the table. */			(void)printf("found %s, age = %d, room = %d\en",			    found_item->key,			    ((struct info *)found_item->data)->age,			    ((struct info *)found_item->data)->room);		} else			(void)printf("no such employee %s\en", name_to_find);	}	return 0;}.Ed.Sh SEE ALSO.Xr bsearch 3 ,.Xr lsearch 3 ,.Xr malloc 3 ,.Xr strcmp 3 ,.Xr tsearch 3.Sh STANDARDSThe.Fn hcreate ,.Fn hdestroy ,and.Fn hsearchfunctions conform to.St -xpg4.2 ..Sh HISTORYThe.Fn hcreate ,.Fn hdestroy ,and.Fn hsearchfunctions first appeared in.At V ..Sh BUGSThe interface permits the use of only one hash table at a time.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -