位图和布隆源码

哈希及其扩展总结:
http://blog.csdn.net/zhangye3017/article/details/79463100

位图操作

#include
#include
#include
#include
#include




typedef struct BitMap
{
    size_t* _bits;
    size_t _ranges;

}BitMap;



void BitMapInit(BitMap* bm, size_t range);//初始化
void BitMapInsert(BitMap* bm, size_t x);//插入
void BitMapReSet(BitMap* bm, size_t x);//重置
int BitMapFind(BitMap* bm, size_t x);//查找数据
void TestBitMap();
void BitMapPrint(BitMap* bm);

//初始化
void BitMapInit(BitMap* bm, size_t range)
{
    bm->_ranges = range;
    size_t N = sizeof(size_t*)*(bm->_ranges >> 5) + 1;

    bm->_bits = (size_t*)malloc(N);
    assert(bm->_bits);

    memset(bm->_bits, 0, N);

}



//插入
void BitMapInsert(BitMap* bm, size_t x)
{
    assert(bm);

    size_t index = x >> 5;//先计算x在数组中的位置
    size_t bit = x % 32;//计算x在8个bit中的位置

    bm->_bits[index] |= x<//插入数据

}


//重置
void BitMapReSet(BitMap* bm, size_t x)
{

    assert(bm);
    size_t index = x >> 5;//先计算x在数组中的位置
    size_t bit = x % 32;//计算x在8个bit中的位置


    bm->_bits[index] &= (~(1 << bit));


}



//查找数据
int BitMapFind(BitMap* bm, size_t x)
{
    assert(bm);

    size_t index = x >> 5;//先计算x在数组中的位置
    size_t bit = x % 32;//计算x在8个bit中的位置

    //如果位图中有x,则相应比特位与1不为0,找到x
    if (((bm->_bits[index] >> bit) & 1) != 0)
        return 0;
    else
        return -1;
}

void BitMapPrint(BitMap* bm)
{
    assert(bm);

    for (size_t i = 0; i < ((bm->_ranges>>5)+1)-1; ++i)
    {
        if (bm->_bits[i] != 0)
        {
            printf("%d->%d\n", i, bm->_bits[i]);
        }
    }
    printf("\n");
}

布隆过滤器操作

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



typedef const char* KeyType;
typedef size_t(*Hash_Func)(KeyType str);

size_t RSHash(KeyType str)
{
     size_t hash = 0;
    size_t magic = 63689;
    size_t ch;
    while ( ch = (size_t)*str++)
    {
        hash = hash * magic + ch;
        magic *= 378551;
    }
    return hash;
}

size_t APHash(KeyType str)
{
    register size_t hash = 0;
    size_t ch;
    for (long i = 0; ch = (size_t)*str++; i++)
    {
        if ((i & 1) == 0)
        {
            hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
        }
        else
        {
            hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
        }
    }
    return hash;
}

size_t BKDRHash(KeyType str)
{
    size_t hash = 0;
    size_t ch;
    while ( ch= (size_t)*str++)
    {
        hash = hash * 131 + ch;   
    }
    return hash;
}


typedef struct BloomFilter
{
    BitMap _bm;
    Hash_Func _HashFunc1;
    Hash_Func _HashFunc2;
    Hash_Func _HashFunc3;

}BloomFilter;

void BloomFilterInit(BloomFilter* bf, size_t range);//布隆的初始化
void BloomFilterInsert(BloomFilter* bf, KeyType key);//布隆的插入
int  BloomFilterFind(BloomFilter* bf, KeyType key);//布隆的查找
void BloomFilterReset(BloomFilter* bf, KeyType key);//布隆的重置
void BloomFilterDestory(BloomFilter* bf);//销毁



//布隆的初始化
void BloomFilterInit(BloomFilter* bf, size_t range)
{
    assert(bf);

    bf->_HashFunc1 = BKDRHash;
    bf->_HashFunc2 = APHash;
    bf->_HashFunc3 = RSHash;

    BitMapInit(&bf->_bm, range);
}


//布隆的插入
void BloomFilterInsert(BloomFilter* bf, KeyType key)
{
    assert(bf);

    size_t hash1 = bf->_HashFunc1(key);
    size_t hash2 = bf->_HashFunc2(key);
    size_t hash3 = bf->_HashFunc3(key);


    BitMapInsert(&bf->_bm, hash1%bf->_bm._ranges);
    BitMapInsert(&bf->_bm, hash2%bf->_bm._ranges);
    BitMapInsert(&bf->_bm, hash3%bf->_bm._ranges);

}


//布隆的查找
//找到返回0,找不到返回-1
int  BloomFilterFind(BloomFilter* bf,KeyType key)
{
    assert(bf);

    size_t hash1, hash2, hash3;
    size_t range = bf->_bm._ranges;
    hash1 = bf->_HashFunc1(key);
    if ((BitMapFind(&bf->_bm, hash1%range)) !=0)
        return -1;
    hash2 = bf->_HashFunc2(key);
    if ((BitMapFind(&bf->_bm, hash2%range)) !=0)
        return -1;
    hash3 = bf->_HashFunc3(key);
    if ((BitMapFind(&bf->_bm, hash3%range)) !=0)
        return -1;

    return 0;

}


//销毁
void BloomFilterDestory(BloomFilter* bf)
{
    assert(bf);

    BitMapDestory(bf->_bm);
}

你可能感兴趣的:(数据结构)