Judy Array - Example

In computer science and software engineering, a Judy array is a data structure that has high performance, low memory usage and implements anassociative array. Unlike normal arrays, Judy arrays may be sparse, that is, they may have large ranges of unassigned indices. They can be used for storing and looking up values using integer or string keys. The key benefits of using a Judy array is its scalability, high performance, memory efficiency and ease of use.[1]

Judy arrays are both speed- and memory-efficient[clarification needed], with no tuning or configuration required and therefore they can sometime replace common in-memory dictionary implementations (like red-black trees or hash tables) and work better with very large data sets[dubious ][citation needed].

Roughly speaking, Judy arrays are highly-optimised 256-ary radix trees.[2] To make memory consumption small, Judy arrays use over 20 different compression techniques to compress trie nodes.

The Judy array was invented by Douglas Baskins and named after his sister.[3]

https://code.google.com/p/judyarray/这里是一个简单高效的实现

下面是用上面Judy Array的一个简单的Example。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "judy64nb.c"

typedef struct data{
    int a;
    int b;
}data;

int main()
{
    Judy *judy;
    void *cell;
    data *p;

    char *key1 = "IEatCrab";
    char *key2 = "IEatCrabToo";
    char *key3 = "CrabEatWorm";

    data data1 = {1, 1};
    data data2 = {2, 2};

    judy = judy_open(100, 0);

    cell = judy_cell(judy, key1, strlen(key1));  
    *(data*)cell = data1;  

    cell = judy_slot(judy, key2, strlen(key2));  
    if(cell == NULL){  
        cell = judy_cell(judy, key2, strlen(key2));  
        (*(data*)cell) = data2;  
    }

    cell = judy_slot(judy, key3, strlen(key3));  
    if(cell == NULL){  
        p = (data*)judy_data(judy, sizeof(data));  
        p->a = 3;  
        p->b = 3;  
        cell = judy_cell(judy, key3, strlen(key3));  
        *(data**)cell = p;           
    }


    cell = judy_slot(judy, key1, strlen(key1));  
    printf("%d %d\n", ((data*)cell)->a, ((data*)cell)->b);  
    cell = judy_slot(judy, key2, strlen(key2));  
    printf("%d %d\n", ((data*)cell)->a, ((data*)cell)->b);  
    cell = judy_slot(judy, key3, strlen(key3));  
    p = *(data**)cell;
    printf("%d %d\n", p->a, p->b);

    judy_close(judy);

    return 0;
}

  上面的代码中给出了利用Judy Array的三种存储key-value的方法。

 

  顺便吐槽一下,judy array的这个版本中的函数命名太S**K。。。

 

posted @ 2013-10-31 21:33  CobbLiu  阅读(1241)  评论(0编辑  收藏  举报