Debian Codegraph @

doc/ext/JudySL_3.htm

raw
1 <HTML>
2 <HEAD>
3 <!-- @(#) $Revision: 4.43 $ $Source: /cvsroot/judy/doc/ext/JudySL_3.htm,v $ --->
4 <TITLE>JudySL(3)</TITLE>
5 </HEAD>
6 <BODY>
7 <TABLE border=0 width="100%"><TR>
8 <TD width="40%" align="left">JudySL(3)</TD>
9 <TD width="10%" align="center"> </TD>
10 <TD width="40%" align="right">JudySL(3)</TD>
11 </TR></TABLE>
12 <P>
13 <DL>
14 <!----------------->
15 <DT><B>NAME</B></DT>
16 <DD>
17 JudySL macros -
18 C library for creating and accessing a dynamic array, using
19 a null-terminated string as an <B>Index</B> (associative array)
20 <!----------------->
21 <P>
22 <DT><B>SYNOPSIS</B></DT>
23 <DD>
24 <B><PRE>
25 cc [flags] <I>sourcefiles</I> -lJudy
26
27 #include &lt;Judy.h&gt;
28
29 #define MAXLINELEN 1000000 // define maximum string length
30
31 Word_t * PValue; // JudySL array element
32 uint8_t Index[MAXLINELEN]; // string
33 int Rc_int; // return value
34 Word_t Rc_word; // full word return value
35
36 Pvoid_t PJSLArray = (Pvoid_t) NULL; // initialize JudySL array
37
38 <A href="#JSLI" >JSLI</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLIns">JudySLIns()</A>
39 <A href="#JSLD" >JSLD</A>( Rc_int, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLDel">JudySLDel()</A>
40 <A href="#JSLG" >JSLG</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLGet">JudySLGet()</A>
41 <A href="#JSLFA">JSLFA</A>(Rc_word, PJSLArray); // <A href="JudySL_funcs_3.htm#JudySLFreeArray">JudySLFreeArray()</A>
42 <A href="#JSLF" >JSLF</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLFirst">JudySLFirst()</A>
43 <A href="#JSLN" >JSLN</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLNext">JudySLNext()</A>
44 <A href="#JSLL" >JSLL</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLLast">JudySLLast()</A>
45 <A href="#JSLP" >JSLP</A>( PValue, PJSLArray, Index); // <A href="JudySL_funcs_3.htm#JudySLPrev">JudySLPrev()</A>
46
47 </PRE></B>
48 <!----------------->
49 <P>
50 <DT><B>DESCRIPTION</B></DT>
51 <DD>
52 A JudySL array is the equivalent of a sorted set of strings, each associated
53 with a <B>Value</B> (word).
54 A <B>Value</B> is addressed by an <B>Index</B> (key), which is a null-terminated
55 character string of any length.
56 Memory to support the array is allocated as index/value pairs are inserted,
57 and released as index/value pairs are deleted.
58 This is a form of associative array, where array elements are also sorted
59 lexicographically (case-sensitive) by indexes.
60 This could be thought of as
61 <P><PRE>
62 void * JudySLArray["Toto, I don't think we're in Kansas any more"];
63 </PRE>
64 <P>
65 A JudySL array is allocated with a <B>NULL</B> pointer
66 <PRE>
67 Pvoid_t PJSLArray = (Pvoid_t) NULL;
68 </PRE>
69 As with an ordinary array, there are no duplicate indexes (strings)
70 in a JudySL array.
71 <P>
72 Using the macros described here, rather than the
73 <A href="JudySL_funcs_3.htm">JudySL function calls</A>,
74 the default error handling sends a
75 message to the standard error and terminates the program with
76 <B>exit(1)</B>.
77 <P>
78 <DT><A name="JSLI"><B>JSLI(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLIns">JudySLIns()</A></DT>
79 <DD>
80 Insert an <B>Index</B> string and <B>Value</B> in the JudySL array <B>PJSLArray</B>.
81 If the <B>Index</B> is successfully inserted,
82 the <B>Value</B> is initialized to 0. If the <B>Index</B> was already present,
83 the <B>Value</B> is not modified.
84 <P>
85 Return <B>PValue</B> pointing to <B>Index</B>'s <B>Value</B>.
86 Your program must use this pointer to modify the <B>Value</B>,
87 for example:
88 <PRE>
89 *PValue = 1234;
90 </PRE>
91 <P>
92 <B>Note</B>:
93 <B>JSLI()</B> and <B>JSLD</B> reorganize the JudySL array.
94 Therefore, pointers returned from previous <B>JudySL</B> calls become
95 invalid and must be reacquired.
96 <P>
97 <DT><A name="JSLD"><B>JSLD(Rc_int, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLDel">JudySLDel()</A></DT>
98 <DD>
99 Delete the specified <B>Index</B>/<B>Value</B> pair (array element) from the
100 JudySL array.
101 <P>
102 Return <B>Rc_int</B> set to 1 if successful.
103 array and it was previously inserted.
104 Return <B>Rc_int</B> set to 0 if <B>Index</B> was not present.
105 <P>
106 <DT><A name="JSLG"><B>JSLG(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLGet">JudySLGet()</A></DT>
107 <DD>
108 Get the pointer to <B>Index</B>'s <B>Value</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="JSLFA"><B>JSLFA(Rc_word, PJSLArray)</B></A> // <A href="JudySL_funcs_3.htm#JudySLFreeArray">JudySLFreeArray()</A></DT>
114 <DD>
115 Given a pointer to a JudySL array (<B>PJSLArray</B>), free the entire array (much faster
116 than using a <B>JSLN()</B>, <B>JSLD()</B> loop.)
117 <P>
118 Return <B>Rc_word</B> set to the number of bytes freed and <B>PJSLArray</B> set to NULL.
119 <P>
120 <DT><B>JudySL Search Functions</B></DT>
121 <DD>
122 The JudySL search functions allow you to search for indexes in the array.
123 You may search inclusively or exclusively,
124 in either forward or reverse directions.
125 <P>
126 If successful,
127 <B>Index</B> is returned set to the found index, and
128 <B>PValue</B> is returned set to a pointer to <B>Index</B>'s <B>Value</B>.
129 If unsuccessful,
130 <B>PValue</B> is returned set to <B>NULL</B>,
131 and <B>Index</B> contains no useful information.
132 <B>PValue</B> must be tested for non-<B>NULL</B> prior
133 to using <B>Index</B>,
134 since a search failure is possible.
135 <P>
136 <B>Note</B>:
137 To accomodate all possible returns, the <B>Index</B> buffer must be
138 at least as large
139 as the largest string stored in the array.
140 <P>
141 <DT><A name="JSLF"><B>JSLF(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLFirst">JudySLFirst()</A></DT>
142 <DD>
143 Search (inclusive) for the first index present that is equal to or greater than the
144 passed <B>Index</B> string.
145 (Start with a null string to find the first index in the array.)
146 <B>JSLF()</B> is typically used to <I>begin</I> a sorted-order scan of
147 the valid indexes in a JudySL array.
148 <PRE>
149 uint8_t Index[MAXLINELEN];
150 strcpy (Index, "");
151 JSLF(PValue, PJSLArray, Index);
152 </PRE>
153 <P>
154 <DT><A name="JSLN"><B>JSLN(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLNext">JudySLNext()</A></DT>
155 <DD>
156 Search (exclusive) for the next index present that is greater than the passed
157 <B>Index</B> string.
158 <B>JSLN()</B> is typically used to <I>continue</I> a sorted-order scan of
159 the valid indexes in a JudySL array, or to locate a "neighbor" of a given
160 index.
161 <P>
162 <DT><A name="JSLL"><B>JSLL(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLLast">JudySLLast()</A></DT>
163 <DD>
164 Search (inclusive) for the last index present that is equal to or less
165 than the passed <B>Index</B> string.
166 (Start with a maximum-valued string to look up the last index in the array,
167 such as a max-length string of 0xff bytes.)
168 <B>JSLL()</B> is typically used to <I>begin</I> a reverse-sorted-order
169 scan of the valid indexes in a JudySL array.
170 <P>
171 <DT><A name="JSLP"><B>JSLP(PValue, PJSLArray, Index)</B></A> // <A href="JudySL_funcs_3.htm#JudySLPrev">JudySLPrev()</A></DT>
172 <DD>
173 Search (exclusive) for the previous index present that is less than the
174 passed <B>Index</B> string.
175 <B>JSLP()</B> is typically used to <I>continue</I> a reverse-sorted-order
176 scan of the valid indexes in a JudySL array, or to locate a "neighbor" of
177 a given index.
178 <!----------------->
179 <P>
180 <DT><A name="JSLERR"><B>ERRORS:</B> See: </A><A href="Judy_3.htm#ERRORS">Judy_3.htm#ERRORS</A></DT>
181 <DD>
182 <!----------------->
183 <P>
184 <DT><B>EXAMPLE</B> of a string sort routine</DT>
185 <P><PRE>
186 #include &lt;stdio.h&gt;
187 #include &lt;Judy.h&gt;
188
189 #define MAXLINE 1000000 // max string (line) length
190
191 uint8_t Index[MAXLINE]; // string to insert
192
193 int // Usage: JudySort &lt; file_to_sort
194 main()
195 {
196 Pvoid_t PJArray = (PWord_t)NULL; // Judy array.
197 PWord_t PValue; // Judy array element.
198 Word_t Bytes; // size of JudySL array.
199
200 while (fgets(Index, MAXLINE, stdin) != (char *)NULL)
201 {
202 JSLI(PValue, PJArray, Index); // store string into array
203 if (PValue == PJERR) // if out of memory?
204 { // so do something
205 printf("Malloc failed -- get more ram\n");
206 exit(1);
207 }
208 ++(*PValue); // count instances of string
209 }
210 Index[0] = '\0'; // start with smallest string.
211 JSLF(PValue, PJArray, Index); // get first string
212 while (PValue != NULL)
213 {
214 while ((*PValue)--) // print duplicates
215 printf("%s", Index);
216 JSLN(PValue, PJArray, Index); // get next string
217 }
218 JSLFA(Bytes, PJArray); // free array
219
220 fprintf(stderr, "The JudySL array used %lu bytes of memory\n", Bytes);
221 return (0);
222 }
223 </PRE>
224 <!----------------->
225 <P>
226 <DT><B>AUTHOR</B></DT>
227 <DD>
228 Judy was invented by Doug Baskins and implemented by Hewlett-Packard.
229 <!----------------->
230 <P>
231 <DT><B>SEE ALSO</B></DT>
232 <DD>
233 <A href="Judy_3.htm">Judy(3)</A>,
234 <A href="Judy1_3.htm">Judy1(3)</A>,
235 <A href="JudyL_3.htm">JudyL(3)</A>,
236 <A href="JudyHS_3.htm">JudyHS(3)</A>,
237 <BR>
238 <I>malloc()</I>,
239 <BR>
240 the Judy website,
241 <A href="http://judy.sourceforge.net">
242 http://judy.sourceforge.net</A>,
243 for further information and Application Notes.
244 </DL>
245 </BODY>
246 </HTML>