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。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值