【数据结构】位图与布隆处理器

1.位图的实现

BitMap.h

#ifndef __BITSET_H__
#define __BITSET_H__



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

typedef  struct BitSet
{
	char*  a;
	size_t  N;//开辟的位个数
}BitSet;

void BitSetInit(BitSet* p, size_t n);

void BitSetDestory(BitSet* p);

//置1
void BitSetSet1(BitSet* p, size_t x);
//把1变为0
void BitSetSet0(BitSet* p, size_t x);



//判断数字是否在
int HaveNumber(BitSet* p, size_t x);

#endif



BitMap.c

#include  "BitSet.h"

void BitSetInit(BitSet* p, size_t n)
{
	assert(p);
	p->N = n;
	//确定需要的字节数,并且+1,比如33/8应该是5个字节
	size_t size = (n >> 3) + 1;
	//动态开辟
	p->a = (char*)malloc(size);
	//初始化
	memset(p->a, 0, size);
}

void BitSetDestory(BitSet* p)
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->N = 0;
}


void BitSetSet1(BitSet* p, size_t x)
{
	assert(p);
	//计算在第几个char中
	int index = x >> 3;
	//第几位
	int num = x % 8;
	p->a[index] |= (1 << num);
}

void BitSetSet0(BitSet* p, size_t x)
{
	assert(p);
	int index = x >> 3;
	int num = x % 8;
	p->a[index] &=  ~(1 << num);
}

//判断数字是否存在
int HaveNumber(BitSet* p, size_t x)
{
	assert(p);
	int index = x >> 3;
	int num = x % 8;
	int ret = p->a[index] & (1 << num);
	if (0 == ret)
		return 0;
	else
		return 1;
}

2.布隆的实现

BloomFilter.h

#ifndef  __BLOOMFITER_H__
#define  __BLOOMFILTER_H__





#include"BitSet.h"



typedef struct BloomFilter
{
	BitSet b;
}BloomFilter;


void BloomFilterInit(BloomFilter* p,size_t n);
void  BloomFilterDestory(BloomFilter* p);
void  BloomFilterSet1(BloomFilter* p, char* x);
int  BloomFilterHaveNumber(BloomFilter* p, char* x);

#endif



BloomFilter.c

#include"BloomFilter.h"



size_t HashFunc1(char* p)
{
	assert(p);
	size_t hash = 0;
	while (*p)
	{
		hash = hash * 131 + (size_t)*p;
		p++;
	}
	return hash;
}


size_t HashFunc2(char* p)
{
	assert(p);
	size_t hash = 0;
	while (*p)
	{
		hash = hash * 31 + (size_t)*p;
		p++;
	}
	return hash;
}



size_t HashFunc3(char* p)
{
	assert(p);
	size_t hash = 0;
	while (*p)
	{
		hash = hash * 1313  + (size_t)*p;
		p++;
	}
	return hash;
}
void BloomFilterInit(BloomFilter* p ,size_t n)
{
	assert(p);
	BitSetInit(&(p->b),n );
}
void  BloomFilterDestory(BloomFilter* p)
{
	assert(p);
	BitSetDestory(&(p->b));
}
void  BloomFilterSet1(BloomFilter* p, char* x)
{
	assert(p);
	size_t index1 = HashFunc1(x);
	size_t index2 = HashFunc2(x);
	size_t index3 = HashFunc3(x);



	BitSetSet1(&(p->b), index1);
	BitSetSet1(&(p->b), index2);
	BitSetSet1(&(p->b), index3);


}
int  BloomFilterHaveNumber(BloomFilter* p, char* x)
{
	assert(p);
	size_t index1 = HashFunc1(x);
	if (HaveNumber(&(p->b), index1))
	{
		size_t index2 = HashFunc2(x);
		if (HaveNumber(&(p->b), index2))
		{
			size_t index3 = HashFunc3(x);
			if (HaveNumber(&(p->b), index3))
			{
				return 1;
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
位图(Bitmap)和布隆过滤器(Bloom Filter)都是常用的数据结构,用于处理大规模数据集合,但它们有着不同的应用场景和用途。 位图是一种压缩数据结构,用于快速地判断某个元素是否在集合中。位图的实现方式是将每个元素映射到一个二进制位上,如果该元素存在于集合中,则将对应的二进制位标记为1,否则标记为0。这样,当需要查询某个元素是否在集合中时,只需要查找对应的二进制位即可。由于位图的实现方式非常简单,因此可以快速地进行插入和查询操作,而且占用的空间也非常小,适合处理大规模数据集合。 布隆过滤器也是一种快速判断元素是否存在于集合中的数据结构,但其实现方式与位图略有不同。布隆过滤器使用一组哈希函数将元素映射到多个二进制位上,并将对应的二进制位标记为1。当查询某个元素是否在集合中时,将该元素进行哈希映射,并查找对应的二进制位,如果所有的二进制位都被标记为1,则说明该元素可能存在于集合中,否则可以确定该元素不存在于集合中。布隆过滤器的优点是可以快速地判断一个元素不存在于集合中,而且占用的空间也比较小,但存在误判率的问题。 因此,位图布隆过滤器虽然都可以用来处理大规模数据集合,但它们的实现方式和应用场景有所不同。位图适用于需要快速地判断某个元素是否在集合中的场景,而布隆过滤器适用于需要快速地判断一个元素不存在于集合中的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值