JudyL(3X) | JudyL(3X) |
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; PWord_t PValue; // pointer to return value Pvoid_t PJLArray = (Pvoid_t) NULL; // initialize JudyL array JLI( PValue, PJLArray, Index); // JudyLIns() JLD( Rc_int, PJLArray, Index); // JudyLDel() JLG( PValue, PJLArray, Index); // JudyLGet() JLC( Rc_word, PJLArray, Index1, Index2); // JudyLCount() JLBC(PValue, PJLArray, Nth, Index); // JudyLByCount() JLFA(Rc_word, PJLArray); // JudyLFreeArray() JLMU(Rc_word, PJLArray); // JudyLMemUsed() JLF( PValue, PJLArray, Index); // JudyLFirst() JLN( PValue, PJLArray, Index); // JudyLNext() JLL( PValue, PJLArray, Index); // JudyLLast() JLP( PValue, PJLArray, Index); // JudyLPrev() JLFE(Rc_int, PJLArray, Index); // JudyLFirstEmpty() JLNE(Rc_int, PJLArray, Index); // JudyLNextEmpty() JLLE(Rc_int, PJLArray, Index); // JudyLLastEmpty() JLPE(Rc_int, PJLArray, Index); // JudyLPrevEmpty()
As with an ordinary array, there are no duplicate indexes in a JudyL array.
The value may be used as a scalar, or a pointer to a structure or block of data (or even another Judy array).
A JudyL array is allocated with a NULL pointer
Pvoid_t PJLArray = (Pvoid_t) NULL;
Using the macros described here, rather than the JudyL 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. JLI( PValue, PJLArray, Index); // JudyLIns()
Because the macro forms are sometimes faster and have a simpler error handling interface than the equivalent JudyL functions, they are the preferred way of calling the JudyL functions.
Return PValue pointing to Value. Your program can use this pointer to read or modify Value until the next JLI() (insert), JLD() (delete) or JLFA() (freearray) is executed on PJLArray. Examples:
*PValue = 1234; Value = *PValue;
Return PValue set to PJERR if a malloc() fail occured. Note: JLI() and JLD() reorganize the JudyL array. Therefore, PValue returned from previous JudyL calls become invalid and must be re-acquired.
Return Rc_int set to 1 if successful. Return Rc_int set to 0 if Index was not present. Return Rc_int set to JERR if a malloc() fail occured.
Return PValue pointing to Value. Return PValue set to NULL if the Index was not present. Return PValue set to PJERR if a malloc() fail occured.
Return Rc_word set to the count. A return value of 0 can be valid as a count.
To count all indexes present in a JudyL array, use:
JLC(Rc_word, PJLArray, 0, -1);
Return PValue pointing to its Value and Index set to the Nth index if found, otherwise return PValue set to NULL (the value of Index is undefined).
Return Rc_word set to the number of bytes freed and PJLArray set to NULL.
JLFE(), JLNE(), JLLE(), JLPE() allow you to search for indexes that are not present ("empty") in the array. You may search inclusively or exclusively, in either forward or reverse directions. If successful, Index is returned set to a not present ("empty") index, and Rc_int is returned set to 1. If unsuccessful, Rc_int is returned set to 0, and and Index contains no useful information. Rc_int must be checked prior to using Index, since a search failure is possible.
Note: The current version of Judy.h changed this flag from 0x4 to 0x1 to allow for a malloc() that does not deliver memory on an 8 byte aligned boundry (such as old versions of valgrind).
The following example code segment can be used to determine whether or not a pointer points to another JudyL:
PValue = (PWord_t)PMultiDimArray; for (Dim = 0; ;Dim++) { if (PValue == (PWord_t)NULL) goto IndexNotFound; /* Advance to next dimension in array */ JLG(PValue, (Pvoid_t)*PValue, Index[Dim]); /* Check if pointer to user buffer: */ if (*PValue & JLAP_INVALID)) break; } UPointer = (UPointer_t) (*PValue & ~JLAP_INVALID); // mask and cast. printf("User object pointer is 0x%lx\n", (Word_t) UPointer); ...
Note: This works because malloc() guarantees to return a pointer with the least bit(s) == 0x0. You must remove JLAP_INVALID before using the pointer.
#include <stdio.h> #include <Judy.h> Word_t Index; // array index Word_t Value; // array element value Word_t * PValue; // pointer to array element value int Rc_int; // return code Pvoid_t PJLArray = (Pvoid_t) NULL; // initialize JudyL array while (scanf("%lu %lu", &Index, &Value)) { JLI(PValue, PJLArray, Index); If (PValue == PJERR) goto process_malloc_failure; *PValue = Value; // store new value } // Next, visit all the stored indexes in sorted order, first ascending, // then descending, and delete each index during the descending pass. Index = 0; JLF(PValue, PJLArray, Index); while (PValue != NULL) { printf("%lu %lu\n", Index, *PValue)); JLN(PValue, PJLArray, Index); } Index = -1; JLL(PValue, PJLArray, Index); while (PValue != NULL) { printf("%lu %lu\n", Index, *PValue)); JLD(Rc_int, PJLArray, Index); if (Rc_int == JERR) goto process_malloc_failure; JLP(PValue, PJLArray, Index); }