| 1 |
<HTML> |
| 2 |
<HEAD> |
| 3 |
<!-- @(#) $Revision: 4.48 $ $Source: /cvsroot/judy/judy/doc/ext/Judy1_3.htm,v $ ---> |
| 4 |
<TITLE>Judy1(3)</TITLE> |
| 5 |
</HEAD> |
| 6 |
|
| 7 |
<BODY> |
| 8 |
<TABLE border=0 width="100%"><TR> |
| 9 |
<TD width="40%" align="left">Judy1(3)</TD> |
| 10 |
<TD width="10%" align="center"> </TD> |
| 11 |
<TD width="40%" align="right">Judy1(3)</TD> |
| 12 |
</TR></TABLE> |
| 13 |
<P> |
| 14 |
<DL> |
| 15 |
<!-----------------> |
| 16 |
<DT><B>NAME</B></DT> |
| 17 |
<DD> |
| 18 |
Judy1 macros - |
| 19 |
C library for creating and accessing a dynamic array of bits, using |
| 20 |
any value of a word as an index. |
| 21 |
<!-----------------> |
| 22 |
<P> |
| 23 |
<DT><B>SYNOPSIS</B></DT> |
| 24 |
<DD> |
| 25 |
<B><PRE> |
| 26 |
cc [flags] <I>sourcefiles</I> -lJudy |
| 27 |
</PRE></B> |
| 28 |
<P> |
| 29 |
<B><PRE> |
| 30 |
#include <Judy.h> |
| 31 |
|
| 32 |
int Rc_int; // return code - integer |
| 33 |
Word_t Rc_word; // return code - unsigned word |
| 34 |
Word_t Index, Index1, Index2, Nth; |
| 35 |
|
| 36 |
Pvoid_t PJ1Array = (Pvoid_t) NULL; // initialize Judy1 array |
| 37 |
|
| 38 |
<A href="#J1S" >J1S</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Set">Judy1Set()</A> |
| 39 |
<A href="#J1U" >J1U</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Unset">Judy1Unset()</A> |
| 40 |
<A href="#J1T" >J1T</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Test">Judy1Test()</A> |
| 41 |
<A href="#J1C" >J1C</A>( Rc_word, PJ1Array, Index1, Index2); // <A href="Judy1_funcs_3.htm#Judy1Count">Judy1Count()</A> |
| 42 |
<A href="#J1BC">J1BC</A>(Rc_int, PJ1Array, Nth, Index); // <A href="Judy1_funcs_3.htm#Judy1ByCount">Judy1ByCount()</A> |
| 43 |
<A href="#J1FA">J1FA</A>(Rc_word, PJ1Array); // <A href="Judy1_funcs_3.htm#Judy1FreeArray">Judy1FreeArray()</A> |
| 44 |
<A href="#J1MU">J1MU</A>(Rc_word, PJ1Array); // <A href="Judy1_funcs_3.htm#Judy1MemUsed">Judy1MemUsed()</A> |
| 45 |
<A href="#J1F" >J1F</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1First">Judy1First()</A> |
| 46 |
<A href="#J1N" >J1N</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Next">Judy1Next()</A> |
| 47 |
<A href="#J1L" >J1L</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Last">Judy1Last()</A> |
| 48 |
<A href="#J1P" >J1P</A>( Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1Prev">Judy1Prev()</A> |
| 49 |
<A href="#J1FE">J1FE</A>(Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1FirstEmpty">Judy1FirstEmpty()</A> |
| 50 |
<A href="#J1NE">J1NE</A>(Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1NextEmpty">Judy1NextEmpty()</A> |
| 51 |
<A href="#J1LE">J1LE</A>(Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1LastEmpty">Judy1LastEmpty()</A> |
| 52 |
<A href="#J1PE">J1PE</A>(Rc_int, PJ1Array, Index); // <A href="Judy1_funcs_3.htm#Judy1PrevEmpty">Judy1PrevEmpty()</A> |
| 53 |
</PRE></B> |
| 54 |
<!-----------------> |
| 55 |
<P> |
| 56 |
<DT><B>DESCRIPTION</B></DT> |
| 57 |
<DD> |
| 58 |
A Judy1 array is the equivalent of a bit array or bit map. |
| 59 |
A bit is addressed by an <B>Index</B> (key). |
| 60 |
The array may be sparse, and the <B>Index</B> may be any word-sized <B>Value</B>. |
| 61 |
If an index is present, it represents a set bit |
| 62 |
(a bit set represents an index present). |
| 63 |
If an index is absent, it represents an unset bit |
| 64 |
(a bit unset represents an absent index). |
| 65 |
<P> |
| 66 |
A Judy1 array is allocated with a <B>NULL</B> pointer |
| 67 |
<PRE> |
| 68 |
Pvoid_t PJ1Array = (Pvoid_t) NULL; |
| 69 |
</PRE> |
| 70 |
Memory to support the array is allocated as bits are set, |
| 71 |
and released as bits are unset. |
| 72 |
If the Judy1 pointer (<B>PJ1Array</B>) is NULL, all bits are unset (and |
| 73 |
the Judy1 array requires no memory). |
| 74 |
<P> |
| 75 |
As with an ordinary array, a Judy1 array contains no duplicate indexes. |
| 76 |
<P> |
| 77 |
Using the macros described here, rather than the |
| 78 |
<A href="Judy1_funcs_3.htm">Judy1 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="#J1ERR">ERRORS</A> section. |
| 84 |
<P> |
| 85 |
Because the macro forms are sometimes faster and have a simpler error |
| 86 |
handling interface than the equivalent |
| 87 |
<A href="Judy1_funcs_3.htm">functions</A>, |
| 88 |
they are the preferred way of calling the Judy1 functions. |
| 89 |
<P> |
| 90 |
<DL> |
| 91 |
<DT><A name="J1S"><B>J1S(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Set">Judy1Set()</A></DT> |
| 92 |
<DD> |
| 93 |
Set <B>Index</B>'s bit in the Judy1 array <B>PJ1Array</B>. |
| 94 |
<P> |
| 95 |
Return <B>Rc_int</B> set to 1 if <B>Index</B>'s bit was previously unset |
| 96 |
(successful), otherwise 0 |
| 97 |
if the bit was already set (unsuccessful). |
| 98 |
<P> |
| 99 |
<DT><A name="J1U"><B>J1U(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Unset">Judy1Unset()</A></DT> |
| 100 |
<DD> |
| 101 |
Unset <B>Index</B>'s bit in the Judy1 array <B>PJ1Array</B>; |
| 102 |
that is, remove <B>Index</B> from the Judy1 array. |
| 103 |
<P> |
| 104 |
Return <B>Rc_int</B> set to 1 if <B>Index</B>'s bit was |
| 105 |
previously set (successful), otherwise 0 |
| 106 |
if the bit was already unset (unsuccessful). |
| 107 |
<P> |
| 108 |
<DT><A name="J1T"><B>J1T(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Test">Judy1Test()</A></DT> |
| 109 |
<DD> |
| 110 |
Test if <B>Index</B>'s bit is set in the |
| 111 |
Judy1 array <B>PJ1Array</B>. |
| 112 |
<P> |
| 113 |
Return <B>Rc_int</B> set to 1 if <B>Index</B>'s bit is set |
| 114 |
(<B>Index</B> is present), |
| 115 |
0 if it is unset (<B>Index</B> is absent). |
| 116 |
<P> |
| 117 |
<DT><A name="J1C"><B>J1C(Rc_word, PJ1Array, Index1, Index2);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Count">Judy1Count()</A></DT> |
| 118 |
<DD> |
| 119 |
Count the number of indexes present in the Judy1 array |
| 120 |
<B>PJ1Array</B> between |
| 121 |
<B>Index1</B> and <B>Index2</B> (inclusive). |
| 122 |
<P> |
| 123 |
Return <B>Rc_word</B> set to the count. |
| 124 |
A return <B>Value</B> of 0 can be valid as a count, |
| 125 |
or it can indicate a special case for fully |
| 126 |
populated array (32-bit machines only). See |
| 127 |
<A href="Judy1_funcs_3.htm#Judy1Count">Judy1Count()</A> |
| 128 |
for ways to resolve this. |
| 129 |
<P> |
| 130 |
To count all indexes present (population) in a Judy1 bit array, use: |
| 131 |
<PRE> |
| 132 |
J1C(Rc_word, PJ1Array, 0, -1); |
| 133 |
</PRE> |
| 134 |
<B>Note:</B> The -1 promotes to the maximum index, that is, all ones. |
| 135 |
<P> |
| 136 |
<DT><A name="J1BC"><B>J1BC(Rc_int, PJ1Array, Nth, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1ByCount">Judy1ByCount()</A></DT> |
| 137 |
<DD> |
| 138 |
Locate the <B>Nth</B> index that is present in the Judy1 array |
| 139 |
<B>PJ1Array</B> (<B>Nth</B> = 1 returns the first index present). |
| 140 |
To refer to the last index in a fully populated array (all indexes |
| 141 |
present, which is rare), use <B>Nth</B> = 0. |
| 142 |
<P> |
| 143 |
Return <B>Rc_int</B> set to 1 and <B>Index</B> set to the |
| 144 |
<B>Nth</B> index if found, otherwise return <B>Rc_int</B> |
| 145 |
set to 0 (the <B>Value</B> of <B>Index</B> contains no |
| 146 |
useful information). |
| 147 |
<P> |
| 148 |
<DT><A name="J1FA"><B>J1FA(Rc_word, PJ1Array);</B></A> // <A href="Judy1_funcs_3.htm#Judy1FreeArray">Judy1FreeArray()</A></DT> |
| 149 |
<DD> |
| 150 |
Free the entire Judy1 array <B>PJ1Array</B> (much faster than using a |
| 151 |
<B>J1N()</B>, <B>J1U()</B> loop). |
| 152 |
<P> |
| 153 |
Return <B>Rc_word</B> set to the number of bytes freed, |
| 154 |
and <B>PJ1Array</B> set to <B>NULL</B>. |
| 155 |
<P> |
| 156 |
<DT><A name="J1MU"><B>J1MU(Rc_word, PJ1Array);</B></A> // <A href="Judy1_funcs_3.htm#Judy1MemUsed">Judy1MemUsed()</A></DT> |
| 157 |
<DD> |
| 158 |
Return <B>Rc_word</B> set to the number of bytes of memory currently in use by |
| 159 |
Judy1 array <B>PJ1Array</B>. This is a very fast routine, and may be used after |
| 160 |
a <B>J1S()</B> or <B>J1U()</B> call with little performance impact. |
| 161 |
<P> |
| 162 |
<DT><B>Judy1 Search Functions</B></DT> |
| 163 |
<DD> |
| 164 |
The Judy1 search functions allow you to search for set or unset bits in the array. |
| 165 |
You may search inclusively or exclusively, |
| 166 |
in either forward or reverse directions. |
| 167 |
All of the search functions use a similar calling sequence. |
| 168 |
<B>Rc_int</B> is returned set to 1 for a successful search and the found <B>Index</B> is returned. |
| 169 |
<B>Rc_int</B> is returned set to 0 for an unsuccessful search, |
| 170 |
and <B>Index</B> contains no useful information. |
| 171 |
The return code <B>Rc_int</B> must be checked prior to using the returned <B>Index</B>, |
| 172 |
since a search failure is possible. |
| 173 |
<P> |
| 174 |
<DT><A name="J1F"><B>J1F(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1First">Judy1First()</A></DT> |
| 175 |
<DD> |
| 176 |
Search (inclusive) for the first index present that is equal |
| 177 |
to or greater than the passed <B>Index</B>. |
| 178 |
(Start with <B>Index</B> = 0 to find the first index in the |
| 179 |
array.) <B>J1F()</B> is typically used to <I>begin</I> a |
| 180 |
sorted-order scan of the indexes present in a Judy1 array. |
| 181 |
<P> |
| 182 |
<DT><A name="J1N"><B>J1N(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Next">Judy1Next()</A></DT> |
| 183 |
<DD> |
| 184 |
Search (exclusive) for the next index present that is |
| 185 |
greater than the passed <B>Index</B>. |
| 186 |
<B>J1N()</B> is typically used to <I>continue</I> a |
| 187 |
sorted-order scan of the indexes present |
| 188 |
in a Judy1 array, or to locate a "neighbor" of a given index. |
| 189 |
<P> |
| 190 |
<DT><A name="J1L"><B>J1L(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Last">Judy1Last()</A></DT> |
| 191 |
<DD> |
| 192 |
Search (inclusive) for the last index present that is equal |
| 193 |
to or less than the passed <B>Index</B>. (Start with |
| 194 |
<B>Index</B> = -1, that is, all ones, to find the last index |
| 195 |
in the array.) <B>J1L()</B> is typically used to <I>begin</I> |
| 196 |
a reverse-sorted-order scan |
| 197 |
of the indexes present in a Judy1 array. |
| 198 |
<P> |
| 199 |
<DT><A name="J1P"><B>J1P(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1Prev">Judy1Prev()</A></DT> |
| 200 |
<DD> |
| 201 |
Search (exclusive) for the previous index present that is |
| 202 |
less than the passed <B>Index</B>. <B>J1P()</B> is typically |
| 203 |
used to <I>continue</I> a reverse-sorted-order scan of the indexes |
| 204 |
present in a Judy1 array, or to locate a "neighbor" of a given index. |
| 205 |
<P> |
| 206 |
<DT><A name="J1FE"><B>J1FE(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1FirstEmpty">Judy1FirstEmpty()</A></DT> |
| 207 |
<DD> |
| 208 |
Search (inclusive) for the first absent index that is equal to |
| 209 |
or greater than the passed <B>Index</B>. (Start with |
| 210 |
<B>Index</B> = 0 to find the first index absent in the array.) |
| 211 |
<P> |
| 212 |
<DT><A name="J1NE"><B>J1NE(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1NextEmpty">Judy1NextEmpty()</A></DT> |
| 213 |
<DD> |
| 214 |
Search (exclusive) for the next absent index that is |
| 215 |
greater than the passed <B>Index</B>. |
| 216 |
<P> |
| 217 |
<DT><A name="J1LE"><B>J1LE(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1LastEmpty">Judy1LastEmpty()</A></DT> |
| 218 |
<DD> |
| 219 |
Search (inclusive) for the last absent index that is |
| 220 |
equal to or less than the passed <B>Index</B>. |
| 221 |
(Start with <B>Index</B> = -1 to find the last index |
| 222 |
absent in the array.) |
| 223 |
<P> |
| 224 |
<DT><A name="J1PE"><B>J1PE(Rc_int, PJ1Array, Index);</B></A> // <A href="Judy1_funcs_3.htm#Judy1PrevEmpty">Judy1PrevEmpty()</A></DT> |
| 225 |
<DD> |
| 226 |
Search (exclusive) for the previous absent index that is |
| 227 |
less than the passed <B>Index</B>. |
| 228 |
</DL> |
| 229 |
<!-----------------> |
| 230 |
<P> |
| 231 |
<DT><A name="J1ERR"><B>ERRORS:</B> See: </A><A href="Judy_3.htm#ERRORS">Judy_3.htm#ERRORS</A></DT> |
| 232 |
<DD> |
| 233 |
<!-----------------> |
| 234 |
<P> |
| 235 |
<DT><A name="J1EX"><B>EXAMPLE</B></A></DT> |
| 236 |
<DD> |
| 237 |
In the following example, errors in the <B>J1S()</B> or <B>J1U()</B> calls |
| 238 |
go to a user-defined procedure, process_malloc_failure. This is not needed |
| 239 |
when you use the default <B>JUDYERROR()</B> macro, since the default causes |
| 240 |
your program to exit on all failures, |
| 241 |
including <I>malloc()</I> failure. |
| 242 |
<P> |
| 243 |
<PRE> |
| 244 |
#include <stdio.h> |
| 245 |
#include <Judy.h> |
| 246 |
|
| 247 |
int main() // Example program of Judy1 macro APIs |
| 248 |
{ |
| 249 |
Word_t Index; // index (or key) |
| 250 |
Word_t Rcount; // count of indexes (or bits set) |
| 251 |
Word_t Rc_word; // full word return value |
| 252 |
int Rc_int; // boolean values returned (0 or 1) |
| 253 |
|
| 254 |
Pvoid_t PJ1Array = (Pvoid_t) NULL; // initialize Judy1 array |
| 255 |
|
| 256 |
Index = 123456; |
| 257 |
J1S(Rc_int, J1Array, Index); // set bit at 123456 |
| 258 |
if (Rc_int == JERR) goto process_malloc_failure; |
| 259 |
if (Rc_int == 1) printf("OK - bit successfully set at %lu\n", Index); |
| 260 |
if (Rc_int == 0) printf("BUG - bit already set at %lu\n", Index); |
| 261 |
|
| 262 |
Index = 654321; |
| 263 |
J1T(Rc_int, J1Array, Index); // test if bit set at 654321 |
| 264 |
if (Rc_int == 1) printf("BUG - set bit at %lu\n", Index); |
| 265 |
if (Rc_int == 0) printf("OK - bit not set at %lu\n", Index); |
| 266 |
|
| 267 |
J1C(Rcount, J1Array, 0, -1); // count all bits set in array |
| 268 |
printf("%lu bits set in Judy1 array\n", Rcount); |
| 269 |
|
| 270 |
Index = 0; |
| 271 |
J1F(Rc_int, J1Array, Index); // find first bit set in array |
| 272 |
if (Rc_int == 1) printf("OK - first bit set is at %lu\n", Index); |
| 273 |
if (Rc_int == 0) printf("BUG - no bits set in array\n"); |
| 274 |
|
| 275 |
J1MU(Rc_word, J1Array); // how much memory was used? |
| 276 |
printf("%lu Indexes used %lu bytes of memory\n", Rcount, Rc_word); |
| 277 |
|
| 278 |
Index = 123456; |
| 279 |
J1U(Rc_int, J1Array, Index); // unset bit at 123456 |
| 280 |
if (Rc_int == JERR) goto process_malloc_failure; |
| 281 |
if (Rc_int == 1) printf("OK - bit successfully unset at %lu\n", Index); |
| 282 |
if (Rc_int == 0) printf("BUG - bit was not set at %lu\n", Index); |
| 283 |
|
| 284 |
return(0); |
| 285 |
} |
| 286 |
</PRE> |
| 287 |
<!-----------------> |
| 288 |
<P> |
| 289 |
<DT><B>AUTHOR</B></DT> |
| 290 |
<DD> |
| 291 |
Judy was invented by Doug Baskins and implemented by Hewlett-Packard. |
| 292 |
<!-----------------> |
| 293 |
<P> |
| 294 |
<DT><B>SEE ALSO</B></DT> |
| 295 |
<DD> |
| 296 |
<A href="Judy_3.htm">Judy(3)</A>, |
| 297 |
<A href="JudyL_3.htm">JudyL(3)</A>, |
| 298 |
<A href="JudySL_3.htm">JudySL(3)</A>, |
| 299 |
<A href="JudyHS_3.htm">JudyHS(3)</A>, |
| 300 |
<BR> |
| 301 |
<I>malloc()</I>, |
| 302 |
<BR> |
| 303 |
the Judy website, |
| 304 |
<A href="http://judy.sourceforge.net"> |
| 305 |
http://judy.sourceforge.net</A>, |
| 306 |
for more information and Application Notes. |
| 307 |
</DL> |
| 308 |
</BODY> |
| 309 |
</HTML> |