“
In and , a Judy array is a that has high performance, low memory usage and implements an. 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.
Judy arrays are both speed- and memory-efficient[], with no tuning or configuration required and therefore they can sometime replace common in-memory dictionary implementations (like or ) and work better with very large data sets[
][].Roughly speaking, Judy arrays are highly-optimised 256-ary . 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.
”
这里是一个简单高效的实现
下面是用上面Judy Array的一个简单的Example。
#include#include #include #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。。。