bitmap.h
#pragma once
#include <stddef.h>
#include<inttypes.h>
typedef struct BitMap
{
uint64_t* data;
size_t capacity; // max bit
} BitMap;
void BitMapInit(BitMap* bm, size_t capacity);
// 把第 index 位置为1
void BitMapSet(BitMap* bm, size_t index);
// 把第 index 位置为0
void BitMapUnset(BitMap* bm, size_t index);
// 测试 index 为是 1 , 还是 0. 如果是1, 就返回1. 否则返回0.
int BitMapTest(BitMap* bm, size_t index);
// 把整个位图所有的位都设为1.
void BitMapFill(BitMap* bm);
// 把整个位图所有的位都设为0.
void BitMapClear(BitMap* bm);
void BitMapDestroy(BitMap* bm);
void ShowSturct(BitMap* bm);
void ShowBit(BitMap* bm);
bitmap.c
#include<stdio.h>
#include<stdlib.h>
#include"bitmap.h"
#define TEST_HEADER printf("\n=================%s===============\n", __FUNCTION__)
void BitMapInit(BitMap* bm, size_t capacity)
{
if(bm==NULL)
{
return;
}
size_t tmp = capacity % 64;
if(tmp==0)
{
bm->data=(uint64_t*)malloc( sizeof(uint64_t) * (capacity / 64) );
bm->capacity =capacity ;
}
else
{
bm->data=(uint64_t*)malloc( sizeof(uint64_t) * ( 1 + (capacity / 64) ) );
bm->capacity = (1 + (capacity / 64))*64 ;
}
}
// 把第 index 位置为1
void BitMapSet(BitMap* bm, size_t index)
{
if(bm==NULL)
{
return;
}
if(bm->capacity<= index)
{
return;
}
size_t subscript= index / 64;
size_t offset = index % 64;
// 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001
bm->data[subscript] |= (uint64_t) 1 << offset;
}
// [00000000]64 [000000000]64
// 把第 index 位置为0
void BitMapUnset(BitMap* bm, size_t index)
{
if(bm==NULL)
{
return;
}
if(bm->capacity <= index)
{
return;
}
uint64_t tmp=0xffffffffffffffff;
size_t subscript = index/64;
size_t offset = index % 64;
tmp ^= (uint64_t)1<<offset;
bm->data[subscript] &= tmp;
}
// 测试 index 为是 1 , 还是 0. 如果是1, 就返回1. 否则返回0.
int BitMapTest(BitMap* bm, size_t index)
{
if(bm==NULL)
{
return -1;
}
if(bm->capacity<= index)
{
return -2;
}
size_t subscript= index/64;
size_t offset = index % 64;
return ( 1 & (bm->data[subscript] >>offset));
}
// 把整个位图所有的位都设为1.
void BitMapFill(BitMap* bm)
{
if(bm==NULL)
{
return;
}
size_t i;
size_t max= bm->capacity/64;
for(i=0;i<max;i++)
{
bm->data[i]=0xffffffffffffffff;
}
}
// 把整个位图所有的位都设为0.
void BitMapClear(BitMap* bm)
{
if(bm==NULL)
{
return;
}
size_t i;
size_t max= bm->capacity/64;
for(i=0;i<max;i++)
{
bm->data[i]=0;
}
}
void BitMapDestroy(BitMap* bm)
{
if(bm==NULL)
{
return;
}
free(bm->data);
bm->data=NULL;
bm->capacity=0;
}
void ShowSturct(BitMap* bm)
{
printf(" Bit Map Sturct \n");
printf("data : %10p\n",bm->data);
printf("capacity : %10lu\n",bm->capacity);
}
void ShowBit(BitMap * bm)
{
if(bm==NULL)
{
return;
}
printf("==================================================================================\n");
size_t i;
for(i=0;i< (bm->capacity/64);i++)
{
uint64_t tmp= bm->data[i];
size_t t;
for(t=0;t<64;t++)
{
if( tmp & (uint64_t)1<<t )
{
printf("1 ");
}
else
{
printf("0 ");
}
}
printf("\n");
}
printf("===========================================================================\n");
}
Bloon_fliter.h
#pragma once
#include<stddef.h>
#include"bitmap.h"
#define HashFuncMaxSize 2
#define BitMapCapcity 1024
typedef size_t (*HashFunc)(const char*);
typedef struct BloomFilter
{
BitMap bitmap;
HashFunc hash_func[HashFuncMaxSize];
}BloomFilter;
void BloomFilterInit(BloomFilter* bf);
void BloomFilterInsert(BloomFilter* bf, const char* str);
int BloomFilterIsExist(BloomFilter* bf, const char* str);
void BloomFilterDestroy(BloomFilter* bf);
// 按照当前的设计, 是不允许删除的.
Bloon_fliter.c
#include <stdio.h>
#include "Bloon_filter.h"
#define TESH_HEADER printf("\n=================%s================\n",__FUNCTION__)
size_t SDBMHash(const char *str)
{
size_t hash = 0;
while (*str)
{
hash = hash + (*str++);
}
return hash ;
}
// RS Hash Function
size_t RSHash(const char *str)
{
size_t b = 3;
size_t a = 6;
size_t hash = 0;
while (*str)
{
hash = hash +a + (*str++);
a += b;
}
return hash;
}
void BloomFilterInit(BloomFilter* bf)
{
if(bf==NULL)
{
return;
}
BitMapInit(&bf->bitmap, BitMapCapcity);
bf-> hash_func[0]=SDBMHash;
bf-> hash_func[1]=RSHash;
}
void BloomFilterInsert(BloomFilter* bf, const char* str)
{
if(bf==NULL)
{
return;
}
size_t i;
for(i=0;i<HashFuncMaxSize;i++)
{
size_t ret=bf->hash_func[i](str);
printf("ret =%lu\n",ret);
BitMapSet(&bf->bitmap,ret);
}
}
int BloomFilterIsExist(BloomFilter* bf, const char* str)
{
if(bf==NULL)
{
return -1;
}
size_t i;
for(i=0;i<HashFuncMaxSize;i++)
{
size_t ret=bf->hash_func[i](str);
printf("ret:%lu \n", ret);
int result=BitMapTest(&bf->bitmap,ret);
if(result!=1)
{
return 0;
}
}
return 1;
}
void BloomFilterDestroy(BloomFilter* bf)
{
if(bf==NULL)
{
return;
}
BitMapDestroy(&bf->bitmap);
size_t i;
for(i=0;i<HashFuncMaxSize;i++)
{
bf->hash_func[i]=NULL;
}
return;
}
// 按照当前的设计, 是不允许删除的.
void ShowBloon(BloomFilter* bf)
{
size_t i;
for(i=0;i<HashFuncMaxSize;i++)
{
printf("Filterfunc[%lu]:%10p\n",i,bf->hash_func[i]);
}
ShowSturct(&bf->bitmap);
ShowBit(&bf->bitmap);
}
void TestAll()
{
TESH_HEADER;
BloomFilter bf;
BloomFilterInit(&bf);
BloomFilterInsert(&bf,"a");
BloomFilterInsert(&bf,"TingHua");
BloomFilterInsert(&bf, "Paking");
int ret= BloomFilterIsExist(&bf, "TingHua");
printf("The result is %d\n", ret);
ret=BloomFilterIsExist(&bf,"wo shi lianjie");
printf("The result is %d\n", ret);
ret=BloomFilterIsExist(&bf,"Tinghua");
printf("The result is %d\n", ret);
ShowBloon(&bf);
BloomFilterDestroy(&bf);
ShowBloon(&bf);
}
int main()
{
TestAll();
return 0;
}
Makefile
bloon_filter:bitmap.c Bloon_filter.c
gcc -o $@ $^
.PHONY:clean
clean:
rm bloon_filter
Test result
[abc123@localhost bloon_filter]$ ./bloon_filter
=================TestAll================
ret =97
ret =103
ret =688
ret =793
ret =602
ret =683
ret:688
ret:793
The result is 1
ret:1350
The result is 0
ret:720
The result is 0
Filterfunc[0]: 0x400a50
Filterfunc[1]: 0x400a8a
Bit Map Sturct
data : 0x75e010
capacity : 1024
==================================================================================
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
===========================================================================
Filterfunc[0]: (nil)
Filterfunc[1]: (nil)
Bit Map Sturct
data : (nil)
capacity : 0
==================================================================================
===========================================================================
[abc123@localhost bloon_filter]$ ./bloon_filter
=================TestAll================
ret =97
ret =103
ret =688
ret =793
ret =602
ret =683
ret:688
ret:793
The result is 1
ret:1350
The result is 0
ret:720
The result is 0
Filterfunc[0]: 0x400a50
Filterfunc[1]: 0x400a8a
Bit Map Sturct
data : 0x10f0010
capacity : 1024
==================================================================================
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
===========================================================================
Filterfunc[0]: (nil)
Filterfunc[1]: (nil)
Bit Map Sturct
data : (nil)
capacity : 0
==================================================================================
===========================================================================