Bit-Map算法的简单实现

BitMap的定义:所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在 
 存储空间方面,可以大大节省。 
 

 

使用bitmap实现8位电话号码的存储,能够实现电话号码的插入、删除、查找。

使用bitmap算法。8位电话号码总共有0-99999999个号码,每位代表一个电话号码,需要12500000个字节的内存来存储电话号码

实现过程如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#define BYTE_SIZE 8
#define BIT_MAP_SIZE 12500000
#define INPUT_DATA_SIZE 10

void add_data(uint8_t *bit_map, uint32_t data)
{
	bit_map += (data/BYTE_SIZE);
	*bit_map = *bit_map|(0x01<<(data%BYTE_SIZE));
}


void find_data(uint8_t *bit_map, uint32_t data)
{
	bit_map += (data/BYTE_SIZE);
	if((*bit_map)&(0x01<<(data%BYTE_SIZE)))
	{
		printf("THE DATA %ld IS IN DATABASE\n", data);
	}
	else
	{
		printf("THE DATA %ld IS NOT IN DATABASE\n", data);
	}
}

void delete_data(uint8_t *bit_map, uint32_t data)
{
	bit_map += (data/BYTE_SIZE);
	if((*bit_map)&(0x01<<(data%BYTE_SIZE)))
	{
		*bit_map = (*bit_map)&(~(0x01<<(data%BYTE_SIZE)));
		printf("THE DATA %ld IS DELETED FROM DATABASE\n", data);
	}
	else
	{
		printf("THE DATA %ld IS NOT IN DATABASE\n", data);
	}
}

int main()
{
	uint8_t bit_map[BIT_MAP_SIZE];
	uint32_t datap[INPUT_DATA_SIZE] = {88081234, 88081235, 88082234, 88082235, 88080790, 88080750, 88080779, 88088321, 88088339, 88088349};
	int i = 0;
	uint32_t data;


	memset(bit_map, 0, BIT_MAP_SIZE);
	
	for(i = 0; i < INPUT_DATA_SIZE; i++)
	{
		add_data(bit_map, datap[i]);
	}


	printf("INPUT FIND DATA:\n");
	scanf("%ld", &data);
	find_data(bit_map, data);
        printf("INPUT DELETE DATA:\n");
        scanf("%ld", &data);
        delete_data(bit_map, data);
        printf("INPUT FIND DATA:\n");
        scanf("%ld", &data);
        find_data(bit_map, data);
	

	return 0;
}




执行结果:

 ./bit_map_byte


Bit-Map算法的简单实现_第1张图片

你可能感兴趣的:(算法,database,delete,input,byte,电话)