| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 
 | .\" 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., 675 Mass Ave, Cambridge, MA 02139,
.\" USA.
.\"
.\" References consulted:
.\"     SunOS 4.1.1 man pages
.\" Modified Sat Sep 30 21:52:01 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\" Translated into spanish by Jos Miguel Gurpegui Mar 12 1998
.\" <jmgurpe@unav.es>
.\"
.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 <search.h>
.sp
.BI "ENTRY *hsearch(ENTRY " item ", ACTION " action ");"
.sp
.BI "int hcreate(unsigned " nel ");"
.sp
.B "void hdestroy(void);"
.RE
.fi
.SH DESCRIPCIN
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 funcin \fBhcreate()\fP.
\fInel\fP es una estimacin del nmero de entradas de la tabla.
\fBhcreate()\fP puede incrementar este valor para mejorar el rendimiento de
la tabla dispersa resultante. La implementacin GNU de
\fBhsearch()\fP tambin agrandar la tabla si sta est casi llena.
Para asignar espacio a la tabla se utiliza \fBmalloc(3)\fP.
.PP
La funcin correspondiente \fBhdestroy()\fP libera la memoria ocupada por la
tabla dispersa de tal forma que se pueda construir una nueva tabla.
.PP
El parmetro \fIitem\fP es del tipo \fBENTRY\fP, que se define mediante typedef
en \fI<search.h>\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
bsqueda. \fIdata\fP apunta a los datos asociados con esa clave.
(Un puntero a cualquier otro tipo distinto de carcter se debe convertir al
tipo "puntero a carcter). \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 bsqueda 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 implementacin slo puede manejar una tabla dispersa a la vez. Se pueden
aadir 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
continuacin imprime algunos de ellos.
.nf
    #include <stdio.h>
    #include <search.h>
    
    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 pequea tabla y dejmosla 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
           estn 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 "VASE TAMBIN"
.BR bsearch (3),
.\"lsearch (3),
.BR tsearch (3),
.BR malloc (3).
 |