File: hsearch.3

package info (click to toggle)
manpages-es 1.55-9
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 7,468 kB
  • ctags: 6
  • sloc: sh: 1,629; makefile: 64
file content (187 lines) | stat: -rw-r--r-- 6,338 bytes parent folder | download | duplicates (4)
1
2
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
.\" 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 <jrv@vanzandt.mv.com>
.\" 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
.\" <jmgurpe@unav.es>
.\" Traducción revisada por Miguel Pérez Ibars <mpi79470@alu.um.es> 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 <search.h>
.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 <search.h>
.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<search.h>\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 <stdio.h>
    #include <search.h>
    
    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)