Debian Codegraph @

doc/ext/JudyHS_3.htm

raw ◀ ▶
1 Debian patch touch this file
1 <HTML>
2 <HEAD>
3 <!-- @(#) $Revision: 4.43 $ $Source: /cvsroot/judy/doc/ext/JudyHS_3.htm,v $ --->
4 <TITLE>JudyHS(3)</TITLE>
5 </HEAD>
6 <BODY>
7 <TABLE border=0 width="100%"><TR>
8 <TD width="40%" align="left">JudyHS(3)</TD>
9 <TD width="10%" align="center"> </TD>
10 <TD width="40%" align="right">JudyHS(3)</TD>
11 </TR></TABLE>
12 <P>
13 <!----------------->
14 <DT><B>NAME</B></DT>
15 <DD>
16 JudyHS macros - C library for creating and accessing a dynamic array,
17 using an array-of-bytes of <B>Length</B> as an <B>Index</B> and a word
18 as a <B>Value</B>.
19 <!----------------->
20 <P>
21 <DT><B>SYNOPSIS</B></DT>
22 <DD>
23 <B><PRE>
24 cc [flags] <I>sourcefiles</I> -lJudy
25
26 #include &lt;Judy.h&gt;
27
28 Word_t * PValue; // JudyHS array element
29 int Rc_int; // return flag
30 Word_t Rc_word; // full word return value
31 Pvoid_t PJHSArray = (Pvoid_t) NULL; // initialize JudyHS array
32 uint8_t * Index; // array-of-bytes pointer
33 Word_t Length; // number of bytes in Index
34
35 <A href="#JHSI" >JHSI</A>( PValue, PJHSArray, Index, Length); // <A href="JudyHS_funcs_3.htm#JudyHSIns">JudyHSIns()</A>
36 <A href="#JHSD" >JHSD</A>( Rc_int, PJHSArray, Index, Length); // <A href="JudyHS_funcs_3.htm#JudyHSDel">JudyHSDel()</A>
37 <A href="#JHSG" >JHSG</A>( PValue, PJHSArray, Index, Length); // <A href="JudyHS_funcs_3.htm#JudyHSGet">JudyHSGet()</A>
38 <A href="#JHSFA">JHSFA</A>(Rc_word, PJHSArray); // <A href="JudyHS_funcs_3.htm#JudyHSFreeArray">JudyHSFreeArray()</A>
39 </PRE></B>
40 <!----------------->
41 <DT><B>DESCRIPTION</B></DT>
42 <DD>
43 A JudyHS array is the equivalent of an array of word-sized
44 value/pointers. An <B>Index</B> is a pointer to an array-of-bytes of
45 specified length: <B>Length</B>. Rather than using a null terminated
46 string, this difference from <A href="JudySL_3.htm">JudySL(3)</A>
47 allows strings to contain all bits (specifically the null character).
48 This new addition (May 2004) to Judy arrays is a hybird using the best
49 capabilities of hashing and Judy methods. <B>JudyHS</B> does not have a
50 poor performance case where knowledge of the hash algorithm can be used
51 to degrade the performance.
52 <P>
53 Since JudyHS is based on a hash method, <B>Indexes</B> are not stored in
54 any particular order. Therefore the JudyHSFirst(), JudyHSNext(),
55 JudyHSPrev() and JudyHSLast() neighbor search functions are not
56 practical. The <B>Length</B> of each array-of-bytes can be from 0 to
57 the limits of <I>malloc()</I> (about 2GB).
58 <P>
59 The hallmark of <B>JudyHS</B> is speed with scalability, but memory
60 efficiency is excellent. The speed is very competitive with the best
61 hashing methods. The memory efficiency is similar to a linked list of
62 the same <B>Indexes</B> and <B>Values</B>. <B>JudyHS</B> is designed to
63 scale from 0 to billions of <B>Indexes</B>.
64 <P>
65 A JudyHS array is allocated with a <B>NULL</B> pointer
66 <PRE>
67 Pvoid_t PJHSArray = (Pvoid_t) NULL;
68 </PRE>
69 <P>
70 Because the macro forms of the API have a simpler error handling
71 interface than the equivalent
72 <A href="JudyHS_funcs_3.htm">functions</A>,
73 they are the preferred way to use JudyHS.
74 <P>
75 <DT>
76 <A name="JHSI"><B>JHSI(PValue, PJHSArray, Index, Length)</B></A> // <A href="JudyHS_funcs_3.htm#JudyHSIns">JudyHSIns()</A></DT>
77 <DD>
78 Given a pointer to a JudyHS array (<B>PJHSArray</B>), insert an
79 <B>Index</B> string of length: <B>Length</B> and a <B>Value</B> into the
80 JudyHS array: <B>PJHSArray</B>. If the <B>Index</B> is successfully
81 inserted, the <B>Value</B> is initialized to 0. If the <B>Index</B> was
82 already present, the <B>Value</B> is not modified.
83 <P>
84 Return <B>PValue</B> pointing to <B>Value</B>. Your program should use
85 this pointer to read or modify the <B>Value</B>, for example:
86 <PRE>
87 Value = *PValue;
88 *PValue = 1234;
89 </PRE>
90 <P>
91 <B>Note</B>:
92 <B>JHSI()</B> and <B>JHSD</B> can reorganize the JudyHS array.
93 Therefore, pointers returned from previous <B>JudyHS</B> calls become
94 invalid and must be re-acquired (using <B>JHSG()</B>).
95 <P>
96 <DT><A name="JHSD"><B>JHSD(Rc_int, PJHSArray, Index, Length)</B></A> // <A href="JudyHS_funcs_3.htm#JudyHSDel">JudyHSDel()</A></DT>
97 <DD>
98 Given a pointer to a JudyHS array (<B>PJHSArray</B>), delete the
99 specified <B>Index</B> along with the <B>Value</B> from the JudyHS
100 array.
101 <P>
102 Return <B>Rc_int</B> set to 1 if successfully removed from the array.
103 Return <B>Rc_int</B> set to 0 if <B>Index</B> was not present.
104 <P>
105 <DT><A name="JHSG"><B>JHSG(PValue, PJHSArray, Index, Length)</B></A> // <A href="JudyHS_funcs_3.htm#JudyHSGet">JudyHSGet()</A></DT>
106 <DD>
107 Given a pointer to a JudyHS array (<B>PJHSArray</B>),
108 find <B>Value</B> associated with <B>Index</B>.
109 <P>
110 Return <B>PValue</B> pointing to <B>Index</B>'s <B>Value</B>.
111 Return <B>PValue</B> set to <B>NULL</B> if the <B>Index</B> was not present.
112 <P>
113 <DT><A name="JHSFA"><B>JHSFA(Rc_word, PJHSArray)</B></A> // <A href="JudyHS_funcs_3.htm#JudyHSFreeArray">JudyHSFreeArray()</A></DT>
114 <DD>
115 Given a pointer to a JudyHS array (<B>PJHSArray</B>), free the entire array.
116 <P>
117 Return <B>Rc_word</B> set to the number of bytes freed and <B>PJHSArray</B> set to NULL.
118 <!----------------->
119 <P>
120 <DT><A name="ERRORS"><B>ERRORS:</B> See: </A><A href="Judy_3.htm#ERRORS">Judy_3.htm#ERRORS</A></DT>
121 <DD>
122 <P>
123 <DT><B>EXAMPLES</B></DT>
124 <DD>
125 Show how to program with the JudyHS macros. This program will print
126 duplicate lines and their line number from <I>stdin</I>.
127 <P><PRE>
128 #include &lt;unistd.h&gt;
129 #include &lt;stdio.h&gt;
130 #include &lt;string.h&gt;
131 #include &lt;Judy.h&gt;
132
133 // Compiled:
134 // cc -O PrintDupLines.c -lJudy -o PrintDupLines
135
136 #define MAXLINE 1000000 /* max fgets length of line */
137 uint8_t Index[MAXLINE]; // string to check
138
139 int // Usage: PrintDupLines &lt; file
140 main()
141 {
142 Pvoid_t PJArray = (PWord_t)NULL; // Judy array.
143 PWord_t PValue; // Judy array element pointer.
144 Word_t Bytes; // size of JudyHS array.
145 Word_t LineNumb = 0; // current line number
146 Word_t Dups = 0; // number of duplicate lines
147
148 while (fgets(Index, MAXLINE, stdin) != (char *)NULL)
149 {
150 LineNumb++; // line number
151
152 // store string into array
153 JHSI(PValue, PJArray, Index, strlen(Index));
154 if (PValue == PJERR) // See ERRORS section
155 {
156 fprintf(stderr, "Out of memory -- exit\n");
157 exit(1);
158 }
•159 if (*PValue != 0) // check if duplicate
160 {
161 Dups++;
162 printf("Duplicate lines %lu:%lu:%s", *PValue, LineNumb, Index);
163 }
164 else
165 {
166 *PValue = LineNumb; // store Line number
167 }
168 }
169 printf("%lu Duplicates, free JudyHS array of %lu Lines\n",
170 Dups, LineNumb - Dups);
171 JHSFA(Bytes, PJArray); // free JudyHS array
172 printf("JudyHSFreeArray() free'ed %lu bytes of memory\n", Bytes);
173 return (0);
174 }
175 </PRE>
176 <!----------------->
177 <P>
178 <DT><B>AUTHOR</B></DT>
179 <DD>
180 JudyHS was invented and implemented by Doug Baskins after retiring from Hewlett-Packard.
181 <!----------------->
182 <P>
183 <DT><B>SEE ALSO</B></DT>
184 <DD>
185 <A href="Judy_3.htm">Judy(3)</A>,
186 <A href="Judy1_3.htm">Judy1(3)</A>,
187 <A href="JudyL_3.htm">JudyL(3)</A>,
188 <A href="JudySL_3.htm">JudySL(3)</A>,
189 <BR>
190 <I>malloc()</I>,
191 <BR>
192 the Judy website,
193 <A href="http://judy.sourceforge.net">
194 http://judy.sourceforge.net</A>,
195 for further information and Application Notes.
196 </BODY>
197 </HTML>