~ubuntu-branches/ubuntu/lucid/judy/lucid

« back to all changes in this revision

Viewing changes to doc/ext/JudySL_3x.htm

  • Committer: Bazaar Package Importer
  • Author(s): Troy Heber
  • Date: 2005-03-22 06:55:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050322065553-syjpkd48r4re18dn
Tags: 1.0.1-5

* Moving LGPL link in copyright back to LGPL-2.1
* Cleanup of debian/rules: removed explicit refs to 32-bit archs, removed
  unnecessary nostrip, using --man dir to install man pages, moving from
  dh_movefiles to dh_install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<HTML>
2
 
<HEAD>
3
 
<!-- @(#) $Revision: 4.41 $ $Source: /judy/doc/ext/JudySL_3x.htm $ --->
4
 
<TITLE>JudySL(3X)</TITLE>
5
 
</HEAD>
6
 
 
7
 
<BODY>
8
 
<TABLE border=0 width="100%"><TR>
9
 
<TD width="40%" align="left">JudySL(3X)</TD>
10
 
<TD width="10%" align="center">     </TD>
11
 
<TD width="40%" align="right">JudySL(3X)</TD>
12
 
</TR></TABLE>
13
 
 
14
 
<P>
15
 
<DL>
16
 
 
17
 
<!----------------->
18
 
<DT><B>NAME</B></DT>
19
 
<DD>
20
 
JudySL macros -
21
 
C library for creating and accessing a dynamic array, using
22
 
a null-terminated string as an index (associative array)
23
 
 
24
 
<!----------------->
25
 
<P>
26
 
<DT><B>SYNOPSIS</B></DT>
27
 
<DD>
28
 
<B><PRE>
29
 
cc [flags] <I>sourcefiles</I> -lJudy
30
 
 
31
 
#include &lt;Judy.h&gt;
32
 
 
33
 
#define MAXLINELEN 65536             // define maximum string length
34
 
 
35
 
Word_t * PValue;                     // JudySL array element
36
 
char     Index[MAXLINELEN];          // string
37
 
int      Rc_int;                     // return value
38
 
Word_t   Rc_word;                    // full word return value
39
 
 
40
 
Pvoid_t PJSLArray = (Pvoid_t) NULL;  // initialize JudySL array
41
 
 
42
 
<A href="#JSLI" >JSLI</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLIns">JudySLIns()</A>
43
 
<A href="#JSLD" >JSLD</A>( Rc_int,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLDel">JudySLDel()</A>
44
 
<A href="#JSLG" >JSLG</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLGet">JudySLGet()</A>
45
 
<A href="#JSLFA">JSLFA</A>(Rc_word, PJSLArray);          // <A href="JudySL_funcs_3x.htm#JudySLFreeArray">JudySLFreeArray()</A>
46
 
<A href="#JSLF" >JSLF</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLFirst">JudySLFirst()</A>
47
 
<A href="#JSLN" >JSLN</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLNext">JudySLNext()</A>
48
 
<A href="#JSLL" >JSLL</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLLast">JudySLLast()</A>
49
 
<A href="#JSLP" >JSLP</A>( PValue,  PJSLArray, Index);   // <A href="JudySL_funcs_3x.htm#JudySLPrev">JudySLPrev()</A>
50
 
 
51
 
</PRE></B>
52
 
 
53
 
<!----------------->
54
 
<P>
55
 
<DT><B>DESCRIPTION</B></DT>
56
 
<DD>
57
 
A JudySL array is the equivalent of a sorted set of strings, each associated
58
 
with a value (word).
59
 
A value is addressed by an <B>Index</B> (key), which is a null-terminated
60
 
character string of any length.
61
 
Memory to support the array is allocated as index/value pairs are inserted,
62
 
and released as index/value pairs are deleted.
63
 
This is a form of associative array, where array elements are also sorted
64
 
lexicographically (case-sensitive) by indexes.
65
 
This could be thought of as
66
 
<P><PRE>
67
 
void * JudySLArray["Toto, I don't think we're in Kansas any more"];
68
 
</PRE>
69
 
<P>
70
 
A JudySL array is allocated with a <B>NULL</B> pointer
71
 
<PRE>
72
 
Pvoid_t PJSLArray = (Pvoid_t) NULL;
73
 
</PRE>
74
 
As with an ordinary array, there are no duplicate indexes (strings)
75
 
in a JudySL array.
76
 
<P>
77
 
Using the macros described here, rather than the
78
 
<A href="JudySL_funcs_3x.htm">JudySL function calls</A>,
79
 
the default error handling sends a
80
 
message to the standard error and terminates the program with
81
 
<B>exit(1)</B>.
82
 
For other error handling methods, see the
83
 
<A href="#JSLERR">ERRORS</A> section.
84
 
 
85
 
<P>
86
 
Because the macro forms are faster and have a simpler error
87
 
handling interface than the equivalent
88
 
<A href="JudySL_funcs_3x.htm">functions</A>,
89
 
they are the preferred way to call the JudySL functions.
90
 
 
91
 
<P>
92
 
<DL>
93
 
 
94
 
<DT><A name="JSLI"><B>JSLI(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLIns">JudySLIns()</A></DT>
95
 
<DD>
96
 
Insert an <B>Index</B> string and value in the JudySL array <B>PJSLArray</B>.
97
 
If the <B>Index</B> is successfully inserted,
98
 
the value is initialized to 0. If the <B>Index</B> was already present,
99
 
the value is not modified.
100
 
<P>
101
 
Return <B>PValue</B> pointing to <B>Index</B>'s value.
102
 
Your program must use this pointer to modify the value,
103
 
for example:
104
 
<PRE>
105
 
*PValue = 1234;
106
 
</PRE>
107
 
<P>
108
 
<B>Note</B>:
109
 
<B>JSLI()</B> and <B>JSLD</B> reorganize the JudySL array.
110
 
Therefore, pointers returned from previous <B>JudySL</B> calls become
111
 
invalid and must be reacquired.
112
 
 
113
 
<P>
114
 
<DT><A name="JSLD"><B>JSLD(Rc_int, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLDel">JudySLDel()</A></DT>
115
 
<DD>
116
 
Delete the specified <B>Index</B>/value pair (array element) from the
117
 
JudySL array.
118
 
<P>
119
 
Return <B>Rc_int</B> set to 1 if successful.
120
 
array and it was previously inserted.
121
 
Return <B>Rc_int</B> set to 0 if <B>Index</B> was not present.
122
 
 
123
 
<P>
124
 
<DT><A name="JSLG"><B>JSLG(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLGet">JudySLGet()</A></DT>
125
 
<DD>
126
 
Get the pointer to <B>Index</B>'s value.
127
 
<P>
128
 
Return <B>PValue</B> pointing to <B>Index</B>'s value.
129
 
Return <B>PValue</B> set to <B>NULL</B> if the <B>Index</B> was not present.
130
 
 
131
 
<P>
132
 
<DT><A name="JSLFA"><B>JSLFA(Rc_word, PJSLArray)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLFreeArray">JudySLFreeArray()</A></DT>
133
 
<DD>
134
 
Given a pointer to a JudySL array (<B>PJSLArray</B>), free the entire array (much faster
135
 
than using a <B>JSLN()</B>, <B>JSLD()</B> loop.)
136
 
<P>
137
 
Return <B>Rc_word</B> set to the number of bytes freed and <B>PJSLArray</B> set to NULL.
138
 
 
139
 
<P>
140
 
<DT><B>JudySL Search Functions</B></DT>
141
 
<DD>
142
 
The JudySL search functions allow you to search for indexes in the array.
143
 
You may search inclusively or exclusively,
144
 
in either forward or reverse directions.
145
 
<P>
146
 
If successful,
147
 
<B>Index</B> is returned set to the found index, and
148
 
<B>PValue</B> is returned set to a pointer to <B>Index</B>'s value.
149
 
If unsuccessful,
150
 
<B>PValue</B> is returned set to <B>NULL</B>,
151
 
and <B>Index</B> contains no useful information.
152
 
<B>PValue</B> must be tested for non-<B>NULL</B> prior
153
 
to using <B>Index</B>,
154
 
since a search failure is possible.
155
 
<P>
156
 
<B>Note</B>:
157
 
To accomodate all possible returns, the <B>Index</B> buffer must be
158
 
at least as large
159
 
as the largest string stored in the array.
160
 
<P>
161
 
<DT><A name="JSLF"><B>JSLF(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLFirst">JudySLFirst()</A></DT>
162
 
<DD>
163
 
Search (inclusive) for the first index present that is equal to or greater than the
164
 
passed <B>Index</B> string.
165
 
(Start with a null string to find the first index in the array.)
166
 
<B>JSLF()</B> is typically used to <I>begin</I> a sorted-order scan of
167
 
the valid indexes in a JudySL array.
168
 
<PRE>
169
 
char    Index[MAXLINELEN];
170
 
strcpy (Index, "");
171
 
JSLF(PValue, PJSLArray, Index);
172
 
</PRE>
173
 
 
174
 
<P>
175
 
<DT><A name="JSLN"><B>JSLN(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLNext">JudySLNext()</A></DT>
176
 
<DD>
177
 
Search (exclusive) for the next index present that is greater than the passed
178
 
<B>Index</B> string.
179
 
<B>JSLN()</B> is typically used to <I>continue</I> a sorted-order scan of
180
 
the valid indexes in a JudySL array, or to locate a "neighbor" of a given
181
 
index.
182
 
 
183
 
<P>
184
 
<DT><A name="JSLL"><B>JSLL(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLLast">JudySLLast()</A></DT>
185
 
<DD>
186
 
Search (inclusive) for the last index present that is equal to or less
187
 
than the passed <B>Index</B> string.
188
 
(Start with a maximum-valued string to look up the last index in the array,
189
 
such as a max-length string of 0xff bytes.)
190
 
<B>JSLL()</B> is typically used to <I>begin</I> a reverse-sorted-order
191
 
scan of the valid indexes in a JudySL array.
192
 
 
193
 
<P>
194
 
<DT><A name="JSLP"><B>JSLP(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3x.htm#JudySLPrev">JudySLPrev()</A></DT>
195
 
<DD>
196
 
Search (exclusive) for the previous index present that is less than the
197
 
passed <B>Index</B> string.
198
 
<B>JSLP()</B> is typically used to <I>continue</I> a reverse-sorted-order
199
 
scan of the valid indexes in a JudySL array, or to locate a "neighbor" of
200
 
a given index.
201
 
 
202
 
</DL>
203
 
 
204
 
 
205
 
<!----------------->
206
 
<P>
207
 
<DT><A name="JSLERR"><B>ERRORS</B></A></DT>
208
 
<DD>
209
 
There are two categories of Judy error returns:
210
 
<BR>
211
 
1) User programming errors (bugs) such as memory corruption or
212
 
invalid pointers.
213
 
<BR>
214
 
2) Out-of-memory (<I>malloc</I>() failure) when modifying a JudySL array with <B>JSLI()</B>
215
 
or <B>JSLD()</B>.
216
 
<P>There are three methods of handling errors when using the macros:
217
 
<BR>
218
 
1) Default error handling.
219
 
<BR>
220
 
2) User-specified <B>JUDYERROR()</B> macro.
221
 
<BR>
222
 
3) Disable macro error handling.
223
 
<DL>
224
 
<P>
225
 
<DT><B>Default Error Handling Method</B></DT>
226
 
<DD>
227
 
The default is to print error messages to <B>stderr</B>, for example:
228
 
<P><PRE>
229
 
File 'YourCfile.c', line 1234: JudySLIns(), JU_ERRNO_* == 2, ID == 321
230
 
</PRE>
231
 
This indicates that an error occurred in a <B>JSLI()</B> call at line 1234 in 'YourCfile.c'.
232
 
JU_ERRNO_* == 2 is JU_ERRNO_NOMEM (as defined in the Judy.h file).
233
 
The ID number indicates the Judy source line number where the error was detected.
234
 
<P>
235
 
Your program then terminates with an <B>exit(1)</B>.
236
 
 
237
 
<P>
238
 
<DT><B>User-Specified JUDYERROR() Macro Method</B> </DT>
239
 
<DD>
240
 
<P>
241
 
The <B>JUDYERROR()</B> macro provides flexibility for handling error returns
242
 
as needed to suit your program while still accessing JudySL arrays using macros
243
 
instead of function calls.  You can modify <B>JUDYERROR()</B> to distinguish between
244
 
the two types of errors (described above), and explicitly test for the remaining
245
 
JU_ERRNO_NOMEM errors possible in your program.
246
 
<P>
247
 
<PRE>
248
 
// This is an example of JudySL macro API to continue when out of memory.
249
 
 
250
 
#ifndef JUDYERROR_NOTEST
251
 
#include &lt;stdio.h&gt;
252
 
 
253
 
// This is the macro that the Judy macro APIs use for return codes of -1:
254
 
 
255
 
#define JUDYERROR(CallerFile, CallerLine, JudyFunc, JudyErrno, JudyErrID) \
256
 
{                                                                 \
257
 
    if ((JudyErrno) != JU_ERRNO_NOMEM) /* ! a malloc() failure */ \
258
 
    {                                                             \
259
 
        (void) fprintf(stderr, "File '%s', line %d: %s(), "       \
260
 
            "JU_ERRNO_* == %d, ID == %d\n",                       \
261
 
            CallerFile, CallerLine,                               \
262
 
            JudyFunc, JudyErrno, JudyErrID);                      \
263
 
         exit(1);                                                 \
264
 
    }                                                             \
265
 
}
266
 
#endif // JUDYERROR_NOTEST not defined
267
 
</PRE>
268
 
This error handling macro must be included before the <B>#include &lt;Judy.h&gt;</B>
269
 
statement in your program.
270
 
 
271
 
<P>
272
 
<DT><B>Disable Macro Error Handling</B> </DT>
273
 
<DD>
274
 
<P>
275
 
When your program is "bug free",
276
 
the only errors occurring should be <I>malloc</I>(3) errors.
277
 
You can turn off error handling included in
278
 
the JudySL macros by using
279
 
<PRE>
280
 
#define JUDYERROR_NOTEST 1
281
 
</PRE>
282
 
(in your program code), or
283
 
<PRE>
284
 
cc -DJUDYERROR_NOTEST <I>sourcefile</I> -lJudy
285
 
</PRE>
286
 
(on your command line).
287
 
</DL>
288
 
 
289
 
<!----------------->
290
 
<P>
291
 
<DT><B>EXAMPLES</B></DT>
292
 
<DD>
293
 
<P><PRE>
294
 
// This is an example of Judy macro API to continue when out of memory
295
 
 
296
 
// Put this before the #include &lt;Judy.h&gt;
297
 
// #define JUDYERROR_NOTEST // or defined with cc -DJUDYERROR_NOTEST ...
298
 
 
299
 
#ifndef JUDYERROR_NOTEST
300
 
#include &lt;stdio.h&gt;
301
 
 
302
 
// This is the macro the Judy macro APIs use for return codes of -1
303
 
#define JUDYERROR(CallerFile, CallerLine, JudyFunc, JudyErrno, JudyErrID) \
304
 
{                                                               \
305
 
    if ((JudyErrno) != JU_ERRNO_NOMEM) /* ! malloc() failure */ \
306
 
    {                                                           \
307
 
        (void) fprintf(stderr, "File '%s', line %d: %s(), "     \
308
 
            "JU_ERRNO_* == %d, ID == %d\n",                     \
309
 
            CallerFile, CallerLine,                             \
310
 
            JudyFunc, JudyErrno, JudyErrID);                    \
311
 
         exit(1);                                               \
312
 
    }                                                           \
313
 
}
314
 
#endif // JUDYERROR_NOTEST
315
 
 
316
 
#include &lt;Judy.h&gt;
317
 
 
318
 
#define MAXLINE 65536           // max string (line) len
319
 
 
320
 
int     // Usage:  JudySort &lt; file_to_sort
321
 
main()
322
 
{
323
 
    Pvoid_t   PJArray = (PWord_t)NULL;  // Judy array.
324
 
    PWord_t   PValue;                   // Judy array element.
325
 
    Word_t    Bytes;                    // size of JudySL array.
326
 
    char      Index[MAXLINE];           // string to check
327
 
 
328
 
    while (fgets(Index, sizeof(Index), stdin) != (char *)NULL)
329
 
    {
330
 
        JSLI(PValue, PJArray, Index);   // store string into array
331
 
        if (PValue == PJERR)            // if out of memory?
332
 
        {                               // so do something
333
 
            printf("Malloc failed -- get more ram\n");
334
 
            exit(1);
335
 
        }
336
 
        ++(*PValue);                    // count instances of string
337
 
    }
338
 
    Index[0] = '\0';                    // start with smallest string.
339
 
    JSLF(PValue, PJArray, Index);       // get first string
340
 
    while (PValue != NULL)
341
 
    {
342
 
        while ((*PValue)--)             // print duplicates
343
 
            printf("%s", Index);
344
 
        JSLN(PValue, PJArray, Index);   // get next string
345
 
    }
346
 
    JSLFA(Bytes, PJArray);              // free array
347
 
 
348
 
    fprintf(stderr, "The JudySL array used %lu bytes of memory\n", Bytes);
349
 
    return (0);
350
 
}
351
 
</PRE>
352
 
 
353
 
<!----------------->
354
 
<P>
355
 
<DT><B>AUTHOR</B></DT>
356
 
<DD>
357
 
Judy was invented by Doug Baskins and implemented by Hewlett-Packard.
358
 
 
359
 
<!----------------->
360
 
<P>
361
 
<DT><B>SEE ALSO</B></DT>
362
 
<DD>
363
 
<A href="Judy_3x.htm">Judy(3X)</A>,
364
 
<A href="Judy1_3x.htm">Judy1(3X)</A>,
365
 
<A href="Judy1_funcs_3x.htm">Judy1_funcs(3X)</A>,
366
 
<A href="JudyL_3x.htm">JudyL(3X)</A>,
367
 
<A href="JudyL_funcs_3x.htm">JudyL_funcs(3X)</A>,
368
 
<A href="JudySL_funcs_3x.htm">JudySL_funcs(3X)</A>,
369
 
<BR>
370
 
<I>malloc</I>(3),
371
 
<BR>
372
 
the Judy website,
373
 
<A href="http://www.sourcejudy.com/">
374
 
http://www.sourcejudy.com/</A>,
375
 
for further information and Application Notes.
376
 
</DL>
377
 
 
378
 
</BODY>
379
 
</HTML>