位图与布隆过滤器

位图

用来快速判断一个整数是否在一堆整数中
二进制用0和1来表示数据,位图根据0和1来存储对应的数据,可以大大节省存储空间,并具备排序特性。

例如数组:int arr[ ] = {1,31,53,41,79,201}

创建字符数组 int bitmap[26](因为数字最大数201,26 * 8=208 < 201)

遍历字符数组的每一位,设置 0 或者 1

通过设置 0 或者 1 来判断这个值是否存在。1表示存在

比如:31
因为(3*8 + 7 = 31)

则需要对 bitmap[3] 的第 7 位设置为 1

位图的基本操作
/*================================================================

# File Name: bitmap.h
# Author: rjm
# mail: rjm96@foxmail.com
# Created Time: 2018年05月22日 星期二 16时30分58秒

================================================================*/

#pragma once


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

#define TEST_HEAD printf("\n=======================%s==================\n", 
                         __FUNCTION__)


typedef uint64_t BitmapType;

typedef struct Bitmap {
    BitmapType* data; // 数组
    uint64_t capacity; // 需要表示的位数
                       // 例如:要表示100位
                       // 需要的数组元素个数为 
                       // (100 / (sizeof(uint64_t)*8)) + 1
                       // 即为 2 个元素
} Bitmap;

// 初始化
void BitmapInit(Bitmap* bm, uint64_t bits);

// 给某一位设置为 1
void BitmapSet(Bitmap* bm, uint64_t bits);

// 给某一位设置为 1
void BitmapUnset(Bitmap* bm, uint64_t bits);

// 给所有位设置为 1
void BitmapAllSet(Bitmap* bm);

// 给所有位设置为 0
void BitmapAllUnset(Bitmap* bm);

// 测试某一位是否为 1
uint64_t BitmapTest(Bitmap* bm, uint64_t bits);

// 销毁
void BitmapDestroy(Bitmap* bm);
/*================================================================

# File Name: bitmap.c
# Author: rjm
# mail: rjm96@foxmail.com
# Created Time: 2018年05月22日 星期二 16时30分48秒

================================================================*/


#include "bitmap.h"

uint64_t DataSize(uint64_t capacity)
{
    return capacity / (sizeof(BitmapType)*8) + 1;
}

// 初始化
void BitmapInit(Bitmap* bm, uin
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值