2.3 Bitmap() bitmap

Reprinted: https://blog.csdn.net/qq_18108083/article/details/85063072 to
realize the bitmap and encapsulate it in the bitmap class.

operating Features Object
bitmap(int n = 8) Constructor, the default capacity is 8 bits bitmap
bitmap(char* file, int n = 8) Constructor, read the bitmap from the specified file bitmap
~bitmap() Destructor, free up bitmap space bitmap
init(int n) Initialize the bitmap space bitmap
set(int k) Set the kth flag bitmap
clear(int k) Reset the kth flag bitmap
test(int k) Take out the specified bit in the specified byte bitmap
dump(char* file) Export the entire bitmap to the specified file bitmap
bits2string(int n) Convert the first n bits to a string bitmap
expand(int k) Expansion bitmap
print(int n) Print bit by bit to verify the bitmap content bitmap
isPrime(int n) Determine whether a number is prime Natural number

(1) bitmap.h

#pragma once
 
#pragma warning(disable : 4996 4800)
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "math.h"
class bitmap {
    
     //位图bitmap类
private:
	char* M; int N; //比特图所存放的空间M[],容量为N*sizeof(char)*8比特
protected:
	void init(int n)   //初始化位图空间
	{
    
    
		M = new char[N = (n + 7) / 8];   //申请内存
		memset(M, 0, N);    //初始化内存块
	}
 
public:
	bitmap(int n = 8) {
    
     init(n); } //按指定或默认规模创建比特图(为测试暂时选用较小的默认值)
	bitmap(char* file, int n = 8) //按指定或默认规模,从指定文件中读取比特图
	{
    
    
		init(n); FILE* fp = fopen(file, "r"); fread(M, sizeof(char), N, fp); fclose(fp);
	}
	~bitmap() {
    
     delete[] M; M = NULL; } //析构时释放比特图空间
 
	void set(int k)   //置位第k个标志位
	{
    
     
		expand(k);   //拓容        
		M[k >> 3] |= (0x80 >> (k & 0x07));  //M[第k个标志位所在的字节(k/8 取整)] |= (第k个标志位在所在字节中的位数(取余)) 
	}
 
	void clear(int k) {
    
    
		expand(k);    //拓容 
		M[k >> 3] &= ~(0x80 >> (k & 0x07)); //M[第k个标志位所在的字节(k/8 取整)] &= ~(第k个标志位在所在字节中的位数(取余))
	}
 
	bool test(int k) {
    
    //取出指定字节中的指定位
		expand(k);    //拓容 
		return M[k >> 3] & (0x80 >> (k & 0x07));  //M[第k个标志位所在的字节(k/8 取整)] &(第k个标志位在所在字节中的位的值)
	}   
 
	void dump(char* file) //将位图整体导出至指定的文件,以便对此后的新位图批量初始化
	{
    
    
		FILE* fp = fopen(file, "w"); fwrite(M, sizeof(char), N, fp); fclose(fp);
	}
 
	char* bits2string(int n) 
	{
    
     //将前n位转换为字符串——
		expand(n - 1); //此时可能被访问的最高位为bitmap[n - 1]
		char* s = new char[n + 1]; s[n] = '\0'; //字符串所占空间,由上层调用者负责释放
		for (int i = 0; i < n; i++) s[i] = test(i) ? '1' : '0';
		return s; //返回字符串位置
	}
 
	void expand(int k) 
	{
    
     //若被访问的bitmap[k]已出界,则需扩容
		if (k < 8 * N) return; //仍在界内,无需扩容
		int oldN = N; char* oldM = M;
		init(2 * k); //与向量类似,加倍策略
		memcpy_s(M, N, oldM, oldN); delete[] oldM; //原数据转移至新空间
	}
 
	void print(int n) //逐位打印以检验位图内容,非必需接口
	{
    
    
		expand(n); 
		for (int i = 0; i < n; i++) 
			printf(test(i) ? "1" : "0");
	}
	
	static bool isPrime(int n) {
    
      //判断某个数是否为素数
	if (n <= 3) {
    
    
		return n > 1;
	}
	// 不在6的倍数两侧的一定不是质数
	if (n % 6 != 1 && n % 6 != 5) {
    
    
		return false;
	}
	int s = (int) sqrt(n);
	for (int i = 5; i <= s; i += 6) {
    
    
		if (n % i == 0 || n % (i + 2) == 0) {
    
    
			return false;
		}
	}
	return true;
}
};

Guess you like

Origin blog.csdn.net/ZXG20000/article/details/113764329