zoukankan      html  css  js  c++  java
  • Judy Array

    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
    ", ((data*)cell)->a, ((data*)cell)->b);  
        cell = judy_slot(judy, key2, strlen(key2));  
        printf("%d %d
    ", ((data*)cell)->a, ((data*)cell)->b);  
        cell = judy_slot(judy, key3, strlen(key3));  
        p = *(data**)cell;
        printf("%d %d
    ", p->a, p->b);
    
        judy_close(judy);
    
        return 0;
    }
    

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

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

  • 相关阅读:
    交易是如何被创建和打包的7
    重回js--js中的块作用域
    重回js--立即执行函数
    解决json跨域
    h5开发安卓ios坑总结
    关于html中对换行的处理
    说说display-inline
    笔记--学习git命令(基本命令版)
    写在最开始
    替换多个文件中的部分内容Demo
  • 原文地址:https://www.cnblogs.com/cobbliu/p/3400257.html
Copyright © 2011-2022 走看看