Debian Codegraph @

doc/ext/Judy_3.htm

raw
1 <HTML>
2 <HEAD>
3 <!-- @(#) $Revision: 4.36 $ $Source: /cvsroot/judy/doc/ext/Judy_3.htm,v $ --->
4 <TITLE>Judy(3)</TITLE>
5 </HEAD>
6 <BODY>
7 <TABLE border=0 width="100%"><TR>
8 <TD width="40%" align="left">Judy(3)</TD>
9 <TD width="10%" align="center"> </TD>
10 <TD width="40%" align="right">Judy(3)</TD>
11 </TR></TABLE>
12 <P>
13 <!----------------->
14 <DT><B>NAME</B></DT>
15 <DD>
16 Judy arrays -
17 C library functions for creating and accessing dynamic arrays
18 </DD>
19 <!----------------->
20 <P>
21 <DT><B>SYNOPSIS</B></DT>
22 <DD>
23 <PRE>
24 <A href="Judy1_3.htm">Judy1</A> - maps an <B>Index</B> (word) to a <B>bit</B>
25 <A href="JudyL_3.htm">JudyL</A> - maps an <B>Index</B> (word) to a <B>Value</B> (word/pointer)
26 <A href="JudySL_3.htm">JudySL</A> - maps an <B>Index</B> (null terminated string) to a <B>Value</B>
27 <A href="JudyHS_3.htm">JudyHS</A> - maps an <B>Index</B> (array-of-bytes) of <B>Length</B> to a <B>Value</B>
28 </PRE>
29 <!----------------->
30 <P>
31 <DT><B>DESCRIPTION</B></DT>
32 <DD>
33 The Judy family of functions supports fully dynamic arrays. These
34 arrays may be indexed by a 32- or 64-bit word (depending on processor
35 word size), a null terminated string or an array-of-bytes plus length.
36 A dynamic array (sparsely populated) can also be thought of as a
37 <I>mapping function</I> or <I>associative memory</I>.
38 <P>
39 A <B>Word_t</B> is a <I>typedef unsigned long int </I> in <B>Judy.h</B>
40 and must be the same size as <I>sizeof(void *)</I> I.E. a pointer.
41 <P>
42 <A href="Judy1_3.htm">Judy1</A> functions: <B>Index</B> is a
43 <B>Word_t</B> and <B>Value</B> is just a <B>bit</B> or simply
44 a flag that <B>Index</B> is present or missing from the array.
45 This can be thought of as a huge bitmap.
46 <P>
47 <A href="JudyL_3.htm">JudyL</A> functions: <B>Index</B> is a
48 <B>Word_t</B> and <B>Value</B> is a <B>Word_t</B>. This makes
49 <B>JudyL</B> a pure word-to-word/pointer mapper. <B>JudySL</B> and
50 <B>JudyHL</B> are based on this property of <B>JudyL</B>.
51 <P>
52 <A href="JudySL_3.htm">JudySL</A> functions: <B>Index</B> is a
53 null-terminated string and <B>Value</B> is a <B>Word_t</B>.
54 <P>
55 <A href="JudyHS_3.htm">JudyHS</A> functions: <B>Index</B> is an
56 array-of-bytes of length: <B>Length</B>. <B>Value</B> is a
57 <B>Word_t</B>. This new addition (May 2004) to Judy is a hybird using
58 the best features of hashing and Judy methods. The author believes
59 <B>JudyHS</B> is a good replacement for a hashing method when resizing
60 the hash table is done during population growth. A correctly tuned hash
61 method with a <B>static</B> hash table size and population is unbeatable
62 for speed. However, <B>JudyHS</B> will perform better than a hashing
63 method with smaller and larger populations than the optimum hash table
64 size. <B>JudyHS</B> does not have a degenerate performance case where
65 knowledge of the hash algorithm can be exploited. (I.E. JudyHS does
66 not use a linked list to handle hash collisions, it uses a tree of
67 <B>JudyL</B> arrays and a virtual hash table size of 4 billion).
68 <P>
69 Judy arrays are both <B>speed-</B> and <B>memory-efficient</B>, with no
70 tuning or configuration required, across a wide range of index set types
71 (sequential, periodic, clustered, random). Judy's speed and memory
72 usage are typically better than other data storage models such as
73 skiplists, linked lists, binary, ternary, b-trees, or even hashing, and
74 improves with very large data sets.
75 <P>
76 A Judy array is created merely by defining a null pointer and then
77 storing (inserting) the first element into the array under that pointer.
78 The memory used by a Judy array is nearly proportional to the population
79 (number of elements).
80 <P>
81 Judy has two Application Program Interfaces (APIs): a C macro
82 interface, and a function call interface. Because the macro forms are
83 sometimes faster and have a simpler error handling interface than the
84 equivalent functions, they are the preferred way of using the Judy
85 functions.
86 <P>
87 Since an initial (empty) Judy array is represented by a null pointer, it
88 is possible to construct an array of Judy arrays. In other words, a
89 Judy array's <B>Values</B> (except Judy1) can be pointers to other Judy
90 arrays. This makes it very simple to construct an array with an
91 arbitrary number of dimensions or <B>Index</B> sizes. (JudySL and
92 JudyHS are implemented using JudyL this way).
93 <!----------------->
94 <P>
95 <DT><B>A 10 MINUTE TECHNICAL DESCRIPTION</B></DT>
96 <DD>
97 may be found at
98 <A href="http://judy.sourceforge.net/downloads/10minutes.htm">http://judy.sourceforge.net/downloads/10minutes.htm</A>
99 <!----------------->
100 <P>
101 <DT><B>A 3 HOUR TECHNICAL DESCRIPTION</B> (out of date and a bit corny)</DT>
102 <DD>
103 may be found at
104 <A href="http://judy.sourceforge.net/application/shop_interm.pdf">http://judy.sourceforge.net/application/shop_interm.pdf</A>
105 <!----------------->
106 <P>
107 <DT>
108 <A name="DOWNLOADS"> <B>DOWNLOADS</B></A></DT>
109 <DD>
110 Judy source downloads are available at
111 <A href="http://sourceforge.net/projects/judy">http://sourceforge.net/projects/judy</A><BR>
112 Binarys may be built and installed in a minute or two
113 after downloading
114 <P>
115 For versions including more platforms and/or new features see:
116 <A href="http://judy.sourceforge.net/downloads/">http://judy.sourceforge.net/downloads/</A>
117 <!----------------->
118 <P>
119 <DT><B>AUTHOR</B></DT>
120 <DD>
121 Judy was invented by Doug Baskins (dougbaskins .AT, yahoo.com) and
122 implemented by Hewlett-Packard. (Note: Judy is named for the
123 inventor's sister, after discarding many proposed names.)
124 <!----------------->
125 <P>
126 <DT>
127 <A name="FILES">
128 <B>FILES</B></A></DT>
129 <DD>
130 Locations of interest include:
131 <BR>
132 <A href="http://sourceforge.net/projects/judy">http://sourceforge.net/projects/judy</A>
133 -- project downloads<BR>
134 <A href="file:/usr/share/doc/Judy/">file:/usr/share/doc/Judy/</A>
135 -- for HTML version of man pages.<BR>
136 /usr/share/doc/Judy/demo/ -- demonstration program source files.<BR>
137 <BR>
138 The author attempted to write interesting application notes using
139 advanced features of Judy. They may be found at
140 <A href="http://judy.sourceforge.net/application/">"http://judy.sourceforge.net/application/</A>
141 (Some may be out of date).
142 <!----------------->
143 <P>
144 <DT><A name="ERRORS"><B>ERRORS</B></A></DT>
145 <DD>
146 A lot of thought (and time) went into making error handling in Judy
147 simple, while maintaining flexibility and capability. Error handling is
148 a very boring subject even to write about. So read this short section
149 and use the recommended second method. It generates the fastest code,
150 uses the least amount of memory and requires you to write extra code
151 only for insert/deletes functions. Also it is compatible with the other
152 two methods. This method is for production code that may want to handle
153 <I>malloc()</I> fails differently than the Judy default. If the Judy
154 default method of handling <I>malloc()</I> fails are OK, then use the
155 first method.
156 <P>
157 There are <I>two (2)</I> categories of Judy error returns, (or for any dynamic ADT):
158 <P>
159 1) User programming errors (bugs) such as memory corruption or
160 invalid pointers.
161 <BR>
162 2) Out-of-memory (<I>malloc()</I> failure) with <B>I</B>nsert
163 (<B>S</B>et) or <B>D</B>elete (<B>U</B>nset) when modifying a Judy
164 array. Not all calls to insert and delete call <I>malloc()</I>, so they
165 may succeed even when a call to <I>malloc()</I> would fail.
166 <BR>
167 <P>
168 There are roughly <I>three (3)</I> methods of handling errors when using
169 the macros:
170 <DL>
171 <P>
172 <DT><B>1) Default Error Handling Method</B></DT>
173 <DD>
174 The default is to print error messages to <B>stderr</B>, for example:
175 <P>
176 <PRE>
177 File 'YourCfile.c', line 1234: JudyLIns(), JU_ERRNO_* == 2, ID == 321
178 </PRE>
179 This indicates that an error occurred in the <B>JudyLIns()</B> function
180 at line 321. Line 1234 is the line in 'YourCfile.c' where the
181 <B>JLI()</B> call failed. JU_ERRNO_* == 2 is equal to JU_ERRNO_NOMEM
182 (as defined in the <B>Judy.h</B> file). The ID number indicates the
183 source line number in the function where the error originated. Your
184 program then terminates with an <I>exit(1);</I>. By default, both
185 categories of Judy error returns are printed this way. (The 'ID == 321'
186 is for die hards that want more detail or for debugging Judy itself.)
187 <BR>
188 <P>
189 <DT><B>2) Disable Macro Error Handling</B> </DT>
190 <DD>
191 When your program is "bug free", the only errors returned should be
192 <I>malloc()</I> failures. Therefore all error returns can be treated as
193 a <I>malloc()</I> failure. By using the below <B>#define</B>, all
194 error testing and printing is turned off. Additional code needs to be
195 added to the code that can have <I>malloc()</I> failures. Judy was
196 designed to leave the same data in the array before the call if a
197 <I>malloc()</I> fail occurs. (During testing of Judy, we found very few
198 <I>malloc()</I>/OS's that were bug free after a <I>malloc()</I> failure.
199 Sometimes it took weeks to discover because most systems go into a
200 paging frenzy before running out of memory).
201 <PRE>
202 #define JUDYERROR_NOTEST 1
203 </PRE>
204 (in your program code), or
205 <PRE>
206 cc -DJUDYERROR_NOTEST <I>sourcefile</I> -lJudy
207 </PRE>
208 (on your command line).
209 <PRE>
210 // This is an example of how to program using method two (2).
211
212 JLI(PValue, PLArray, Index);
213 if (PValue == PJERR) goto out_of_memory_handling;
214 ...
215
216 JLD(RC_int, PLArray, Index);
217 if (RC_int == JERR) goto out_of_memory_handling;
218 ...
219
220 J1S(RC_int, P1Array, Index);
221 if (RC_int == JERR) goto out_of_memory_handling;
222 ...
223
224 J1U(RC_int, P1Array, Index);
225 if (RC_int == JERR) goto out_of_memory_handling;
226 ...
227
228 </PRE>
229 Note: Without 'JUDYERROR_NOTEST' defined, the 'goto
230 out_of_memory_handling' will never be executed and will be optimized out
231 by the compiler. The default method will be used -- Macro will print
232 error information if an error occurs as explained above.
233 <P>
234 With 'JUDYERROR_NOTEST' defined, the 'goto out_of_memory_handling' will
235 be executed when an error occurs -- which should only happen when
236 <I>malloc()</I> fails.
237 <DT><B>3) User-Specified JUDYERROR() Macro Method</B> </DT>
238 <DD>
239 The <B>JUDYERROR()</B> macro (in <B>Judy.h</B>) provides flexibility for
240 handling error returns as needed to suit your program while still using
241 the Judy array macros instead of function calls. You can use a
242 different <B>JUDYERROR()</B> macro to suit your needs. The following
243 example is a possible alternative to the default. It is used to
244 distinguish between the two types of errors (described above), and
245 explicitly test for the remaining JU_ERRNO_NOMEM errors possible in your
246 program.
247 <P>
248 <PRE>
249 // This is an example of Judy macro API to continue when out of memory
250 // and print and exit(1) when any other error occurs.
251
252 #ifndef JUDYERROR_NOTEST
253 #include &lt;stdio.h&gt; // needed for fprintf()
254
255 // This is the macro that the Judy macro APIs use for return codes of -1:
256
257 #define JUDYERROR(CallerFile, CallerLine, JudyFunc, JudyErrno, JudyErrID) \
258 { \
259 if ((JudyErrno) != JU_ERRNO_NOMEM) /* ! a malloc() failure */ \
260 { \
261 (void) fprintf(stderr, "File '%s', line %d: %s(), " \
262 "JU_ERRNO_* == %d, ID == %d\n", \
263 CallerFile, CallerLine, \
264 JudyFunc, JudyErrno, JudyErrID); \
265 exit(1); \
266 } \
267 }
268 #endif // JUDYERROR_NOTEST not defined
269 <BR>
270 </PRE>
271 This error handling macro must be included before the <B>#include &lt;Judy.h&gt;</B>
272 statement in your program.
273 </DL>
274 <!----------------->
275 <P>
276 <DT><B>SEE ALSO</B></DT>
277 <DD>
278 <A href="Judy1_3.htm">Judy1(3)</A>,
279 <A href="JudyL_3.htm">JudyL(3)</A>,
280 <A href="JudySL_3.htm">JudySL(3)</A>,
281 <A href="JudyHS_3.htm">JudyHS(3)</A>
282 </BODY>
283 </HTML>