JudySL(3X) | JudySL(3X) |
cc [flags] sourcefiles -lJudy #include <Judy.h> #define MAXLINELEN 1000000 // define maximum string length Word_t * PValue; // JudySL array element uint8_t Index[MAXLINELEN]; // string int Rc_int; // return value Word_t Rc_word; // full word return value Pvoid_t PJSLArray = (Pvoid_t) NULL; // initialize JudySL array JSLI( PValue, PJSLArray, Index); // JudySLIns() JSLD( Rc_int, PJSLArray, Index); // JudySLDel() JSLG( PValue, PJSLArray, Index); // JudySLGet() JSLFA(Rc_word, PJSLArray); // JudySLFreeArray() JSLF( PValue, PJSLArray, Index); // JudySLFirst() JSLN( PValue, PJSLArray, Index); // JudySLNext() JSLL( PValue, PJSLArray, Index); // JudySLLast() JSLP( PValue, PJSLArray, Index); // JudySLPrev()
void * JudySLArray["Toto, I don't think we're in Kansas any more"];
A JudySL array is allocated with a NULL pointer
Pvoid_t PJSLArray = (Pvoid_t) NULL;As with an ordinary array, there are no duplicate indexes (strings) in a JudySL array.
Using the macros described here, rather than the JudySL function calls, the default error handling sends a message to the standard error and terminates the program with exit(1).
Return PValue pointing to Index's Value. Your program must use this pointer to modify the Value, for example:
*PValue = 1234;
Note: JSLI() and JSLD reorganize the JudySL array. Therefore, pointers returned from previous JudySL calls become invalid and must be reacquired.
Return Rc_int set to 1 if successful. array and it was previously inserted. Return Rc_int set to 0 if Index was not present.
Return PValue pointing to Index's Value. Return PValue set to NULL if the Index was not present.
Return Rc_word set to the number of bytes freed and PJSLArray set to NULL.
If successful, Index is returned set to the found index, and PValue is returned set to a pointer to Index's Value. If unsuccessful, PValue is returned set to NULL, and Index contains no useful information. PValue must be tested for non-NULL prior to using Index, since a search failure is possible.
Note: To accomodate all possible returns, the Index buffer must be at least as large as the largest string stored in the array.
uint8_t Index[MAXLINELEN]; strcpy (Index, ""); JSLF(PValue, PJSLArray, Index);
#include <stdio.h> #include <Judy.h> #define MAXLINE 1000000 // max string (line) length uint8_t Index[MAXLINE]; // string to insert int // Usage: JudySort < file_to_sort main() { Pvoid_t PJArray = (PWord_t)NULL; // Judy array. PWord_t PValue; // Judy array element. Word_t Bytes; // size of JudySL array. while (fgets(Index, MAXLINE, stdin) != (char *)NULL) { JSLI(PValue, PJArray, Index); // store string into array if (PValue == PJERR) // if out of memory? { // so do something printf("Malloc failed -- get more ram\n"); exit(1); } ++(*PValue); // count instances of string } Index[0] = '\0'; // start with smallest string. JSLF(PValue, PJArray, Index); // get first string while (PValue != NULL) { while ((*PValue)--) // print duplicates printf("%s", Index); JSLN(PValue, PJArray, Index); // get next string } JSLFA(Bytes, PJArray); // free array fprintf(stderr, "The JudySL array used %lu bytes of memory\n", Bytes); return (0); }