.\" Hey Emacs! This file is -*- nroff -*- source. .\" Copyright 1993 Ulrich Drepper (drepper@karlsruhe.gmd.de) .\" .\" This is free documentation; you can redistribute it and/or .\" modify it under the terms of the GNU General Public License as .\" published by the Free Software Foundation; either version 2 of .\" the License, or (at your option) any later version. .\" .\" The GNU General Public License's references to "object code" .\" and "executables" are to be interpreted as the output of any .\" document formatting or typesetting system, including .\" intermediate and printed output. .\" .\" This manual is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public .\" License along with this manual; if not, write to the Free .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, .\" USA. .\" .\" References consulted: .\" SunOS 4.1.1 man pages .\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt .\" Remarks from dhw@gamgee.acad.emich.edu Fri Jun 19 06:46:31 1998 .\" Modified 2001-12-26, aeb .\" .\" Translated into spanish by José Miguel Gurpegui Mar 12 1998 .\" .\" Traducción revisada por Miguel Pérez Ibars el 19-marzo-2005 .\" .TH HSEARCH 3 "26 diciembre 2001" "GNU" "Manual del Programador de Linux" .SH NOMBRE hcreate, hdestroy, hsearch \- funciones para manejar una tabla dispersa (hash) .SH SINOPSIS .B #include .sp .BI "int hcreate(size_t " nel ); .sp .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action ); .sp .B "void hdestroy(void);" .sp 2 .B #define _GNU_SOURCE .br .B #include .sp .BI "int hcreate_r(size_t " nel ", struct hsearch_data *" tab ); .sp .BI "int *hsearch_r(ENTRY " item ", ACTION " action , .BI "ENTRY **" ret ", struct hsearch_data *" tab ); .sp .BI "void hdestroy_r(struct hsearch_data *" tab ); .SH DESCRIPCIÓN Las tres funciones .BR hcreate , .BR hsearch , y .BR hdestroy permiten al usuario crear una tabla dispersa (sólo una al mismo tiempo) que asocia una clave con cualquier dato. Las tres funciones .BR hcreate_r , .BR hsearch_r , .BR hdestroy_r son versiones reentrantes que permiten el uso de más de una tabla. .PP En primer lugar, se debe crear la tabla con la función \fBhcreate()\fP. El argumento \fInel\fP es una estimación del número de entradas de la tabla. La función \fBhcreate()\fP puede incrementar este valor para mejorar el rendimiento de la tabla dispersa resultante. .PP La función correspondiente \fBhdestroy()\fP libera la memoria ocupada por la tabla dispersa de tal forma que se pueda construir una nueva tabla. .PP El argumento \fIitem\fP es del tipo \fBENTRY\fP, que se define mediante typedef en \fI\fP e incluye estos elementos: .sp .nf typedef struct entry { char *\fIkey\fP; void *\fIdata\fP; } ENTRY; .fi .sp El campo \fIkey\fP apunta a una cadena terminada en NUL que es la clave de búsqueda. El campo \fIdata\fP apunta a los datos asociados con esa clave. La función \fBhsearch()\fP busca en la tabla dispersa un elemento con la misma clave que \fIitem\fP (donde "la misma" se determina usando .BR strcmp (3)), y si tiene éxito devuelve un puntero al mismo. El argumento \fIaction\fP determina qué debe hacer \fBhsearch()\fP tras una búsqueda sin éxito. El valor \fBENTER\fP le indica que debe insertar una copia de \fIitem\fP, mientras que un valor \fBFIND\fP significa que debe devolver \fBNULL\fP. .SH "VALOR DEVUELTO" \fBhcreate()\fP y \fBhcreate_r()\fP devuelven 0 cuando falla la reserva de memoria para la tabla dispersa, o un valor distinto de cero en otro caso. .LP \fBhsearch()\fP devuelve \fBNULL\fP si \fIaction\fP es \fBENTER\fP y la tabla dispersa está completa, o \fIaction\fP es \fBFIND\fP e \fIitem\fP no puede ser encontrado en la tabla dispersa. .LP \fBhsearch_r()\fP devuelve 0 si \fIaction\fP es \fBENTER\fP y la tabla dispersa está completa, y un valor distinto de cero en otro caso. .SH ERRORES .TP .B ENOMEM Memoria insuficiente. .SH "CONFORME A" Las funciones .BR hcreate , .BR hsearch , y .BR hdestroy son de SVID, y están descritas en POSIX 1003.1-2001. Las funciones .BR hcreate_r , .BR hsearch_r , .BR hdestroy_r son extensiones de GNU. .SH FALLOS SVID y POSIX 1003.1-2001 especifican que el argumento \fIaction\fP es significativo sólo para búsquedas sin éxito, por lo que ENTER no debería hacer nada para una búsqueda exitosa. Las implementaciones de libc y glibc actualizan \fIdata\fP para una clave \fIkey\fP dada en este caso. .\" Tue Jan 29 09:27:40 2002: fixed in latest glibc snapshot .LP Se pueden añadir a la tabla dispersa entradas individuales pero no se pueden eliminar. .SH EJEMPLO .PP El siguiente programa inserta 24 elementos en una tabla dispersa y a continuación imprime algunos de ellos. .nf #include #include char *data[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliet", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whisky", "x-ray", "yankee", "zulu" }; int main() { ENTRY e, *ep; int i; /* Comencemos con una pequeña tabla y dejémosla que crezca */ hcreate(30); for (i = 0; i < 24; i++) { e.key = data[i]; /* Los datos son enteros, en lugar de punteros a alguna cosa */ e.data = (char *)i; ep = hsearch(e, ENTER); /* No debe haber fallos */ if(ep == NULL) { fprintf(stderr, "Fallo en la entrada\\n"); exit(1); } } for (i = 22; i < 26; i++) { /* Imprime dos entradas de la tabla y demuestra que otras dos no están en la tabla */ e.key = data[i]; ep = hsearch(e, FIND); printf("%9.9s -> %9.9s:%d\\n", e.key, ep?ep->key:"NULL", ep?(int)(ep->data):0); } return 0; } .fi .SH "VÉASE TAMBIÉN" .BR bsearch (3), .BR lsearch (3), .BR tsearch (3), .BR malloc (3)