Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Programming with Judy: C LanguageJudy Version 4.0 > Chapter 3 Using Judy

Using JudySL

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

The JudySL functions provide a way to use string values as indexes into a Judy array. In JudySL arrays, elements are sorted lexicographically (case-sensitive) by indexes:

void * JudySLArray ["Toto, I don't think we're in Kansas any more"];

Since an initial (empty) JudySL array is represented by a null pointer, you can make JudySL arrays multi-dimensional:

void * JudySLArray [char *][char *]...;

The table below shows the JudySL functions. For more information, see the JudySL(3X) man page.

Table 3-4 Title not available (Using JudySL)

JudySL functions

Actions

JudySLGet

Retrieves a value from a JudySL string array.

JudySLIns

Inserts a value into a JudySL array.

JudySLDel

Deletes the specified JudySL array element.

JudySLFirst

Typically begins a sorted-order scan of the set indexes in a JudySL array.

JudySLNext

Typically continues a sorted-order scan of the set indexes in a JudySL array or locates a neighbor of an index.

JudySLLast

Typically begins a reverse sorted-order scan of the set indexes in a JudySL array.

JudySLPrev

Typically continues a reverse sorted-order scan of the set indexes in a JudySL array or locates a neighbor of an index.

JudySLFreeArray

Frees the entire JudyL array. This is much faster than using JudySLNext and JudySLDel.

 

JudySL sorting example

You can find the code for this example and others on the Judy web site: http://devresource.hp.com/judy/

// JudySort.c// Judy demonstration code:  Judy equivalent of sort -u.  While Judy is not// primarily intended as a sorting algorithm, in many cases it's faster to// store values in a Judy array and read them back in sorted order than to sort// them using standard algorithms.//// Usage:  <program> <file-to-sort>//// Code and comments are included if you want to modify the program to output// duplicates as an exercise.//// Note:  If an input line is longer than BUFSIZ, it's broken into two or more// lines.#include <sys/types.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>#include "Judy.h"main (int argc, char ** argv){    Pvoid_t  PJArray = (Pvoid_t) NULL;  // Judy array.    PPvoid_t PPValue;                   // Judy array element.    char     Index [BUFSIZ];            // string to sort.    char *   Pc;                        // place in Index.    FILE *   fpin;                      // to read file.    JError_t JError;                    // Judy error structure// CHECK FOR REQUIRED INPUT FILE PARAMETER:    if (argc != 2)    {        (void) fprintf (stderr, "Usage: %s <file-to-sort>\n", argv[0]);        (void) fputs   ("Uses each line as a JudySL index.\n", stderr);        exit (1);    }// OPEN INPUT FILE:    if ((fpin = fopen (argv[1], "r")) == (FILE *) NULL)    {        (void) fprintf (stderr, "%s: Cannot open file \"%s\": %s "                        "(errno = %d)\n", argv[0], argv[1], strerror (errno),                        errno);        exit (1);    }// INPUT/STORE LOOP://// Read each input line (up to Index size) and save the line as an index into a// JudySL array.  If the line doesn't overflow Index, it ends with a newline,// which must be removed for sorting comparable to sort(1).    while (fgets (Index, sizeof (Index), fpin) != (char *) NULL)    {        if ((Pc = strchr (Index, '\n')) != (char *) NULL)            *Pc = '\0';                         // trim at newline.        if ((PPValue = JudySLIns (& PJArray, Index, &JError))             == PPJERR)        {            fprintf(stderr, "File: '%s', line: %d: Judy error: %d\n",                    __FILE__, __LINE__, JU_ERRNO(&JError));            exit (1);        }// To modify the program to output duplication counts, like uniq -d, or to emit// multiple copies of repeated strings:#ifdef notdef        ++(*((ulong_t *) PPValue));#endif    }// PRINT SORTED STRINGS://// To output all strings and not just the unique ones, output Index multiple// times = *((ulong_t *) PPValue).    Index[0] = '\0';                            // start with smallest string.    for (PPValue  = JudySLFirst (PJArray, Index, &JError);         PPValue != (PPvoid_t) NULL;         PPValue  = JudySLNext  (PJArray, Index, &JError))    {        (void) puts (Index);                    // see comment above.    }    exit (0);    /*NOTREACHED*/} // main()

In summary

The Judy technology provides three array types each supporting a different kind of index and/or value. These are:

Judy1

Map ulong_t to bit

JudyL

Map ulong_t to ulong_t

JudySL

Map char* to ulong_t

Each of these array types have associated functions described in Table 3-1 “A summary of Judy functions”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.