.\" 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 .\" Translated into spanish by José Miguel Gurpegui Mar 12 1998 .\" .\" .TH HSEARCH 3 "30 Septiembre 1995" "GNU" "Manual del Programador de Linux" .SH NOMBRE hcreate, hdestroy, hsearch \- funciones para manejar una tabla dispersa (hash) .SH SINOPSIS .nf .B #include .sp .BI "ENTRY *hsearch(ENTRY " item ", ACTION " action ");" .sp .BI "int hcreate(unsigned " nel ");" .sp .B "void hdestroy(void);" .RE .fi .SH DESCRIPCIÓN Estas tres funciones permiten al usuario crear una tabla dispersa que asocia una clave con cualquier dato. .PP En primer lugar, se debe crear la tabla con la función \fBhcreate()\fP. \fInel\fP es una estimación del número de entradas de la tabla. \fBhcreate()\fP puede incrementar este valor para mejorar el rendimiento de la tabla dispersa resultante. La implementación GNU de \fBhsearch()\fP también agrandará la tabla si ésta está casi llena. Para asignar espacio a la tabla se utiliza \fBmalloc(3)\fP. .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 parámetro \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; char *\fIdata\fP; } ENTRY; .fi .sp \fIkey\fP apunta a una cadena ASCII terminada en '\\0' que es la clave de búsqueda. \fIdata\fP apunta a los datos asociados con esa clave. (Un puntero a cualquier otro tipo distinto de carácter se debe convertir al tipo "puntero a carácter). \fBhsearch()\fP busca en la tabla dispersa un elemento con la misma clave que \fIitem\fP y, si tiene éxito, devuelve un puntero al mismo. \fIaction\fP determina qué debe hacer \fBhsearch()\fP tras una búsqueda sin éxito. El valor \fBENTER\fP le indica que debe insertar un nuevo elemento mientras que un valor \fBFIND\fP significa que debe devolver \fBNULL\fP. .SH "VALOR DEVUELTO" \fBhcreate()\fP devuelve \fBNULL\fP si la tabla dispersa no se puede crear con éxito. .PP \fBhsearch()\fP devuelve \fBNULL\fP si \fIaction\fP es \fBENTER\fP y no hay suficiente memoria para expandir la tabla dispersa o si \fIaction\fP es \fBFIND\fP y \fIitem\fP no se puede encontrar en la tabla dispersa. .SH "CONFORME A" .PP SVID, excepto que en SysV, la tabla dispersa no puede crecer. .SH FALLOS La implementación sólo puede manejar una tabla dispersa a la vez. 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", "charley", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliette", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "x-ray", "yankee", "zulu" }; int main() { ENTRY e, *ep; int i; /* Comencemos con una pequeña tabla y dejémosla que crezca */ hcreate(3); 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), .\"lsearch (3), .BR tsearch (3), .BR malloc (3).