BitMap 算法原理及实现

BitMap 算法c++实现

算法原理

教程1

教程2

代码

#include <iostream>
#include <vector>
#include <math.h>
#include <ctime>
using namespace std;

class BitMap
{
private:
    int maxValue;
    int size = 32;
    int capacity;
    int *byteBits = nullptr;

public:
    BitMap(int maxValue_)
    {
        maxValue = maxValue_;
        capacity = (maxValue + 1) / size + 1;
        byteBits = new int[capacity];
    }
    ~BitMap()
    {
        delete[] byteBits;
    }
    void addValue(int value)
    {
        int index = value / size;
        int position = value % size;
        byteBits[index] = byteBits[index] | 1 << position;
        return;
    }
    void deleteValue(int value)
    {
        if (hasValue(value))
        {
            int index = value / size;
            int position = value % size;
            byteBits[index] = byteBits[index] ^ 1 << position;
        }
        return;
    }
    bool hasValue(int value)
    {
        int index = value / size;
        int position = value % size;
        int flag = byteBits[index] & 1 << position;
        return flag;
    }
    vector<int> getAllValues()
    {
        vector<int> res;
        for (int i = 0; i <= maxValue; i++)
        {
            if (hasValue(i))
            {
                res.push_back(i);
            }
        }
        return res;
    }
};

int main()
{
    BitMap bitmap(9);
    vector<int> values;
    srand((unsigned int)time(nullptr));
    cout << "去重前:" << endl;
    for (int i = 0; i < 100; i++)
    {
        int value = rand() % 10;
        cout << value << endl;
        bitmap.addValue(value);
    }
    cout << "去重后:" << endl;
    values = bitmap.getAllValues();
    for (auto v : values)
    {
        cout << v << endl;
    }
    system("pause");
    return 0;
}

image-20230418100836912

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BITMAP调度算法是一种常用的内存管理算法,用于管理分配和释放内存块。下面是一个使用C语言实现BITMAP调度算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_BLOCKS 1024 #define BLOCK_SIZE 4096 typedef struct { unsigned char* bitmap; unsigned char* memory; } BitmapScheduler; BitmapScheduler* createScheduler() { BitmapScheduler* scheduler = (BitmapScheduler*)malloc(sizeof(BitmapScheduler)); scheduler->bitmap = (unsigned char*)calloc(MAX_BLOCKS / 8, sizeof(unsigned char)); scheduler->memory = (unsigned char*)malloc(MAX_BLOCKS * BLOCK_SIZE * sizeof(unsigned char)); return scheduler; } void destroyScheduler(BitmapScheduler* scheduler) { free(scheduler->bitmap); free(scheduler->memory); free(scheduler); } void* allocateBlock(BitmapScheduler* scheduler) { for (int i = 0; i < MAX_BLOCKS; i++) { int byteIndex = i / 8; int bitIndex = i % 8; if ((scheduler->bitmap[byteIndex] & (1 << bitIndex)) == 0) { scheduler->bitmap[byteIndex] |= (1 << bitIndex); return scheduler->memory + (i * BLOCK_SIZE); } } return NULL; } void freeBlock(BitmapScheduler* scheduler, void* block) { int blockIndex = ((unsigned char*)block - scheduler->memory) / BLOCK_SIZE; int byteIndex = blockIndex / 8; int bitIndex = blockIndex % 8; scheduler->bitmap[byteIndex] &= ~(1 << bitIndex); } void printBitmap(BitmapScheduler* scheduler) { printf("Bitmap:\n"); for (int i = 0; i < MAX_BLOCKS; i++) { int byteIndex = i / 8; int bitIndex = i % 8; bool allocated = (scheduler->bitmap[byteIndex] & (1 << bitIndex)) != 0; printf("%d ", allocated); if ((i + 1) % 32 == 0) { printf("\n"); } } } int main() { BitmapScheduler* scheduler = createScheduler(); // 分配内存块 void* block1 = allocateBlock(scheduler); void* block2 = allocateBlock(scheduler); void* block3 = allocateBlock(scheduler); // 释放内存块 freeBlock(scheduler, block2); // 打印位图 printBitmap(scheduler); destroyScheduler(scheduler); return 0; } ``` 这个示例代码实现BITMAP调度算法的内存分配和释放功能。通过`allocateBlock`函数可以分配一个内存块,通过`freeBlock`函数可以释放一个内存块。`printBitmap`函数用于打印当前的位图情况。 希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值