Judy1(3)
okay so bestie, this is Judy1(3) from the Library Functions Manual β yes, the Judy1 macros page β and it's literally the C-library moment for creating and accessing a dynamic array of bits, using any value of a word as an index. no cap, let's get into it. π
πΈ NAME πΈ
Judy1 macros β a C library for creating and accessing a dynamic array of bits, using any value of a word as an index. π
β¨ SYNOPSIS β¨
compile it like this, bestie:
cc [flags] sourcefiles -lJudy
#include <Judy.h>
int Rc_int; // return code - integer
Word_t Rc_word; // return code - unsigned word
Word_t Index, Index1, Index2, Nth;
Pvoid_t PJ1Array = (Pvoid_t) NULL; // initialize Judy1 array
J1S( Rc_int, PJ1Array, Index); // Judy1Set()
J1U( Rc_int, PJ1Array, Index); // Judy1Unset()
J1T( Rc_int, PJ1Array, Index); // Judy1Test()
J1C( Rc_word, PJ1Array, Index1, Index2); // Judy1Count()
J1BC(Rc_int, PJ1Array, Nth, Index); // Judy1ByCount()
J1FA(Rc_word, PJ1Array); // Judy1FreeArray()
J1MU(Rc_word, PJ1Array); // Judy1MemUsed()
J1F( Rc_int, PJ1Array, Index); // Judy1First()
J1N( Rc_int, PJ1Array, Index); // Judy1Next()
J1L( Rc_int, PJ1Array, Index); // Judy1Last()
J1P( Rc_int, PJ1Array, Index); // Judy1Prev()
J1FE(Rc_int, PJ1Array, Index); // Judy1FirstEmpty()
J1NE(Rc_int, PJ1Array, Index); // Judy1NextEmpty()
J1LE(Rc_int, PJ1Array, Index); // Judy1LastEmpty()
J1PE(Rc_int, PJ1Array, Index); // Judy1PrevEmpty()
that's the whole lineup, periodt. π
π¦ DESCRIPTION π¦
A Judy1 array is the equivalent of a bit array or bit map. A bit is addressed by an Index (key). The array may be sparse, and the Index may be any word-sized Value. If an index is present, it represents a set bit (a bit set represents an index present). If an index is absent, it represents an unset bit (a bit unset represents an absent index). π«Ά
A Judy1 array is allocated with a NULL pointer:
Pvoid_t PJ1Array = (Pvoid_t) NULL;
Memory to support the array is allocated as bits are set, and released as bits are unset. If the Judy1 pointer (PJ1Array) is NULL, all bits are unset (and the Judy1 array requires no memory). lowkey iconic. β¨
As with an ordinary array, a Judy1 array contains no duplicate indexes. π
Using the macros described here, rather than the Judy1 function calls, the default error handling sends a message to the standard error and terminates the program with exit(1). For other error handling methods, see the ERRORS section. π
Because the macro forms are sometimes faster and have a simpler error handling interface than the equivalent functions, they are the preferred way of calling the Judy1 functions. slay. π₯
J1S(Rc_int, PJ1Array, Index);β this isJudy1Set(). It setsIndex's bit in the Judy1 arrayPJ1Array. ReturnRc_intset to1ifIndex's bit was previously unset (successful), otherwise0if the bit was already set (unsuccessful). πJ1U(Rc_int, PJ1Array, Index);β this isJudy1Unset(). It unsetsIndex's bit in the Judy1 arrayPJ1Array; that is, removeIndexfrom the Judy1 array. ReturnRc_intset to1ifIndex's bit was previously set (successful), otherwise0if the bit was already unset (unsuccessful). π¦J1T(Rc_int, PJ1Array, Index);β this isJudy1Test(). It tests ifIndex's bit is set in the Judy1 arrayPJ1Array. ReturnRc_intset to1ifIndex's bit is set (Indexis present),0if it is unset (Indexis absent). πJ1C(Rc_word, PJ1Array, Index1, Index2);β this isJudy1Count(). It counts the number of indexes present in the Judy1 arrayPJ1ArraybetweenIndex1andIndex2(inclusive). ReturnRc_wordset to the count. A returnValueof0can be valid as a count, or it can indicate a special case for fully populated array (32-bit machines only). SeeJudy1Count()for ways to resolve this. To count all indexes present (population) in a Judy1 bit array, use:
J1C(Rc_word, PJ1Array, 0, -1);
Note: The -1 promotes to the maximum index, that is, all ones. π§
J1BC(Rc_int, PJ1Array, Nth, Index);β this isJudy1ByCount(). It locates theNthindex that is present in the Judy1 arrayPJ1Array(Nth = 1returns the first index present). To refer to the last index in a fully populated array (all indexes present, which is rare), useNth = 0. ReturnRc_intset to1andIndexset to theNthindex if found, otherwise returnRc_intset to0(theValueofIndexcontains no useful information). πJ1FA(Rc_word, PJ1Array);β this isJudy1FreeArray(). It frees the entire Judy1 arrayPJ1Array(much faster than using aJ1N(),J1U()loop). ReturnRc_wordset to the number of bytes freed, andPJ1Arrayset toNULL. πJ1MU(Rc_word, PJ1Array);β this isJudy1MemUsed(). ReturnRc_wordset to the number of bytes of memory currently in use by Judy1 arrayPJ1Array. This is a very fast routine, and may be used after aJ1S()orJ1U()call with little performance impact. πΈ
π« Judy1 Search Functions π«
The Judy1 search functions allow you to search for set or unset bits in the array. You may search inclusively or exclusively, in either forward or reverse directions. All of the search functions use a similar calling sequence. Rc_int is returned set to 1 for a successful search and the found Index is returned. Rc_int is returned set to 0 for an unsuccessful search, and Index contains no useful information. π
β οΈ fr fr: the return codeRc_intmust be checked prior to using the returnedIndex, since a search failure is possible.
J1F(Rc_int, PJ1Array, Index);β this isJudy1First(). It searches (inclusive) for the first index present that is equal to or greater than the passedIndex. (Start withIndex = 0to find the first index in the array.)J1F()is typically used to begin a sorted-order scan of the indexes present in a Judy1 array. πJ1N(Rc_int, PJ1Array, Index);β this isJudy1Next(). It searches (exclusive) for the next index present that is greater than the passedIndex.J1N()is typically used to continue a sorted-order scan of the indexes present in a Judy1 array, or to locate a "neighbor" of a given index. π¦J1L(Rc_int, PJ1Array, Index);β this isJudy1Last(). It searches (inclusive) for the last index present that is equal to or less than the passedIndex. (Start withIndex = -1, that is, all ones, to find the last index in the array.)J1L()is typically used to begin a reverse-sorted-order scan of the indexes present in a Judy1 array. β¨J1P(Rc_int, PJ1Array, Index);β this isJudy1Prev(). It searches (exclusive) for the previous index present that is less than the passedIndex.J1P()is typically used to continue a reverse-sorted-order scan of the indexes present in a Judy1 array, or to locate a "neighbor" of a given index. πJ1FE(Rc_int, PJ1Array, Index);β this isJudy1FirstEmpty(). It searches (inclusive) for the first absent index that is equal to or greater than the passedIndex. (Start withIndex = 0to find the first index absent in the array.) πΈJ1NE(Rc_int, PJ1Array, Index);β this isJudy1NextEmpty(). It searches (exclusive) for the next absent index that is greater than the passedIndex. πJ1LE(Rc_int, PJ1Array, Index);β this isJudy1LastEmpty(). It searches (inclusive) for the last absent index that is equal to or less than the passedIndex. (Start withIndex = -1to find the last index absent in the array.) π§J1PE(Rc_int, PJ1Array, Index);β this isJudy1PrevEmpty(). It searches (exclusive) for the previous absent index that is less than the passedIndex. π
π ERRORS π
See: Judy_3.htm#ERRORS β that's the whole error section, bestie, it's linking you out. π«Ά
π EXAMPLE π
In the following example, errors in the J1S() or J1U() calls go to a user-defined procedure, process_malloc_failure. This is not needed when you use the default JUDYERROR() macro, since the default causes your program to exit on all failures, including malloc() failure. π
#include <stdio.h>
#include <Judy.h>
int main() // Example program of Judy1 macro APIs
{
Word_t Index; // index (or key)
Word_t Rcount; // count of indexes (or bits set)
Word_t Rc_word; // full word return value
int Rc_int; // boolean values returned (0 or 1)
Pvoid_t PJ1Array = (Pvoid_t) NULL; // initialize Judy1 array
Index = 123456;
J1S(Rc_int, J1Array, Index); // set bit at 123456
if (Rc_int == JERR) goto process_malloc_failure;
if (Rc_int == 1) printf("OK - bit successfully set at %lu\n", Index);
if (Rc_int == 0) printf("BUG - bit already set at %lu\n", Index);
Index = 654321;
J1T(Rc_int, J1Array, Index); // test if bit set at 654321
if (Rc_int == 1) printf("BUG - set bit at %lu\n", Index);
if (Rc_int == 0) printf("OK - bit not set at %lu\n", Index);
J1C(Rcount, J1Array, 0, -1); // count all bits set in array
printf("%lu bits set in Judy1 array\n", Rcount);
Index = 0;
J1F(Rc_int, J1Array, Index); // find first bit set in array
if (Rc_int == 1) printf("OK - first bit set is at %lu\n", Index);
if (Rc_int == 0) printf("BUG - no bits set in array\n");
J1MU(Rc_word, J1Array); // how much memory was used?
printf("%lu Indexes used %lu bytes of memory\n", Rcount, Rc_word);
Index = 123456;
J1U(Rc_int, J1Array, Index); // unset bit at 123456
if (Rc_int == JERR) goto process_malloc_failure;
if (Rc_int == 1) printf("OK - bit successfully unset at %lu\n", Index);
if (Rc_int == 0) printf("BUG - bit was not set at %lu\n", Index);
return(0);
}
π AUTHOR π
Judy was invented by Doug Baskins and implemented by Hewlett-Packard. π
π SEE ALSO π
Judy(3), JudyL(3), JudySL(3), JudyHS(3), malloc(), the Judy website, http://judy.sourceforge.net, for more information and Application Notes. π
xoxo, your Judy1 bestie π
β footer tea: the source/version tag here is just Judy1(3), and there's no date listed, so that's the whole sign-off vibe. π
π¦ where this page comes from
- arch rolling: judy 1.0.5-10 Β· LGPL-2.0-or-later
- nixpkgs unstable: judy 1.0.5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms