|
NAMEJudyL macros - C library for creating and accessing a dynamic array of words, using a word as an index.SYNOPSIScc [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() DESCRIPTIONA JudyL array is the equivalent of an array of word-sized values. A Value is addressed by an Index (key). The array may be sparse, and the Index may be any word-sized number. Memory to support the array is allocated as index/value pairs are inserted, and released as index/value pairs are deleted. A JudyL array can also be thought of as a mapper, that is "map" a word to another word/pointer.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.
Multi-dimensional JudyL ArraysStoring a pointer to another JudyL array in a JudyL array's Value is a simple way to support dynamic multi-dimensional arrays. These arrays (or trees) built using JudyL arrays are very fast and memory efficient. (In fact, that is how JudySL and JudyHS are implemented). An arbitrary number of dimensions can be realized this way. To terminate the number of dimensions (or tree), the Value pointer is marked to NOT point to another Judy array. A JLAP_INVALID flag is used in the least significant bit(s) of the pointer. After the flag JLAP_INVALID is removed, it is used as a pointer to the users data. The Judy.h header file defines JLAP_INVALID. See code fragment below.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. ERRORS: See: Judy_3.htm#ERRORSEXAMPLERead a series of index/value pairs from the standard input, store in a JudyL array, and then print out in sorted order.#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); } AUTHORJudy was invented by Doug Baskins and implemented by Hewlett-Packard.SEE ALSOJudy(3), Judy1(3), JudySL(3), JudyHS(3),malloc(), http://judy.sourceforge.net, for more information and Application Notes. Visit the GSP FreeBSD Man Page Interface. |