Debian Codegraph @

doc/gperf_4.html

raw
1 <HTML>
2 <HEAD>
3 <!-- This HTML file has been created by texi2html 1.52b
4 from gperf.texi on 16 April 2025 -->
5
6 <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
7 <TITLE>Perfect Hash Function Generator - 3 Static search structures and GNU gperf</TITLE>
8 </HEAD>
9 <BODY>
10 Go to the <A HREF="gperf_1.html">first</A>, <A HREF="gperf_3.html">previous</A>, <A HREF="gperf_5.html">next</A>, <A HREF="gperf_10.html">last</A> section, <A HREF="gperf_toc.html">table of contents</A>.
11 <P><HR><P>
12
13
14 <H1><A NAME="SEC4" HREF="gperf_toc.html#TOC4">3 Static search structures and GNU <CODE>gperf</CODE></A></H1>
15 <P>
16 <A NAME="IDX2"></A>
17
18 </P>
19 <P>
20 A <EM>static search structure</EM> is an Abstract Data Type with certain
21 fundamental operations, e.g., <EM>initialize</EM>, <EM>insert</EM>,
22 and <EM>retrieve</EM>. Conceptually, all insertions occur before any
23 retrievals. In practice, <CODE>gperf</CODE> generates a <EM>static</EM> array
24 containing search set keywords and any associated attributes specified
25 by the user. Thus, there is essentially no execution-time cost for the
26 insertions. It is a useful data structure for representing <EM>static
27 search sets</EM>. Static search sets occur frequently in software system
28 applications. Typical static search sets include compiler reserved
29 words, assembler instruction opcodes, and built-in shell interpreter
30 commands. Search set members, called <EM>keywords</EM>, are inserted into
31 the structure only once, usually during program initialization, and are
32 not generally modified at run-time.
33
34 </P>
35 <P>
36 Numerous static search structure implementations exist, e.g.,
37 arrays, linked lists, binary search trees, digital search tries, and
38 hash tables. Different approaches offer trade-offs between space
39 utilization and search time efficiency. For example, an <VAR>n</VAR> element
40 sorted array is space efficient, though the average-case time
41 complexity for retrieval operations using binary search is
42 proportional to log <VAR>n</VAR>. Conversely, hash table implementations
43 often locate a table entry in constant time, but typically impose
44 additional memory overhead and exhibit poor worst case performance.
45
46 </P>
47 <P>
48 <A NAME="IDX3"></A>
49 <EM>Minimal perfect hash functions</EM> provide an optimal solution for a
50 particular class of static search sets. A minimal perfect hash
51 function is defined by two properties:
52
53 </P>
54
55 <UL>
56 <LI>
57
58 It allows keyword recognition in a static search set using at most
59 <EM>one</EM> probe into the hash table. This represents the “perfect”
60 property.
61 <LI>
62
63 The actual memory allocated to store the keywords is precisely large
64 enough for the keyword set, and <EM>no larger</EM>. This is the
65 “minimal” property.
66 </UL>
67
68 <P>
69 For most applications it is far easier to generate <EM>perfect</EM> hash
70 functions than <EM>minimal perfect</EM> hash functions. Moreover,
71 non-minimal perfect hash functions frequently execute faster than
72 minimal ones in practice. This phenomena occurs since searching a
73 sparse keyword table increases the probability of locating a “null”
74 entry, thereby reducing string comparisons. <CODE>gperf</CODE>'s default
75 behavior generates <EM>near-minimal</EM> perfect hash functions for
76 keyword sets. However, <CODE>gperf</CODE> provides many options that permit
77 user control over the degree of minimality and perfection.
78
79 </P>
80 <P>
81 Static search sets often exhibit relative stability over time. For
82 example, Ada's 63 reserved words have remained constant for nearly a
83 decade. It is therefore frequently worthwhile to expend concerted
84 effort building an optimal search structure <EM>once</EM>, if it
85 subsequently receives heavy use multiple times. <CODE>gperf</CODE> removes
86 the drudgery associated with constructing time- and space-efficient
87 search structures by hand. It has proven a useful and practical tool
88 for serious programming projects. Output from <CODE>gperf</CODE> is currently
89 used in several production and research compilers, including GNU C, GNU
90 C++, GNU Java, GNU Pascal, and GNU Modula 3. The latter two compilers are
91 not yet part of the official GNU distribution. Each compiler utilizes
92 <CODE>gperf</CODE> to automatically generate static search structures that
93 efficiently identify their respective reserved keywords.
94
95 </P>
96 <P><HR><P>
97 Go to the <A HREF="gperf_1.html">first</A>, <A HREF="gperf_3.html">previous</A>, <A HREF="gperf_5.html">next</A>, <A HREF="gperf_10.html">last</A> section, <A HREF="gperf_toc.html">table of contents</A>.
98 </BODY>
99 </HTML>